Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Sections/Environment/Environment.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 "Environment.h"
21
22 #include "tools/atlas/AtlasUI/CustomControls/ColorDialog/ColorDialog.h"
23 #include "tools/atlas/AtlasUI/General/Observable.h"
24 #include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h"
25 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Common/Sidebar.h"
26 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Environment/LightControl.h"
27 #include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h"
28 #include "tools/atlas/GameInterface/Messages.h"
29 #include "tools/atlas/GameInterface/Shareable.h"
30 #include "tools/atlas/GameInterface/SharedTypes.h"
31
32 #include <cmath>
33 #include <cstddef>
34 #include <list>
35 #include <string>
36 #include <vector>
37 #include <wx/arrstr.h>
38 #include <wx/button.h>
39 #include <wx/chartype.h>
40 #include <wx/colour.h>
41 #include <wx/colourdata.h>
42 #include <wx/combobox.h>
43 #include <wx/gdicmn.h>
44 #include <wx/panel.h>
45 #include <wx/scrolwin.h>
46 #include <wx/sizer.h>
47 #include <wx/slider.h>
48 #include <wx/statbox.h>
49 #include <wx/string.h>
50 #include <wx/toolbar.h>
51 #include <wx/translation.h>
52
53 class wxWindow;
54
55 using AtlasMessage::Shareable;
56
57 static Observable<AtlasMessage::sEnvironmentSettings> g_EnvironmentSettings;
58
59 //////////////////////////////////////////////////////////////////////////
60
61 class VariableSliderBox : public wxPanel
62 {
63 static const int range = 1024;
64 public:
65 VariableSliderBox(wxWindow* parent, const wxString& label, Shareable<float>& var, float min, float max)
66 : wxPanel(parent),
67 m_Var(var), m_Min(min), m_Max(max)
68 {
69 m_Conn = g_EnvironmentSettings.RegisterObserver(0, &VariableSliderBox::OnSettingsChange, this);
70
71 m_Sizer = new wxStaticBoxSizer(wxVERTICAL, this, label);
72 SetSizer(m_Sizer);
73
74 m_Slider = new wxSlider(m_Sizer->GetStaticBox(), -1, 0, 0, range);
75 m_Sizer->Add(m_Slider, wxSizerFlags().Expand());
76 }
77
78 void OnSettingsChange(const AtlasMessage::sEnvironmentSettings& WXUNUSED(env))
79 {
80 m_Slider->SetValue((m_Var - m_Min) * (range / (m_Max - m_Min)));
81 }
82
83 void OnScroll(wxScrollEvent& evt)
84 {
85 m_Var = m_Min + (m_Max - m_Min)*(evt.GetInt() / (float)range);
86
87 g_EnvironmentSettings.NotifyObserversExcept(m_Conn);
88 }
89
90 private:
91 ObservableScopedConnection m_Conn;
92 wxStaticBoxSizer* m_Sizer;
93 wxSlider* m_Slider;
94 Shareable<float>& m_Var;
95 float m_Min, m_Max;
96
97 DECLARE_EVENT_TABLE();
98 };
99
100 BEGIN_EVENT_TABLE(VariableSliderBox, wxPanel)
101 EVT_SCROLL(VariableSliderBox::OnScroll)
102 END_EVENT_TABLE()
103
104 //////////////////////////////////////////////////////////////////////////
105
106 class VariableListBox : public wxPanel
107 {
108 public:
109 VariableListBox(wxWindow* parent, const wxString& label, Shareable<std::wstring>& var)
110 : wxPanel(parent),
111 m_Var(var)
112 {
113 m_Conn = g_EnvironmentSettings.RegisterObserver(
114 0, &VariableListBox::OnSettingsChange, this);
115
116 m_Sizer = new wxStaticBoxSizer(wxVERTICAL, this, label);
117 SetSizer(m_Sizer);
118
119 m_Combo = new wxComboBox(
120 m_Sizer->GetStaticBox(), -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
121 wxArrayString(), wxCB_READONLY),
122 m_Sizer->Add(m_Combo, wxSizerFlags().Expand());
123 }
124
125 void SetChoices(const std::vector<std::wstring>& choices)
126 {
127 wxArrayString choices_arraystr;
128 for (size_t i = 0; i < choices.size(); ++i)
129 choices_arraystr.Add(choices[i].c_str());
130
131 m_Combo->Clear();
132 m_Combo->Append(choices_arraystr);
133
134 m_Combo->SetValue(m_Var.c_str());
135 }
136
137 void OnSettingsChange(const AtlasMessage::sEnvironmentSettings& WXUNUSED(env))
138 {
139 m_Combo->SetValue(m_Var.c_str());
140 }
141
142 void OnSelect(wxCommandEvent& WXUNUSED(evt))
143 {
144 m_Var = std::wstring(m_Combo->GetValue().c_str());
145
146 g_EnvironmentSettings.NotifyObserversExcept(m_Conn);
147 }
148
149 private:
150 ObservableScopedConnection m_Conn;
151 wxStaticBoxSizer* m_Sizer;
152 wxComboBox* m_Combo;
153 Shareable<std::wstring>& m_Var;
154
155 DECLARE_EVENT_TABLE();
156 };
157
158 BEGIN_EVENT_TABLE(VariableListBox, wxPanel)
159 EVT_COMBOBOX(wxID_ANY, VariableListBox::OnSelect)
160 END_EVENT_TABLE()
161
162 //////////////////////////////////////////////////////////////////////////
163
164 class VariableColorBox : public wxPanel
165 {
166 public:
167 VariableColorBox(wxWindow* parent, const wxString& label, Shareable<AtlasMessage::Color>& color)
168 : wxPanel(parent),
169 m_Color(color)
170 {
171 m_Conn = g_EnvironmentSettings.RegisterObserver(0, &VariableColorBox::OnSettingsChange, this);
172
173 m_Sizer = new wxStaticBoxSizer(wxVERTICAL, this, label);
174 SetSizer(m_Sizer);
175
176 m_Button = new wxButton(m_Sizer->GetStaticBox(), -1);
177 m_Sizer->Add(m_Button, wxSizerFlags().Expand());
178 }
179
180 void OnSettingsChange(const AtlasMessage::sEnvironmentSettings& WXUNUSED(env))
181 {
182 UpdateButton();
183 }
184
185 void OnClick(wxCommandEvent& WXUNUSED(evt))
186 {
187 ColorDialog dlg (this, _T("Scenario Editor/LightingColor"),
188 wxColor(m_Color->r, m_Color->g, m_Color->b));
189
190 if (dlg.ShowModal() == wxID_OK)
191 {
192 wxColor& c = dlg.GetColourData().GetColour();
193 m_Color = AtlasMessage::Color(c.Red(), c.Green(), c.Blue());
194 UpdateButton();
195
196 g_EnvironmentSettings.NotifyObserversExcept(m_Conn);
197 }
198 }
199
200 void UpdateButton()
201 {
202 m_Button->SetBackgroundColour(wxColor(m_Color->r, m_Color->g, m_Color->b));
203 m_Button->SetLabel(wxString::Format(_T("%02X %02X %02X"), m_Color->r, m_Color->g, m_Color->b));
204
205 int y = 3*m_Color->r + 6*m_Color->g + 1*m_Color->b;
206 if (y > 1280)
207 m_Button->SetForegroundColour(wxColor(0, 0, 0));
208 else
209 m_Button->SetForegroundColour(wxColor(255, 255, 255));
210 }
211
212
213 private:
214 ObservableScopedConnection m_Conn;
215 wxStaticBoxSizer* m_Sizer;
216 wxButton* m_Button;
217 Shareable<AtlasMessage::Color>& m_Color;
218
219 DECLARE_EVENT_TABLE();
220 };
221
222 BEGIN_EVENT_TABLE(VariableColorBox, wxPanel)
223 EVT_BUTTON(wxID_ANY, VariableColorBox::OnClick)
224 END_EVENT_TABLE()
225
226 //////////////////////////////////////////////////////////////////////////
227
228 enum {
229 ID_RecomputeWaterData,
230 ID_PickWaterHeight
231 };
232 static void SendToGame(const AtlasMessage::sEnvironmentSettings& settings)
233 {
234 POST_COMMAND(SetEnvironmentSettings, (settings));
235 }
236
237 EnvironmentSidebar::EnvironmentSidebar(
238 ScenarioEditor& scenarioEditor, wxWindow* sidebarContainer, wxWindow* bottomBarContainer)
239 : Sidebar(scenarioEditor, sidebarContainer, bottomBarContainer)
240 {
241 wxSizer* scrollSizer = new wxBoxSizer(wxVERTICAL);
242 wxScrolledWindow* scrolledWindow = new wxScrolledWindow(this);
243 scrolledWindow->SetScrollRate(10, 10);
244 scrolledWindow->SetSizer(scrollSizer);
245 m_MainSizer->Add(scrolledWindow, wxSizerFlags().Proportion(1).Expand());
246
247 wxStaticBoxSizer* waterSizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _T("Water settings"));
248 scrollSizer->Add(waterSizer, wxSizerFlags().Expand());
249 waterSizer->Add(new wxButton(
250 waterSizer->GetStaticBox(), ID_RecomputeWaterData, _("Reset Water Data")), wxSizerFlags().Expand());
251 waterSizer->Add(m_WaterTypeList = new VariableListBox(
252 waterSizer->GetStaticBox(), _("Water Type"), g_EnvironmentSettings.watertype), wxSizerFlags().Expand());
253 waterSizer->Add(new VariableSliderBox(
254 waterSizer->GetStaticBox(), _("Water height"), g_EnvironmentSettings.waterheight, 0.f, 1.2f), wxSizerFlags().Expand());
255 waterSizer->Add(new wxButton(
256 waterSizer->GetStaticBox(), ID_PickWaterHeight, _("Pick Water Height")), wxSizerFlags().Expand());
257 waterSizer->Add(new VariableSliderBox(
258 waterSizer->GetStaticBox(), _("Water waviness"), g_EnvironmentSettings.waterwaviness, 0.f, 10.f), wxSizerFlags().Expand());
259 waterSizer->Add(new VariableSliderBox(
260 waterSizer->GetStaticBox(), _("Water murkiness"), g_EnvironmentSettings.watermurkiness, 0.f, 1.f), wxSizerFlags().Expand());
261 waterSizer->Add(new VariableSliderBox(
262 waterSizer->GetStaticBox(), _("Wind angle"), g_EnvironmentSettings.windangle, -static_cast<float>(M_PI), static_cast<float>(M_PI)), wxSizerFlags().Expand());
263 waterSizer->Add(new VariableColorBox(
264 waterSizer->GetStaticBox(), _("Water color"), g_EnvironmentSettings.watercolor), wxSizerFlags().Expand());
265 waterSizer->Add(new VariableColorBox(
266 waterSizer->GetStaticBox(), _("Water tint"), g_EnvironmentSettings.watertint), wxSizerFlags().Expand());
267
268 std::vector<std::wstring> list;
269 list.push_back(L"ocean"); list.push_back(L"lake"); list.push_back(L"clap");
270 m_WaterTypeList->SetChoices(list);
271
272
273 wxStaticBoxSizer* sunSizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _T("Sun / lighting settings"));
274 scrollSizer->Add(sunSizer, wxSizerFlags().Expand().Border(wxTOP, 8));
275
276 sunSizer->Add(new VariableSliderBox(
277 sunSizer->GetStaticBox(), _("Sun rotation"), g_EnvironmentSettings.sunrotation, -static_cast<float>(M_PI), static_cast<float>(M_PI)), wxSizerFlags().Expand());
278 sunSizer->Add(new VariableSliderBox(
279 sunSizer->GetStaticBox(), _("Sun elevation"), g_EnvironmentSettings.sunelevation, -static_cast<float>(M_PI) / 2.0f, static_cast<float>(M_PI) / 2.0f), wxSizerFlags().Expand());
280 sunSizer->Add(new VariableSliderBox(
281 sunSizer->GetStaticBox(), _("Sun overbrightness"), g_EnvironmentSettings.sunoverbrightness, 1.0f, 3.0f), wxSizerFlags().Expand());
282 sunSizer->Add(new LightControl(
283 sunSizer->GetStaticBox(), wxSize(150, 150), g_EnvironmentSettings));
284 sunSizer->Add(new VariableColorBox(
285 sunSizer->GetStaticBox(), _("Sun color"), g_EnvironmentSettings.suncolor), wxSizerFlags().Expand());
286 sunSizer->Add(m_SkyList = new VariableListBox(
287 sunSizer->GetStaticBox(), _("Sky set"), g_EnvironmentSettings.skyset), wxSizerFlags().Expand());
288 sunSizer->Add(new VariableSliderBox(
289 sunSizer->GetStaticBox(), _("Fog Factor"), g_EnvironmentSettings.fogfactor, 0.0f, 0.01f), wxSizerFlags().Expand());
290 sunSizer->Add(new VariableSliderBox(
291 sunSizer->GetStaticBox(), _("Fog Thickness"), g_EnvironmentSettings.fogmax, 0.5f, 0.0f), wxSizerFlags().Expand());
292 sunSizer->Add(new VariableColorBox(
293 sunSizer->GetStaticBox(), _("Fog color"), g_EnvironmentSettings.fogcolor), wxSizerFlags().Expand());
294 sunSizer->Add(new VariableColorBox(
295 sunSizer->GetStaticBox(), _("Ambient color"), g_EnvironmentSettings.ambientcolor), wxSizerFlags().Expand());
296
297 wxStaticBoxSizer* postProcSizer = new wxStaticBoxSizer(wxVERTICAL, scrolledWindow, _T("Post-processing settings"));
298 scrollSizer->Add(postProcSizer, wxSizerFlags().Expand().Border(wxTOP, 8));
299
300 postProcSizer->Add(m_PostEffectList = new VariableListBox(
301 postProcSizer->GetStaticBox(), _("Post Effect"), g_EnvironmentSettings.posteffect), wxSizerFlags().Expand());
302 postProcSizer->Add(new VariableSliderBox(
303 postProcSizer->GetStaticBox(), _("Brightness"), g_EnvironmentSettings.brightness, -0.5f, 0.5f), wxSizerFlags().Expand());
304 postProcSizer->Add(new VariableSliderBox(
305 postProcSizer->GetStaticBox(), _("Contrast (HDR)"), g_EnvironmentSettings.contrast, 0.5f, 1.5f), wxSizerFlags().Expand());
306 postProcSizer->Add(new VariableSliderBox(
307 postProcSizer->GetStaticBox(), _("Saturation"), g_EnvironmentSettings.saturation, 0.0f, 2.0f), wxSizerFlags().Expand());
308 postProcSizer->Add(new VariableSliderBox(
309 postProcSizer->GetStaticBox(), _("Bloom"), g_EnvironmentSettings.bloom, 0.2f, 0.0f), wxSizerFlags().Expand());
310
311 m_Conn = g_EnvironmentSettings.RegisterObserver(0, &SendToGame);
312 }
313
314 void EnvironmentSidebar::OnFirstDisplay()
315 {
316 // Load the list of skies. (Can only be done now rather than in the constructor,
317 // after the game has been initialised.)
318 AtlasMessage::qGetSkySets qry_skysets;
319 qry_skysets.Post();
320 m_SkyList->SetChoices(*qry_skysets.skysets);
321
322 AtlasMessage::qGetPostEffects qry_effects;
323 qry_effects.Post();
324 m_PostEffectList->SetChoices(*qry_effects.posteffects);
325
326 UpdateEnvironmentSettings();
327 }
328
329 void EnvironmentSidebar::OnMapReload()
330 {
331 UpdateEnvironmentSettings();
332 }
333
334 void EnvironmentSidebar::RecomputeWaterData(wxCommandEvent& WXUNUSED(evt))
335 {
336 POST_COMMAND(RecalculateWaterData, (0.0f));
337 }
338
339 void EnvironmentSidebar::UpdateEnvironmentSettings()
340 {
341 AtlasMessage::qGetEnvironmentSettings qry_env;
342 qry_env.Post();
343 g_EnvironmentSettings = qry_env.settings;
344
345 g_EnvironmentSettings.NotifyObservers();
346 }
347
348 void EnvironmentSidebar::OnPickWaterHeight(wxCommandEvent& WXUNUSED(evt))
349 {
350 m_ScenarioEditor.GetToolManager().SetCurrentTool(_T("PickWaterHeight"), this);
351 }
352
353 BEGIN_EVENT_TABLE(EnvironmentSidebar, Sidebar)
354 EVT_BUTTON(ID_RecomputeWaterData, EnvironmentSidebar::RecomputeWaterData)
355 EVT_BUTTON(ID_PickWaterHeight, EnvironmentSidebar::OnPickWaterHeight)
356 END_EVENT_TABLE();
357