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)
}
+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;
//
#include "editorwindow.h"
+#include <algorithm>
#include "editorwidget.h"
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()));
}
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);
+ }
}
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);
+}
+