| 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 "VariationControl.h" |
| 21 |
|
| 22 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.h" |
| 23 |
#include "tools/atlas/AtlasUI/General/Observable.h" |
| 24 |
|
| 25 |
#include <cstddef> |
| 26 |
#include <list> |
| 27 |
#include <set> |
| 28 |
#include <wx/arrstr.h> |
| 29 |
#include <wx/combobox.h> |
| 30 |
#include <wx/debug.h> |
| 31 |
#include <wx/defs.h> |
| 32 |
#include <wx/event.h> |
| 33 |
#include <wx/gdicmn.h> |
| 34 |
#include <wx/object.h> |
| 35 |
#include <wx/sizer.h> |
| 36 |
#include <wx/string.h> |
| 37 |
#include <wx/toolbar.h> |
| 38 |
#include <wx/types.h> |
| 39 |
|
| 40 |
class wxWindow; |
| 41 |
|
| 42 |
VariationControl::VariationControl(wxWindow* parent, Observable<ObjectSettings>& objectSettings) |
| 43 |
: wxScrolledWindow(parent, -1), |
| 44 |
m_ObjectSettings(objectSettings) |
| 45 |
{ |
| 46 |
m_Conn = m_ObjectSettings.RegisterObserver(1, &VariationControl::OnObjectSettingsChange, this); |
| 47 |
|
| 48 |
SetScrollRate(0, 5); |
| 49 |
|
| 50 |
m_Sizer = new wxBoxSizer(wxVERTICAL); |
| 51 |
SetSizer(m_Sizer); |
| 52 |
} |
| 53 |
|
| 54 |
// Event handler shared by all the combo boxes created by this window |
| 55 |
void VariationControl::OnSelect(wxCommandEvent& evt) |
| 56 |
{ |
| 57 |
std::set<wxString> selections; |
| 58 |
|
| 59 |
// It's possible for a variant name to appear in multiple groups. |
| 60 |
// If so, assume that all the names in each group are the same, so |
| 61 |
// we don't have to worry about some impossible combinations (e.g. |
| 62 |
// one group "a,b", a second "b,c", and a third "c,a", where's there's |
| 63 |
// no set of selections that matches one (and only one) of each group). |
| 64 |
// |
| 65 |
// So... When a combo box is changed from 'a' to 'b', add 'b' to the new |
| 66 |
// selections and make sure any other combo boxes containing both 'a' and |
| 67 |
// 'b' no longer contain 'a'. |
| 68 |
|
| 69 |
wxComboBox* thisComboBox = wxDynamicCast(evt.GetEventObject(), wxComboBox); |
| 70 |
wxCHECK(thisComboBox != NULL, ); |
| 71 |
wxString newValue = thisComboBox->GetValue(); |
| 72 |
|
| 73 |
selections.insert(newValue); |
| 74 |
|
| 75 |
for (size_t i = 0; i < m_ComboBoxes.size(); ++i) |
| 76 |
{ |
| 77 |
wxComboBox* comboBox = m_ComboBoxes[i]; |
| 78 |
// If our newly selected value is used in another combobox, we want |
| 79 |
// that combobox to use the new value, so don't add its old value |
| 80 |
// to the list of selections |
| 81 |
if (comboBox->FindString(newValue) == wxNOT_FOUND) |
| 82 |
selections.insert(comboBox->GetValue()); |
| 83 |
} |
| 84 |
|
| 85 |
m_ObjectSettings.SetActorSelections(selections); |
| 86 |
m_ObjectSettings.NotifyObserversExcept(m_Conn); |
| 87 |
RefreshObjectSettings(); |
| 88 |
} |
| 89 |
|
| 90 |
void VariationControl::OnObjectSettingsChange(const ObjectSettings& settings) |
| 91 |
{ |
| 92 |
Freeze(); |
| 93 |
|
| 94 |
const std::vector<ObjectSettings::Group>& variation = settings.GetActorVariation(); |
| 95 |
|
| 96 |
// Creating combo boxes seems to be pretty expensive - so we create as |
| 97 |
// few as possible, by never deleting any. |
| 98 |
|
| 99 |
size_t oldCount = m_ComboBoxes.size(); |
| 100 |
size_t newCount = variation.size(); |
| 101 |
|
| 102 |
// If we have too many combo boxes, hide the excess ones |
| 103 |
for (size_t i = newCount; i < oldCount; ++i) |
| 104 |
{ |
| 105 |
m_ComboBoxes[i]->Show(false); |
| 106 |
} |
| 107 |
|
| 108 |
for (size_t i = 0; i < variation.size(); ++i) |
| 109 |
{ |
| 110 |
const ObjectSettings::Group& group = variation[i]; |
| 111 |
|
| 112 |
if (i < oldCount) |
| 113 |
{ |
| 114 |
// Already got enough boxes available, so use an old one |
| 115 |
wxComboBox* comboBox = m_ComboBoxes[i]; |
| 116 |
// Replace the contents of the old combobox with the new data |
| 117 |
comboBox->Freeze(); |
| 118 |
comboBox->Clear(); |
| 119 |
comboBox->Append(group.variants); |
| 120 |
comboBox->SetValue(group.chosen); |
| 121 |
comboBox->Show(true); |
| 122 |
comboBox->Thaw(); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
// Create an initially empty combobox, because we can fill it |
| 127 |
// quicker than the default constructor can |
| 128 |
wxComboBox* combo = new wxComboBox(this, -1, wxEmptyString, wxDefaultPosition, |
| 129 |
wxSize(80, wxDefaultCoord), wxArrayString(), wxCB_READONLY); |
| 130 |
// Freeze it before adding all the values |
| 131 |
combo->Freeze(); |
| 132 |
combo->Append(group.variants); |
| 133 |
combo->SetValue(group.chosen); |
| 134 |
combo->Thaw(); |
| 135 |
// Add the on-select event handler |
| 136 |
combo->Connect(wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, |
| 137 |
wxCommandEventHandler(VariationControl::OnSelect), NULL, this); |
| 138 |
// Add box to sizer and list |
| 139 |
m_Sizer->Add(combo, wxSizerFlags().Expand()); |
| 140 |
m_ComboBoxes.push_back(combo); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
Layout(); |
| 145 |
|
| 146 |
Thaw(); |
| 147 |
|
| 148 |
// Make the scrollbars appear when appropriate |
| 149 |
FitInside(); |
| 150 |
} |
| 151 |
|
| 152 |
void VariationControl::RefreshObjectSettings() |
| 153 |
{ |
| 154 |
const std::vector<ObjectSettings::Group>& variation = m_ObjectSettings.GetActorVariation(); |
| 155 |
|
| 156 |
// For each group, set the corresponding combobox's value to the chosen one |
| 157 |
size_t i = 0; |
| 158 |
for (std::vector<ObjectSettings::Group>::const_iterator group = variation.begin(); |
| 159 |
group != variation.end() && i < m_ComboBoxes.size(); |
| 160 |
++group, ++i) |
| 161 |
{ |
| 162 |
m_ComboBoxes[i]->SetValue(group->chosen); |
| 163 |
} |
| 164 |
} |