Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Tools/PlaceObject.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/Observable.h"
21 #include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h"
22 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.h"
23 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h"
24 #include "tools/atlas/GameInterface/MessagePasser.h"
25 #include "tools/atlas/GameInterface/Messages.h"
26 #include "tools/atlas/GameInterface/SharedTypes.h"
27
28 #include <cmath>
29 #include <ctime>
30 #include <list>
31 #include <random>
32 #include <string>
33 #include <wx/chartype.h>
34 #include <wx/debug.h>
35 #include <wx/defs.h>
36 #include <wx/event.h>
37 #include <wx/object.h>
38 #include <wx/string.h>
39
40 using AtlasMessage::Position;
41
42 static float g_DefaultAngle = (float)(M_PI*3.0/4.0);
43
44 class PlaceObject : public StateDrivenTool<PlaceObject>
45 {
46 DECLARE_DYNAMIC_CLASS(PlaceObject);
47
48 Position m_ScreenPos, m_ObjPos, m_Target;
49 wxString m_ObjectID;
50 unsigned int m_ActorSeed;
51 int m_RotationDirection;
52
53 public:
54 PlaceObject(): m_RotationDirection(0)
55 {
56 SetState(&Waiting);
57 }
58
59 void SendObjectMsg(bool preview)
60 {
61 int dragDistSq =
62 (m_ScreenPos.type1.x-m_Target.type1.x)*(m_ScreenPos.type1.x-m_Target.type1.x)
63 + (m_ScreenPos.type1.y-m_Target.type1.y)*(m_ScreenPos.type1.y-m_Target.type1.y);
64 bool useTarget = (dragDistSq >= 16*16);
65 if (preview)
66 POST_MESSAGE(ObjectPreview, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed, true));
67 else
68 {
69 POST_COMMAND(CreateObject, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed));
70 RandomizeActorSeed();
71 }
72 }
73
74 virtual void Init(void* initData, ScenarioEditor* scenarioEditor)
75 {
76 StateDrivenTool<PlaceObject>::Init(initData, scenarioEditor);
77
78 wxASSERT(initData);
79 wxString& id = *static_cast<wxString*>(initData);
80 m_ObjectID = id;
81 SendObjectMsg(true);
82 }
83
84 void OnEnable()
85 {
86 RandomizeActorSeed();
87 }
88
89 void OnDisable()
90 {
91 m_ObjectID = _T("");
92 SendObjectMsg(true);
93 }
94
95 /*
96 Object placement:
97 * Select unit from list
98 * Move mouse around screen; preview of unit follows mouse
99 * Left mouse down -> remember position, fix preview to point
100 * Mouse move -> if moved > [limit], rotate unit to face mouse; else default orientation
101 * Left mouse release -> finalise placement of object on map
102
103 * Page up/down -> rotate default orientation
104
105 * Escape -> cancel placement tool
106
107 TOOD: what happens if somebody saves while the preview is active?
108 */
109
110 bool OnMouseOverride(wxMouseEvent& WXUNUSED(evt))
111 {
112 // This used to let the scroll-wheel rotate units, but that overrides
113 // the camera zoom and makes navigation very awkward, so it doesn't
114 // any more.
115 return false;
116 }
117
118 bool OnKeyOverride(wxKeyEvent& evt, KeyEventType type)
119 {
120 if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
121 {
122 SetState(&Disabled);
123 return true;
124 }
125 else if (evt.GetKeyCode() == WXK_PAGEDOWN)
126 {
127 if (type == KEY_DOWN)
128 m_RotationDirection = 1;
129 else if (type == KEY_UP)
130 m_RotationDirection = 0;
131 else
132 return false;
133 return true;
134 }
135 else if (evt.GetKeyCode() == WXK_PAGEUP)
136 {
137 if (type == KEY_DOWN)
138 m_RotationDirection = -1;
139 else if (type == KEY_UP)
140 m_RotationDirection = 0;
141 else
142 return false;
143 return true;
144 }
145 else
146 return false;
147 }
148
149 void RotateTick(float dt)
150 {
151 if (m_RotationDirection)
152 {
153 float speed = M_PI/2.f * ScenarioEditor::GetSpeedModifier(); // radians per second
154 g_DefaultAngle += (m_RotationDirection * dt * speed);
155 SendObjectMsg(true);
156 }
157 }
158
159 void RandomizeActorSeed()
160 {
161 std::mt19937 engine(std::time(nullptr));
162 std::uniform_int_distribution<unsigned int> distribution(0, 65535);
163 m_ActorSeed = distribution(engine);
164 }
165
166 struct sWaiting : public State
167 {
168 bool OnMouse(PlaceObject* obj, wxMouseEvent& evt)
169 {
170 if (obj->OnMouseOverride(evt))
171 return true;
172 else if (evt.LeftDown())
173 {
174 obj->m_ObjPos = obj->m_ScreenPos = obj->m_Target = Position(evt.GetPosition());
175 obj->SendObjectMsg(true);
176 obj->m_ObjPos = Position::Unchanged(); // make sure object is stationary even if the camera moves
177 SET_STATE(Placing);
178 return true;
179 }
180 else if (evt.Moving())
181 {
182 obj->m_ObjPos = obj->m_ScreenPos = obj->m_Target = Position(evt.GetPosition());
183 obj->SendObjectMsg(true);
184 return true;
185 }
186 else
187 return false;
188 }
189 bool OnKey(PlaceObject* obj, wxKeyEvent& evt, KeyEventType type)
190 {
191 if (type == KEY_CHAR && (evt.GetKeyCode() >= '0' && evt.GetKeyCode() <= '9'))
192 {
193 int playerID = evt.GetKeyCode() - '0';
194 obj->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerID);
195 obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
196 obj->SendObjectMsg(true);
197 return true;
198 }
199 else
200 return obj->OnKeyOverride(evt, type);
201 }
202 void OnTick(PlaceObject* obj, float dt)
203 {
204 obj->RotateTick(dt);
205 }
206 }
207 Waiting;
208
209 struct sPlacing : public State
210 {
211 bool OnMouse(PlaceObject* obj, wxMouseEvent& evt)
212 {
213 if (obj->OnMouseOverride(evt))
214 return true;
215 else if (evt.LeftUp())
216 {
217 obj->m_Target = Position(evt.GetPosition());
218 // Create the actual object
219 obj->SendObjectMsg(false);
220 // Go back to preview mode
221 SET_STATE(Waiting);
222 obj->m_ObjPos = obj->m_ScreenPos = obj->m_Target;
223 obj->SendObjectMsg(true);
224 return true;
225 }
226 else if (evt.Dragging())
227 {
228 obj->m_Target = Position(evt.GetPosition());
229 obj->SendObjectMsg(true);
230 return true;
231 }
232 else
233 return false;
234 }
235 bool OnKey(PlaceObject* obj, wxKeyEvent& evt, KeyEventType type)
236 {
237 return obj->OnKeyOverride(evt, type);
238 }
239 void OnTick(PlaceObject* obj, float dt)
240 {
241 obj->RotateTick(dt);
242 }
243 }
244 Placing;
245 };
246
247 IMPLEMENT_DYNAMIC_CLASS(PlaceObject, StateDrivenTool<PlaceObject>);