]> Shamusworld >> Repos - warehouse-man-deluxe/blob - src/editorwindow.cpp
713dc740caebea5e0eb2721f00b42a140c95a4e4
[warehouse-man-deluxe] / src / editorwindow.cpp
1 //
2 // editorwindow.cpp: Game editor window
3 //
4 // by James Hammons
5 // (C) 2014 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  07/03/2014  Created this file
12 //
13
14 #include "editorwindow.h"
15 #include <algorithm>
16 #include "editorwidget.h"
17
18
19 EditorWindow::EditorWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
20 {
21         list = new QListWidget;
22         up = new QPushButton("^");
23         down = new QPushButton("v");
24         add = new QPushButton("+");
25         remove = new QPushButton("-");
26         play = new QPushButton(">");
27         editor = new EditorWidget(parent);
28
29         list->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
30 //      editor->setFocus(); //DISNOWOK
31 //      editor->Load();
32
33         for(unsigned int i=0; i<editor->levelStorage.size(); i++)
34         {
35                 list->addItem(QString(editor->levelStorage[i].name));
36                 QListWidgetItem * item = list->item(i);
37                 item->setFlags(item->flags() | Qt::ItemIsEditable);
38         }
39
40         QHBoxLayout * buttonBox = new QHBoxLayout;
41         QVBoxLayout * listBox = new QVBoxLayout;
42         QHBoxLayout * mainBox = new QHBoxLayout;
43
44         buttonBox->addWidget(up);
45         buttonBox->addWidget(down);
46         buttonBox->addWidget(add);
47         buttonBox->addWidget(remove);
48         buttonBox->addWidget(play);
49
50         listBox->addWidget(list);
51         listBox->addLayout(buttonBox);
52
53         mainBox->addLayout(listBox);
54         mainBox->addWidget(editor);
55
56         setLayout(mainBox);
57         setWindowTitle("Editor");
58
59         connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(SetEditorLevel(int)));
60         connect(list->itemDelegate(), SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), this, SLOT(SetNewName(QWidget *, QAbstractItemDelegate::EndEditHint)));
61
62         connect(up, SIGNAL(clicked()), this, SLOT(MoveLevelUp()));
63         connect(down, SIGNAL(clicked()), this, SLOT(MoveLevelDown()));
64         connect(add, SIGNAL(clicked()), this, SLOT(AddLevel()));
65         connect(remove, SIGNAL(clicked()), this, SLOT(DeleteLevel()));
66         connect(play, SIGNAL(clicked()), this, SLOT(PlayLevel()));
67 }
68
69
70 EditorWindow::~EditorWindow(void)
71 {
72 //      editor->Save();
73         delete editor;
74         delete up;
75         delete down;
76         delete add;
77         delete remove;
78         delete list;
79 }
80
81
82 void EditorWindow::SetEditorLevel(int level)
83 {
84 //printf("SetEditorWindow: level=%i\n", level);
85         editor->currentLevel = level;
86         editor->update();
87
88         if (level == 0)
89                 up->setDisabled(true);
90         else if ((unsigned int)level == (editor->levelStorage.size() - 1))
91                 down->setDisabled(true);
92         else
93         {
94                 up->setDisabled(false);
95                 down->setDisabled(false);
96         }
97 }
98
99
100 void EditorWindow::SetNewName(QWidget * widget, QAbstractItemDelegate::EndEditHint)
101 {
102         QString newName = reinterpret_cast<QLineEdit *>(widget)->text();
103         editor->SetNameOnCurrentLevel(newName.toAscii().data());
104 }
105
106
107 void EditorWindow::MoveLevelUp(void)
108 {
109         // Swap in the vector...
110         int n = editor->currentLevel;
111         std::swap(editor->levelStorage[n], editor->levelStorage[n - 1]);
112
113         // Swap in the QListWidget too...
114         int currentRow = list->currentRow();
115         QListWidgetItem * currentItem = list->takeItem(currentRow);
116         list->insertItem(currentRow - 1, currentItem);
117         list->setCurrentRow(currentRow - 1);
118 }
119
120
121 void EditorWindow::MoveLevelDown(void)
122 {
123         int n = editor->currentLevel;
124         std::swap(editor->levelStorage[n], editor->levelStorage[n + 1]);
125
126         int currentRow = list->currentRow();
127         QListWidgetItem * currentItem = list->takeItem(currentRow);
128         list->insertItem(currentRow + 1, currentItem);
129         list->setCurrentRow(currentRow + 1);
130 }
131
132
133 void EditorWindow::AddLevel(void)
134 {
135         editor->AddNewLevelAtCurrentPosition();
136         editor->SetNameOnCurrentLevel("New Level");
137
138         QListWidgetItem * item = new QListWidgetItem("New Level");
139         item->setFlags(item->flags() | Qt::ItemIsEditable);
140         int currentRow = list->currentRow();
141         list->insertItem(currentRow, item);
142         list->setCurrentRow(currentRow);
143 }
144
145
146 void EditorWindow::DeleteLevel(void)
147 {
148 //printf("DeleteLevel: Erasing level from levelStorage...\n");
149         editor->levelStorage.erase(editor->levelStorage.begin() + editor->currentLevel);
150
151 //printf("DeleteLevel: Erasing level from list...\n");
152         int currentRow = list->currentRow();
153 //printf("DeleteLevel: currentRow = %i\n", currentRow);
154         QListWidgetItem * item = list->takeItem(currentRow);
155 //Does nothing--the same as a click on the item, it's the wrong number (item + 1)...!
156 //      list->setCurrentRow(currentRow);
157         // According to the docs, it's now our responsibility to care for this item...
158 //printf("DeleteLevel: Deleting item...\n");
159         delete item;
160
161         // We need to do this because of a bug in QListWidget::takeItem(). For some reason,
162         // it sets the current item to the one following the one taken. Bad!
163         SetEditorLevel(currentRow);
164 }
165
166
167 void EditorWindow::PlayLevel(void)
168 {
169         emit SetupLevel(&(editor->levelStorage[editor->currentLevel]));
170 }
171