]> Shamusworld >> Repos - warehouse-man-deluxe/commitdiff
Finish adding functionality to editor.
authorShamus Hammons <jlhamm@acm.org>
Tue, 8 Jul 2014 14:00:03 +0000 (09:00 -0500)
committerShamus Hammons <jlhamm@acm.org>
Tue, 8 Jul 2014 14:00:03 +0000 (09:00 -0500)
src/editorwidget.cpp
src/editorwidget.h
src/editorwindow.cpp
src/editorwindow.h

index 0c6ab283239781c0c5f3966281a3326c179cdcae..1431edc243c3db38397d694558e28d24f0d4f356 100644 (file)
@@ -178,40 +178,60 @@ void EditorWidget::keyPressEvent(QKeyEvent * event)
 
        if (key == Qt::Key_Up)
        {
-               if (cursor.y > 0)
-                       cursor.y--;
+               if (event->modifiers() == Qt::ShiftModifier)
+                       ShiftLevel(0, -1);
+               else
+               {
+                       if (cursor.y > 0)
+                               cursor.y--;
 
-               if (((cursor.y - corner.y) == 0) && (corner.y > -1))
-                       corner.y--;
+                       if (((cursor.y - corner.y) == 0) && (corner.y > -1))
+                               corner.y--;
+               }
        }
        else if (key == Qt::Key_Down)
        {
-               if (cursor.y < (BOARDSIZE - 1))
-                       cursor.y++;
-
-               if (cursor.y >= (range.y + corner.y - 1))
+               if (event->modifiers() == Qt::ShiftModifier)
+                       ShiftLevel(0, +1);
+               else
                {
-                       if (corner.y < (BOARDSIZE - range.y))
-                               corner.y++;
+                       if (cursor.y < (BOARDSIZE - 1))
+                               cursor.y++;
+
+                       if (cursor.y >= (range.y + corner.y - 1))
+                       {
+                               if (corner.y < (BOARDSIZE - range.y))
+                                       corner.y++;
+                       }
                }
        }
        else if (key == Qt::Key_Left)
        {
-               if (cursor.x > 0)
-                       cursor.x--;
+               if (event->modifiers() == Qt::ShiftModifier)
+                       ShiftLevel(-1, 0);
+               else
+               {
+                       if (cursor.x > 0)
+                               cursor.x--;
 
-               if (((cursor.x - corner.x) == 0) && (corner.x > -1))
-                       corner.x--;
+                       if (((cursor.x - corner.x) == 0) && (corner.x > -1))
+                               corner.x--;
+               }
        }
        else if (key == Qt::Key_Right)
        {
-               if (cursor.x < (BOARDSIZE - 1))
-                       cursor.x++;
-
-               if (cursor.x >= (range.x + corner.x - 1))
+               if (event->modifiers() == Qt::ShiftModifier)
+                       ShiftLevel(+1, 0);
+               else
                {
-                       if (corner.x < (BOARDSIZE - range.x))
-                               corner.x++;
+                       if (cursor.x < (BOARDSIZE - 1))
+                               cursor.x++;
+
+                       if (cursor.x >= (range.x + corner.x - 1))
+                       {
+                               if (corner.x < (BOARDSIZE - range.x))
+                                       corner.x++;
+                       }
                }
        }
        else if (key == Qt::Key_Period)
@@ -421,6 +441,74 @@ void EditorWidget::SetNameOnCurrentLevel(const char * newName)
 }
 
 
