File: TransformObject.cpp

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (613 lines) | stat: -rw-r--r-- 15,705 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* Copyright (C) 2015 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "ScenarioEditor/ScenarioEditor.h"
#include "Common/Tools.h"
#include "Common/Brushes.h"
#include "Common/MiscState.h"
#include "Common/ObjectSettings.h"
#include "GameInterface/Messages.h"
#include <wx/clipbrd.h>
#include <wx/xml/xml.h>
#include <wx/sstream.h>
#include <wx/version.h>

using AtlasMessage::Position;

class TransformObject : public StateDrivenTool<TransformObject>
{
	DECLARE_DYNAMIC_CLASS(TransformObject);

	int m_dx, m_dy;
	AtlasMessage::ObjectID m_lastSelected;
	wxPoint m_startPoint;
	Position m_entPosition;

	// TODO: If we don't plan to change hotkeys, just replace with evt.ShiftDown(), etc.
	static const wxKeyCode SELECTION_ADD_HOTKEY = WXK_SHIFT;
	static const wxKeyCode SELECTION_REMOVE_HOTKEY = WXK_CONTROL;	// COMMAND on Macs
	static const wxKeyCode SELECTION_ACTORS_HOTKEY = WXK_ALT;

public:
	TransformObject() : m_lastSelected(0)
	{
		SetState(&Waiting);
	}

    virtual void OnCommand(const wxString& command, void* WXUNUSED(userData))
    {
        if (command == _T("copy"))
            OnCopy();
        else if (command == _T("paste"))
            OnPasteStart();
    }

	void OnDisable()
	{
		g_SelectedObjects.clear();
		g_SelectedObjects.NotifyObservers();
		POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
	}

    bool OnCopy() const
    {
        if (g_SelectedObjects.empty())
            return false;

        AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
        info.Post();

        if (wxTheClipboard->Open())
        {
            wxString text(info.xmldata.c_str());
            wxTheClipboard->SetData(new wxTextDataObject(text));
            wxTheClipboard->Close();
        }

        return true;
    }

