summary refs log tree commit diff stats
path: root/tools/mapedit/src/frame.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mapedit/src/frame.cpp')
-rw-r--r--tools/mapedit/src/frame.cpp197
1 files changed, 173 insertions, 24 deletions
diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index 66775e1..3646d4e 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp
@@ -19,6 +19,8 @@ enum {
19 MENU_FILE_CLOSE, 19 MENU_FILE_CLOSE,
20 MENU_MAP_ADD_ROOT, 20 MENU_MAP_ADD_ROOT,
21 MENU_MAP_ADD_CHILD, 21 MENU_MAP_ADD_CHILD,
22 MENU_EDIT_UNDO,
23 MENU_EDIT_REDO,
22 TOOL_FILE_NEW, 24 TOOL_FILE_NEW,
23 TOOL_FILE_OPEN, 25 TOOL_FILE_OPEN,
24 TOOL_FILE_SAVE, 26 TOOL_FILE_SAVE,
@@ -43,6 +45,8 @@ wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame)
43 EVT_MENU(MENU_FILE_CLOSE, MapeditFrame::OnClose) 45 EVT_MENU(MENU_FILE_CLOSE, MapeditFrame::OnClose)
44 EVT_MENU(MENU_MAP_ADD_ROOT, MapeditFrame::OnAddRoot) 46 EVT_MENU(MENU_MAP_ADD_ROOT, MapeditFrame::OnAddRoot)
45 EVT_MENU(MENU_MAP_ADD_CHILD, MapeditFrame::OnAddChild) 47 EVT_MENU(MENU_MAP_ADD_CHILD, MapeditFrame::OnAddChild)
48 EVT_MENU(MENU_EDIT_UNDO, MapeditFrame::OnUndo)
49 EVT_MENU(MENU_EDIT_REDO, MapeditFrame::OnRedo)
46 EVT_TOOL(TOOL_FILE_NEW, MapeditFrame::OnNew) 50 EVT_TOOL(TOOL_FILE_NEW, MapeditFrame::OnNew)
47 EVT_TOOL(TOOL_FILE_OPEN, MapeditFrame::OnOpen) 51 EVT_TOOL(TOOL_FILE_OPEN, MapeditFrame::OnOpen)
48 EVT_TOOL(TOOL_FILE_SAVE, MapeditFrame::OnSave) 52 EVT_TOOL(TOOL_FILE_SAVE, MapeditFrame::OnSave)
@@ -51,7 +55,6 @@ wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame)
51 EVT_CLOSE(MapeditFrame::OnExit) 55 EVT_CLOSE(MapeditFrame::OnExit)
52 EVT_NOTEBOOK_PAGE_CHANGED(MAP_EDITOR_NOTEBOOK, MapeditFrame::OnTabChange) 56 EVT_NOTEBOOK_PAGE_CHANGED(MAP_EDITOR_NOTEBOOK, MapeditFrame::OnTabChange)
53 EVT_NOTEBOOK_PAGE_CHANGING(MAP_EDITOR_NOTEBOOK, MapeditFrame::OnTabChanging) 57 EVT_NOTEBOOK_PAGE_CHANGING(MAP_EDITOR_NOTEBOOK, MapeditFrame::OnTabChanging)
54 EVT_TREE_SEL_CHANGING(MAP_EDITOR_TREE, MapeditFrame::OnWillSelectMap)
55 EVT_TREE_SEL_CHANGED(MAP_EDITOR_TREE, MapeditFrame::OnDidSelectMap) 58 EVT_TREE_SEL_CHANGED(MAP_EDITOR_TREE, MapeditFrame::OnDidSelectMap)
56 EVT_TREE_BEGIN_DRAG(MAP_EDITOR_TREE, MapeditFrame::OnWillDragMap) 59 EVT_TREE_BEGIN_DRAG(MAP_EDITOR_TREE, MapeditFrame::OnWillDragMap)
57 EVT_TREE_END_DRAG(MAP_EDITOR_TREE, MapeditFrame::OnDidDragMap) 60 EVT_TREE_END_DRAG(MAP_EDITOR_TREE, MapeditFrame::OnDidDragMap)
@@ -80,12 +83,18 @@ MapeditFrame::MapeditFrame(std::unique_ptr<World> world) : wxFrame(NULL, wxID_AN
80 menuFile->AppendSeparator(); 83 menuFile->AppendSeparator();
81 menuFile->Append(wxID_EXIT); 84 menuFile->Append(wxID_EXIT);
82 85
86 menuEdit = new wxMenu;
87 menuEdit->Append(MENU_EDIT_UNDO, "Undo\tCtrl-Z");
88 menuEdit->Append(MENU_EDIT_REDO, "Redo\tCtrl-Shift-Z");
89 UpdateUndoLabels();
90
83 wxMenu* menuView = new wxMenu; 91 wxMenu* menuView = new wxMenu;
84 menuView->Append(MENU_VIEW_ZOOM_IN, "Zoom In\tCtrl-+"); 92 menuView->Append(MENU_VIEW_ZOOM_IN, "Zoom In\tCtrl-+");
85 menuView->Append(MENU_VIEW_ZOOM_OUT, "Zoom Out\tCtrl--"); 93 menuView->Append(MENU_VIEW_ZOOM_OUT, "Zoom Out\tCtrl--");
86 94
87 wxMenuBar* menuBar = new wxMenuBar; 95 wxMenuBar* menuBar = new wxMenuBar;
88 menuBar->Append(menuFile, "&File"); 96 menuBar->Append(menuFile, "&File");
97 menuBar->Append(menuEdit, "&Edit");
89 menuBar->Append(menuView, "&View"); 98 menuBar->Append(menuView, "&View");
90 99
91 SetMenuBar(menuBar); 100 SetMenuBar(menuBar);
@@ -118,7 +127,7 @@ MapeditFrame::MapeditFrame(std::unique_ptr<World> world) : wxFrame(NULL, wxID_AN
118 127
119 // Set up property editor 128 // Set up property editor
120 wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY); 129 wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY);
121 titleBox = new wxTextCtrl(propertyEditor, MAP_TITLE_TEXTBOX, currentMap->getTitle()); 130 titleBox = new UndoableTextBox(propertyEditor, MAP_TITLE_TEXTBOX, currentMap->getTitle(), "Edit Map Title", this);
122 titleBox->SetMaxLength(40); 131 titleBox->SetMaxLength(40);
123 132
124 wxStaticText* titleLabel = new wxStaticText(propertyEditor, wxID_ANY, "Title:"); 133 wxStaticText* titleLabel = new wxStaticText(propertyEditor, wxID_ANY, "Title:");
@@ -216,8 +225,9 @@ MapeditFrame::MapeditFrame(std::unique_ptr<World> world) : wxFrame(NULL, wxID_AN
216 toolbar->EnableTool(TOOL_FILE_SAVE, this->world->getDirty()); 225 toolbar->EnableTool(TOOL_FILE_SAVE, this->world->getDirty());
217 toolbar->Realize(); 226 toolbar->Realize();
218 227
219 mapTree->SetFocusedItem(currentMap->getTreeItemId()); 228 dontSelectMap = true;
220 SelectMap(currentMap); 229 mapTree->SelectItem(currentMap->getTreeItemId());
230 dontSelectMap = false;
221 231
222 Maximize(true); 232 Maximize(true);
223} 233}
@@ -324,10 +334,12 @@ void MapeditFrame::OnQuit(wxCommandEvent&)
324 } 334 }
325} 335}
326 336
327void MapeditFrame::OnTitleChange(wxCommandEvent&) 337void MapeditFrame::OnTitleChange(wxCommandEvent& event)
328{ 338{
329 currentMap->setTitle(titleBox->GetValue().ToStdString()); 339 currentMap->setTitle(titleBox->GetValue().ToStdString());
330 mapTree->SetItemText(currentMap->getTreeItemId(), currentMap->getTitle()); 340 mapTree->SetItemText(currentMap->getTreeItemId(), titleBox->GetValue());
341
342 event.Skip();
331} 343}
332 344
333void MapeditFrame::OnTabChange(wxBookCtrlEvent& event) 345void MapeditFrame::OnTabChange(wxBookCtrlEvent& event)
@@ -378,33 +390,83 @@ void MapeditFrame::OnCancelAddEntity(wxCommandEvent&)
378void MapeditFrame::OnAddRoot(wxCommandEvent&) 390void MapeditFrame::OnAddRoot(wxCommandEvent&)
379{ 391{
380 auto map = world->newMap(); 392 auto map = world->newMap();
381 wxTreeItemId node = mapTree->AppendItem(mapTree->GetItemParent(mapTree->GetSelection()), map->getTitle()); 393
382 map->setTreeItemId(node); 394 commitAction(std::make_shared<Undoable>("New Map", [=] () {
383 mapTree->SetItemData(node, new MapPtrCtr(map.get())); 395 map->setHidden(false);
384 mapTree->SetFocusedItem(node); 396
385 SelectMap(map.get()); 397 wxTreeItemId sel = mapTree->GetSelection();
398 wxTreeItemId par = mapTree->GetItemParent(sel);
399 wxTreeItemId node = mapTree->AppendItem(par, map->getTitle());
400 map->setTreeItemId(node);
401 mapTree->SetItemData(node, new MapPtrCtr(map.get()));
402
403 dontSelectMap = true;
404 mapTree->SelectItem(node);
405 dontSelectMap = false;
406 }, [=] () {
407 map->setHidden(true);
408
409 wxTreeItemId sel = mapTree->GetPrevSibling(map->getTreeItemId());
410 mapTree->Delete(map->getTreeItemId());
411
412 dontSelectMap = true;
413 mapTree->SelectItem(sel);
414 dontSelectMap = false;
415 }));
416
386} 417}
387 418
388void MapeditFrame::OnAddChild(wxCommandEvent&) 419void MapeditFrame::OnAddChild(wxCommandEvent&)
389{ 420{
390 auto map = world->newMap(); 421 auto map = world->newMap();
391 wxTreeItemId node = mapTree->AppendItem(mapTree->GetSelection(), map->getTitle()); 422
392 map->setTreeItemId(node); 423 commitAction(std::make_shared<Undoable>("New Map", [=] () {
393 mapTree->SetItemData(node, new MapPtrCtr(map.get())); 424 map->setHidden(false);
394 mapTree->SetFocusedItem(node); 425
395 mapTree->Expand(mapTree->GetSelection()); 426 wxTreeItemId sel = mapTree->GetSelection();
396 SelectMap(map.get()); 427 wxTreeItemId node = mapTree->AppendItem(sel, map->getTitle());
428 map->setTreeItemId(node);
429 mapTree->SetItemData(node, new MapPtrCtr(map.get()));
430
431 mapTree->Expand(sel);
432
433 dontSelectMap = true;
434 mapTree->SelectItem(node);
435 dontSelectMap = false;
436 }, [=] () {
437 map->setHidden(true);
438
439 wxTreeItemId sel = mapTree->GetItemParent(map->getTreeItemId());
440 mapTree->Delete(map->getTreeItemId());
441
442 dontSelectMap = true;
443 mapTree->SelectItem(sel);
444 dontSelectMap = false;
445 }));
397} 446}
398 447
399void MapeditFrame::OnDidSelectMap(wxTreeEvent& event) 448void MapeditFrame::OnDidSelectMap(wxTreeEvent& event)
400{ 449{
401 MapPtrCtr* data = (MapPtrCtr*) mapTree->GetItemData(event.GetItem()); 450 MapPtrCtr* data = (MapPtrCtr*) mapTree->GetItemData(event.GetItem());
402 SelectMap(data->map); 451 SelectMap(data->map);
403} 452
404 453 if (!dontSelectMap)
405void MapeditFrame::OnWillSelectMap(wxTreeEvent& event) 454 {
406{ 455 commitAfter(std::make_shared<Undoable>("Selecting " + data->map->getTitle(), [=] () {
407 event.Skip(); 456 wxTreeItemId toSelect = event.GetItem();
457 dontSelectMap = true;
458 mapTree->SelectItem(toSelect);
459 dontSelectMap = false;
460 SelectMap(data->map);
461 }, [=] () {
462 wxTreeItemId toSelect = event.GetOldItem();
463 MapPtrCtr* oldData = (MapPtrCtr*) mapTree->GetItemData(toSelect);
464 dontSelectMap = true;
465 mapTree->SelectItem(toSelect);
466 dontSelectMap = false;
467 SelectMap(oldData->map);
468 }));
469 }
408} 470}
409 471
410void MapeditFrame::OnWillDragMap(wxTreeEvent& event) 472void MapeditFrame::OnWillDragMap(wxTreeEvent& event)
@@ -426,9 +488,24 @@ void MapeditFrame::OnDidDragMap(wxTreeEvent& event)
426 newParent = mapTree->GetRootItem(); 488 newParent = mapTree->GetRootItem();
427 } 489 }
428 490
429 wxTreeItemId newChild = MoveTreeNode(dragMap, newParent); 491 wxTreeItemId curParent = mapTree->GetItemParent(event.GetItem());
492 wxTreeItemId dragMapCopy = dragMap;
430 dragMap.Unset(); 493 dragMap.Unset();
431 mapTree->SelectItem(newChild); 494
495 Map* theMap = ((MapPtrCtr*) mapTree->GetItemData(dragMap))->map;
496 commitAction(std::make_shared<Undoable>("Arranging " + theMap->getTitle(), [=] () {
497 wxTreeItemId newChild = MoveTreeNode(dragMapCopy, newParent);
498
499 dontSelectMap = true;
500 mapTree->SelectItem(newChild);
501 dontSelectMap = false;
502 }, [=] () {
503 wxTreeItemId newChild = MoveTreeNode(dragMapCopy, curParent);
504
505 dontSelectMap = true;
506 mapTree->SelectItem(newChild);
507 dontSelectMap = false;
508 }));
432} 509}
433 510
434void MapeditFrame::OnRightClickTree(wxTreeEvent& event) 511void MapeditFrame::OnRightClickTree(wxTreeEvent& event)
@@ -451,6 +528,28 @@ void MapeditFrame::OnCancelSetStartpos(wxCommandEvent&)
451 mapEditor->SetIsSettingStart(false); 528 mapEditor->SetIsSettingStart(false);
452} 529}
453 530
531void MapeditFrame::OnUndo(wxCommandEvent&)
532{
533 (*currentAction)->endChanges();
534 (*currentAction)->undo();
535 currentAction++;
536
537 UpdateUndoLabels();
538}
539
540void MapeditFrame::OnRedo(wxCommandEvent&)
541{
542 if (currentAction != end(history))
543 {
544 (*currentAction)->endChanges();
545 }
546
547 currentAction--;
548 (*currentAction)->apply();
549
550 UpdateUndoLabels();
551}
552
454void MapeditFrame::NewWorld() 553void MapeditFrame::NewWorld()
455{ 554{
456 LaunchWindow(std::unique_ptr<World>(new World())); 555 LaunchWindow(std::unique_ptr<World>(new World()));
@@ -531,6 +630,10 @@ void MapeditFrame::SelectMap(Map* map)
531{ 630{
532 currentMap = map; 631 currentMap = map;
533 mapEditor->SetMap(map); 632 mapEditor->SetMap(map);
633
634 SetIsAddingEntity(false);
635 SetIsSettingStart(false);
636
534 titleBox->ChangeValue(map->getTitle()); 637 titleBox->ChangeValue(map->getTitle());
535 world->setLastMap(map); 638 world->setLastMap(map);
536} 639}
@@ -592,3 +695,49 @@ void MapeditFrame::SetStartposLabel()
592 695
593 startposLabel->SetLabel(mappos_out.str()); 696 startposLabel->SetLabel(mappos_out.str());
594} 697}
698
699void MapeditFrame::UpdateUndoLabels()
700{
701 if (currentAction != end(history))
702 {
703 menuEdit->SetLabel(MENU_EDIT_UNDO, "Undo " + (*currentAction)->getTitle() + "\tCtrl-Z");
704 menuEdit->Enable(MENU_EDIT_UNDO, true);
705 } else {
706 menuEdit->SetLabel(MENU_EDIT_UNDO, "Undo\tCtrl-Z");
707 menuEdit->Enable(MENU_EDIT_UNDO, false);
708 }
709
710 if (currentAction != begin(history))
711 {
712 menuEdit->SetLabel(MENU_EDIT_REDO, "Redo " + (*std::prev(currentAction))->getTitle() + "\tCtrl-Shift-Z");
713 menuEdit->Enable(MENU_EDIT_REDO, true);
714 } else {
715 menuEdit->SetLabel(MENU_EDIT_REDO, "Redo\tCtrl-Shift-Z");
716 menuEdit->Enable(MENU_EDIT_REDO, false);
717 }
718}
719
720void MapeditFrame::commitAction(std::shared_ptr<Undoable> action)
721{
722 action->apply();
723
724 commitAfter(action);
725}
726
727void MapeditFrame::commitAfter(std::shared_ptr<Undoable> action)
728{
729 if (currentAction != end(history))
730 {
731 (*currentAction)->endChanges();
732 }
733
734 history.erase(begin(history), currentAction);
735 currentAction = history.insert(begin(history), action);
736
737 UpdateUndoLabels();
738
739 if (history.size() > 100)
740 {
741 history.pop_back();
742 }
743}