Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.cpp

raw
1 /* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "precompiled.h"
19
20 #include "Tools.h"
21
22 #include "tools/atlas/AtlasUI/CustomControls/Buttons/ToolButton.h"
23 #include "tools/atlas/AtlasUI/General/AtlasWindowCommand.h"
24 #include "tools/atlas/AtlasUI/General/Observable.h"
25 #include "tools/atlas/GameInterface/MessagePasser.h"
26 #include "tools/atlas/GameInterface/Messages.h"
27
28 #include <list>
29 #include <vector>
30 #include <wx/object.h>
31 #include <wx/string.h>
32 #include <wx/toolbar.h>
33
34 class DummyTool : public ITool
35 {
36 void Init(void*, ScenarioEditor*) {}
37 void Shutdown() {}
38 bool OnMouse(wxMouseEvent& WXUNUSED(evt)) { return false; }
39 bool OnKey(wxKeyEvent& WXUNUSED(evt), KeyEventType) { return false; }
40 void OnTick(float) {}
41 void OnCommand(const wxString& WXUNUSED(command), void* WXUNUSED(userData)) {}
42 } defaultTool;
43
44 struct ToolManagerImpl
45 {
46 ToolManagerImpl() : CurrentTool(&defaultTool) {}
47
48 ObservablePtr<ITool> CurrentTool;
49 wxString CurrentToolName;
50 };
51
52 ToolManager::ToolManager(ScenarioEditor* scenarioEditor)
53 : m(new ToolManagerImpl), m_ScenarioEditor(scenarioEditor)
54 {
55 }
56
57 ToolManager::~ToolManager()
58 {
59 delete m;
60 }
61
62 ObservablePtr<ITool>& ToolManager::GetCurrentTool()
63 {
64 return m->CurrentTool;
65 }
66
67 wxString ToolManager::GetCurrentToolName()
68 {
69 return m->CurrentTool->GetClassInfo()->GetClassName();
70 }
71
72 void SetActive(bool active, const wxString& name);
73
74 void ToolManager::SetCurrentTool(const wxString& name, void* initData)
75 {
76 if (*m->CurrentTool != &defaultTool)
77 {
78 m->CurrentTool->Shutdown();
79 delete* m->CurrentTool;
80 m->CurrentTool = &defaultTool;
81 }
82
83 SetActive(false, m->CurrentToolName);
84
85 ITool* tool = NULL;
86 if (name.Len())
87 {
88 tool = wxDynamicCast(wxCreateDynamicObject(name), ITool);
89 wxASSERT(tool);
90 }
91
92 if (tool)
93 {
94 m->CurrentTool = tool;
95 tool->Init(initData, m_ScenarioEditor);
96 }
97
98 m->CurrentToolName = name;
99 SetActive(true, m->CurrentToolName);
100
101 m->CurrentTool.NotifyObservers();
102 }
103
104 //////////////////////////////////////////////////////////////////////////
105
106 struct toolbarButton
107 {
108 wxString name;
109 wxToolBar* toolbar;
110 int id;
111 };
112 struct toolButton
113 {
114 wxString name;
115 ToolButton* button;
116 };
117
118 typedef std::vector<toolbarButton> toolbarButtons_t;
119 typedef std::vector<toolButton> toolButtons_t;
120
121 static toolbarButtons_t toolbarButtons;
122 static toolButtons_t toolButtons;
123
124 void SetActive(bool active, const wxString& name)
125 {
126 for (toolbarButtons_t::iterator it = toolbarButtons.begin(); it != toolbarButtons.end(); ++it)
127 if (it->name == name)
128 it->toolbar->ToggleTool(it->id, active);
129
130 for (toolButtons_t::iterator it = toolButtons.begin(); it != toolButtons.end(); ++it)
131 if (it->name == name)
132 it->button->SetSelectedAppearance(active);
133 }
134
135 void RegisterToolButton(ToolButton* button, const wxString& toolName)
136 {
137 toolButton b;
138 b.name = toolName;
139 b.button = button;
140 toolButtons.push_back(b);
141 }
142
143 void RegisterToolBarButton(wxToolBar* toolbar, int buttonId, const wxString& toolName)
144 {
145 toolbarButton b;
146 b.name = toolName;
147 b.toolbar = toolbar;
148 b.id = buttonId;
149 toolbarButtons.push_back(b);
150 }
151
152
153 //////////////////////////////////////////////////////////////////////////
154
155 IMPLEMENT_CLASS(WorldCommand, AtlasWindowCommand);
156
157 WorldCommand::WorldCommand(AtlasMessage::mWorldCommand* command)
158 : AtlasWindowCommand(true, wxString::FromAscii(command->GetName())), m_Command(command), m_AlreadyDone(false)
159 {
160 }
161
162 WorldCommand::~WorldCommand()
163 {
164 // m_Command was allocated by POST_COMMAND
165 delete m_Command;
166 }
167
168 bool WorldCommand::Do()
169 {
170 if (m_AlreadyDone)
171 POST_MESSAGE(RedoCommand, ());
172 else
173 {
174 // The DoCommand message clones the data from m_Command, and posts that
175 // (passing ownership to the game), so we're free to delete m_Command
176 // at any time
177 POST_MESSAGE(DoCommand, (m_Command));
178 m_AlreadyDone = true;
179 }
180 return true;
181 }
182
183 bool WorldCommand::Undo()
184 {
185 POST_MESSAGE(UndoCommand, ());
186 return true;
187 }
188
189 bool WorldCommand::Merge(AtlasWindowCommand* p)
190 {
191 WorldCommand* prev = wxDynamicCast(p, WorldCommand);
192
193 if (!prev)
194 return false;
195
196 if (m_Command->GetName() != prev->m_Command->GetName()) // comparing char* pointers, because they're unique-per-class constants
197 return false;
198
199 if (!m_Command->IsMergeable())
200 return false;
201
202 POST_MESSAGE(MergeCommand, ());
203 return true;
204 }