	void OnPasteStart()
	{
		wxString entities;
		if (wxTheClipboard->Open())
		{
			if (wxTheClipboard->IsSupported(wxDF_TEXT))
			{
				wxTextDataObject data;
				wxTheClipboard->GetData(data);
				entities = data.GetText();
			}
			wxTheClipboard->Close();
		}

		// First do we need to check if it is a correct xml string
		wxInputStream* is = new wxStringInputStream(entities);
		wxXmlDocument doc;
		if (!doc.Load(*is))
			return;

		// Entities, Entity(1.*)
		wxXmlNode* root = doc.GetRoot();
		if (root->GetName() != wxT("Entities"))
			return;

		// Template, position,orientation
		const wxXmlNode* child = root->GetChildren();

		while (child)
		{
			if (child->GetName() != wxT("Entity"))
				return;

			child = child->GetNext();
		}

		g_SelectedObjects.clear();
		POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));

		// is the source code get here now you can add the objects to scene(preview)
		// store id to move
		child = root->GetChildren();

		while (child)
		{
			wxString templateName;
			Position entityPos;
			long playerId = 0;
			double orientation = 0;
			unsigned int actorSeed = 0;

			const wxXmlNode* xmlData = child->GetChildren();

			while (xmlData)
			{
				if (xmlData->GetName() == wxT("Template"))
					templateName = xmlData->GetNodeContent();
				else if (xmlData->GetName() == wxT("Position"))
				{
					wxString x, z;
#if wxCHECK_VERSION(3, 0, 0)
					xmlData->GetAttribute(wxT("x"), &x);
					xmlData->GetAttribute(wxT("z"), &z);
#else
					xmlData->GetPropVal(wxT("x"), &x);
					xmlData->GetPropVal(wxT("z"), &z);
#endif

					double aux, aux2;
					x.ToDouble(&aux);
					z.ToDouble(&aux2);

					entityPos = Position(aux, 0, aux2);
				}
				else if (xmlData->GetName() == wxT("Orientation"))
				{
					wxString y;
#if wxCHECK_VERSION(3, 0, 0)
					xmlData->GetAttribute(wxT("y"), &y);
#else
					xmlData->GetPropVal(wxT("y"), &y);
#endif
					y.ToDouble(&orientation);
				}
				else if (xmlData->GetName() == wxT("Player"))
				{
					wxString x(xmlData->GetNodeContent());
					x.ToLong(&playerId);
				}
				else if (xmlData->GetName() == wxT("ActorSeed"))
				{
					wxString x(xmlData->GetNodeContent());
					unsigned long xTmp = 0;
					x.ToULong(&xTmp);
					wxASSERT(xTmp <= (unsigned long)std::numeric_limits<unsigned int>::max());
					actorSeed = xTmp;
				}

				xmlData = xmlData->GetNext();
			}

			//Update current Ownership
			this->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerId);
			this->GetScenarioEditor().GetObjectSettings().NotifyObservers();

			POST_MESSAGE(ObjectPreview, ((std::wstring)templateName.c_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), entityPos, false, Position(), orientation, actorSeed, false));

			child = child->GetNext();
		}

		//Set state paste for preview the new objects
		this->SetState(&Pasting);

		//Update the objects to current mouse position
		OnMovingPaste();
	}

	void OnMovingPaste()
	{
		//Move the preview(s) object(s)
		POST_MESSAGE(MoveObjectPreview, ((m_entPosition)));
	}

	void OnPasteEnd(bool canceled)
	{
		if (canceled)
			//delete previews objects
			POST_MESSAGE(ObjectPreview, (_T(""), GetScenarioEditor().GetObjectSettings().GetSettings(), Position(), false, Position(), 0, 0, true));
		else
		{
			ScenarioEditor::GetCommandProc().FinaliseLastCommand();

			//Create new Objects and delete preview objects
			POST_MESSAGE(ObjectPreviewToEntity, ());

			AtlasMessage::qGetCurrentSelection currentSelection;
			currentSelection.Post();

			g_SelectedObjects = *currentSelection.ids;
		}


		//when all is done set default state
		this->SetState(&Waiting);
	}

	// TODO: keys to rotate/move object?

	struct sWaiting : public State
	{
		bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
		{
			if (evt.LeftDClick() && AtlasMessage::ObjectIDIsValid(obj->m_lastSelected))
			{
				SET_STATE(SelectSimilar);
				return true;
			}
			else if (evt.LeftDown())
			{
				bool selectionAdd = wxGetKeyState(SELECTION_ADD_HOTKEY);
				bool selectionRemove = wxGetKeyState(SELECTION_REMOVE_HOTKEY);
				bool selectionActors = wxGetKeyState(SELECTION_ACTORS_HOTKEY);

				// New selection - never merge with movements of other objects
				ScenarioEditor::GetCommandProc().FinaliseLastCommand();

				// Select the object clicked on:

				AtlasMessage::qPickObject qry(Position(evt.GetPosition()), selectionActors);
				qry.Post();

				// Check they actually clicked on a valid object
				if (AtlasMessage::ObjectIDIsValid(qry.id))
				{
					std::vector<AtlasMessage::ObjectID>::iterator it = std::find(g_SelectedObjects.begin(), g_SelectedObjects.end(), qry.id);
					bool objectIsSelected = (it != g_SelectedObjects.end());

					if (selectionRemove)
					{
						// Remove from selection
						if (objectIsSelected)
							g_SelectedObjects.erase(it);
					}
					else if (!objectIsSelected)
					{
						// Add to selection
						if (!selectionAdd)
							g_SelectedObjects.clear();

						g_SelectedObjects.push_back(qry.id);
					}

					obj->m_lastSelected = qry.id;

					// If we're selecting the whole group
					if (!selectionAdd && !selectionRemove && !g_SelectedObjects.empty())
					{
						// Remember the screen-space offset of the mouse from the
						// object's centre, so we can add that back when moving it
						// (instead of just moving the object's centre to directly
						// beneath the mouse)
						obj->m_dx = qry.offsetx;
						obj->m_dy = qry.offsety;
						SET_STATE(Dragging);
					}

					g_SelectedObjects.NotifyObservers();
					POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
				}
				else
				{
					// Bandboxing
					obj->m_lastSelected = 0;
					obj->m_startPoint = evt.GetPosition();
					SET_STATE(Bandboxing);
				}

				return true;
			}
			else if (!g_SelectedObjects.empty() && ((evt.Dragging() && evt.RightIsDown()) || evt.RightDown()))
			{
				SET_STATE(Rotating);
				return true;
			}
			else if (evt.Moving())
			{
				//Save position for smooth paste position
				obj->m_entPosition = Position(evt.GetPosition());

				// Prevent certain events from reaching game UI in this mode
				//	to prevent selection ring confusion
				return true;
			}
			else
				return false;
		}

		bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
		{
			if (type == KEY_CHAR && evt.GetKeyCode() == WXK_DELETE)
			{
				POST_COMMAND(DeleteObjects, (g_SelectedObjects));

				g_SelectedObjects.clear();
				g_SelectedObjects.NotifyObservers();

				POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
				return true;
			}
			else if (type == KEY_CHAR && (evt.GetKeyCode() >= '0' && evt.GetKeyCode() <= '9'))
			{
				int playerID = evt.GetKeyCode() - '0';
				obj->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerID);
				obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
				return true;
			}
			else if (evt.GetModifiers() == wxMOD_CONTROL)
			{
				if (evt.GetKeyCode() == 'C')
                    return obj->OnCopy();
				else if (evt.GetKeyCode() == 'V')
				{
					obj->OnPasteStart();
					return true;
				}
			}

			return false;
		}
	}
	Waiting;

	struct sDragging : public State
	{
		bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
		{
			if (evt.LeftUp())
			{
				POST_MESSAGE(ResetSelectionColor, ());
				SET_STATE(Waiting);
				return true;
			}
			else if (evt.Dragging())
			{
				Position pos(evt.GetPosition() + wxPoint(obj->m_dx, obj->m_dy));

				POST_COMMAND(MoveObjects, (g_SelectedObjects, obj->m_lastSelected, pos));
				return true;
			}
			else
				return false;
		}

		bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
		{
			if (type == KEY_UP && evt.GetKeyCode() == WXK_ESCAPE)
			{
				// Cancel move action
				ScenarioEditor::GetCommandProc().FinaliseLastCommand();
				ScenarioEditor::GetCommandProc().Undo();
				SET_STATE(Waiting);

				return true;
			}
			else
				return false;
		}
	}
	Dragging;

	struct sBandboxing : public State
	{
		bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
		{
			if (evt.LeftIsDown() && evt.Dragging())
			{
				// Update bandbox overlay
				POST_MESSAGE(SetBandbox, (true, obj->m_startPoint.x, obj->m_startPoint.y, evt.GetPosition().x, evt.GetPosition().y));
				return true;
			}
			else if (evt.LeftUp())
			{
				bool selectionAdd = wxGetKeyState(SELECTION_ADD_HOTKEY);
				bool selectionRemove = wxGetKeyState(SELECTION_REMOVE_HOTKEY);
				bool selectionActors = wxGetKeyState(SELECTION_ACTORS_HOTKEY);

				// Now we have both corners of the box, pick objects
				AtlasMessage::qPickObjectsInRect qry(Position(obj->m_startPoint), Position(evt.GetPosition()), selectionActors);
				qry.Post();

				std::vector<AtlasMessage::ObjectID> ids = *qry.ids;

				if (!selectionAdd && !selectionRemove)
				{
					// Just copy new selections (clears list if no selections)
					g_SelectedObjects = ids;
				}
				else
				{
					for (size_t i = 0; i < ids.size(); ++i)
					{
						std::vector<AtlasMessage::ObjectID>::iterator it = std::find(g_SelectedObjects.begin(), g_SelectedObjects.end(), ids[i]);
						bool objectIsSelected = (it != g_SelectedObjects.end());
						if (selectionRemove)
						{
							// Remove from selection
							if (objectIsSelected)
								g_SelectedObjects.erase(it);
						}
						else if (!objectIsSelected)
						{
							// Add to selection
							g_SelectedObjects.push_back(ids[i]);
						}
					}
				}

				POST_MESSAGE(SetBandbox, (false, 0, 0, 0, 0));

				g_SelectedObjects.NotifyObservers();
				POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));

				SET_STATE(Waiting);
				return true;
			}
			else
				return false;
		}

		bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
		{
			if (type == KEY_UP && evt.GetKeyCode() == WXK_ESCAPE)
			{
				// Clear bandbox and return to waiting state
				POST_MESSAGE(SetBandbox, (false, 0, 0, 0, 0));
				SET_STATE(Waiting);
				return true;
			}
			else
				return false;
		}
	}
	Bandboxing;

	struct sSelectSimilar : public State
	{
		bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
		{
			if (evt.LeftUp())
			{
				bool selectionAdd = wxGetKeyState(SELECTION_ADD_HOTKEY);
				bool selectionRemove = wxGetKeyState(SELECTION_REMOVE_HOTKEY);

				// Select similar objects
				AtlasMessage::qPickSimilarObjects qry(obj->m_lastSelected);
				qry.Post();

				std::vector<AtlasMessage::ObjectID> ids = *qry.ids;

				if (!selectionAdd && !selectionRemove)
				{
					// Just copy new selections (clears list if no selections)
					g_SelectedObjects = ids;
				}
				else
				{
					for (size_t i = 0; i < ids.size(); ++i)
					{
						std::vector<AtlasMessage::ObjectID>::iterator it = std::find(g_SelectedObjects.begin(), g_SelectedObjects.end(), ids[i]);
						bool objectIsSelected = (it != g_SelectedObjects.end());
						if (selectionRemove)
						{
							// Remove from selection
							if (objectIsSelected)
								g_SelectedObjects.erase(it);
						}
						else if (!objectIsSelected)
						{
							// Add to selection
							g_SelectedObjects.push_back(ids[i]);
						}
					}
				}

				g_SelectedObjects.NotifyObservers();
				POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));

				SET_STATE(Waiting);
				return true;
			}
			else
				return false;
		}
	}
	SelectSimilar;

	struct sPasting : public State
	{
		bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
		{
			if (evt.Moving())
			{
				//Move the object
				obj->m_entPosition = Position(evt.GetPosition());
				obj->OnMovingPaste();
				return true;
			}
			else if (evt.LeftDown())
			{
				//Place the object and update
				obj->OnPasteEnd(false);
				return true;
			}
			else
				return false;
		}
		bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
		{
			if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
			{
				obj->OnPasteEnd(true);
				return true;
			}
			else
				return false;
		}
	}
	Pasting;

	struct sRotating : public State
	{
		bool fromCenterPoint;

		void OnEnter(TransformObject* WXUNUSED(obj))
		{
			fromCenterPoint = true;
		}

		bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
		{
			if (evt.RightUp())
			{
				POST_MESSAGE(ResetSelectionColor, ());
				SET_STATE(Waiting);
				return true;
			}
			else if (evt.Dragging())
			{
				bool fromGlobalAndIndividualCenterPoints = !evt.ControlDown() && !evt.ShiftDown();
				bool newFromCenterPoint = evt.ShiftDown() || fromGlobalAndIndividualCenterPoints;
				if (newFromCenterPoint != fromCenterPoint)
				{
					ScenarioEditor::GetCommandProc().FinaliseLastCommand();
					fromCenterPoint = newFromCenterPoint;
				}

				Position pos(evt.GetPosition());

				if (fromCenterPoint)
					POST_COMMAND(RotateObjectsFromCenterPoint, (g_SelectedObjects, pos, fromGlobalAndIndividualCenterPoints));
				else
					POST_COMMAND(RotateObject, (g_SelectedObjects, pos));

				return true;
			}

			return false;
		}

		bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
		{
			if (type == KEY_UP && evt.GetKeyCode() == WXK_ESCAPE)
			{
				// Cancel move action
				ScenarioEditor::GetCommandProc().FinaliseLastCommand();
				ScenarioEditor::GetCommandProc().Undo();
				SET_STATE(Waiting);

				return true;
			}

			return false;
		}
	}
	Rotating;
};

IMPLEMENT_DYNAMIC_CLASS(TransformObject, StateDrivenTool<TransformObject>);