Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Tools/TransformObject.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/MiscState.h"
24 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.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 <algorithm>
32 #include <cstddef>
33 #include <limits>
34 #include <list>
35 #include <string>
36 #include <vector>
37 #include <wx/chartype.h>
38 #include <wx/clipbrd.h>
39 #include <wx/dataobj.h>
40 #include <wx/debug.h>
41 #include <wx/defs.h>
42 #include <wx/event.h>
43 #include <wx/gdicmn.h>
44 #include <wx/object.h>
45 #include <wx/sstream.h>
46 #include <wx/string.h>
47 #include <wx/utils.h>
48 #include <wx/xml/xml.h>
49
50 class wxInputStream;
51
52 using AtlasMessage::Position;
53
54 class TransformObject : public StateDrivenTool<TransformObject>
55 {
56 DECLARE_DYNAMIC_CLASS(TransformObject);
57
58 int m_dx, m_dy;
59 AtlasMessage::ObjectID m_lastSelected;
60 wxPoint m_startPoint;
61 Position m_entPosition;
62
63 // TODO: If we don't plan to change hotkeys, just replace with evt.ShiftDown(), etc.
64 static const wxKeyCode SELECTION_ADD_HOTKEY = WXK_SHIFT;
65 static const wxKeyCode SELECTION_REMOVE_HOTKEY = WXK_CONTROL; // COMMAND on Macs
66 static const wxKeyCode SELECTION_ACTORS_HOTKEY = WXK_ALT;
67
68 public:
69 TransformObject() : m_lastSelected(0)
70 {
71 SetState(&Waiting);
72 }
73
74 virtual void OnCommand(const wxString& command, void* WXUNUSED(userData))
75 {
76 if (command == _T("copy"))
77 OnCopy();
78 else if (command == _T("paste"))
79 OnPasteStart();
80 }
81
82 void OnDisable()
83 {
84 g_SelectedObjects.clear();
85 g_SelectedObjects.NotifyObservers();
86 POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
87 }
88
89 bool OnCopy() const
90 {
91 if (g_SelectedObjects.empty())
92 return false;
93
94 AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
95 info.Post();
96
97 if (wxTheClipboard->Open())
98 {
99 wxString text(info.xmldata.c_str());
100 wxTheClipboard->SetData(new wxTextDataObject(text));
101 wxTheClipboard->Close();
102 }
103
104 return true;
105 }
106
107 void OnPasteStart()
108 {
109 wxString entities;
110 if (wxTheClipboard->Open())
111 {
112 if (wxTheClipboard->IsSupported(wxDF_TEXT))
113 {
114 wxTextDataObject data;
115 wxTheClipboard->GetData(data);
116 entities = data.GetText();
117 }
118 wxTheClipboard->Close();
119 }
120
121 // First do we need to check if it is a correct xml string
122 wxInputStream* is = new wxStringInputStream(entities);
123 wxXmlDocument doc;
124 if (!doc.Load(*is))
125 return;
126
127 // Entities, Entity(1.*)
128 wxXmlNode* root = doc.GetRoot();
129 if (root->GetName() != wxT("Entities"))
130 return;
131
132 // Template, position,orientation
133 const wxXmlNode* child = root->GetChildren();
134
135 while (child)
136 {
137 if (child->GetName() != wxT("Entity"))
138 return;
139
140 child = child->GetNext();
141 }
142
143 g_SelectedObjects.clear();
144 POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
145
146 // is the source code get here now you can add the objects to scene(preview)
147 // store id to move
148 child = root->GetChildren();
149
150 while (child)
151 {
152 wxString templateName;
153 Position entityPos;
154 long playerId = 0;
155 double orientation = 0;
156 unsigned int actorSeed = 0;
157
158 const wxXmlNode* xmlData = child->GetChildren();
159
160 while (xmlData)
161 {
162 if (xmlData->GetName() == wxT("Template"))
163 templateName = xmlData->GetNodeContent();
164 else if (xmlData->GetName() == wxT("Position"))
165 {
166 wxString x, z;
167 xmlData->GetAttribute(wxT("x"), &x);
168 xmlData->GetAttribute(wxT("z"), &z);
169
170 double aux, aux2;
171 x.ToDouble(&aux);
172 z.ToDouble(&aux2);
173
174 entityPos = Position(aux, 0, aux2);
175 }
176 else if (xmlData->GetName() == wxT("Orientation"))
177 {
178 wxString y;
179 xmlData->GetAttribute(wxT("y"), &y);
180 y.ToDouble(&orientation);
181 }
182 else if (xmlData->GetName() == wxT("Player"))
183 {
184 wxString x(xmlData->GetNodeContent());
185 x.ToLong(&playerId);
186 }
187 else if (xmlData->GetName() == wxT("ActorSeed"))
188 {
189 wxString x(xmlData->GetNodeContent());
190 unsigned long xTmp = 0;
191 x.ToULong(&xTmp);
192 wxASSERT(xTmp <= (unsigned long)std::numeric_limits<unsigned int>::max());
193 actorSeed = xTmp;
194 }
195
196 xmlData = xmlData->GetNext();
197 }
198
199 //Update current Ownership
200 this->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerId);
201 this->GetScenarioEditor().GetObjectSettings().NotifyObservers();
202
203 POST_MESSAGE(ObjectPreview, ((std::wstring)templateName.c_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), entityPos, false, Position(), orientation, actorSeed, false));
204
205 child = child->GetNext();
206 }
207
208 //Set state paste for preview the new objects
209 this->SetState(&Pasting);
210
211 //Update the objects to current mouse position
212 OnMovingPaste();
213 }
214
215 void OnMovingPaste()
216 {
217 //Move the preview(s) object(s)
218 POST_MESSAGE(MoveObjectPreview, ((m_entPosition)));
219 }
220
221 void OnPasteEnd(bool canceled)
222 {
223 if (canceled)
224 //delete previews objects
225 POST_MESSAGE(ObjectPreview, (_T(""), GetScenarioEditor().GetObjectSettings().GetSettings(), Position(), false, Position(), 0, 0, true));
226 else
227 {
228 ScenarioEditor::GetCommandProc().FinaliseLastCommand();
229
230 //Create new Objects and delete preview objects
231 POST_MESSAGE(ObjectPreviewToEntity, ());
232
233 AtlasMessage::qGetCurrentSelection currentSelection;
234 currentSelection.Post();
235
236 g_SelectedObjects = *currentSelection.ids;
237 }
238
239
240 //when all is done set default state
241 this->SetState(&Waiting);
242 }
243
244 // TODO: keys to rotate/move object?
245
246 struct sWaiting : public State
247 {
248 bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
249 {
250 if (evt.LeftDClick() && AtlasMessage::ObjectIDIsValid(obj->m_lastSelected))
251 {
252 SET_STATE(SelectSimilar);
253 return true;
254 }
255 else if (evt.LeftDown())
256 {
257 bool selectionAdd = wxGetKeyState(SELECTION_ADD_HOTKEY);
258 bool selectionRemove = wxGetKeyState(SELECTION_REMOVE_HOTKEY);
259 bool selectionActors = wxGetKeyState(SELECTION_ACTORS_HOTKEY);
260
261 // New selection - never merge with movements of other objects
262 ScenarioEditor::GetCommandProc().FinaliseLastCommand();
263
264 // Select the object clicked on:
265
266 AtlasMessage::qPickObject qry(Position(evt.GetPosition()), selectionActors);
267 qry.Post();
268
269 // Check they actually clicked on a valid object
270 if (AtlasMessage::ObjectIDIsValid(qry.id))
271 {
272 std::vector<AtlasMessage::ObjectID>::iterator it = std::find(g_SelectedObjects.begin(), g_SelectedObjects.end(), qry.id);
273 bool objectIsSelected = (it != g_SelectedObjects.end());
274
275 if (selectionRemove)
276 {
277 // Remove from selection
278 if (objectIsSelected)
279 g_SelectedObjects.erase(it);
280 }
281 else if (!objectIsSelected)
282 {
283 // Add to selection
284 if (!selectionAdd)
285 g_SelectedObjects.clear();
286
287 g_SelectedObjects.push_back(qry.id);
288 }
289
290 obj->m_lastSelected = qry.id;
291
292 // If we're selecting the whole group
293 if (!selectionAdd && !selectionRemove && !g_SelectedObjects.empty())
294 {
295 // Remember the screen-space offset of the mouse from the
296 // object's centre, so we can add that back when moving it
297 // (instead of just moving the object's centre to directly
298 // beneath the mouse)
299 obj->m_dx = qry.offsetx;
300 obj->m_dy = qry.offsety;
301 SET_STATE(Dragging);
302 }
303
304 g_SelectedObjects.NotifyObservers();
305 POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
306 }
307 else
308 {
309 // Bandboxing
310 obj->m_lastSelected = 0;
311 obj->m_startPoint = evt.GetPosition();
312 SET_STATE(Bandboxing);
313 }
314
315 return true;
316 }
317 else if (!g_SelectedObjects.empty() && ((evt.Dragging() && evt.RightIsDown()) || evt.RightDown()))
318 {
319 SET_STATE(Rotating);
320 return true;
321 }
322 else if (evt.Moving())
323 {
324 //Save position for smooth paste position
325 obj->m_entPosition = Position(evt.GetPosition());
326
327 // Prevent certain events from reaching game UI in this mode
328 // to prevent selection ring confusion
329 return true;
330 }
331 else
332 return false;
333 }
334
335 bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
336 {
337 if (type == KEY_CHAR && evt.GetKeyCode() == WXK_DELETE)
338 {
339 POST_COMMAND(DeleteObjects, (g_SelectedObjects));
340
341 g_SelectedObjects.clear();
342 g_SelectedObjects.NotifyObservers();
343
344 POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
345 return true;
346 }
347 else if (type == KEY_CHAR && (evt.GetKeyCode() >= '0' && evt.GetKeyCode() <= '9'))
348 {
349 int playerID = evt.GetKeyCode() - '0';
350 obj->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerID);
351 obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
352 return true;
353 }
354 else if (evt.GetModifiers() == wxMOD_CONTROL)
355 {
356 if (evt.GetKeyCode() == 'C')
357 return obj->OnCopy();
358 else if (evt.GetKeyCode() == 'V')
359 {
360 obj->OnPasteStart();
361 return true;
362 }
363 }
364
365 return false;
366 }
367 }
368 Waiting;
369
370 struct sDragging : public State
371 {
372 bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
373 {
374 if (evt.LeftUp())
375 {
376 POST_MESSAGE(ResetSelectionColor, ());
377 SET_STATE(Waiting);
378 return true;
379 }
380 else if (evt.Dragging())
381 {
382 Position pos(evt.GetPosition() + wxPoint(obj->m_dx, obj->m_dy));
383
384 POST_COMMAND(MoveObjects, (g_SelectedObjects, obj->m_lastSelected, pos));
385 return true;
386 }
387 else
388 return false;
389 }
390
391 bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
392 {
393 if (type == KEY_UP && evt.GetKeyCode() == WXK_ESCAPE)
394 {
395 // Cancel move action
396 ScenarioEditor::GetCommandProc().FinaliseLastCommand();
397 ScenarioEditor::GetCommandProc().Undo();
398 SET_STATE(Waiting);
399
400 return true;
401 }
402 else
403 return false;
404 }
405 }
406 Dragging;
407
408 struct sBandboxing : public State
409 {
410 bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
411 {
412 if (evt.LeftIsDown() && evt.Dragging())
413 {
414 // Update bandbox overlay
415 POST_MESSAGE(SetBandbox, (true, obj->m_startPoint.x, obj->m_startPoint.y, evt.GetPosition().x, evt.GetPosition().y));
416 return true;
417 }
418 else if (evt.LeftUp())
419 {
420 bool selectionAdd = wxGetKeyState(SELECTION_ADD_HOTKEY);
421 bool selectionRemove = wxGetKeyState(SELECTION_REMOVE_HOTKEY);
422 bool selectionActors = wxGetKeyState(SELECTION_ACTORS_HOTKEY);
423
424 // Now we have both corners of the box, pick objects
425 AtlasMessage::qPickObjectsInRect qry(Position(obj->m_startPoint), Position(evt.GetPosition()), selectionActors);
426 qry.Post();
427
428 std::vector<AtlasMessage::ObjectID> ids = *qry.ids;
429
430 if (!selectionAdd && !selectionRemove)
431 {
432 // Just copy new selections (clears list if no selections)
433 g_SelectedObjects = ids;
434 }
435 else
436 {
437 for (size_t i = 0; i < ids.size(); ++i)
438 {
439 std::vector<AtlasMessage::ObjectID>::iterator it = std::find(g_SelectedObjects.begin(), g_SelectedObjects.end(), ids[i]);
440 bool objectIsSelected = (it != g_SelectedObjects.end());
441 if (selectionRemove)
442 {
443 // Remove from selection
444 if (objectIsSelected)
445 g_SelectedObjects.erase(it);
446 }
447 else if (!objectIsSelected)
448 {
449 // Add to selection
450 g_SelectedObjects.push_back(ids[i]);
451 }
452 }
453 }
454
455 POST_MESSAGE(SetBandbox, (false, 0, 0, 0, 0));
456
457 g_SelectedObjects.NotifyObservers();
458 POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
459
460 SET_STATE(Waiting);
461 return true;
462 }
463 else
464 return false;
465 }
466
467 bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
468 {
469 if (type == KEY_UP && evt.GetKeyCode() == WXK_ESCAPE)
470 {
471 // Clear bandbox and return to waiting state
472 POST_MESSAGE(SetBandbox, (false, 0, 0, 0, 0));
473 SET_STATE(Waiting);
474 return true;
475 }
476 else
477 return false;
478 }
479 }
480 Bandboxing;
481
482 struct sSelectSimilar : public State
483 {
484 bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
485 {
486 if (evt.LeftUp())
487 {
488 bool selectionAdd = wxGetKeyState(SELECTION_ADD_HOTKEY);
489 bool selectionRemove = wxGetKeyState(SELECTION_REMOVE_HOTKEY);
490
491 // Select similar objects
492 AtlasMessage::qPickSimilarObjects qry(obj->m_lastSelected);
493 qry.Post();
494
495 std::vector<AtlasMessage::ObjectID> ids = *qry.ids;
496
497 if (!selectionAdd && !selectionRemove)
498 {
499 // Just copy new selections (clears list if no selections)
500 g_SelectedObjects = ids;
501 }
502 else
503 {
504 for (size_t i = 0; i < ids.size(); ++i)
505 {
506 std::vector<AtlasMessage::ObjectID>::iterator it = std::find(g_SelectedObjects.begin(), g_SelectedObjects.end(), ids[i]);
507 bool objectIsSelected = (it != g_SelectedObjects.end());
508 if (selectionRemove)
509 {
510 // Remove from selection
511 if (objectIsSelected)
512 g_SelectedObjects.erase(it);
513 }
514 else if (!objectIsSelected)
515 {
516 // Add to selection
517 g_SelectedObjects.push_back(ids[i]);
518 }
519 }
520 }
521
522 g_SelectedObjects.NotifyObservers();
523 POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
524
525 SET_STATE(Waiting);
526 return true;
527 }
528 else
529 return false;
530 }
531 }
532 SelectSimilar;
533
534 struct sPasting : public State
535 {
536 bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
537 {
538 if (evt.Moving())
539 {
540 //Move the object
541 obj->m_entPosition = Position(evt.GetPosition());
542 obj->OnMovingPaste();
543 return true;
544 }
545 else if (evt.LeftDown())
546 {
547 //Place the object and update
548 obj->OnPasteEnd(false);
549 return true;
550 }
551 else
552 return false;
553 }
554 bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
555 {
556 if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
557 {
558 obj->OnPasteEnd(true);
559 return true;
560 }
561 else
562 return false;
563 }
564 }
565 Pasting;
566
567 struct sRotating : public State
568 {
569 bool fromCenterPoint;
570
571 void OnEnter(TransformObject* WXUNUSED(obj))
572 {
573 fromCenterPoint = true;
574 }
575
576 bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
577 {
578 if (evt.RightUp())
579 {
580 POST_MESSAGE(ResetSelectionColor, ());
581 SET_STATE(Waiting);
582 return true;
583 }
584 else if (evt.Dragging())
585 {
586 bool fromGlobalAndIndividualCenterPoints = !evt.ControlDown() && !evt.ShiftDown();
587 bool newFromCenterPoint = evt.ShiftDown() || fromGlobalAndIndividualCenterPoints;
588 if (newFromCenterPoint != fromCenterPoint)
589 {
590 ScenarioEditor::GetCommandProc().FinaliseLastCommand();
591 fromCenterPoint = newFromCenterPoint;
592 }
593
594 Position pos(evt.GetPosition());
595
596 if (fromCenterPoint)
597 POST_COMMAND(RotateObjectsFromCenterPoint, (g_SelectedObjects, pos, fromGlobalAndIndividualCenterPoints));
598 else
599 POST_COMMAND(RotateObject, (g_SelectedObjects, pos));
600
601 return true;
602 }
603
604 return false;
605 }
606
607 bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
608 {
609 if (type == KEY_UP && evt.GetKeyCode() == WXK_ESCAPE)
610 {
611 // Cancel move action
612 ScenarioEditor::GetCommandProc().FinaliseLastCommand();
613 ScenarioEditor::GetCommandProc().Undo();
614 SET_STATE(Waiting);
615
616 return true;
617 }
618
619 return false;
620 }
621 }
622 Rotating;
623 };
624
625 IMPLEMENT_DYNAMIC_CLASS(TransformObject, StateDrivenTool<TransformObject>);