diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-16 16:53:05 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-16 16:53:05 -0400 |
commit | 0d30e9b57229905f78e7bd60fe5d3cde72851f28 (patch) | |
tree | 4ca2abff9fb1933685f570e97a8b9e88a976c95f /tools/mapedit/src/frame.cpp | |
parent | 36536297aac5c07e3d5fb96abed74570fc7615e9 (diff) | |
download | therapy-0d30e9b57229905f78e7bd60fe5d3cde72851f28.tar.gz therapy-0d30e9b57229905f78e7bd60fe5d3cde72851f28.tar.bz2 therapy-0d30e9b57229905f78e7bd60fe5d3cde72851f28.zip |
Rewrote map editor so a single file contains all maps
Maps are viewed in a tree control on the left. They can be dragged and dropped. Maps are bolded when they are dirty. Saving saves expansion status and order of maps in tree.
Diffstat (limited to 'tools/mapedit/src/frame.cpp')
-rw-r--r-- | tools/mapedit/src/frame.cpp | 321 |
1 files changed, 265 insertions, 56 deletions
diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index a38b861..9d489b8 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp | |||
@@ -5,6 +5,7 @@ | |||
5 | #include <wx/statline.h> | 5 | #include <wx/statline.h> |
6 | #include "panel.h" | 6 | #include "panel.h" |
7 | #include <list> | 7 | #include <list> |
8 | #include <exception> | ||
8 | 9 | ||
9 | static std::list<wxWindow*> openWindows; | 10 | static std::list<wxWindow*> openWindows; |
10 | 11 | ||
@@ -15,9 +16,18 @@ enum { | |||
15 | MENU_FILE_OPEN, | 16 | MENU_FILE_OPEN, |
16 | MENU_FILE_SAVE, | 17 | MENU_FILE_SAVE, |
17 | MENU_FILE_CLOSE, | 18 | MENU_FILE_CLOSE, |
19 | MENU_MAP_ADD_ROOT, | ||
20 | MENU_MAP_ADD_CHILD, | ||
18 | TOOL_FILE_NEW, | 21 | TOOL_FILE_NEW, |
19 | TOOL_FILE_OPEN, | 22 | TOOL_FILE_OPEN, |
20 | TOOL_FILE_SAVE | 23 | TOOL_FILE_SAVE, |
24 | TOOL_MAP_ADD_ROOT, | ||
25 | TOOL_MAP_ADD_CHILD, | ||
26 | MAP_EDITOR_NOTEBOOK, | ||
27 | MAP_EDITOR_TREE, | ||
28 | MAP_TITLE_TEXTBOX, | ||
29 | ADD_ENTITY_BUTTON, | ||
30 | CANCEL_ENTITY_BUTTON | ||
21 | }; | 31 | }; |
22 | 32 | ||
23 | wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) | 33 | wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) |
@@ -27,22 +37,41 @@ wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) | |||
27 | EVT_MENU(MENU_FILE_NEW, MapeditFrame::OnNew) | 37 | EVT_MENU(MENU_FILE_NEW, MapeditFrame::OnNew) |
28 | EVT_MENU(MENU_FILE_OPEN, MapeditFrame::OnOpen) | 38 | EVT_MENU(MENU_FILE_OPEN, MapeditFrame::OnOpen) |
29 | EVT_MENU(MENU_FILE_SAVE, MapeditFrame::OnSave) | 39 | EVT_MENU(MENU_FILE_SAVE, MapeditFrame::OnSave) |
40 | EVT_MENU(MENU_FILE_CLOSE, MapeditFrame::OnClose) | ||
41 | EVT_MENU(MENU_MAP_ADD_ROOT, MapeditFrame::OnAddRoot) | ||
42 | EVT_MENU(MENU_MAP_ADD_CHILD, MapeditFrame::OnAddChild) | ||
30 | EVT_TOOL(TOOL_FILE_NEW, MapeditFrame::OnNew) | 43 | EVT_TOOL(TOOL_FILE_NEW, MapeditFrame::OnNew) |
31 | EVT_TOOL(TOOL_FILE_OPEN, MapeditFrame::OnOpen) | 44 | EVT_TOOL(TOOL_FILE_OPEN, MapeditFrame::OnOpen) |
32 | EVT_TOOL(TOOL_FILE_SAVE, MapeditFrame::OnSave) | 45 | EVT_TOOL(TOOL_FILE_SAVE, MapeditFrame::OnSave) |
33 | EVT_MENU(MENU_FILE_CLOSE, MapeditFrame::OnClose) | 46 | EVT_TOOL(TOOL_MAP_ADD_ROOT, MapeditFrame::OnAddRoot) |
47 | EVT_TOOL(TOOL_MAP_ADD_CHILD, MapeditFrame::OnAddChild) | ||
34 | EVT_CLOSE(MapeditFrame::OnExit) | 48 | EVT_CLOSE(MapeditFrame::OnExit) |
49 | EVT_NOTEBOOK_PAGE_CHANGED(MAP_EDITOR_NOTEBOOK, MapeditFrame::OnTabChange) | ||
50 | EVT_NOTEBOOK_PAGE_CHANGING(MAP_EDITOR_NOTEBOOK, MapeditFrame::OnTabChanging) | ||
51 | EVT_TREE_SEL_CHANGING(MAP_EDITOR_TREE, MapeditFrame::OnWillSelectMap) | ||
52 | EVT_TREE_SEL_CHANGED(MAP_EDITOR_TREE, MapeditFrame::OnDidSelectMap) | ||
53 | EVT_TREE_BEGIN_DRAG(MAP_EDITOR_TREE, MapeditFrame::OnWillDragMap) | ||
54 | EVT_TREE_END_DRAG(MAP_EDITOR_TREE, MapeditFrame::OnDidDragMap) | ||
55 | EVT_TEXT(MAP_TITLE_TEXTBOX, MapeditFrame::OnTitleChange) | ||
56 | EVT_BUTTON(ADD_ENTITY_BUTTON, MapeditFrame::OnAddEntity) | ||
57 | EVT_BUTTON(CANCEL_ENTITY_BUTTON, MapeditFrame::OnCancelAddEntity) | ||
35 | wxEND_EVENT_TABLE() | 58 | wxEND_EVENT_TABLE() |
36 | 59 | ||
37 | MapeditFrame::MapeditFrame(Map map, std::string filename) : wxFrame(NULL, wxID_ANY, "Map Editor", wxDefaultPosition, wxSize(GAME_WIDTH*3, GAME_HEIGHT*2)), map(map), filename(filename) | 60 | MapeditFrame::MapeditFrame(std::unique_ptr<World> world) : wxFrame(NULL, wxID_ANY, "Map Editor", wxDefaultPosition, wxSize(GAME_WIDTH*2+TILE_WIDTH*6*6+10+10+150, GAME_HEIGHT*3)) |
38 | { | 61 | { |
39 | this->map.frame = this; | 62 | this->world = std::move(world); |
63 | this->world->setParent(this); | ||
64 | currentMap = this->world->getLastMap(); | ||
40 | 65 | ||
41 | wxMenu* menuFile = new wxMenu; | 66 | menuFile = new wxMenu; |
42 | menuFile->Append(MENU_FILE_NEW, "New\tCtrl-N"); | 67 | menuFile->Append(MENU_FILE_NEW, "New\tCtrl-N"); |
43 | menuFile->Append(MENU_FILE_OPEN, "Open\tCtrl-O"); | 68 | menuFile->Append(MENU_FILE_OPEN, "Open\tCtrl-O"); |
44 | menuFile->Append(MENU_FILE_SAVE, "Save\tCtrl-S"); | 69 | menuFile->Append(MENU_FILE_SAVE, "Save\tCtrl-S"); |
45 | menuFile->Append(MENU_FILE_CLOSE, "Close\tCtrl-W"); | 70 | menuFile->Append(MENU_FILE_CLOSE, "Close\tCtrl-W"); |
71 | menuFile->AppendSeparator(); | ||
72 | menuFile->Append(MENU_MAP_ADD_ROOT, "New Map\tCtrl-Alt-N"); | ||
73 | menuFile->Append(MENU_MAP_ADD_CHILD, "New Child Map\tCtrl-Alt-Shift-N"); | ||
74 | menuFile->AppendSeparator(); | ||
46 | menuFile->Append(wxID_EXIT); | 75 | menuFile->Append(wxID_EXIT); |
47 | 76 | ||
48 | wxMenu* menuView = new wxMenu; | 77 | wxMenu* menuView = new wxMenu; |
@@ -59,22 +88,27 @@ MapeditFrame::MapeditFrame(Map map, std::string filename) : wxFrame(NULL, wxID_A | |||
59 | // Layout 2: Non-splitter between layout 3 and notebook | 88 | // Layout 2: Non-splitter between layout 3 and notebook |
60 | // Layout 3: Splitter between map editor and properties editor | 89 | // Layout 3: Splitter between map editor and properties editor |
61 | 90 | ||
62 | wxSplitterWindow* layout3 = new wxSplitterWindow(this, wxID_ANY); | 91 | wxSplitterWindow* layout1 = new wxSplitterWindow(this, wxID_ANY); |
92 | mapTree = new wxTreeCtrl(layout1, MAP_EDITOR_TREE, wxDefaultPosition, wxSize(200, 0), wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS); | ||
93 | wxTreeItemId mapTreeRoot = mapTree->AddRoot("root"); | ||
94 | populateMapTree(mapTreeRoot, this->world->getRootMaps()); | ||
95 | |||
96 | wxPanel* layout2 = new wxPanel(layout1, wxID_ANY); | ||
97 | |||
98 | wxSplitterWindow* layout3 = new wxSplitterWindow(layout2, wxID_ANY); | ||
63 | layout3->SetSashGravity(1.0); | 99 | layout3->SetSashGravity(1.0); |
64 | layout3->SetMinimumPaneSize(20); | ||
65 | 100 | ||
66 | notebook = new wxNotebook(this, wxID_ANY); | 101 | notebook = new wxNotebook(layout2, MAP_EDITOR_NOTEBOOK); |
67 | 102 | ||
68 | tileEditor = new TileWidget(notebook, wxID_ANY, 6, 6, wxPoint(0,0), wxSize(TILE_WIDTH*6*6,TILE_HEIGHT*10*6)); | 103 | tileEditor = new TileWidget(notebook, wxID_ANY, 6, 6, wxPoint(0,0), wxSize(TILE_WIDTH*6*6,TILE_HEIGHT*10*6)); |
69 | notebook->AddPage(tileEditor, "Tile Chooser", true); | 104 | notebook->AddPage(tileEditor, "Tile Chooser", false); |
70 | 105 | ||
71 | mapEditor = new MapeditWidget(layout3, wxID_ANY, &this->map, tileEditor, wxPoint(0,0), wxSize(GAME_WIDTH*2, GAME_HEIGHT*2)); | 106 | mapEditor = new MapeditWidget(layout3, wxID_ANY, currentMap, tileEditor, wxPoint(0,0), wxSize(GAME_WIDTH*2, GAME_HEIGHT*2)); |
72 | mapEditor->frame = this; | 107 | mapEditor->frame = this; |
73 | 108 | ||
74 | // Set up property editor | 109 | // Set up property editor |
75 | wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY); | 110 | wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY); |
76 | titleBox = new wxTextCtrl(propertyEditor, wxID_ANY, map.getTitle()); | 111 | titleBox = new wxTextCtrl(propertyEditor, MAP_TITLE_TEXTBOX, currentMap->getTitle()); |
77 | titleBox->Bind(wxEVT_TEXT, &MapeditFrame::OnTitleChange, this); | ||
78 | 112 | ||
79 | wxStaticText* titleLabel = new wxStaticText(propertyEditor, wxID_ANY, "Title:"); | 113 | wxStaticText* titleLabel = new wxStaticText(propertyEditor, wxID_ANY, "Title:"); |
80 | 114 | ||
@@ -103,12 +137,9 @@ MapeditFrame::MapeditFrame(Map map, std::string filename) : wxFrame(NULL, wxID_A | |||
103 | entityTypeBox->Append(entry.second->getType(), entry.second.get()); | 137 | entityTypeBox->Append(entry.second->getType(), entry.second.get()); |
104 | } | 138 | } |
105 | 139 | ||
106 | addEntityButton = new wxButton(entityEditor, wxID_ANY, "Add Entity"); | 140 | addEntityButton = new wxButton(entityEditor, ADD_ENTITY_BUTTON, "Add Entity"); |
107 | addEntityButton->Bind(wxEVT_BUTTON, &MapeditFrame::OnAddEntity, this); | 141 | cancelEntityButton = new wxButton(entityEditor, CANCEL_ENTITY_BUTTON, "Cancel"); |
108 | |||
109 | cancelEntityButton = new wxButton(entityEditor, wxID_ANY, "Cancel"); | ||
110 | cancelEntityButton->Disable(); | 142 | cancelEntityButton->Disable(); |
111 | cancelEntityButton->Bind(wxEVT_BUTTON, &MapeditFrame::OnCancelAddEntity, this); | ||
112 | 143 | ||
113 | wxStaticText* entityInfoLabel = new wxStaticText(entityEditor, wxID_ANY, "Click and drag an entity to move it.\nRight click an entity to delete it."); | 144 | wxStaticText* entityInfoLabel = new wxStaticText(entityEditor, wxID_ANY, "Click and drag an entity to move it.\nRight click an entity to delete it."); |
114 | 145 | ||
@@ -129,51 +160,65 @@ MapeditFrame::MapeditFrame(Map map, std::string filename) : wxFrame(NULL, wxID_A | |||
129 | 160 | ||
130 | // Finish setting up the layouts | 161 | // Finish setting up the layouts |
131 | layout3->SplitHorizontally(mapEditor, propertyEditor); | 162 | layout3->SplitHorizontally(mapEditor, propertyEditor); |
163 | layout1->SplitVertically(mapTree, layout2); | ||
132 | 164 | ||
133 | notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, &MapeditFrame::OnTabChange, this); | 165 | wxBoxSizer* sizer1 = new wxBoxSizer(wxHORIZONTAL); |
134 | notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGING, &MapeditFrame::OnTabChanging, this); | 166 | sizer1->Add(layout1, 1, wxEXPAND, 0); |
167 | sizer1->Add(mapTree, 0, wxALIGN_TOP | wxALIGN_LEFT, 0); | ||
168 | sizer1->Add(layout2, 1, wxEXPAND, 0); | ||
169 | layout1->SetSizer(sizer1); | ||
170 | sizer1->SetSizeHints(layout1); | ||
135 | 171 | ||
136 | wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL); | 172 | wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL); |
137 | sizer2->Add(layout3, 1, wxEXPAND, 0); | 173 | sizer2->Add(layout3, 1, wxEXPAND, 0); |
138 | sizer2->Add(notebook, 0, wxALIGN_TOP | wxALIGN_CENTER_HORIZONTAL | wxLEFT | wxEXPAND, 2); | 174 | sizer2->Add(notebook, 0, wxALIGN_TOP | wxALIGN_CENTER_HORIZONTAL | wxLEFT | wxEXPAND, 2); |
139 | this->SetSizer(sizer2); | 175 | layout2->SetSizer(sizer2); |
140 | sizer2->SetSizeHints(this); | 176 | sizer2->SetSizeHints(layout2); |
141 | 177 | ||
142 | wxBoxSizer* splitterSizer = new wxBoxSizer(wxVERTICAL); | 178 | wxBoxSizer* splitterSizer = new wxBoxSizer(wxVERTICAL); |
143 | splitterSizer->Add(layout3, 1, wxEXPAND, 0); | 179 | splitterSizer->Add(layout3, 1, wxEXPAND, 0); |
144 | splitterSizer->Add(mapEditor, 1, wxEXPAND, 0); | 180 | splitterSizer->Add(mapEditor, 1, wxEXPAND, 0); |
145 | splitterSizer->Add(propertyEditor, 0, wxALIGN_TOP, wxALIGN_LEFT, 0); | 181 | splitterSizer->Add(propertyEditor, 0, wxALIGN_TOP | wxALIGN_LEFT, 0); |
146 | layout3->SetSizer(splitterSizer); | 182 | layout3->SetSizer(splitterSizer); |
147 | splitterSizer->SetSizeHints(layout3); | 183 | splitterSizer->SetSizeHints(layout3); |
148 | 184 | ||
149 | // Toolbar time! | 185 | // Toolbar time! |
150 | toolbar = CreateToolBar(); | 186 | toolbar = CreateToolBar(); |
151 | toolbar->AddTool(TOOL_FILE_NEW, "New", wxBitmap(wxImage("res/page_add.png"))); | 187 | toolbar->AddTool(TOOL_FILE_NEW, "New", wxBitmap(wxImage("res/page.png"))); |
152 | toolbar->AddTool(TOOL_FILE_OPEN, "Open", wxBitmap(wxImage("res/folder_page.png"))); | 188 | toolbar->AddTool(TOOL_FILE_OPEN, "Open", wxBitmap(wxImage("res/folder_page.png"))); |
153 | toolbar->AddTool(TOOL_FILE_SAVE, "Save", wxBitmap(wxImage("res/disk.png"))); | 189 | toolbar->AddTool(TOOL_FILE_SAVE, "Save", wxBitmap(wxImage("res/disk.png"))); |
154 | toolbar->EnableTool(TOOL_FILE_SAVE, this->map.getDirty()); | 190 | toolbar->AddSeparator(); |
191 | toolbar->AddTool(TOOL_MAP_ADD_ROOT, "Add Map", wxBitmap(wxImage("res/page_add.png"))); | ||
192 | toolbar->AddTool(TOOL_MAP_ADD_CHILD, "Add Child Map", wxBitmap(wxImage("res/page_white_add.png"))); | ||
193 | toolbar->EnableTool(TOOL_FILE_SAVE, this->world->getDirty()); | ||
155 | toolbar->Realize(); | 194 | toolbar->Realize(); |
195 | |||
196 | mapTree->SetFocusedItem(currentMap->getTreeItemId()); | ||
197 | SelectMap(currentMap); | ||
198 | |||
199 | Maximize(true); | ||
156 | } | 200 | } |
157 | 201 | ||
158 | void MapeditFrame::OnExit(wxCloseEvent& event) | 202 | void MapeditFrame::OnExit(wxCloseEvent& event) |
159 | { | 203 | { |
160 | if (event.CanVeto() && map.hasUnsavedChanges()) | 204 | if (event.CanVeto() && world->getDirty()) |
161 | { | 205 | { |
162 | switch (wxMessageBox("Current map has unsaved changes. Save before closing?", "Please confirm", wxICON_QUESTION|wxYES_NO|wxCANCEL, this)) | 206 | switch (wxMessageBox("One or more maps have unsaved changes. Save before closing?", "Please confirm", wxICON_QUESTION|wxYES_NO|wxCANCEL, this)) |
163 | { | 207 | { |
164 | case wxYES: | 208 | case wxYES: |
165 | if (filename == "") | 209 | if (world->getFilename() == "") |
166 | { | 210 | { |
167 | wxFileDialog saveFileDialog(this, "Save map", "", "", "XML files (*.xml)|*.xml", wxFD_SAVE); | 211 | wxFileDialog saveFileDialog(this, "Save world", "", "", "XML files (*.xml)|*.xml", wxFD_SAVE); |
168 | if (saveFileDialog.ShowModal() == wxID_CANCEL) | 212 | if (saveFileDialog.ShowModal() == wxID_CANCEL) |
169 | { | 213 | { |
170 | return; | 214 | return; |
171 | } | 215 | } |
172 | 216 | ||
173 | filename = saveFileDialog.GetPath().ToStdString(); | 217 | world->save(saveFileDialog.GetPath().ToStdString(), mapTree); |
218 | } else { | ||
219 | world->save(world->getFilename(), mapTree); | ||
174 | } | 220 | } |
175 | 221 | ||
176 | map.save(filename); | ||
177 | break; | 222 | break; |
178 | 223 | ||
179 | case wxCANCEL: | 224 | case wxCANCEL: |
@@ -204,34 +249,34 @@ void MapeditFrame::ZoomOut(wxCommandEvent&) | |||
204 | 249 | ||
205 | void MapeditFrame::OnNew(wxCommandEvent&) | 250 | void MapeditFrame::OnNew(wxCommandEvent&) |
206 | { | 251 | { |
207 | NewMap(); | 252 | NewWorld(); |
208 | } | 253 | } |
209 | 254 | ||
210 | void MapeditFrame::OnOpen(wxCommandEvent&) | 255 | void MapeditFrame::OnOpen(wxCommandEvent&) |
211 | { | 256 | { |
212 | wxFileDialog openFileDialog(this, "Open map", "", "", "XML files (*.xml)|*.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST); | 257 | wxFileDialog openFileDialog(this, "Open world", "", "", "XML files (*.xml)|*.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST); |
213 | if (openFileDialog.ShowModal() == wxID_CANCEL) | 258 | if (openFileDialog.ShowModal() == wxID_CANCEL) |
214 | { | 259 | { |
215 | return; | 260 | return; |
216 | } | 261 | } |
217 | 262 | ||
218 | OpenMap(openFileDialog.GetPath().c_str()); | 263 | OpenWorld(openFileDialog.GetPath().ToStdString()); |
219 | } | 264 | } |
220 | 265 | ||
221 | void MapeditFrame::OnSave(wxCommandEvent&) | 266 | void MapeditFrame::OnSave(wxCommandEvent&) |
222 | { | 267 | { |
223 | if (filename == "") | 268 | if (world->getFilename() == "") |
224 | { | 269 | { |
225 | wxFileDialog saveFileDialog(this, "Save map", "", "", "XML files (*.xml)|*.xml", wxFD_SAVE); | 270 | wxFileDialog saveFileDialog(this, "Save world", "", "", "XML files (*.xml)|*.xml", wxFD_SAVE); |
226 | if (saveFileDialog.ShowModal() == wxID_CANCEL) | 271 | if (saveFileDialog.ShowModal() == wxID_CANCEL) |
227 | { | 272 | { |
228 | return; | 273 | return; |
229 | } | 274 | } |
230 | 275 | ||
231 | filename = saveFileDialog.GetPath().ToStdString(); | 276 | world->save(saveFileDialog.GetPath().ToStdString(), mapTree); |
277 | } else { | ||
278 | world->save(world->getFilename(), mapTree); | ||
232 | } | 279 | } |
233 | |||
234 | map.save(filename); | ||
235 | } | 280 | } |
236 | 281 | ||
237 | void MapeditFrame::OnClose(wxCommandEvent&) | 282 | void MapeditFrame::OnClose(wxCommandEvent&) |
@@ -252,24 +297,8 @@ void MapeditFrame::OnQuit(wxCommandEvent&) | |||
252 | 297 | ||
253 | void MapeditFrame::OnTitleChange(wxCommandEvent&) | 298 | void MapeditFrame::OnTitleChange(wxCommandEvent&) |
254 | { | 299 | { |
255 | map.setTitle(titleBox->GetLineText(0).ToStdString()); | 300 | currentMap->setTitle(titleBox->GetValue().ToStdString()); |
256 | } | 301 | mapTree->SetItemText(currentMap->getTreeItemId(), currentMap->getTitle()); |
257 | |||
258 | void MapeditFrame::NewMap() | ||
259 | { | ||
260 | LaunchWindow(Map(), ""); | ||
261 | } | ||
262 | |||
263 | void MapeditFrame::OpenMap(const char* filename) | ||
264 | { | ||
265 | LaunchWindow(Map(filename), filename); | ||
266 | } | ||
267 | |||
268 | void MapeditFrame::LaunchWindow(Map map, const char* filename) | ||
269 | { | ||
270 | MapeditFrame* frame = new MapeditFrame(map, filename); | ||
271 | frame->closer = openWindows.insert(end(openWindows), frame); | ||
272 | frame->Show(true); | ||
273 | } | 302 | } |
274 | 303 | ||
275 | void MapeditFrame::OnTabChange(wxBookCtrlEvent& event) | 304 | void MapeditFrame::OnTabChange(wxBookCtrlEvent& event) |
@@ -317,11 +346,113 @@ void MapeditFrame::OnCancelAddEntity(wxCommandEvent&) | |||
317 | mapEditor->CancelAddingEntity(); | 346 | mapEditor->CancelAddingEntity(); |
318 | } | 347 | } |
319 | 348 | ||
349 | void MapeditFrame::OnAddRoot(wxCommandEvent&) | ||
350 | { | ||
351 | auto map = world->newMap(); | ||
352 | wxTreeItemId node = mapTree->AppendItem(mapTree->GetItemParent(mapTree->GetSelection()), map->getTitle()); | ||
353 | map->setTreeItemId(node); | ||
354 | mapTree->SetItemData(node, new MapPtrCtr(map.get())); | ||
355 | mapTree->SetFocusedItem(node); | ||
356 | SelectMap(map.get()); | ||
357 | } | ||
358 | |||
359 | void MapeditFrame::OnAddChild(wxCommandEvent&) | ||
360 | { | ||
361 | auto map = world->newMap(); | ||
362 | wxTreeItemId node = mapTree->AppendItem(mapTree->GetSelection(), map->getTitle()); | ||
363 | map->setTreeItemId(node); | ||
364 | mapTree->SetItemData(node, new MapPtrCtr(map.get())); | ||
365 | mapTree->SetFocusedItem(node); | ||
366 | mapTree->Expand(mapTree->GetSelection()); | ||
367 | SelectMap(map.get()); | ||
368 | } | ||
369 | |||
370 | void MapeditFrame::OnDidSelectMap(wxTreeEvent& event) | ||
371 | { | ||
372 | MapPtrCtr* data = (MapPtrCtr*) mapTree->GetItemData(event.GetItem()); | ||
373 | SelectMap(data->map); | ||
374 | } | ||
375 | |||
376 | void MapeditFrame::OnWillSelectMap(wxTreeEvent& event) | ||
377 | { | ||
378 | if (addingEntity) | ||
379 | { | ||
380 | event.Veto(); | ||
381 | return; | ||
382 | } | ||
383 | |||
384 | event.Skip(); | ||
385 | } | ||
386 | |||
387 | void MapeditFrame::OnWillDragMap(wxTreeEvent& event) | ||
388 | { | ||
389 | if (!addingEntity) | ||
390 | { | ||
391 | event.Allow(); | ||
392 | dragMap = event.GetItem(); | ||
393 | } | ||
394 | } | ||
395 | |||
396 | void MapeditFrame::OnDidDragMap(wxTreeEvent& event) | ||
397 | { | ||
398 | if (!dragMap.IsOk()) | ||
399 | { | ||
400 | return; | ||
401 | } | ||
402 | |||
403 | wxTreeItemId newParent = event.GetItem(); | ||
404 | if (!newParent.IsOk()) | ||
405 | { | ||
406 | newParent = mapTree->GetRootItem(); | ||
407 | } | ||
408 | |||
409 | wxTreeItemId newChild = MoveTreeNode(dragMap, newParent); | ||
410 | dragMap.Unset(); | ||
411 | mapTree->SelectItem(newChild); | ||
412 | } | ||
413 | |||
414 | void MapeditFrame::NewWorld() | ||
415 | { | ||
416 | LaunchWindow(std::unique_ptr<World>(new World())); | ||
417 | } | ||
418 | |||
419 | void MapeditFrame::OpenWorld(std::string filename) | ||
420 | { | ||
421 | try | ||
422 | { | ||
423 | auto world = std::unique_ptr<World>(new World(filename)); | ||
424 | |||
425 | LaunchWindow(std::move(world)); | ||
426 | } catch (std::exception& ex) | ||
427 | { | ||
428 | wxMessageBox(ex.what(), "Error loading world", wxOK | wxCENTRE | wxICON_ERROR); | ||
429 | } | ||
430 | } | ||
431 | |||
432 | void MapeditFrame::LaunchWindow(std::unique_ptr<World> world) | ||
433 | { | ||
434 | MapeditFrame* frame = new MapeditFrame(std::move(world)); | ||
435 | frame->closer = openWindows.insert(end(openWindows), frame); | ||
436 | frame->Show(true); | ||
437 | } | ||
438 | |||
320 | void MapeditFrame::StartAddingEntity() | 439 | void MapeditFrame::StartAddingEntity() |
321 | { | 440 | { |
322 | addingEntity = true; | 441 | addingEntity = true; |
323 | addEntityButton->Disable(); | 442 | addEntityButton->Disable(); |
324 | cancelEntityButton->Enable(); | 443 | cancelEntityButton->Enable(); |
444 | |||
445 | toolbar->EnableTool(TOOL_FILE_NEW, false); | ||
446 | toolbar->EnableTool(TOOL_FILE_OPEN, false); | ||
447 | toolbar->EnableTool(TOOL_FILE_SAVE, false); | ||
448 | toolbar->EnableTool(TOOL_MAP_ADD_ROOT, false); | ||
449 | toolbar->EnableTool(TOOL_MAP_ADD_CHILD, false); | ||
450 | |||
451 | menuFile->Enable(MENU_FILE_NEW, false); | ||
452 | menuFile->Enable(MENU_FILE_OPEN, false); | ||
453 | menuFile->Enable(MENU_FILE_SAVE, false); | ||
454 | menuFile->Enable(MENU_MAP_ADD_ROOT, false); | ||
455 | menuFile->Enable(MENU_MAP_ADD_CHILD, false); | ||
325 | } | 456 | } |
326 | 457 | ||
327 | void MapeditFrame::FinishAddingEntity() | 458 | void MapeditFrame::FinishAddingEntity() |
@@ -329,9 +460,87 @@ void MapeditFrame::FinishAddingEntity() | |||
329 | addingEntity = false; | 460 | addingEntity = false; |
330 | addEntityButton->Enable(); | 461 | addEntityButton->Enable(); |
331 | cancelEntityButton->Disable(); | 462 | cancelEntityButton->Disable(); |
463 | toolbar->Enable(); | ||
464 | |||
465 | toolbar->EnableTool(TOOL_FILE_NEW, true); | ||
466 | toolbar->EnableTool(TOOL_FILE_OPEN, true); | ||
467 | toolbar->EnableTool(TOOL_FILE_SAVE, world->getDirty()); | ||
468 | toolbar->EnableTool(TOOL_MAP_ADD_ROOT, true); | ||
469 | toolbar->EnableTool(TOOL_MAP_ADD_CHILD, true); | ||
470 | |||
471 | menuFile->Enable(MENU_FILE_NEW, true); | ||
472 | menuFile->Enable(MENU_FILE_OPEN, true); | ||
473 | menuFile->Enable(MENU_FILE_SAVE, world->getDirty()); | ||
474 | menuFile->Enable(MENU_MAP_ADD_ROOT, true); | ||
475 | menuFile->Enable(MENU_MAP_ADD_CHILD, true); | ||
332 | } | 476 | } |
333 | 477 | ||
334 | void MapeditFrame::MapDirtyDidChange(bool dirty) | 478 | void MapeditFrame::MapDirtyDidChange(bool dirty) |
335 | { | 479 | { |
336 | toolbar->EnableTool(TOOL_FILE_SAVE, dirty); | 480 | toolbar->EnableTool(TOOL_FILE_SAVE, dirty); |
481 | menuFile->Enable(MENU_FILE_SAVE, dirty); | ||
482 | |||
483 | if (dirty) | ||
484 | { | ||
485 | mapTree->SetItemBold(currentMap->getTreeItemId(), true); | ||
486 | } else { | ||
487 | for (auto map : world->getMaps()) | ||
488 | { | ||
489 | mapTree->SetItemBold(map.second->getTreeItemId(), false); | ||
490 | } | ||
491 | } | ||
492 | } | ||
493 | |||
494 | void MapeditFrame::populateMapTree(wxTreeItemId node, std::list<std::shared_ptr<Map>> maps) | ||
495 | { | ||
496 | for (auto map : maps) | ||
497 | { | ||
498 | wxTreeItemId childNode = mapTree->AppendItem(node, map->getTitle()); | ||
499 | mapTree->SetItemData(childNode, new MapPtrCtr(map.get())); | ||
500 | map->setTreeItemId(childNode); | ||
501 | |||
502 | populateMapTree(childNode, map->getChildren()); | ||
503 | |||
504 | if (map->getExpanded()) | ||
505 | { | ||
506 | mapTree->Expand(childNode); | ||
507 | } | ||
508 | } | ||
509 | } | ||
510 | |||
511 | void MapeditFrame::SelectMap(Map* map) | ||
512 | { | ||
513 | currentMap = map; | ||
514 | mapEditor->SetMap(map); | ||
515 | titleBox->ChangeValue(map->getTitle()); | ||
516 | } | ||
517 | |||
518 | wxTreeItemId MapeditFrame::MoveTreeNode(wxTreeItemId toCopy, wxTreeItemId newParent) | ||
519 | { | ||
520 | MapPtrCtr* ctl1 = (MapPtrCtr*) mapTree->GetItemData(toCopy); | ||
521 | MapPtrCtr* ctl2 = new MapPtrCtr(ctl1->map); | ||
522 | |||
523 | wxTreeItemId copied = mapTree->AppendItem(newParent, mapTree->GetItemText(toCopy), -1, -1, ctl2); | ||
524 | if (mapTree->IsBold(toCopy)) | ||
525 | { | ||
526 | mapTree->SetItemBold(toCopy, true); | ||
527 | } | ||
528 | |||
529 | if (mapTree->ItemHasChildren(toCopy)) | ||
530 | { | ||
531 | wxTreeItemIdValue cookie; | ||
532 | for (wxTreeItemId it = mapTree->GetFirstChild(toCopy, cookie); it.IsOk(); it = mapTree->GetNextChild(toCopy, cookie)) | ||
533 | { | ||
534 | MoveTreeNode(it, copied); | ||
535 | } | ||
536 | } | ||
537 | |||
538 | if (mapTree->IsExpanded(toCopy)) | ||
539 | { | ||
540 | mapTree->Expand(copied); | ||
541 | } | ||
542 | |||
543 | mapTree->Delete(toCopy); | ||
544 | |||
545 | return copied; | ||
337 | } | 546 | } |