Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Player/Player.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 "Player.h"
21
22 #include "tools/atlas/AtlasObject/AtlasObject.h"
23 #include "tools/atlas/AtlasUI/CustomControls/ColorDialog/ColorDialog.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/Tools/Common/Tools.h"
28 #include "tools/atlas/GameInterface/MessagePasser.h"
29 #include "tools/atlas/GameInterface/Messages.h"
30 #include "tools/atlas/GameInterface/Shareable.h"
31 #include "tools/atlas/GameInterface/SharedTypes.h"
32
33 #include <climits>
34 #include <list>
35 #include <string>
36 #include <vector>
37 #include <wx/arrstr.h>
38 #include <wx/bookctrl.h>
39 #include <wx/button.h>
40 #include <wx/chartype.h>
41 #include <wx/checkbox.h>
42 #include <wx/choice.h>
43 #include <wx/choicebk.h>
44 #include <wx/clntdata.h>
45 #include <wx/collpane.h>
46 #include <wx/colour.h>
47 #include <wx/colourdata.h>
48 #include <wx/debug.h>
49 #include <wx/gdicmn.h>
50 #include <wx/msgdlg.h>
51 #include <wx/object.h>
52 #include <wx/panel.h>
53 #include <wx/scrolwin.h>
54 #include <wx/sizer.h>
55 #include <wx/spinbutt.h>
56 #include <wx/spinctrl.h>
57 #include <wx/statbox.h>
58 #include <wx/stattext.h>
59 #include <wx/textctrl.h>
60 #include <wx/toolbar.h>
61 #include <wx/translation.h>
62 #include <wx/window.h>
63
64 enum
65 {
66 ID_NumPlayers,
67 ID_PlayerFood,
68 ID_PlayerWood,
69 ID_PlayerMetal,
70 ID_PlayerStone,
71 ID_PlayerPop,
72 ID_PlayerColor,
73
74 ID_DefaultName,
75 ID_DefaultCiv,
76 ID_DefaultColor,
77 ID_DefaultAI,
78 ID_DefaultFood,
79 ID_DefaultWood,
80 ID_DefaultMetal,
81 ID_DefaultStone,
82 ID_DefaultPop,
83 ID_DefaultTeam,
84
85 ID_CameraSet,
86 ID_CameraView,
87 ID_CameraClear
88 };
89
90 // TODO: Some of these helper things should be moved out of this file
91 // and into shared locations
92
93 // Helper function for adding tooltips
94 static wxWindow* Tooltipped(wxWindow* window, const wxString& tip)
95 {
96 window->SetToolTip(tip);
97 return window;
98 }
99
100 //////////////////////////////////////////////////////////////////////////
101
102 class DefaultCheckbox : public wxCheckBox
103 {
104 public:
105 DefaultCheckbox(wxWindow* parent, wxWindowID id, wxWindow* control, bool initialValue = false)
106 : wxCheckBox(parent, id, wxEmptyString), m_Control(control)
107 {
108 SetValue(initialValue);
109 }
110
111 virtual void SetValue(bool value)
112 {
113 m_Control->Enable(value);
114 wxCheckBox::SetValue(value);
115 }
116
117 void OnChecked(wxCommandEvent& evt)
118 {
119 m_Control->Enable(evt.IsChecked());
120
121 evt.Skip();
122 }
123
124 private:
125 wxWindow* m_Control;
126
127 DECLARE_EVENT_TABLE();
128 };
129
130 BEGIN_EVENT_TABLE(DefaultCheckbox, wxCheckBox)
131 EVT_CHECKBOX(wxID_ANY, DefaultCheckbox::OnChecked)
132 END_EVENT_TABLE();
133
134
135 class PlayerNotebookPage : public wxPanel
136 {
137
138 public:
139 PlayerNotebookPage(wxWindow* parent, const wxString& name, size_t playerID)
140 : wxPanel(parent, wxID_ANY), m_Name(name), m_PlayerID(playerID)
141 {
142
143 m_Controls.page = this;
144
145 Freeze();
146
147 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
148 SetSizer(sizer);
149
150 {
151 /////////////////////////////////////////////////////////////////////////
152 // Player Info
153 wxStaticBoxSizer* playerInfoSizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Player info"));
154 wxFlexGridSizer* gridSizer = new wxFlexGridSizer(3, 5, 5);
155 gridSizer->AddGrowableCol(2);
156
157 wxTextCtrl* nameCtrl = new wxTextCtrl(playerInfoSizer->GetStaticBox(), wxID_ANY);
158 gridSizer->Add(new DefaultCheckbox(playerInfoSizer->GetStaticBox(), ID_DefaultName, nameCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
159 gridSizer->Add(new wxStaticText(playerInfoSizer->GetStaticBox(), wxID_ANY, _("Name")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
160 gridSizer->Add(nameCtrl, wxSizerFlags(1).Expand().Align(wxALIGN_RIGHT));
161 m_Controls.name = nameCtrl;
162
163 wxChoice* civChoice = new wxChoice(playerInfoSizer->GetStaticBox(), wxID_ANY);
164 gridSizer->Add(new DefaultCheckbox(playerInfoSizer->GetStaticBox(), ID_DefaultCiv, civChoice), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
165 gridSizer->Add(new wxStaticText(playerInfoSizer->GetStaticBox(), wxID_ANY, _("Civilisation")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
166 gridSizer->Add(civChoice, wxSizerFlags(1).Expand().Align(wxALIGN_RIGHT));
167 m_Controls.civ = civChoice;
168
169 wxButton* colorButton = new wxButton(playerInfoSizer->GetStaticBox(), ID_PlayerColor);
170 gridSizer->Add(new DefaultCheckbox(playerInfoSizer->GetStaticBox(), ID_DefaultColor, colorButton), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
171 gridSizer->Add(new wxStaticText(playerInfoSizer->GetStaticBox(), wxID_ANY, _("Color")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
172 gridSizer->Add(Tooltipped(colorButton,
173 _("Set player color")), wxSizerFlags(1).Expand().Align(wxALIGN_RIGHT));
174 m_Controls.color = colorButton;
175
176 wxChoice* aiChoice = new wxChoice(playerInfoSizer->GetStaticBox(), wxID_ANY);
177 gridSizer->Add(new DefaultCheckbox(playerInfoSizer->GetStaticBox(), ID_DefaultAI, aiChoice), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
178 gridSizer->Add(new wxStaticText(playerInfoSizer->GetStaticBox(), wxID_ANY, _("AI")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
179 gridSizer->Add(Tooltipped(aiChoice,
180 _("Select AI")), wxSizerFlags(1).Expand().Align(wxALIGN_RIGHT));
181 m_Controls.ai = aiChoice;
182
183 playerInfoSizer->Add(gridSizer, wxSizerFlags(1).Expand());
184 sizer->Add(playerInfoSizer, wxSizerFlags().Expand().Border(wxTOP, 10));
185 }
186
187 {
188 /////////////////////////////////////////////////////////////////////////
189 // Resources
190 wxStaticBoxSizer* resourceSizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Resources"));
191 wxFlexGridSizer* gridSizer = new wxFlexGridSizer(3, 5, 5);
192 gridSizer->AddGrowableCol(2);
193
194 wxSpinCtrl* foodCtrl = new wxSpinCtrl(resourceSizer->GetStaticBox(), ID_PlayerFood, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, INT_MAX);
195 gridSizer->Add(new DefaultCheckbox(resourceSizer->GetStaticBox(), ID_DefaultFood, foodCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
196 gridSizer->Add(new wxStaticText(resourceSizer->GetStaticBox(), wxID_ANY, _("Food")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
197 gridSizer->Add(Tooltipped(foodCtrl,
198 _("Initial value of food resource")), wxSizerFlags().Expand());
199 m_Controls.food = foodCtrl;
200
201 wxSpinCtrl* woodCtrl = new wxSpinCtrl(resourceSizer->GetStaticBox(), ID_PlayerWood, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, INT_MAX);
202 gridSizer->Add(new DefaultCheckbox(resourceSizer->GetStaticBox(), ID_DefaultWood, woodCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
203 gridSizer->Add(new wxStaticText(resourceSizer->GetStaticBox(), wxID_ANY, _("Wood")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
204 gridSizer->Add(Tooltipped(woodCtrl,
205 _("Initial value of wood resource")), wxSizerFlags().Expand());
206 m_Controls.wood = woodCtrl;
207
208 wxSpinCtrl* metalCtrl = new wxSpinCtrl(resourceSizer->GetStaticBox(), ID_PlayerMetal, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, INT_MAX);
209 gridSizer->Add(new DefaultCheckbox(resourceSizer->GetStaticBox(), ID_DefaultMetal, metalCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
210 gridSizer->Add(new wxStaticText(resourceSizer->GetStaticBox(), wxID_ANY, _("Metal")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
211 gridSizer->Add(Tooltipped(metalCtrl,
212 _("Initial value of metal resource")), wxSizerFlags().Expand());
213 m_Controls.metal = metalCtrl;
214
215 wxSpinCtrl* stoneCtrl = new wxSpinCtrl(resourceSizer->GetStaticBox(), ID_PlayerStone, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, INT_MAX);
216 gridSizer->Add(new DefaultCheckbox(resourceSizer->GetStaticBox(), ID_DefaultStone, stoneCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
217 gridSizer->Add(new wxStaticText(resourceSizer->GetStaticBox(), wxID_ANY, _("Stone")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
218 gridSizer->Add(Tooltipped(stoneCtrl,
219 _("Initial value of stone resource")), wxSizerFlags().Expand());
220 m_Controls.stone = stoneCtrl;
221
222 wxSpinCtrl* popCtrl = new wxSpinCtrl(resourceSizer->GetStaticBox(), ID_PlayerPop, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, INT_MAX);
223 gridSizer->Add(new DefaultCheckbox(resourceSizer->GetStaticBox(), ID_DefaultPop, popCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
224 gridSizer->Add(new wxStaticText(resourceSizer->GetStaticBox(), wxID_ANY, _("Pop limit")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT));
225 gridSizer->Add(Tooltipped(popCtrl,
226 _("Population limit for this player")), wxSizerFlags().Expand());
227 m_Controls.pop = popCtrl;
228
229 resourceSizer->Add(gridSizer, wxSizerFlags(1).Expand());
230 sizer->Add(resourceSizer, wxSizerFlags().Expand().Border(wxTOP, 10));
231 }
232 {
233 /////////////////////////////////////////////////////////////////////////
234 // Diplomacy
235 wxStaticBoxSizer* diplomacySizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Diplomacy"));
236 wxBoxSizer* boxSizer = new wxBoxSizer(wxHORIZONTAL);
237 wxChoice* teamCtrl = new wxChoice(diplomacySizer->GetStaticBox(), wxID_ANY);
238 boxSizer->Add(new DefaultCheckbox(diplomacySizer->GetStaticBox(), ID_DefaultTeam, teamCtrl), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
239 boxSizer->AddSpacer(5);
240 boxSizer->Add(new wxStaticText(diplomacySizer->GetStaticBox(), wxID_ANY, _("Team")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
241 boxSizer->AddSpacer(5);
242 teamCtrl->Append(_("None"));
243 teamCtrl->Append(_T("1"));
244 teamCtrl->Append(_T("2"));
245 teamCtrl->Append(_T("3"));
246 teamCtrl->Append(_T("4"));
247 boxSizer->Add(teamCtrl);
248 m_Controls.team = teamCtrl;
249 diplomacySizer->Add(boxSizer, wxSizerFlags(1).Expand());
250
251 // TODO: possibly have advanced panel where each player's diplomacy can be set?
252 // Advanced panel
253 /*wxCollapsiblePane* advPane = new wxCollapsiblePane(this, wxID_ANY, _("Advanced"));
254 wxWindow* pane = advPane->GetPane();
255 diplomacySizer->Add(advPane, 0, wxGROW | wxALL, 2);*/
256
257 sizer->Add(diplomacySizer, wxSizerFlags().Expand().Border(wxTOP, 10));
258 }
259
260 {
261 /////////////////////////////////////////////////////////////////////////
262 // Camera
263 wxStaticBoxSizer* cameraSizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Starting Camera"));
264 wxGridSizer* gridSizer = new wxGridSizer(3);
265 wxButton* cameraSet = new wxButton(cameraSizer->GetStaticBox(), ID_CameraSet, _("Set"), wxDefaultPosition, wxSize(48, -1));
266 gridSizer->Add(Tooltipped(cameraSet,
267 _("Set player camera to cameraSizer->GetStaticBox() view")), wxSizerFlags().Expand());
268 wxButton* cameraView = new wxButton(cameraSizer->GetStaticBox(), ID_CameraView, _("View"), wxDefaultPosition, wxSize(48, -1));
269 cameraView->Enable(false);
270 gridSizer->Add(Tooltipped(cameraView,
271 _("View the player camera")), wxSizerFlags().Expand());
272 wxButton* cameraClear = new wxButton(cameraSizer->GetStaticBox(), ID_CameraClear, _("Clear"), wxDefaultPosition, wxSize(48, -1));
273 cameraClear->Enable(false);
274 gridSizer->Add(Tooltipped(cameraClear,
275 _("Clear player camera")), wxSizerFlags().Expand());
276 cameraSizer->Add(gridSizer, wxSizerFlags().Expand());
277
278 sizer->Add(cameraSizer, wxSizerFlags().Expand().Border(wxTOP, 10));
279 }
280
281 Layout();
282 Thaw();
283
284 }
285
286 void OnDisplay()
287 {
288 }
289
290 PlayerPageControls GetControls()
291 {
292 return m_Controls;
293 }
294
295 wxString GetPlayerName()
296 {
297 return m_Name;
298 }
299
300 size_t GetPlayerID()
301 {
302 return m_PlayerID;
303 }
304
305 bool IsCameraDefined()
306 {
307 return m_CameraDefined;
308 }
309
310 sCameraInfo GetCamera()
311 {
312 return m_Camera;
313 }
314
315 void SetCamera(sCameraInfo info, bool isDefined = true)
316 {
317 m_Camera = info;
318 m_CameraDefined = isDefined;
319
320 // Enable/disable controls
321 wxDynamicCast(FindWindow(ID_CameraView), wxButton)->Enable(isDefined);
322 wxDynamicCast(FindWindow(ID_CameraClear), wxButton)->Enable(isDefined);
323 }
324
325 private:
326 void OnColor(wxCommandEvent& evt)
327 {
328 // Show color dialog
329 ColorDialog colorDlg(this, _T("Scenario Editor/PlayerColor"), m_Controls.color->GetBackgroundColour());
330
331 if (colorDlg.ShowModal() == wxID_OK)
332 {
333 m_Controls.color->SetBackgroundColour(colorDlg.GetColourData().GetColour());
334
335 // Pass event on to next handler
336 evt.Skip();
337 }
338 }
339
340 void OnCameraSet(wxCommandEvent& evt)
341 {
342 AtlasMessage::qGetView qryView;
343 qryView.Post();
344 SetCamera(qryView.info, true);
345
346 // Pass event on to next handler
347 evt.Skip();
348 }
349
350 void OnCameraView(wxCommandEvent& WXUNUSED(evt))
351 {
352 POST_MESSAGE(SetView, (m_Camera));
353 }
354
355 void OnCameraClear(wxCommandEvent& evt)
356 {
357 SetCamera(sCameraInfo(), false);
358
359 // Pass event on to next handler
360 evt.Skip();
361 }
362
363 sCameraInfo m_Camera;
364 bool m_CameraDefined;
365 wxString m_Name;
366 size_t m_PlayerID;
367
368 PlayerPageControls m_Controls;
369
370 DECLARE_EVENT_TABLE();
371 };
372
373 BEGIN_EVENT_TABLE(PlayerNotebookPage, wxPanel)
374 EVT_BUTTON(ID_PlayerColor, PlayerNotebookPage::OnColor)
375 EVT_BUTTON(ID_CameraSet, PlayerNotebookPage::OnCameraSet)
376 EVT_BUTTON(ID_CameraView, PlayerNotebookPage::OnCameraView)
377 EVT_BUTTON(ID_CameraClear, PlayerNotebookPage::OnCameraClear)
378 END_EVENT_TABLE();
379
380 //////////////////////////////////////////////////////////////////////////
381
382 class PlayerNotebook : public wxChoicebook
383 {
384 public:
385 PlayerNotebook(wxWindow *parent)
386 : wxChoicebook(parent, wxID_ANY/*, wxDefaultPosition, wxDefaultSize, wxNB_FIXEDWIDTH*/)
387 {
388 }
389
390 PlayerPageControls AddPlayer(wxString name, size_t player)
391 {
392 PlayerNotebookPage* playerPage = new PlayerNotebookPage(this, name, player);
393 AddPage(playerPage, name);
394 m_Pages.push_back(playerPage);
395 return playerPage->GetControls();
396 }
397
398 void ResizePlayers(size_t numPlayers)
399 {
400 wxASSERT(numPlayers <= m_Pages.size());
401
402 // We don't really want to destroy the windows corresponding
403 // to the tabs, so we've kept them in a vector and will
404 // only remove and add them to the notebook as needed
405 int selection = GetSelection();
406 size_t pageCount = GetPageCount();
407
408 if (numPlayers > pageCount)
409 {
410 // Add previously removed pages
411 for (size_t i = pageCount; i < numPlayers; ++i)
412 {
413 AddPage(m_Pages[i], m_Pages[i]->GetPlayerName());
414 }
415 }
416 else
417 {
418 // Remove previously added pages
419 // we have to manually hide them or they remain visible
420 for (size_t i = pageCount - 1; i >= numPlayers; --i)
421 {
422 m_Pages[i]->Hide();
423 RemovePage(i);
424 }
425 }
426
427 // Workaround for bug on wxGTK 2.8: wxChoice selection doesn't update
428 // (in fact it loses its selection when adding/removing pages)
429 GetChoiceCtrl()->SetSelection(selection);
430 }
431
432 protected:
433 void OnPageChanged(wxChoicebookEvent& evt)
434 {
435 if (evt.GetSelection() >= 0 && evt.GetSelection() < (int)GetPageCount())
436 {
437 static_cast<PlayerNotebookPage*>(GetPage(evt.GetSelection()))->OnDisplay();
438 }
439 evt.Skip();
440 }
441
442 private:
443 std::vector<PlayerNotebookPage*> m_Pages;
444
445 DECLARE_EVENT_TABLE();
446 };
447
448 BEGIN_EVENT_TABLE(PlayerNotebook, wxChoicebook)
449 EVT_CHOICEBOOK_PAGE_CHANGED(wxID_ANY, PlayerNotebook::OnPageChanged)
450 END_EVENT_TABLE();
451
452 //////////////////////////////////////////////////////////////////////////
453
454 class PlayerSettingsControl : public wxPanel
455 {
456 public:
457 PlayerSettingsControl(wxWindow* parent, ScenarioEditor& scenarioEditor);
458 void CreateWidgets();
459 void LoadDefaults();
460 void ReadFromEngine();
461 AtObj UpdateSettingsObject();
462
463 private:
464 void SendToEngine();
465
466 void OnEdit(wxCommandEvent& WXUNUSED(evt))
467 {
468 if (!m_InGUIUpdate)
469 {
470 SendToEngine();
471 }
472 }
473
474 void OnEditSpin(wxSpinEvent& WXUNUSED(evt))
475 {
476 if (!m_InGUIUpdate)
477 {
478 SendToEngine();
479 }
480 }
481
482 void OnPlayerColor(wxCommandEvent& WXUNUSED(evt))
483 {
484 if (!m_InGUIUpdate)
485 {
486 SendToEngine();
487
488 // Update player settings, to show new color
489 POST_MESSAGE(LoadPlayerSettings, (false));
490 }
491 }
492
493 void OnNumPlayersText(wxCommandEvent& WXUNUSED(evt))
494 { // Ignore because it will also trigger EVT_SPINCTRL
495 // and we don't want to handle the same event twice
496 }
497
498 void OnNumPlayersSpin(wxSpinEvent& evt)
499 {
500 if (!m_InGUIUpdate)
501 {
502 wxASSERT(evt.GetInt() > 0);
503
504 // When wxMessageBox pops up, wxSpinCtrl loses focus, which
505 // forces another EVT_SPINCTRL event, which we don't want
506 // to handle, so we check here for a change
507 if (evt.GetInt() == (int)m_NumPlayers)
508 {
509 return; // No change
510 }
511
512 size_t oldNumPlayers = m_NumPlayers;
513 m_NumPlayers = evt.GetInt();
514
515 if (m_NumPlayers < oldNumPlayers)
516 {
517 // Remove players, but check if they own any entities
518 bool notified = false;
519 for (size_t i = oldNumPlayers; i > m_NumPlayers; --i)
520 {
521 qGetPlayerObjects objectsQry(i);
522 objectsQry.Post();
523
524 std::vector<AtlasMessage::ObjectID> ids = *objectsQry.ids;
525
526 if (ids.size() > 0)
527 {
528 if (!notified)
529 {
530 // TODO: Add option to reassign objects?
531 if (wxMessageBox(_("WARNING: All objects belonging to the removed players will be deleted. Continue anyway?"), _("Remove player confirmation"), wxICON_EXCLAMATION | wxYES_NO) != wxYES)
532 {
533 // Restore previous player count
534 m_NumPlayers = oldNumPlayers;
535 wxDynamicCast(FindWindow(ID_NumPlayers), wxSpinCtrl)->SetValue(m_NumPlayers);
536 return;
537 }
538
539 notified = true;
540 }
541
542 // Delete objects
543 // TODO: Merge multiple commands?
544 POST_COMMAND(DeleteObjects, (ids));
545 }
546 }
547 }
548
549 m_Players->ResizePlayers(m_NumPlayers);
550 SendToEngine();
551
552 // Reload players, notify observers
553 POST_MESSAGE(LoadPlayerSettings, (true));
554 m_MapSettings.NotifyObservers();
555 }
556 }
557
558 // TODO: we shouldn't hardcode this, but instead dynamically create
559 // new player notebook pages on demand; of course the default data
560 // will be limited by the entries in player_defaults.json
561 static const size_t MAX_NUM_PLAYERS = 8;
562
563 bool m_InGUIUpdate;
564 AtObj m_PlayerDefaults;
565 PlayerNotebook* m_Players;
566 std::vector<PlayerPageControls> m_PlayerControls;
567 Observable<AtObj>& m_MapSettings;
568 size_t m_NumPlayers;
569
570 DECLARE_EVENT_TABLE();
571 };
572
573 BEGIN_EVENT_TABLE(PlayerSettingsControl, wxPanel)
574 EVT_BUTTON(ID_PlayerColor, PlayerSettingsControl::OnPlayerColor)
575 EVT_BUTTON(ID_CameraSet, PlayerSettingsControl::OnEdit)
576 EVT_BUTTON(ID_CameraClear, PlayerSettingsControl::OnEdit)
577 EVT_CHECKBOX(wxID_ANY, PlayerSettingsControl::OnEdit)
578 EVT_CHOICE(wxID_ANY, PlayerSettingsControl::OnEdit)
579 EVT_TEXT(ID_NumPlayers, PlayerSettingsControl::OnNumPlayersText)
580 EVT_TEXT(wxID_ANY, PlayerSettingsControl::OnEdit)
581 EVT_SPINCTRL(ID_NumPlayers, PlayerSettingsControl::OnNumPlayersSpin)
582 EVT_SPINCTRL(ID_PlayerFood, PlayerSettingsControl::OnEditSpin)
583 EVT_SPINCTRL(ID_PlayerWood, PlayerSettingsControl::OnEditSpin)
584 EVT_SPINCTRL(ID_PlayerMetal, PlayerSettingsControl::OnEditSpin)
585 EVT_SPINCTRL(ID_PlayerStone, PlayerSettingsControl::OnEditSpin)
586 EVT_SPINCTRL(ID_PlayerPop, PlayerSettingsControl::OnEditSpin)
587 END_EVENT_TABLE();
588
589 PlayerSettingsControl::PlayerSettingsControl(wxWindow* parent, ScenarioEditor& scenarioEditor)
590 : wxPanel(parent, wxID_ANY), m_InGUIUpdate(false), m_MapSettings(scenarioEditor.GetMapSettings()), m_NumPlayers(0)
591 {
592 // To prevent recursion, don't handle GUI events right now
593 m_InGUIUpdate = true;
594
595 wxStaticBoxSizer* sizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Player settings"));
596 SetSizer(sizer);
597
598 wxBoxSizer* boxSizer = new wxBoxSizer(wxHORIZONTAL);
599 boxSizer->Add(new wxStaticText(sizer->GetStaticBox(), wxID_ANY, _("Num players")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
600 wxSpinCtrl* numPlayersSpin = new wxSpinCtrl(sizer->GetStaticBox(), ID_NumPlayers, wxEmptyString, wxDefaultPosition, wxSize(40, -1));
601 numPlayersSpin->SetValue(MAX_NUM_PLAYERS);
602 numPlayersSpin->SetRange(1, MAX_NUM_PLAYERS);
603 boxSizer->Add(numPlayersSpin);
604 sizer->Add(boxSizer, wxSizerFlags().Expand().Proportion(0));
605 sizer->AddSpacer(5);
606 m_Players = new PlayerNotebook(sizer->GetStaticBox());
607 sizer->Add(m_Players, wxSizerFlags().Expand().Proportion(1));
608
609 m_InGUIUpdate = false;
610 }
611
612 void PlayerSettingsControl::CreateWidgets()
613 {
614 // To prevent recursion, don't handle GUI events right now
615 m_InGUIUpdate = true;
616
617 // Load default civ and player data
618 wxArrayString civNames;
619 wxArrayString civCodes;
620 AtlasMessage::qGetCivData qryCiv;
621 qryCiv.Post();
622 std::vector<std::vector<std::wstring>> civData = *qryCiv.data;
623 for (const std::vector<std::wstring>& civ : civData)
624 {
625 civCodes.Add(civ[0]);
626 civNames.Add(civ[1]);
627 }
628
629 // Load AI data
630 ArrayOfAIData ais(AIData::CompareAIData);
631 AtlasMessage::qGetAIData qryAI;
632 qryAI.Post();
633 AtObj aiData = AtlasObject::LoadFromJSON(*qryAI.data);
634 for (AtIter a = aiData["AIData"]["item"]; a.defined(); ++a)
635 {
636 ais.Add(new AIData(wxString::FromUTF8(a["id"]), wxString::FromUTF8(a["data"]["name"])));
637 }
638
639 // Create player pages
640 AtIter playerDefs = m_PlayerDefaults["item"];
641 if (playerDefs.defined())
642 ++playerDefs; // Skip gaia
643
644 for (size_t i = 0; i < MAX_NUM_PLAYERS; ++i)
645 {
646 // Create new player tab and get controls
647 wxString name(_("Unknown"));
648 if (playerDefs["Name"].defined())
649 name = playerDefs["Name"];
650
651 PlayerPageControls controls = m_Players->AddPlayer(name, i);
652 m_PlayerControls.push_back(controls);
653
654 // Populate civ choice box
655 wxChoice* civChoice = controls.civ;
656 for (size_t j = 0; j < civNames.Count(); ++j)
657 civChoice->Append(civNames[j], new wxStringClientData(civCodes[j]));
658 civChoice->SetSelection(0);
659
660 // Populate ai choice box
661 wxChoice* aiChoice = controls.ai;
662 aiChoice->Append(_("<None>"), new wxStringClientData());
663 for (size_t j = 0; j < ais.Count(); ++j)
664 aiChoice->Append(ais[j]->GetName(), new wxStringClientData(ais[j]->GetID()));
665 aiChoice->SetSelection(0);
666
667 if (playerDefs.defined())
668 ++playerDefs;
669 }
670
671 m_InGUIUpdate = false;
672 }
673
674 void PlayerSettingsControl::LoadDefaults()
675 {
676 AtlasMessage::qGetPlayerDefaults qryPlayers;
677 qryPlayers.Post();
678 AtObj playerData = AtlasObject::LoadFromJSON(*qryPlayers.defaults);
679 m_PlayerDefaults = *playerData["PlayerData"];
680 }
681
682 void PlayerSettingsControl::ReadFromEngine()
683 {
684 AtlasMessage::qGetMapSettings qry;
685 qry.Post();
686
687 if (!(*qry.settings).empty())
688 {
689 // Prevent error if there's no map settings to parse
690 m_MapSettings = AtlasObject::LoadFromJSON(*qry.settings);
691 }
692 else
693 {
694 // Use blank object, it will be created next
695 m_MapSettings = AtObj();
696 }
697
698 AtIter player = m_MapSettings["PlayerData"]["item"];
699 if (!m_MapSettings.defined() || !player.defined() || player.count() == 0)
700 {
701 // Player data missing - set number of players to max
702 m_NumPlayers = MAX_NUM_PLAYERS;
703 }
704 else
705 {
706 ++player; // skip gaia
707 m_NumPlayers = player.count();
708 }
709
710 wxASSERT(m_NumPlayers <= MAX_NUM_PLAYERS && m_NumPlayers != 0);
711
712 // To prevent recursion, don't handle GUI events right now
713 m_InGUIUpdate = true;
714
715 wxDynamicCast(FindWindow(ID_NumPlayers), wxSpinCtrl)->SetValue(m_NumPlayers);
716
717 // Remove / add extra player pages as needed
718 m_Players->ResizePlayers(m_NumPlayers);
719
720 // Update player controls with player data
721 AtIter playerDefs = m_PlayerDefaults["item"];
722 if (playerDefs.defined())
723 ++playerDefs; // skip gaia
724
725 for (size_t i = 0; i < MAX_NUM_PLAYERS; ++i)
726 {
727 const PlayerPageControls& controls = m_PlayerControls[i];
728
729 // name
730 wxString name(_("Unknown"));
731 bool defined = player["Name"].defined();
732 if (defined)
733 name = wxString::FromUTF8(player["Name"]);
734 else if (playerDefs["Name"].defined())
735 name = wxString::FromUTF8(playerDefs["Name"]);
736
737 controls.name->SetValue(name);
738 wxDynamicCast(FindWindowById(ID_DefaultName, controls.page), DefaultCheckbox)->SetValue(defined);
739
740 // civ
741 wxChoice* choice = controls.civ;
742 defined = player["Civ"].defined();
743 wxString civCode = wxString::FromUTF8(defined ? player["Civ"] : playerDefs["Civ"]);
744
745 for (size_t j = 0; j < choice->GetCount(); ++j)
746 {
747 wxStringClientData* str = dynamic_cast<wxStringClientData*>(choice->GetClientObject(j));
748 if (str->GetData() == civCode)
749 {
750 choice->SetSelection(j);
751 break;
752 }
753 }
754 wxDynamicCast(FindWindowById(ID_DefaultCiv, controls.page), DefaultCheckbox)->SetValue(defined);
755
756 // color
757 wxColor color;
758 AtObj clrObj = *player["Color"];
759 defined = clrObj.defined();
760 if (!defined)
761 clrObj = *playerDefs["Color"];
762 color = wxColor((*clrObj["r"]).getInt(), (*clrObj["g"]).getInt(), (*clrObj["b"]).getInt());
763 controls.color->SetBackgroundColour(color);
764 wxDynamicCast(FindWindowById(ID_DefaultColor, controls.page), DefaultCheckbox)->SetValue(defined);
765
766 // player type
767 defined = player["AI"].defined();
768 wxString aiID = wxString::FromUTF8(defined ? player["AI"] : playerDefs["AI"]);
769
770 choice = controls.ai;
771 if (!aiID.empty())
772 {
773 // AI
774 for (size_t j = 0; j < choice->GetCount(); ++j)
775 {
776 wxStringClientData* str = dynamic_cast<wxStringClientData*>(choice->GetClientObject(j));
777 if (str->GetData() == aiID)
778 {
779 choice->SetSelection(j);
780 break;
781 }
782 }
783 }
784 else // Human
785 choice->SetSelection(0);
786 wxDynamicCast(FindWindowById(ID_DefaultAI, controls.page), DefaultCheckbox)->SetValue(defined);
787
788 // resources
789 AtObj resObj = *player["Resources"];
790 defined = resObj.defined() && resObj["food"].defined();
791 if (defined)
792 controls.food->SetValue((*resObj["food"]).getInt());
793 else
794 controls.food->SetValue(0);
795 wxDynamicCast(FindWindowById(ID_DefaultFood, controls.page), DefaultCheckbox)->SetValue(defined);
796
797 defined = resObj.defined() && resObj["wood"].defined();
798 if (defined)
799 controls.wood->SetValue((*resObj["wood"]).getInt());
800 else
801 controls.wood->SetValue(0);
802 wxDynamicCast(FindWindowById(ID_DefaultWood, controls.page), DefaultCheckbox)->SetValue(defined);
803
804 defined = resObj.defined() && resObj["metal"].defined();
805 if (defined)
806 controls.metal->SetValue((*resObj["metal"]).getInt());
807 else
808 controls.metal->SetValue(0);
809 wxDynamicCast(FindWindowById(ID_DefaultMetal, controls.page), DefaultCheckbox)->SetValue(defined);
810
811 defined = resObj.defined() && resObj["stone"].defined();
812 if (defined)
813 controls.stone->SetValue((*resObj["stone"]).getInt());
814 else
815 controls.stone->SetValue(0);
816 wxDynamicCast(FindWindowById(ID_DefaultStone, controls.page), DefaultCheckbox)->SetValue(defined);
817
818 // population limit
819 defined = player["PopulationLimit"].defined();
820 if (defined)
821 controls.pop->SetValue((*player["PopulationLimit"]).getInt());
822 else
823 controls.pop->SetValue(0);
824 wxDynamicCast(FindWindowById(ID_DefaultPop, controls.page), DefaultCheckbox)->SetValue(defined);
825
826 // team
827 defined = player["Team"].defined();
828 if (defined)
829 controls.team->SetSelection((*player["Team"]).getInt() + 1);
830 else
831 controls.team->SetSelection(0);
832 wxDynamicCast(FindWindowById(ID_DefaultTeam, controls.page), DefaultCheckbox)->SetValue(defined);
833
834 // camera
835 if (player["StartingCamera"].defined())
836 {
837 sCameraInfo info;
838 // Don't use wxAtof because it depends on locales which
839 // may cause problems with decimal points
840 // see: http://www.wxwidgets.org/docs/faqgtk.htm#locale
841 AtObj camPos = *player["StartingCamera"]["Position"];
842 info.pX = (float)(*camPos["x"]).getDouble();
843 info.pY = (float)(*camPos["y"]).getDouble();
844 info.pZ = (float)(*camPos["z"]).getDouble();
845 AtObj camRot = *player["StartingCamera"]["Rotation"];
846 info.rX = (float)(*camRot["x"]).getDouble();
847 info.rY = (float)(*camRot["y"]).getDouble();
848 info.rZ = (float)(*camRot["z"]).getDouble();
849
850 controls.page->SetCamera(info, true);
851 }
852 else
853 controls.page->SetCamera(sCameraInfo(), false);
854
855 if (player.defined())
856 ++player;
857
858 if (playerDefs.defined())
859 ++playerDefs;
860 }
861
862 // Send default properties to engine, since they might not be set
863 SendToEngine();
864
865 m_InGUIUpdate = false;
866 }
867
868 AtObj PlayerSettingsControl::UpdateSettingsObject()
869 {
870 // Update player data in the map settings
871 AtObj players;
872 players.set("@array", "");
873
874 wxASSERT(m_NumPlayers <= MAX_NUM_PLAYERS);
875
876 AtIter playerDefs = m_PlayerDefaults["item"];
877 if (playerDefs.defined())
878 ++playerDefs; // Skip gaia
879
880 for (size_t i = 0; i < m_NumPlayers; ++i)
881 {
882 PlayerPageControls controls = m_PlayerControls[i];
883
884 AtObj player;
885
886 // name
887 wxTextCtrl* text = controls.name;
888 if (text->IsEnabled())
889 player.set("Name", text->GetValue().utf8_str());
890
891 // civ
892 wxChoice* choice = controls.civ;
893 if (choice->IsEnabled() && choice->GetSelection() >= 0)
894 {
895 wxStringClientData* str = dynamic_cast<wxStringClientData*>(choice->GetClientObject(choice->GetSelection()));
896 player.set("Civ", str->GetData().utf8_str());
897 }
898 else
899 player.unset("Civ");
900
901 // color
902 if (controls.color->IsEnabled())
903 {
904 wxColor color = controls.color->GetBackgroundColour();
905 AtObj clrObj;
906 clrObj.setInt("r", (int)color.Red());
907 clrObj.setInt("g", (int)color.Green());
908 clrObj.setInt("b", (int)color.Blue());
909 player.set("Color", clrObj);
910 }
911
912 // player type
913 choice = controls.ai;
914 if (choice->IsEnabled() && choice->GetSelection() > 0)
915 {
916 // ai - get id
917 wxStringClientData* str = dynamic_cast<wxStringClientData*>(choice->GetClientObject(choice->GetSelection()));
918 player.set("AI", str->GetData().utf8_str());
919 }
920 else // human
921 player.unset("AI");
922
923 // resources
924 AtObj resObj;
925 if (controls.food->IsEnabled())
926 resObj.setInt("food", controls.food->GetValue());
927 if (controls.wood->IsEnabled())
928 resObj.setInt("wood", controls.wood->GetValue());
929 if (controls.metal->IsEnabled())
930 resObj.setInt("metal", controls.metal->GetValue());
931 if (controls.stone->IsEnabled())
932 resObj.setInt("stone", controls.stone->GetValue());
933 if (resObj.defined())
934 player.set("Resources", resObj);
935
936 // population limit
937 if (controls.pop->IsEnabled())
938 player.setInt("PopulationLimit", controls.pop->GetValue());
939
940 // team
941 choice = controls.team;
942 if (choice->IsEnabled() && choice->GetSelection() >= 0)
943 player.setInt("Team", choice->GetSelection() - 1);
944
945 // camera
946 AtObj camObj;
947 if (controls.page->IsCameraDefined())
948 {
949 sCameraInfo cam = controls.page->GetCamera();
950 AtObj camPos;
951 camPos.setDouble("x", cam.pX);
952 camPos.setDouble("y", cam.pY);
953 camPos.setDouble("z", cam.pZ);
954 camObj.set("Position", camPos);
955
956 AtObj camRot;
957 camRot.setDouble("x", cam.rX);
958 camRot.setDouble("y", cam.rY);
959 camRot.setDouble("z", cam.rZ);
960 camObj.set("Rotation", camRot);
961 }
962 player.set("StartingCamera", camObj);
963
964 players.add("item", player);
965
966 if (playerDefs.defined())
967 ++playerDefs;
968 }
969
970 m_MapSettings.set("PlayerData", players);
971
972 return m_MapSettings;
973 }
974
975 void PlayerSettingsControl::SendToEngine()
976 {
977 UpdateSettingsObject();
978
979 std::string json = AtlasObject::SaveToJSON(m_MapSettings);
980
981 // TODO: would be nice if we supported undo for settings changes
982
983 POST_COMMAND(SetMapSettings, (json));
984 }
985
986 //////////////////////////////////////////////////////////////////////////
987
988 PlayerSidebar::PlayerSidebar(ScenarioEditor& scenarioEditor, wxWindow* sidebarContainer, wxWindow* bottomBarContainer)
989 : Sidebar(scenarioEditor, sidebarContainer, bottomBarContainer), m_Loaded(false)
990 {
991 wxSizer* scrollSizer = new wxBoxSizer(wxVERTICAL);
992 wxScrolledWindow* scrolledWindow = new wxScrolledWindow(this);
993 scrolledWindow->SetScrollRate(10, 10);
994 scrolledWindow->SetSizer(scrollSizer);
995 m_MainSizer->Add(scrolledWindow, wxSizerFlags().Proportion(1).Expand());
996
997 m_PlayerSettingsCtrl = new PlayerSettingsControl(scrolledWindow, m_ScenarioEditor);
998 scrollSizer->Add(m_PlayerSettingsCtrl, wxSizerFlags().Expand());
999 }
1000
1001 void PlayerSidebar::OnCollapse(wxCollapsiblePaneEvent& WXUNUSED(evt))
1002 {
1003 Freeze();
1004
1005 // Toggling the collapsing doesn't seem to update the sidebar layout
1006 // automatically, so do it explicitly here
1007 Layout();
1008
1009 Refresh(); // fixes repaint glitch on Windows
1010
1011 Thaw();
1012 }
1013
1014 void PlayerSidebar::OnFirstDisplay()
1015 {
1016 // We do this here becase messages are used which requires simulation to be init'd
1017 m_PlayerSettingsCtrl->LoadDefaults();
1018 m_PlayerSettingsCtrl->CreateWidgets();
1019 m_PlayerSettingsCtrl->ReadFromEngine();
1020
1021 m_Loaded = true;
1022
1023 Layout();
1024 }
1025
1026 void PlayerSidebar::OnMapReload()
1027 {
1028 // Make sure we've loaded the controls
1029 if (m_Loaded)
1030 {
1031 m_PlayerSettingsCtrl->ReadFromEngine();
1032 }
1033 }
1034
1035 BEGIN_EVENT_TABLE(PlayerSidebar, Sidebar)
1036 EVT_COLLAPSIBLEPANE_CHANGED(wxID_ANY, PlayerSidebar::OnCollapse)
1037 END_EVENT_TABLE();