Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Tools/PaintTerrain.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/atlas/AtlasUI/General/AtlasWindowCommandProc.h"
21 #include "tools/atlas/AtlasUI/General/Observable.h"
22 #include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h"
23 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Brushes.h"
24 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/MiscState.h"
25 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h"
26 #include "tools/atlas/GameInterface/MessagePasser.h"
27 #include "tools/atlas/GameInterface/Messages.h"
28 #include "tools/atlas/GameInterface/Shareable.h"
29 #include "tools/atlas/GameInterface/SharedTypes.h"
30
31 #include <list>
32 #include <string>
33 #include <wx/defs.h>
34 #include <wx/event.h>
35 #include <wx/gdicmn.h>
36 #include <wx/object.h>
37 #include <wx/string.h>
38
39 using AtlasMessage::Position;
40
41 class PaintTerrain : public StateDrivenTool<PaintTerrain>
42 {
43 DECLARE_DYNAMIC_CLASS(PaintTerrain);
44
45 Position m_Pos;
46
47 // Brush for eyedropper preview
48 // (it's confusing if we use the arbitrarily sized paint brush)
49 Brush m_EyedropperBrush;
50
51 static const wxKeyCode EYEDROPPER_HOTKEY = WXK_SHIFT;
52
53 public:
54 PaintTerrain()
55 {
56 SetState(&Waiting);
57
58 m_EyedropperBrush.SetSquare(2);
59 }
60
61
62 void OnEnable()
63 {
64 // TODO: multiple independent brushes?
65 g_Brush_Elevation.MakeActive();
66 }
67
68 void OnDisable()
69 {
70 POST_MESSAGE(BrushPreview, (false, Position()));
71 }
72
73 struct sWaiting : public State
74 {
75 bool OnKey(PaintTerrain* obj, wxKeyEvent& evt, KeyEventType type)
76 {
77 if (type == KEY_DOWN && evt.GetKeyCode() == EYEDROPPER_HOTKEY)
78 {
79 SET_STATE(Eyedropper);
80 return true;
81 }
82 else
83 {
84 return false;
85 }
86 }
87
88 bool OnMouse(PaintTerrain* obj, wxMouseEvent& evt)
89 {
90 if (evt.LeftDown())
91 {
92 obj->m_Pos = Position(evt.GetPosition());
93 SET_STATE(PaintingHigh);
94 return true;
95 }
96 else if (evt.RightDown())
97 {
98 obj->m_Pos = Position(evt.GetPosition());
99 SET_STATE(PaintingLow);
100 return true;
101 }
102 else if (evt.Moving())
103 {
104 POST_MESSAGE(BrushPreview, (true, Position(evt.GetPosition())));
105 return true;
106 }
107 else
108 {
109 return false;
110 }
111 }
112 }
113 Waiting;
114
115
116 struct sPainting_common : public State
117 {
118 void OnEnter(PaintTerrain* obj)
119 {
120 Paint(obj);
121 }
122
123 void OnLeave(PaintTerrain*)
124 {
125 ScenarioEditor::GetCommandProc().FinaliseLastCommand();
126 }
127
128 bool OnMouse(PaintTerrain* obj, wxMouseEvent& evt)
129 {
130 if (IsMouseUp(evt))
131 {
132 SET_STATE(Waiting);
133 return true;
134 }
135 else if (evt.Dragging())
136 {
137 wxPoint pos = evt.GetPosition();
138 obj->m_Pos = Position(pos);
139 Paint(obj);
140 return true;
141 }
142 else
143 {
144 return false;
145 }
146 }
147
148 void Paint(PaintTerrain* obj)
149 {
150 POST_MESSAGE(BrushPreview, (true, obj->m_Pos));
151 POST_COMMAND(PaintTerrain, (obj->m_Pos, (std::wstring)g_SelectedTexture.wc_str(), GetPriority()));
152 }
153
154 virtual bool IsMouseUp(wxMouseEvent& evt) = 0;
155 virtual int GetPriority() = 0;
156 };
157
158 struct sPaintingHigh : public sPainting_common
159 {
160 bool IsMouseUp(wxMouseEvent& evt) { return evt.LeftUp(); }
161 int GetPriority() { return AtlasMessage::ePaintTerrainPriority::HIGH; }
162 }
163 PaintingHigh;
164
165 struct sPaintingLow : public sPainting_common
166 {
167 bool IsMouseUp(wxMouseEvent& evt) { return evt.RightUp(); }
168 int GetPriority() { return AtlasMessage::ePaintTerrainPriority::LOW; }
169 }
170 PaintingLow;
171
172 struct sEyedropper : public State
173 {
174 void OnEnter(PaintTerrain* obj)
175 {
176 obj->m_EyedropperBrush.MakeActive();
177 }
178
179 void OnLeave(PaintTerrain* WXUNUSED(obj))
180 {
181 g_Brush_Elevation.MakeActive();
182 }
183
184 bool OnKey(PaintTerrain* obj, wxKeyEvent& evt, KeyEventType type)
185 {
186 if (type == KEY_UP && evt.GetKeyCode() == EYEDROPPER_HOTKEY)
187 {
188 SET_STATE(Waiting);
189 return true;
190 }
191 else
192 {
193 return false;
194 }
195 }
196
197 bool OnMouse(PaintTerrain* WXUNUSED(obj), wxMouseEvent& evt)
198 {
199 if (evt.ButtonDown() || evt.Dragging())
200 {
201 POST_MESSAGE(BrushPreview, (true, evt.GetPosition()));
202 AtlasMessage::qGetTerrainTexture qry(evt.GetPosition());
203 qry.Post();
204
205 g_SelectedTexture = wxString(qry.texture.c_str());
206 g_SelectedTexture.NotifyObservers();
207 return true;
208 }
209 else if (evt.Moving())
210 {
211 POST_MESSAGE(BrushPreview, (true, Position(evt.GetPosition())));
212 return true;
213 }
214 else
215 {
216 return false;
217 }
218 }
219 }
220 Eyedropper;
221 };
222
223 IMPLEMENT_DYNAMIC_CLASS(PaintTerrain, StateDrivenTool<PaintTerrain>);