Debian Codegraph @

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

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 #ifndef INCLUDED_TOOLS
19 #define INCLUDED_TOOLS
20
21 #include "tools/atlas/AtlasUI/General/AtlasWindowCommand.h"
22 #include "tools/atlas/AtlasUI/General/AtlasWindowCommandProc.h" // IWYU pragma: keep - required for POST_COMMAND
23
24 #include <cstddef>
25 #include <wx/debug.h>
26 #include <wx/defs.h>
27 #include <wx/object.h>
28
29 class ScenarioEditor;
30 class wxKeyEvent;
31 class wxMouseEvent;
32 class wxString;
33 class wxToolBar;
34 template <typename T> class ObservablePtr;
35
36 class ITool : public wxObject
37 {
38 public:
39 enum KeyEventType { KEY_DOWN, KEY_UP, KEY_CHAR };
40
41 virtual void Init(void* initData, ScenarioEditor* scenarioEditor) = 0;
42 virtual void Shutdown() = 0;
43 virtual bool OnMouse(wxMouseEvent& evt) = 0; // return true if handled
44 virtual bool OnKey(wxKeyEvent& evt, KeyEventType dir) = 0; // return true if handled
45 virtual void OnTick(float dt) = 0; // dt in seconds
46 virtual void OnCommand(const wxString& command, void* userData) = 0;
47
48 virtual ~ITool() {};
49 };
50
51 struct ToolManagerImpl;
52 class ToolManager
53 {
54 private: // NONCOPYABLE
55 ToolManager(const ToolManager&);
56 const ToolManager& operator=(const ToolManager&);
57
58 public:
59 ToolManager(ScenarioEditor* scenarioEditor);
60 ~ToolManager();
61 ObservablePtr<ITool>& GetCurrentTool();
62 wxString GetCurrentToolName();
63 void SetCurrentTool(const wxString& name, void* initData = NULL);
64 private:
65 ToolManagerImpl* m;
66 ScenarioEditor* m_ScenarioEditor;
67 };
68
69 class ToolButton;
70 extern void RegisterToolButton(ToolButton* button, const wxString& toolName);
71 extern void RegisterToolBarButton(wxToolBar* toolbar, int buttonId, const wxString& toolName);
72
73 //////////////////////////////////////////////////////////////////////////
74
75
76 namespace AtlasMessage { struct mWorldCommand; }
77 class WorldCommand : public AtlasWindowCommand
78 {
79 DECLARE_CLASS(WorldCommand);
80
81 bool m_AlreadyDone;
82 public:
83 WorldCommand(AtlasMessage::mWorldCommand* command);
84 ~WorldCommand();
85 bool Do();
86 bool Undo();
87 bool Merge(AtlasWindowCommand* previousCommand);
88
89 private:
90 AtlasMessage::mWorldCommand* m_Command;
91 };
92
93 #define POST_COMMAND(type, data) ScenarioEditor::GetCommandProc().Submit(new WorldCommand(new AtlasMessage::m##type(AtlasMessage::d##type data)))
94
95 //////////////////////////////////////////////////////////////////////////
96
97 #define SET_STATE(s) obj->SetState(&obj->s)
98
99 template <typename T>
100 class StateDrivenTool : public ITool
101 {
102 public:
103 StateDrivenTool()
104 : m_CurrentState(&Disabled), m_ScenarioEditor(NULL)
105 {
106 }
107
108 virtual void Init(void* WXUNUSED(initData), ScenarioEditor* scenarioEditor)
109 {
110 m_ScenarioEditor = scenarioEditor;
111 }
112
113 virtual void Shutdown()
114 {
115 // This can't be done in the destructor, because ~StateDrivenTool
116 // is not called until after the subclass has been destroyed and its
117 // vtable (containing OnDisable) has been removed.
118 SetState(&Disabled);
119 }
120
121 protected:
122 // Called when the tool is enabled/disabled; always called in zero or
123 // more enable-->disable pairs per object instance.
124 virtual void OnEnable() {}
125 virtual void OnDisable() {}
126
127 struct State
128 {
129 virtual ~State() {}
130 virtual void OnEnter(T* WXUNUSED(obj)) {}
131 virtual void OnLeave(T* WXUNUSED(obj)) {}
132 virtual void OnTick (T* WXUNUSED(obj), float WXUNUSED(dt)) {}
133
134 // Should return true if the event has been handled (else the event will
135 // be passed to a lower-priority level)
136 virtual bool OnMouse(T* WXUNUSED(obj), wxMouseEvent& WXUNUSED(evt)) { return false; }
137 virtual bool OnKey(T* WXUNUSED(obj), wxKeyEvent& WXUNUSED(evt), KeyEventType WXUNUSED(type)) { return false; }
138 };
139
140
141 struct sDisabled : public State
142 {
143 void OnEnter(T* obj) { obj->OnDisable(); }
144 void OnLeave(T* obj) { obj->OnEnable(); }
145 }
146 Disabled;
147
148 void SetState(State* state)
149 {
150 m_CurrentState->OnLeave(static_cast<T*>(this));
151 // this cast is safe as long as the class is used as in
152 // "class Something : public StateDrivenTool<Something> { ... }"
153 m_CurrentState = state;
154 m_CurrentState->OnEnter(static_cast<T*>(this));
155 }
156
157 ScenarioEditor& GetScenarioEditor() { wxASSERT(m_ScenarioEditor); return *m_ScenarioEditor; }
158
159 private:
160 State* m_CurrentState;
161
162 ScenarioEditor* m_ScenarioEditor; // not NULL, except before Init has been called
163
164 virtual bool OnMouse(wxMouseEvent& evt)
165 {
166 return m_CurrentState->OnMouse(static_cast<T*>(this), evt);
167 }
168
169 virtual bool OnKey(wxKeyEvent& evt, KeyEventType dir)
170 {
171 return m_CurrentState->OnKey(static_cast<T*>(this), evt, dir);
172 }
173
174 virtual void OnTick(float dt)
175 {
176 m_CurrentState->OnTick(static_cast<T*>(this), dt);
177 }
178
179 virtual void OnCommand(const wxString& WXUNUSED(command), void* WXUNUSED(userData))
180 {
181 }
182 };
183
184
185 #endif // INCLUDED_TOOLS