+void EditorWidget::AddNewLevelAtCurrentPosition(void)
+{
+       Level l;
+       memset(&l, 0, sizeof(l));
+       l.corner.x = l.corner.y = -1;
+
+       levelStorage.insert(levelStorage.begin() + currentLevel, l);
+}
+
+
+// Note: Only one parameter can be non-zero if you want expected behavior!
+void EditorWidget::ShiftLevel(int dx, int dy)
+{
+       Level & level = levelStorage[currentLevel];
+
+       // Shift grid in the X direction
+       if (dy == 0)
+       {
+               for(int y=0; y<BOARDSIZE; y++)
+               {
+                       if (dx == 1)
+                       {
+                               uint8_t temp = level.board[BOARDSIZE - 1][y];
+
+                               for(int x=BOARDSIZE-1; x>0; x--)
+                                       level.board[x][y] = level.board[x - 1][y];
+
+                               level.board[0][y] = temp;
+                       }
+                       else
+                       {
+                               uint8_t temp = level.board[0][y];
+
+                               for(int x=0; x<BOARDSIZE; x++)
+                                       level.board[x][y] = level.board[x + 1][y];
+
+                               level.board[BOARDSIZE - 1][y] = temp;
+                       }
+               }
+       }
+       // Shift grid in the Y direction
+       else
+       {
+               for(int x=0; x<BOARDSIZE; x++)
+               {
+                       if (dy == 1)
+                       {
+                               uint8_t temp = level.board[x][BOARDSIZE - 1];
+
+                               for(int y=BOARDSIZE-1; y>0; y--)
+                                       level.board[x][y] = level.board[x][y - 1];
+
+                               level.board[x][0] = temp;
+                       }
+                       else
+                       {
+                               uint8_t temp = level.board[x][0];
+
+                               for(int y=0; y<BOARDSIZE; y++)
+                                       level.board[x][y] = level.board[x][y + 1];
+
+                               level.board[x][BOARDSIZE - 1] = temp;
+                       }
+               }
+       }
+}
+
+
 void EditorWidget::GetSizeAndCorner(Level * level, Point & size, Point & corner)
 {
        size.x = size.y = corner.x = corner.y = 0;
index 0b29acb0ae7f78f1769a25f74fd4ab6d03fce028..eb89e0dd8519e50b6f8ed99bb077c559f7a95662 100644 (file)
@@ -53,7 +53,9 @@ class EditorWidget: public QWidget
                bool Load(void);
                bool Save(void);
                void SetNameOnCurrentLevel(const char *);
+               void AddNewLevelAtCurrentPosition(void);
        private:
+               void ShiftLevel(int, int);
                void GetSizeAndCorner(Level *, Point &, Point &);
                void ResizeGrid(void);
                void Pause(int);
index 1ccc14c43adde23c10a8d2769c039a288e887dcf..9c95ba17accfafabfa3e5c1d9875301d0eed83ad 100644 (file)
@@ -12,6 +12,7 @@
 //
 
 #include "editorwindow.h"
+#include <algorithm>
 #include "editorwidget.h"
 
 
@@ -55,6 +56,11 @@ EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
 
        connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
        connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
+
+       connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
+       connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
+       connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
+       connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
 }
 
 
@@ -72,8 +78,19 @@ EditorWindow::~EditorWindow(void)
 
 void EditorWindow::SetEditorLevel(int level)
 {
+//printf("SetEditorWindow: level=%i\n", level);
        editor->currentLevel = level;
        editor->update();
+
+       if (level == 0)
+               up->setDisabled(true);
+       else if ((unsigned int)level == (editor->levelStorage.size() - 1))
+               down->setDisabled(true);
+       else
+       {
+               up->setDisabled(false);
+               down->setDisabled(false);
+       }
 }
 
 
@@ -83,3 +100,63 @@ void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHi
        editor->SetNameOnCurrentLevel(newName.toAscii().data());
 }
 
+
+void EditorWindow::MoveLevelUp(void)
+{
+       // Swap in the vector...
+       int n = editor->currentLevel;
+       std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
+
+       // Swap in the QListWidget too...
+       int currentRow = list->currentRow();
+       QListWidgetItem * currentItem = list->takeItem(currentRow);
+       list->insertItem(currentRow - 1, currentItem);
+       list->setCurrentRow(currentRow - 1);
+}
+
+
+void EditorWindow::MoveLevelDown(void)
+{
+       int n = editor->currentLevel;
+       std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
+
+       int currentRow = list->currentRow();
+       QListWidgetItem * currentItem = list->takeItem(currentRow);
+       list->insertItem(currentRow + 1, currentItem);
+       list->setCurrentRow(currentRow + 1);
+}
+
+
+void EditorWindow::AddLevel(void)
+{
+       editor->AddNewLevelAtCurrentPosition();
+       editor->SetNameOnCurrentLevel("New Level");
+
+       QListWidgetItem * item = new QListWidgetItem("New Level");
+       item->setFlags(item->flags() | Qt::ItemIsEditable);
+       int currentRow = list->currentRow();
+       list->insertItem(currentRow, item);
+       list->setCurrentRow(currentRow);
+}
+
+
+void EditorWindow::DeleteLevel(void)
+{
+//printf("DeleteLevel: Erasing level from levelStorage...\n");
+       editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
+
+//printf("DeleteLevel: Erasing level from list...\n");
+       int currentRow = list->currentRow();
+//printf("DeleteLevel: currentRow = %i\n", currentRow);
+       QListWidgetItem * item = list->takeItem(currentRow);
+//Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
+//     list->setCurrentRow(currentRow);
+       // According to the docs, it's now our responsibility to care for this item...
+//printf("DeleteLevel: Deleting item...\n");
+       delete item;
+
+       // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
+       // it sets the current item to the one following the one taken. Bad!
+       SetEditorLevel(currentRow);
+}
+
index 3b50d7c3cebc00425744006c6576e83daf56db35..d758cf01a4ad5768e3563a6529e27cce24c05bb1 100644 (file)
@@ -16,6 +16,10 @@ class EditorWindow: public QWidget
        private slots:
                void SetEditorLevel(int);
                void SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint);
+               void MoveLevelUp(void);
+               void MoveLevelDown(void);
+               void AddLevel(void);
+               void DeleteLevel(void);
 
        private:
                QListWidget * list;