Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.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 "ObjectSettings.h"
21
22 #include "tools/atlas/AtlasUI/General/Observable.h"
23 #include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h"
24 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h"
25 #include "tools/atlas/GameInterface/Messages.h"
26 #include "tools/atlas/GameInterface/Shareable.h"
27 #include "tools/atlas/GameInterface/SharedTypes.h"
28
29 #include <cstddef>
30 #include <list>
31 #include <string>
32
33 ObjectSettings::ObjectSettings(Observable<std::vector<AtlasMessage::ObjectID> >& selectedObjects, int view)
34 : m_PlayerID(0), m_SelectedObjects(selectedObjects), m_View(view)
35 {
36 m_Conn = m_SelectedObjects.RegisterObserver(0, &ObjectSettings::OnSelectionChange, this);
37 }
38
39 int ObjectSettings::GetPlayerID() const
40 {
41 return m_PlayerID;
42 }
43
44 void ObjectSettings::SetPlayerID(int playerID)
45 {
46 m_PlayerID = playerID;
47 PostToGame();
48 }
49
50 void ObjectSettings::SetView(int view)
51 {
52 m_View = view;
53 }
54
55 const std::set<wxString>& ObjectSettings::GetActorSelections() const
56 {
57 return m_ActorSelections;
58 }
59
60 void ObjectSettings::SetActorSelections(const std::set<wxString>& selections)
61 {
62 m_ActorSelections = selections;
63 PostToGame();
64 }
65
66 const std::vector<ObjectSettings::Group> ObjectSettings::GetActorVariation() const
67 {
68 std::vector<Group> variation;
69
70 for (std::vector<wxArrayString>::const_iterator grp = m_VariantGroups.begin();
71 grp != m_VariantGroups.end();
72 ++grp)
73 {
74 Group group;
75 group.variants = *grp;
76
77 // Variant choice method, as used by the game: Choose the first variant
78 // which matches any of the selections
79
80 size_t chosen = 0; // default to first
81 for (size_t i = 0; i < grp->GetCount(); ++i)
82 {
83 if (m_ActorSelections.find(grp->Item(i)) != m_ActorSelections.end())
84 {
85 chosen = i;
86 break;
87 }
88 }
89 group.chosen = grp->Item(chosen);
90
91 variation.push_back(group);
92 }
93
94 return variation;
95 }
96
97 AtlasMessage::sObjectSettings ObjectSettings::GetSettings() const
98 {
99 AtlasMessage::sObjectSettings settings;
100
101 settings.player = m_PlayerID;
102
103 // Copy selections from set into vector
104 std::vector<std::wstring> selections;
105 for (std::set<wxString>::const_iterator it = m_ActorSelections.begin();
106 it != m_ActorSelections.end();
107 ++it)
108 {
109 selections.push_back((std::wstring)it->wc_str());
110 }
111 settings.selections = selections;
112
113 return settings;
114 }
115
116 void ObjectSettings::OnSelectionChange(const std::vector<AtlasMessage::ObjectID>& selection)
117 {
118 // TODO: what would be the sensible action if nothing's selected?
119 // and if multiple objects are selected?
120
121 if (selection.empty())
122 return;
123
124 AtlasMessage::qGetObjectSettings qry (m_View, selection[0]);
125 qry.Post();
126
127 m_PlayerID = qry.settings->player;
128
129 m_ActorSelections.clear();
130 m_VariantGroups.clear();
131
132 std::vector<std::vector<std::wstring> > variation = *qry.settings->variantGroups;
133 for (std::vector<std::vector<std::wstring> >::iterator grp = variation.begin();
134 grp != variation.end();
135 ++grp)
136 {
137 wxArrayString variants;
138
139 for (std::vector<std::wstring>::iterator it = grp->begin();
140 it != grp->end();
141 ++it)
142 {
143 variants.Add(it->c_str());
144 }
145
146 m_VariantGroups.push_back(variants);
147 }
148
149 std::vector<std::wstring> selections = *qry.settings->selections;
150 for (std::vector<std::wstring>::iterator sel = selections.begin();
151 sel != selections.end();
152 ++sel)
153 {
154 m_ActorSelections.insert(sel->c_str());
155 }
156
157 static_cast<Observable<ObjectSettings>*>(this)->NotifyObservers();
158 }
159
160 void ObjectSettings::PostToGame()
161 {
162 for (AtlasMessage::ObjectID& obj : m_SelectedObjects)
163 POST_COMMAND(SetObjectSettings, (m_View, obj, GetSettings()));
164 }