]> Shamusworld >> Repos - warehouse-man-deluxe/blobdiff - src/editorwidget.cpp
Added/fixed editor, added play level functionality.
[warehouse-man-deluxe] / src / editorwidget.cpp
index 1431edc243c3db38397d694558e28d24f0d4f356..40596326abe800ed1f6ea6bee42e31058dbe203b 100644 (file)
@@ -130,6 +130,21 @@ void EditorWidget::paintEvent(QPaintEvent * /*event*/)
        painter.setBrush(Qt::transparent);
        painter.setPen(Qt::darkGreen);
        painter.drawEllipse((x * GRIDSIZE) + 4, (y * GRIDSIZE) + 4, GRIDSIZE - 8, GRIDSIZE - 8);
+
+       char s[256];
+       int boxes, spots;
+       CountBoxesAndSpots(boxes, spots);
+
+       if (boxes == spots)
+               sprintf(s, "Boxes = Spots");
+       else if (boxes < spots)
+               sprintf(s, "Need %i more box%s", spots - boxes, (spots - boxes == 1 ? "" : "es"));
+       else if (spots < boxes)
+               sprintf(s, "Need %i more spot%s", boxes - spots, (boxes - spots == 1 ? "" : "s"));
+
+       painter.setPen(Qt::blue);
+       painter.drawText(0, 0, clientArea.width(), clientArea.height(),
+               Qt::AlignRight | Qt::AlignBottom, QString(s));
 }
 
 
@@ -236,20 +251,25 @@ void EditorWidget::keyPressEvent(QKeyEvent * event)
        }
        else if (key == Qt::Key_Period)
        {
-               level.board[cursor.x][cursor.y] ^= GTBoxSpot;
+               if (!(level.board[cursor.x][cursor.y] & (GTWall | GTNull)))
+                       level.board[cursor.x][cursor.y] ^= GTBoxSpot;
        }
        else if (key == Qt::Key_B)
        {
-               level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox);
-               level.board[cursor.x][cursor.y] |= GTBox;
+               level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTNull);
+               level.board[cursor.x][cursor.y] ^= GTBox;
        }
        else if (key == Qt::Key_W)
        {
-               level.board[cursor.x][cursor.y] = GTWall;
+               // If it's not a wall, just stick one in; otherwise, toggle it off
+               if (level.board[cursor.x][cursor.y] != GTWall)
+                       level.board[cursor.x][cursor.y] = GTWall;
+               else
+                       level.board[cursor.x][cursor.y] = GTSpace;
        }
        else if (key == Qt::Key_Space)
        {
-               level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox);
+               level.board[cursor.x][cursor.y] &= ~(GTSpace | GTWall | GTBox | GTNull);
                level.board[cursor.x][cursor.y] |= GTSpace;
        }
        else if (key == Qt::Key_O)
@@ -265,7 +285,11 @@ void EditorWidget::keyPressEvent(QKeyEvent * event)
        }
        else if (key == Qt::Key_V)
        {
-               level.board[cursor.x][cursor.y] = GTNull;
+               // If it's not a void, just stick one in; otherwise, toggle it off
+               if (level.board[cursor.x][cursor.y] != GTNull)
+                       level.board[cursor.x][cursor.y] = GTNull;
+               else
+                       level.board[cursor.x][cursor.y] = GTSpace;
        }
        else
                return;         // Only update screen if keypress was recognized
@@ -451,6 +475,25 @@ void EditorWidget::AddNewLevelAtCurrentPosition(void)
 }
 
 
+void EditorWidget::CountBoxesAndSpots(int & boxes, int & spots)
+{
+       Level & level = levelStorage[currentLevel];
+       boxes = spots = 0;
+
+       for(int x=0; x<BOARDSIZE; x++)
+       {
+               for(int y=0; y<BOARDSIZE; y++)
+               {
+                       if (level.board[x][y] & GTBox)
+                               boxes++;
+
+                       if (level.board[x][y] & GTBoxSpot)
+                               spots++;
+               }
+       }
+}
+
+
 // Note: Only one parameter can be non-zero if you want expected behavior!
 void EditorWidget::ShiftLevel(int dx, int dy)
 {
@@ -509,7 +552,7 @@ void EditorWidget::ShiftLevel(int dx, int dy)
 }
 
 
-void EditorWidget::GetSizeAndCorner(Level * level, Point & size, Point & corner)
+/*static*/ void EditorWidget::GetSizeAndCorner(Level * level, Point & size, Point & corner)
 {
        size.x = size.y = corner.x = corner.y = 0;
        Point min, max;