Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Object/Object.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 "Object.h"
21
22 #include "tools/atlas/AtlasObject/AtlasObject.h"
23 #include "tools/atlas/AtlasUI/General/Datafile.h"
24 #include "tools/atlas/AtlasUI/General/Observable.h"
25 #include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h"
26 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Common/Sidebar.h"
27 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Object/VariationControl.h"
28 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/MiscState.h"
29 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.h"
30 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h"
31 #include "tools/atlas/GameInterface/MessagePasser.h"
32 #include "tools/atlas/GameInterface/Messages.h"
33 #include "tools/atlas/GameInterface/Shareable.h"
34 #include "tools/atlas/GameInterface/SharedTypes.h"
35
36 #include <algorithm>
37 #include <cstddef>
38 #include <list>
39 #include <string>
40 #include <vector>
41 #include <wx/arrstr.h>
42 #include <wx/busyinfo.h>
43 #include <wx/button.h>
44 #include <wx/chartype.h>
45 #include <wx/checkbox.h>
46 #include <wx/choice.h>
47 #include <wx/clntdata.h>
48 #include <wx/combobox.h>
49 #include <wx/control.h>
50 #include <wx/gdicmn.h>
51 #include <wx/listbox.h>
52 #include <wx/object.h>
53 #include <wx/panel.h>
54 #include <wx/scrolwin.h>
55 #include <wx/sizer.h>
56 #include <wx/statbox.h>
57 #include <wx/stattext.h>
58 #include <wx/string.h>
59 #include <wx/textctrl.h>
60 #include <wx/toolbar.h>
61 #include <wx/translation.h>
62 #include <wx/unichar.h>
63 #include <wx/window.h>
64 #include <wx/wxcrt.h>
65
66 enum
67 {
68 ID_ObjectType = 1,
69 ID_ObjectFilter,
70 ID_ObjectExactFilter,
71 ID_PlayerSelect,
72 ID_SelectObject,
73 ID_ToggleViewer,
74 ID_ViewerWireframe,
75 ID_ViewerMove,
76 ID_ViewerGround,
77 ID_ViewerWater,
78 ID_ViewerShadows,
79 ID_ViewerPolyCount,
80 ID_ViewerAnimation,
81 ID_ViewerBoundingBox,
82 ID_ViewerAxesMarker,
83 ID_ViewerPropPoints,
84 ID_ViewerPlay,
85 ID_ViewerPause,
86 ID_ViewerSlow
87 };
88
89 // Helper function for adding tooltips
90 static wxWindow* Tooltipped(wxWindow* window, const wxString& tip)
91 {
92 window->SetToolTip(tip);
93 return window;
94 }
95
96 class ObjectBottomBar : public wxPanel
97 {
98 public:
99 ObjectBottomBar(
100 wxWindow* parent,
101 Observable<ObjectSettings>& objectSettings,
102 Observable<AtObj>& mapSettings,
103 ObjectSidebarImpl* p
104 );
105
106 void OnFirstDisplay();
107 void ShowActorViewer(bool show);
108 void OnSelectedObjectsChange(const std::vector<AtlasMessage::ObjectID>& selectedObjects);
109
110 private:
111 void OnViewerSetting(wxCommandEvent& evt);
112 void OnSelectAnim(wxCommandEvent& evt);
113 void OnSpeed(wxCommandEvent& evt);
114
115 bool m_ViewerWireframe;
116 bool m_ViewerMove;
117 bool m_ViewerGround;
118 bool m_ViewerWater;
119 bool m_ViewerPolyCount;
120 bool m_ViewerBoundingBox;
121 bool m_ViewerAxesMarker;
122 int m_ViewerPropPointsMode; // 0 disabled, 1 for point markers, 2 for point markers + axes
123
124 wxPanel* m_ViewerPanel;
125 wxScrolledWindow* m_TemplateNames;
126
127 ObjectSidebarImpl* p;
128 DECLARE_EVENT_TABLE();
129 };
130
131 struct ObjectSidebarImpl
132 {
133 ObjectSidebarImpl(ScenarioEditor& scenarioEditor) :
134 m_ObjectListBox(NULL), m_ActorViewerActive(false),
135 m_ActorViewerEntity(L"actor|structures/fndn_1x1.xml"),
136 m_ActorViewerAnimation("idle"), m_ActorViewerSpeed(0.f),
137 m_ObjectSettings(scenarioEditor.GetObjectSettings())
138 {
139 }
140
141 wxListBox* m_ObjectListBox;
142 std::vector<AtlasMessage::sObjectsListItem> m_Objects;
143 ObservableScopedConnection m_ToolConn;
144
145 bool m_ActorViewerActive;
146 std::wstring m_ActorViewerEntity;
147 std::string m_ActorViewerAnimation;
148 float m_ActorViewerSpeed;
149 Observable<ObjectSettings>& m_ObjectSettings;
150
151 struct SearchItem
152 {
153 // Weight of the pattern (higher - better).
154 size_t weight;
155 // Index in the original sequence to save the order.
156 size_t index;
157
158 bool operator<(const SearchItem& other) const
159 {
160 if (weight != other.weight)
161 return weight > other.weight;
162 return index < other.index;
163 }
164 };
165 // Cache the std::vector to prevent frequent reallocations.
166 std::vector<SearchItem> m_SearchItems;
167
168 struct SearchItemFilter
169 {
170 SearchItemFilter(const wxString& filterName)
171 {
172 bool firstCharacterInWord = true;
173 for (const wxUniChar& ch : filterName)
174 {
175 if (wxIsalpha(ch))
176 {
177 if (firstCharacterInWord)
178 m_FilterWords.push_back(wxString());
179 firstCharacterInWord = false;
180 m_FilterWords.back().Append(ch);
181 }
182 else
183 firstCharacterInWord = true;
184 }
185 }
186
187 // Returns positive integer if filter words can present in the name.
188 // Otherwise returns zero.
189 size_t CalculateWeight(const wxString& name)
190 {
191 if (m_FilterWords.empty() || name.IsEmpty())
192 return 0;
193 std::vector<size_t> filterWordWeights(m_FilterWords.size(), 0);
194 for (size_t wordBegin = 0; wordBegin < name.Length();)
195 {
196 if (!wxIsalpha(name[wordBegin]))
197 {
198 ++wordBegin;
199 continue;
200 }
201 size_t wordEnd = wordBegin;
202 while (wordEnd < name.Length() && wxIsalpha(name[wordEnd]))
203 ++wordEnd;
204 for (size_t i = 0; i < m_FilterWords.size(); ++i)
205 {
206 // TODO: we might have a copy here, maybe we can use
207 // something similar to std::string_view but for wxString.
208 size_t weight = CalculateLCS(
209 m_FilterWords[i], name.Mid(wordBegin, wordEnd - wordBegin));
210 // We use a simple heuristic, if LCS shorter than a half
211 // of the filter word, then it's not good for us.
212 if (weight < m_FilterWords[i].size() / 2)
213 continue;
214 filterWordWeights[i] = std::max(filterWordWeights[i], weight);
215 }
216 wordBegin = wordEnd;
217 }
218 size_t totalWeight = 0;
219 for (size_t weight : filterWordWeights)
220 {
221 // If a filter word was't found at all, then we can't accept
222 // the search item name.
223 if (!weight)
224 return 0;
225 totalWeight += weight;
226 }
227 return totalWeight;
228 }
229
230 // We use longest common subsequence to calculate the weight
231 // between two words: filter and item names.
232 size_t CalculateLCS(const wxString& filterWord, const wxString& word)
233 {
234 if (filterWord.IsEmpty() || word.IsEmpty())
235 return 0;
236 m_LCSGrid.resize(filterWord.Length() * word.Length());
237 for (size_t i = 0; i < filterWord.Length(); ++i)
238 for (size_t j = 0; j < word.Length(); ++j)
239 {
240 #define GRID(x, y) m_LCSGrid[(x) * word.Length() + (y)]
241 GRID(i, j) = wxTolower(filterWord.GetChar(i)) == wxTolower(word.GetChar(j));
242 if (i && j)
243 GRID(i, j) += GRID(i - 1, j - 1);
244 if (i)
245 GRID(i, j) = std::max(GRID(i, j), GRID(i - 1, j));
246 if (j)
247 GRID(i, j) = std::max(GRID(i, j), GRID(i, j - 1));
248 #undef GRID
249 }
250 return m_LCSGrid.back();
251 }
252
253 std::vector<wxString> m_FilterWords;
254 // Cache the std::vector to prevent frequent reallocations.
255 std::vector<size_t> m_LCSGrid;
256 };
257
258 void ActorViewerPostToGame()
259 {
260 POST_MESSAGE(SetActorViewer, (m_ActorViewerEntity.c_str(), m_ActorViewerAnimation.c_str(), m_ObjectSettings.GetPlayerID(), m_ActorViewerSpeed, false));
261 }
262 };
263
264 ObjectSidebar::ObjectSidebar(
265 ScenarioEditor& scenarioEditor,
266 wxWindow* sidebarContainer,
267 wxWindow* bottomBarContainer
268 )
269 : Sidebar(scenarioEditor, sidebarContainer, bottomBarContainer),
270 m_Impl(new ObjectSidebarImpl(scenarioEditor))
271 {
272 wxSizer* scrollSizer = new wxBoxSizer(wxVERTICAL);
273 wxScrolledWindow* scrolledWindow = new wxScrolledWindow(this);
274 scrolledWindow->SetScrollRate(10, 10);
275 scrolledWindow->SetSizer(scrollSizer);
276 m_MainSizer->Add(scrolledWindow, wxSizerFlags().Proportion(1).Expand());
277
278 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
279 sizer->Add(new wxStaticText(scrolledWindow, wxID_ANY, _("Filter")), wxSizerFlags().Align(wxALIGN_CENTER));
280 sizer->AddSpacer(2);
281 sizer->Add(
282 Tooltipped(
283 new wxTextCtrl(scrolledWindow, ID_ObjectFilter),
284 _("Enter text to filter object list")
285 ),
286 wxSizerFlags().Expand().Proportion(1)
287 );
288 scrollSizer->Add(sizer, wxSizerFlags().Expand());
289 scrollSizer->AddSpacer(3);
290 wxCheckBox* exactSearchCheckBox = new wxCheckBox(
291 scrolledWindow, ID_ObjectExactFilter, _("Exact Search"));
292 exactSearchCheckBox->SetValue(true);
293 scrollSizer->Add(Tooltipped(exactSearchCheckBox,
294 _("Provides a search with a strict string equality")));
295 scrollSizer->AddSpacer(3);
296
297 // ------------------------------------------------------------------------------------------
298
299 wxArrayString strings;
300 strings.Add(_("Entities"));
301 strings.Add(_("Actors (all)"));
302 wxChoice* objectType = new wxChoice(scrolledWindow, ID_ObjectType, wxDefaultPosition, wxDefaultSize, strings);
303 objectType->SetSelection(0);
304 scrollSizer->Add(objectType, wxSizerFlags().Expand());
305 scrollSizer->AddSpacer(3);
306
307 // ------------------------------------------------------------------------------------------
308
309 m_Impl->m_ObjectListBox = new wxListBox(scrolledWindow, ID_SelectObject, wxDefaultPosition, wxDefaultSize, 0, nullptr, wxLB_SINGLE|wxLB_HSCROLL);
310 scrollSizer->Add(m_Impl->m_ObjectListBox, wxSizerFlags().Proportion(1).Expand());
311 scrollSizer->AddSpacer(3);
312
313 // ------------------------------------------------------------------------------------------
314
315 scrollSizer->Add(new wxButton(scrolledWindow, ID_ToggleViewer, _("Switch to Actor Viewer")), wxSizerFlags().Expand());
316
317 // ------------------------------------------------------------------------------------------
318
319 m_BottomBar = new ObjectBottomBar(
320 bottomBarContainer,
321 scenarioEditor.GetObjectSettings(),
322 scenarioEditor.GetMapSettings(),
323 m_Impl
324 );
325
326 m_Impl->m_ToolConn = scenarioEditor.GetToolManager().GetCurrentTool().RegisterObserver(0, &ObjectSidebar::OnToolChange, this);
327 }
328
329 ObjectSidebar::~ObjectSidebar()
330 {
331 delete m_Impl;
332 }
333
334 void ObjectSidebar::OnToolChange(ITool* tool)
335 {
336 if (wxString(tool->GetClassInfo()->GetClassName()) == _T("ActorViewerTool"))
337 {
338 m_Impl->m_ActorViewerActive = true;
339 m_Impl->ActorViewerPostToGame();
340 wxDynamicCast(FindWindow(ID_ToggleViewer), wxButton)->SetLabel(_("Return to game view"));
341 }
342 else
343 {
344 m_Impl->m_ActorViewerActive = false;
345 wxDynamicCast(FindWindow(ID_ToggleViewer), wxButton)->SetLabel(_("Switch to Actor Viewer"));
346 }
347
348 static_cast<ObjectBottomBar*>(m_BottomBar)->ShowActorViewer(m_Impl->m_ActorViewerActive);
349 }
350
351 void ObjectSidebar::OnFirstDisplay()
352 {
353 static_cast<ObjectBottomBar*>(m_BottomBar)->OnFirstDisplay();
354
355 wxBusyInfo busy (_("Loading list of objects"));
356
357 // Get the list of objects from the game
358 AtlasMessage::qGetObjectsList qry;
359 qry.Post();
360 m_Impl->m_Objects = *qry.objects;
361 // Display first group of objects
362 FilterObjects();
363 }
364
365 void ObjectSidebar::FilterObjects()
366 {
367 int filterType = wxDynamicCast(FindWindow(ID_ObjectType), wxChoice)->GetSelection();
368 wxString filterName = wxDynamicCast(FindWindow(ID_ObjectFilter), wxTextCtrl)->GetValue();
369
370 m_Impl->m_ObjectListBox->Freeze();
371 m_Impl->m_ObjectListBox->Clear();
372 if (wxDynamicCast(FindWindow(ID_ObjectExactFilter), wxCheckBox)->IsChecked() || filterName.IsEmpty())
373 {
374 for (std::vector<AtlasMessage::sObjectsListItem>::iterator it = m_Impl->m_Objects.begin(); it != m_Impl->m_Objects.end(); ++it)
375 {
376 if (it->type != filterType)
377 continue;
378 wxString id = it->id.c_str();
379 wxString name = it->name.c_str();
380
381 if (name.Lower().Find(filterName.Lower()) != wxNOT_FOUND)
382 m_Impl->m_ObjectListBox->Append(name, new wxStringClientData(id));
383 }
384 }
385 else
386 {
387 ObjectSidebarImpl::SearchItemFilter filter(filterName);
388 for (size_t idx = 0; idx < m_Impl->m_Objects.size(); ++idx)
389 {
390 const AtlasMessage::sObjectsListItem& it = m_Impl->m_Objects[idx];
391 if (it.type != filterType)
392 continue;
393 wxString name = it.name.c_str();
394 size_t weight = filter.CalculateWeight(name);
395 // We don't want to show items with zero weight.
396 if (!weight)
397 continue;
398 m_Impl->m_SearchItems.push_back({weight, idx});
399 }
400 std::sort(m_Impl->m_SearchItems.begin(), m_Impl->m_SearchItems.end());
401 for (const ObjectSidebarImpl::SearchItem& item : m_Impl->m_SearchItems)
402 {
403 const AtlasMessage::sObjectsListItem& it = m_Impl->m_Objects[item.index];
404 wxString id = it.id.c_str();
405 wxString name = it.name.c_str();
406 m_Impl->m_ObjectListBox->Append(name, new wxStringClientData(id));
407 }
408 m_Impl->m_SearchItems.clear();
409 }
410 m_Impl->m_ObjectListBox->Thaw();
411 }
412
413 void ObjectSidebar::OnToggleViewer(wxCommandEvent& WXUNUSED(evt))
414 {
415 if (m_Impl->m_ActorViewerActive)
416 {
417 m_ScenarioEditor.GetToolManager().SetCurrentTool(_T(""), NULL);
418 }
419 else
420 {
421 m_ScenarioEditor.GetToolManager().SetCurrentTool(_T("ActorViewerTool"), NULL);
422 }
423 }
424
425 void ObjectSidebar::OnSelectType(wxCommandEvent& WXUNUSED(evt))
426 {
427 FilterObjects();
428 }
429
430 void ObjectSidebar::OnSelectObject(wxCommandEvent& evt)
431 {
432 if (evt.GetInt() < 0)
433 return;
434
435 wxString id = static_cast<wxStringClientData*>(evt.GetClientObject())->GetData();
436
437 // Always update the actor viewer's state even if it's inactive,
438 // so it will be correct when first enabled
439 m_Impl->m_ActorViewerEntity = id;
440
441 if (m_Impl->m_ActorViewerActive)
442 {
443 m_Impl->ActorViewerPostToGame();
444 }
445 else
446 {
447 // On selecting an object, enable the PlaceObject tool with this object
448 m_ScenarioEditor.GetToolManager().SetCurrentTool(_T("PlaceObject"), &id);
449 }
450 }
451
452 void ObjectSidebar::OnSelectFilter(wxCommandEvent& WXUNUSED(evt))
453 {
454 FilterObjects();
455 }
456
457 void ObjectSidebar::OnToggleExactFilter(wxCommandEvent& WXUNUSED(evt))
458 {
459 FilterObjects();
460 }
461
462 BEGIN_EVENT_TABLE(ObjectSidebar, Sidebar)
463 EVT_CHOICE(ID_ObjectType, ObjectSidebar::OnSelectType)
464 EVT_TEXT(ID_ObjectFilter, ObjectSidebar::OnSelectFilter)
465 EVT_LISTBOX(ID_SelectObject, ObjectSidebar::OnSelectObject)
466 EVT_BUTTON(ID_ToggleViewer, ObjectSidebar::OnToggleViewer)
467 EVT_CHECKBOX(ID_ObjectExactFilter, ObjectSidebar::OnToggleExactFilter)
468 END_EVENT_TABLE();
469
470 //////////////////////////////////////////////////////////////////////////
471
472 class PlayerComboBox : public wxComboBox
473 {
474 public:
475 PlayerComboBox(wxWindow* parent, Observable<ObjectSettings>& objectSettings, Observable<AtObj>& mapSettings)
476 : wxComboBox(parent, ID_PlayerSelect, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, 0, wxCB_READONLY)
477 , m_ObjectSettings(objectSettings), m_MapSettings(mapSettings)
478 {
479 m_ObjectConn = m_ObjectSettings.RegisterObserver(1, &PlayerComboBox::OnObjectSettingsChange, this);
480 m_MapConn = m_MapSettings.RegisterObserver(1, &PlayerComboBox::OnMapSettingsChange, this);
481 }
482
483 void SetPlayers(wxArrayString& names)
484 {
485 m_Players = names;
486 OnMapSettingsChange(m_MapSettings);
487 }
488
489 private:
490
491 ObservableScopedConnection m_ObjectConn;
492 Observable<ObjectSettings>& m_ObjectSettings;
493 ObservableScopedConnection m_MapConn;
494 Observable<AtObj>& m_MapSettings;
495 wxArrayString m_Players;
496
497 void SetSelection(int playerID)
498 {
499 // This control may not be loaded yet (before first display)
500 // or may have less items than we expect, which could cause
501 // an assertion failure, so handle that here
502 if ((unsigned int)playerID < GetCount())
503 {
504 wxComboBox::SetSelection(playerID);
505 }
506 else
507 {
508 // Invalid selection
509 wxComboBox::SetSelection(wxNOT_FOUND);
510 }
511 }
512
513 void OnObjectSettingsChange(const ObjectSettings& settings)
514 {
515 SetSelection(settings.GetPlayerID());
516 }
517
518 void OnMapSettingsChange(const AtObj& settings)
519 {
520 // Reload displayed player names
521 Clear();
522 size_t numPlayers = settings["PlayerData"]["item"].count();
523 for (size_t i = 0; i <= numPlayers && i < m_Players.Count(); ++i)
524 {
525 Append(m_Players[i]);
526 }
527
528 SetSelection(m_ObjectSettings.GetPlayerID());
529 }
530
531 void OnSelect(wxCommandEvent& evt)
532 {
533 m_ObjectSettings.SetPlayerID(evt.GetInt());
534 m_ObjectSettings.NotifyObserversExcept(m_ObjectConn);
535 }
536
537 DECLARE_EVENT_TABLE();
538 };
539 BEGIN_EVENT_TABLE(PlayerComboBox, wxComboBox)
540 EVT_COMBOBOX(wxID_ANY, PlayerComboBox::OnSelect)
541 END_EVENT_TABLE();
542
543 //////////////////////////////////////////////////////////////////////////
544
545 ObjectBottomBar::ObjectBottomBar(
546 wxWindow* parent,
547 Observable<ObjectSettings>& objectSettings,
548 Observable<AtObj>& mapSettings,
549 ObjectSidebarImpl* p
550 )
551 : wxPanel(parent, wxID_ANY), p(p)
552 {
553 m_ViewerWireframe = false;
554 m_ViewerMove = false;
555 m_ViewerGround = true;
556 m_ViewerWater = false;
557 m_ViewerPolyCount = false;
558 m_ViewerBoundingBox = false;
559 m_ViewerAxesMarker = false;
560 m_ViewerPropPointsMode = 0;
561
562 wxSizer* mainSizer = new wxBoxSizer(wxHORIZONTAL);
563
564 // --- viewer options panel -------------------------------------------------------------------------------
565
566 m_ViewerPanel = new wxPanel(this, wxID_ANY);
567 wxSizer* viewerSizer = new wxBoxSizer(wxHORIZONTAL);
568
569 wxStaticBoxSizer* viewerButtonsSizer = new wxStaticBoxSizer(wxHORIZONTAL, m_ViewerPanel, _("Display settings"));
570 {
571 wxSizer* viewerButtonsLeft = new wxBoxSizer(wxVERTICAL);
572 viewerButtonsLeft->SetMinSize(110, -1);
573 viewerButtonsLeft->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerWireframe, _("Wireframe")), _("Toggle wireframe / solid rendering")), wxSizerFlags().Expand());
574 viewerButtonsLeft->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerMove, _("Move")), _("Toggle movement along ground when playing walk/run animations")), wxSizerFlags().Expand());
575 viewerButtonsLeft->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerGround, _("Ground")), _("Toggle the ground plane")), wxSizerFlags().Expand());
576 // TODO: disabled until https://gitea.wildfiregames.com/0ad/0ad/issues/2692 is fixed
577 wxButton* waterButton = new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerWater, _("Water"));
578 waterButton->Enable(false);
579 viewerButtonsLeft->Add(Tooltipped(waterButton, _("Toggle the water plane")), wxSizerFlags().Expand());
580 viewerButtonsLeft->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerShadows, _("Shadows")), _("Toggle shadow rendering")), wxSizerFlags().Expand());
581 viewerButtonsLeft->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerPolyCount, _("Poly count")), _("Toggle polygon-count statistics - turn off ground and shadows for more useful data")), wxSizerFlags().Expand());
582
583 wxSizer* viewerButtonsRight = new wxBoxSizer(wxVERTICAL);
584 viewerButtonsRight->SetMinSize(110,-1);
585 viewerButtonsRight->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerBoundingBox, _("Bounding Boxes")), _("Toggle bounding boxes")), wxSizerFlags().Expand());
586 viewerButtonsRight->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerAxesMarker, _("Axes Marker")), _("Toggle the axes marker (R=X, G=Y, B=Z)")), wxSizerFlags().Expand());
587 viewerButtonsRight->Add(Tooltipped(new wxButton(viewerButtonsSizer->GetStaticBox(), ID_ViewerPropPoints, _("Prop Points")), _("Toggle prop points (works best in wireframe mode)")), wxSizerFlags().Expand());
588
589 viewerButtonsSizer->Add(viewerButtonsLeft, wxSizerFlags().Expand());
590 viewerButtonsSizer->Add(viewerButtonsRight, wxSizerFlags().Expand());
591 }
592
593 viewerSizer->Add(viewerButtonsSizer, wxSizerFlags().Expand());
594 viewerSizer->AddSpacer(3);
595
596 // --- animations panel -------------------------------------------------------------------------------
597
598 wxStaticBoxSizer* viewerAnimSizer = new wxStaticBoxSizer(wxVERTICAL, m_ViewerPanel, _("Animation"));
599
600 // TODO: this list should come from the actor
601 wxArrayString animChoices;
602 AtObj anims (Datafile::ReadList("animations"));
603 for (AtIter a = anims["item"]; a.defined(); ++a)
604 {
605 animChoices.Add(wxString(*a));
606 }
607 wxChoice* viewerAnimSelector = new wxChoice(viewerAnimSizer->GetStaticBox(), ID_ViewerAnimation, wxDefaultPosition, wxDefaultSize, animChoices);
608 viewerAnimSelector->SetSelection(0);
609 viewerAnimSizer->Add(viewerAnimSelector, wxSizerFlags().Expand());
610
611 wxSizer* viewerAnimSpeedSizer = new wxBoxSizer(wxHORIZONTAL);
612 viewerAnimSpeedSizer->Add(new wxButton(viewerAnimSizer->GetStaticBox(), ID_ViewerPlay, _("Play"), wxDefaultPosition, wxSize(50, -1)), wxSizerFlags().Expand());
613 viewerAnimSpeedSizer->Add(new wxButton(viewerAnimSizer->GetStaticBox(), ID_ViewerPause, _("Pause"), wxDefaultPosition, wxSize(50, -1)), wxSizerFlags().Expand());
614 viewerAnimSpeedSizer->Add(new wxButton(viewerAnimSizer->GetStaticBox(), ID_ViewerSlow, _("Slow"), wxDefaultPosition, wxSize(50, -1)), wxSizerFlags().Expand());
615 viewerAnimSizer->Add(viewerAnimSpeedSizer);
616
617 viewerSizer->Add(viewerAnimSizer, wxSizerFlags().Expand());
618
619 // --- add viewer-specific options -------------------------------------------------------------------------------
620
621 m_ViewerPanel->SetSizer(viewerSizer);
622 mainSizer->Add(m_ViewerPanel, wxSizerFlags().Expand());
623
624 m_ViewerPanel->Layout(); // prevents strange visibility glitch of the animation buttons on my machine (Vista 32-bit SP1) -- vtsj
625 m_ViewerPanel->Show(false);
626
627 // --- add player/variation selection -------------------------------------------------------------------------------
628
629 wxSizer* playerSelectionSizer = new wxBoxSizer(wxHORIZONTAL);
630 wxSizer* playerVariationSizer = new wxBoxSizer(wxVERTICAL);
631
632 // TODO: make this a wxChoice instead
633 wxComboBox* playerSelect = new PlayerComboBox(this, objectSettings, mapSettings);
634 playerSelectionSizer->Add(new wxStaticText(this, wxID_ANY, _("Player:")), wxSizerFlags().Align(wxALIGN_CENTER));
635 playerSelectionSizer->AddSpacer(3);
636 playerSelectionSizer->Add(playerSelect);
637
638 playerVariationSizer->Add(playerSelectionSizer);
639 playerVariationSizer->AddSpacer(3);
640
641
642 wxStaticBoxSizer* variationSizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Variation"));
643 wxWindow* variationSelect = new VariationControl(variationSizer->GetStaticBox(), objectSettings);
644 variationSelect->SetMinSize(wxSize(160, -1));
645 variationSizer->Add(variationSelect, wxSizerFlags().Proportion(1).Expand());
646 playerVariationSizer->Add(variationSizer, wxSizerFlags().Proportion(1));
647
648 mainSizer->AddSpacer(3);
649 mainSizer->Add(playerVariationSizer, wxSizerFlags().Expand());
650
651 // ----------------------------------------------------------------------------------
652 // --- display template name
653 wxStaticBoxSizer* displaySizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Selected entities"));
654 m_TemplateNames = new wxScrolledWindow(displaySizer->GetStaticBox());
655 m_TemplateNames->SetMinSize(wxSize(250, -1));
656 m_TemplateNames->SetScrollRate(0, 5);
657 wxSizer* scrollwindowSizer = new wxBoxSizer(wxVERTICAL);
658 m_TemplateNames->SetSizer(scrollwindowSizer);
659 displaySizer->Add(m_TemplateNames, wxSizerFlags().Proportion(1).Expand());
660 m_TemplateNames->Layout();
661 mainSizer->AddSpacer(3);
662 mainSizer->Add(displaySizer, wxSizerFlags().Proportion(1).Expand());
663
664 g_SelectedObjects.RegisterObserver(0, &ObjectBottomBar::OnSelectedObjectsChange, this);
665
666 SetSizer(mainSizer);
667 }
668
669 static wxControl* CreateTemplateNameObject(wxWindow* parent, const std::string& templateName, int counterTemplate)
670 {
671 wxString idTemplate(wxString::FromUTF8(templateName.c_str()));
672 if (counterTemplate > 1)
673 idTemplate.Append(wxString::Format(wxT(" (%i)"), counterTemplate));
674
675 wxStaticText* templateNameObject = new wxStaticText(parent, wxID_ANY, idTemplate);
676 return templateNameObject;
677 }
678
679 void ObjectBottomBar::OnSelectedObjectsChange(const std::vector<AtlasMessage::ObjectID>& selectedObjects)
680 {
681 Freeze();
682 wxSizer* sizer = m_TemplateNames->GetSizer();
683 sizer->Clear(true);
684
685 AtlasMessage::qGetSelectedObjectsTemplateNames objectTemplatesName(selectedObjects);
686 objectTemplatesName.Post();
687 std::vector<std::string> names = *objectTemplatesName.names;
688
689 int counterTemplate = 0;
690 std::string lastTemplateName = "";
691 for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
692 {
693 if (lastTemplateName == "")
694 lastTemplateName = (*it);
695
696 if (lastTemplateName == (*it))
697 {
698 ++counterTemplate;
699 continue;
700 }
701
702 sizer->Add(CreateTemplateNameObject(m_TemplateNames, lastTemplateName, counterTemplate), wxSizerFlags().Align(wxALIGN_LEFT));
703
704 lastTemplateName = (*it);
705 counterTemplate = 1;
706 }
707 // Add the remaining template
708 sizer->Add(CreateTemplateNameObject(m_TemplateNames, lastTemplateName, counterTemplate), wxSizerFlags().Align(wxALIGN_LEFT));
709
710 Thaw();
711 sizer->FitInside(m_TemplateNames);
712 }
713
714 void ObjectBottomBar::OnFirstDisplay()
715 {
716 // We use messages here because the simulation is not init'd otherwise (causing a crash)
717
718 // Get player names
719 wxArrayString players;
720 AtlasMessage::qGetPlayerDefaults qryPlayers;
721 qryPlayers.Post();
722 AtObj playerData = AtlasObject::LoadFromJSON(*qryPlayers.defaults);
723 AtObj playerDefs = *playerData["PlayerData"];
724 for (AtIter iterator = playerDefs["item"]; iterator.defined(); ++iterator)
725 players.Add(wxString::FromUTF8(iterator["Name"]));
726
727 wxDynamicCast(FindWindow(ID_PlayerSelect), PlayerComboBox)->SetPlayers(players);
728
729 // Initialise the game with the default settings
730 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"wireframe", m_ViewerWireframe));
731 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"walk", m_ViewerMove));
732 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"ground", m_ViewerGround));
733 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"water", m_ViewerWater));
734 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"stats", m_ViewerPolyCount));
735 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"bounding_box", m_ViewerBoundingBox));
736 POST_MESSAGE(SetViewParamI, (AtlasMessage::eRenderView::ACTOR, L"prop_points", m_ViewerPropPointsMode));
737 }
738
739 void ObjectBottomBar::ShowActorViewer(bool show)
740 {
741 m_ViewerPanel->Show(show);
742 Layout();
743 }
744
745 void ObjectBottomBar::OnViewerSetting(wxCommandEvent& evt)
746 {
747 switch (evt.GetId())
748 {
749 case ID_ViewerWireframe:
750 m_ViewerWireframe = !m_ViewerWireframe;
751 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"wireframe", m_ViewerWireframe));
752 break;
753 case ID_ViewerMove:
754 m_ViewerMove = !m_ViewerMove;
755 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"walk", m_ViewerMove));
756 break;
757 case ID_ViewerGround:
758 m_ViewerGround = !m_ViewerGround;
759 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"ground", m_ViewerGround));
760 break;
761 case ID_ViewerWater:
762 m_ViewerWater = !m_ViewerWater;
763 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"water", m_ViewerWater));
764 break;
765 case ID_ViewerShadows:
766 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"shadows", true));
767 break;
768 case ID_ViewerPolyCount:
769 m_ViewerPolyCount = !m_ViewerPolyCount;
770 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"stats", m_ViewerPolyCount));
771 break;
772 case ID_ViewerBoundingBox:
773 m_ViewerBoundingBox = !m_ViewerBoundingBox;
774 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"bounding_box", m_ViewerBoundingBox));
775 break;
776 case ID_ViewerAxesMarker:
777 m_ViewerAxesMarker = !m_ViewerAxesMarker;
778 POST_MESSAGE(SetViewParamB, (AtlasMessage::eRenderView::ACTOR, L"axes_marker", m_ViewerAxesMarker));
779 break;
780 case ID_ViewerPropPoints:
781 m_ViewerPropPointsMode = (m_ViewerPropPointsMode+1) % 3;
782 POST_MESSAGE(SetViewParamI, (AtlasMessage::eRenderView::ACTOR, L"prop_points", m_ViewerPropPointsMode));
783 break;
784 }
785 }
786
787 void ObjectBottomBar::OnSelectAnim(wxCommandEvent& evt)
788 {
789 p->m_ActorViewerAnimation = evt.GetString();
790 p->ActorViewerPostToGame();
791 }
792
793 void ObjectBottomBar::OnSpeed(wxCommandEvent& evt)
794 {
795 switch (evt.GetId())
796 {
797 case ID_ViewerPlay: p->m_ActorViewerSpeed = 1.0f; break;
798 case ID_ViewerPause: p->m_ActorViewerSpeed = 0.0f; break;
799 case ID_ViewerSlow: p->m_ActorViewerSpeed = 0.1f; break;
800 }
801 p->ActorViewerPostToGame();
802 }
803
804 BEGIN_EVENT_TABLE(ObjectBottomBar, wxPanel)
805 EVT_BUTTON(ID_ViewerWireframe, ObjectBottomBar::OnViewerSetting)
806 EVT_BUTTON(ID_ViewerMove, ObjectBottomBar::OnViewerSetting)
807 EVT_BUTTON(ID_ViewerGround, ObjectBottomBar::OnViewerSetting)
808 EVT_BUTTON(ID_ViewerWater, ObjectBottomBar::OnViewerSetting)
809 EVT_BUTTON(ID_ViewerShadows, ObjectBottomBar::OnViewerSetting)
810 EVT_BUTTON(ID_ViewerPolyCount, ObjectBottomBar::OnViewerSetting)
811 EVT_CHOICE(ID_ViewerAnimation, ObjectBottomBar::OnSelectAnim)
812 EVT_BUTTON(ID_ViewerPlay, ObjectBottomBar::OnSpeed)
813 EVT_BUTTON(ID_ViewerPause, ObjectBottomBar::OnSpeed)
814 EVT_BUTTON(ID_ViewerSlow, ObjectBottomBar::OnSpeed)
815 EVT_BUTTON(ID_ViewerBoundingBox, ObjectBottomBar::OnViewerSetting)
816 EVT_BUTTON(ID_ViewerAxesMarker, ObjectBottomBar::OnViewerSetting)
817 EVT_BUTTON(ID_ViewerPropPoints, ObjectBottomBar::OnViewerSetting)
818 END_EVENT_TABLE();