From: Shamus Hammons Date: Tue, 8 Jul 2014 14:00:03 +0000 (-0500) Subject: Finish adding functionality to editor. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=warehouse-man-deluxe;a=commitdiff_plain;h=195a185d42504cbface8c29911467ce8cff27229 Finish adding functionality to editor. --- diff --git a/src/editorwidget.cpp b/src/editorwidget.cpp index 0c6ab28..1431edc 100644 --- a/src/editorwidget.cpp +++ b/src/editorwidget.cpp @@ -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; y0; 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; x0; 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 #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); +} + diff --git a/src/editorwindow.h b/src/editorwindow.h index 3b50d7c..d758cf0 100644 --- a/src/editorwindow.h +++ b/src/editorwindow.h @@ -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;