Initial commit
[vexed] / fieldview.cpp
diff --git a/fieldview.cpp b/fieldview.cpp
new file mode 100644 (file)
index 0000000..2ccb757
--- /dev/null
@@ -0,0 +1,150 @@
+#include <QtCore>
+#include <QtGui>
+#include <stdio.h>
+
+#include "playfield.h"
+#include "fieldview.h"
+
+FieldView::FieldView(QWidget *parent) :
+        QWidget(parent)
+{
+    selX=-1;
+    selY=-1;
+    moving=false;
+    moves=new QList<Animation*>();
+    playField=0;
+
+}
+
+void FieldView::mousePressEvent(QMouseEvent *event)
+{
+    QPoint sel=cell(event->pos());
+
+    selX=sel.x();
+    selY=sel.y();
+    moveX=-1;
+    moveY=-1;
+    moving=true;
+    update();
+}
+
+void FieldView::mouseReleaseEvent(QMouseEvent *event)
+{
+    if(moving)
+    {
+        moving=false;
+        QPoint mov=cell(event->pos());
+        moveX=mov.x();
+        moveY=mov.y();
+        if(moveX==selX && moveY==selY)
+        {
+            moveX=-1;
+            return;
+        }
+    }
+    delete moves;
+    moves=new QList<Animation*>();
+    playField->move(selX, selY, moveX);
+    playMoves();
+    emit updateMoves(playField->moves);
+}
+
+PlayField* FieldView::setPlayField(PlayField *pf)
+{
+        if(playField)
+        {
+                delete playField;
+        }
+        playField = new PlayField(pf);
+        QObject::connect(playField,SIGNAL(cellMoved(int,int,int,int)),this,SLOT(cellMoved(int,int,int,int)));
+        QObject::connect(playField,SIGNAL(cellGone(int,int)),this,SLOT(cellGone(int,int)));
+
+        updateWidgets();
+        show();
+        emit updateMoves(playField->moves);
+        return playField;
+}
+
+void FieldView::cellMoved(int w, int h, int wnew, int hnew)
+{
+        QRect c=coo(w,h);
+        QRect cnew=coo(wnew,hnew);
+        moves->append(new Move(c,cnew));
+}
+void FieldView::cellGone(int w, int h)
+{
+        QRect c=coo(w,h);
+        moves->append(new Hide(c));
+}
+QWidget* FieldView::cellWidget(int w, int h)
+{
+        return childAt(coo(w,h).topLeft());
+}
+void FieldView::playMove()
+{
+        if(moves->size()>0)
+        {
+                Animation *move=moves->at(0);
+                QWidget *item=childAt(move->src.topLeft());
+                if(!item)
+                {
+                        QPoint p=cell(move->src.left(),move->src.top());
+                }
+                QPropertyAnimation *a=move->getAnimation(item);
+                moves->removeAt(0);
+                connect(a,SIGNAL(finished()),this,SLOT(playMove()));
+                a->start();
+                delete move;
+        } else if(playField->checkSolved())
+        {
+            emit solved(playField->moves);
+        }
+}
+
+void FieldView::playMoves()
+{
+    playMove();
+}
+
+void FieldView::undo()
+{
+    playField->undo();
+    updateWidgets();
+    emit updateMoves(playField->moves);
+}
+
+void FieldView::updateWidgets()
+{
+    QList<QWidget *> childs=findChildren<QWidget *>();
+    QListIterator<QWidget *> childsI(childs);
+    while(childsI.hasNext())
+    {
+        delete childsI.next();
+    }
+
+    for(int w=0;w<PF::FIELD_WIDTH;w++)
+        for(int h=0;h<PF::FIELD_HEIGHT;h++)
+        {
+        QRect r=coo(w,h,0);
+        int cell=playField->get(w,h);
+        QLabel *cellW;
+        switch(cell)
+        {
+                case PF::CELL_EMPTY:
+            break;
+                case PF::CELL_WALL:
+            cellW=new QLabel(this);
+            cellW->setPixmap(iconSet.wallIcon());
+            cellW->setGeometry(r);
+            cellW->setFrameStyle(QFrame::NoFrame);
+            cellW->show();
+            break;
+                default:
+            cellW=new QLabel(this);
+            cellW->setPixmap(iconSet.icon(cell-2));
+            cellW->setGeometry(r);
+            cellW->setFrameStyle(QFrame::NoFrame);
+            cellW->show();
+        }
+    }
+}