Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Brushes.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 "Brushes.h"
21
22 #include "tools/atlas/GameInterface/MessagePasser.h"
23 #include "tools/atlas/GameInterface/Messages.h"
24
25 #include <cmath>
26 #include <cstddef>
27 #include <wx/arrstr.h>
28 #include <wx/chartype.h>
29 #include <wx/debug.h>
30 #include <wx/defs.h>
31 #include <wx/event.h>
32 #include <wx/radiobox.h>
33 #include <wx/sizer.h>
34 #include <wx/spinbutt.h>
35 #include <wx/spinctrl.h>
36 #include <wx/stattext.h>
37 #include <wx/string.h>
38 #include <wx/toolbar.h>
39 #include <wx/translation.h>
40
41 Brush g_Brush_Elevation; // shared between several elevation-related tools; other tools have their own brushes
42
43 static Brush* g_Brush_CurrentlyActive = NULL; // only one brush can be active at once
44
45 const float Brush::STRENGTH_MULTIPLIER = 1024.f;
46
47 Brush::Brush()
48 : m_Shape(CIRCLE), m_Size(4), m_Strength(1.f), m_IsActive(false)
49 {
50 }
51
52 Brush::~Brush()
53 {
54 // Avoid dangling pointers
55 if (g_Brush_CurrentlyActive == this)
56 g_Brush_CurrentlyActive = NULL;
57 }
58
59 void Brush::MakeActive()
60 {
61 if (g_Brush_CurrentlyActive)
62 g_Brush_CurrentlyActive->m_IsActive = false;
63
64 g_Brush_CurrentlyActive = this;
65 m_IsActive = true;
66
67 Send();
68 }
69
70 void Brush::Send()
71 {
72 if (m_IsActive)
73 POST_MESSAGE(Brush, (GetWidth(), GetHeight(), GetData()));
74 }
75
76 int Brush::GetWidth() const
77 {
78 switch (m_Shape)
79 {
80 case CIRCLE:
81 return m_Size;
82 case SQUARE:
83 return m_Size;
84 default:
85 wxFAIL;
86 return -1;
87 }
88 }
89
90 int Brush::GetHeight() const
91 {
92 /*
93 switch (m_Shape)
94 {
95 case RECTANGLE or something:
96 default:
97 return GetWidth();
98 }
99 */
100 return GetWidth();
101 }
102
103 std::vector<float> Brush::GetData() const
104 {
105 int width = GetWidth();
106 int height = GetHeight();
107
108 std::vector<float> data (width*height);
109
110 switch (m_Shape)
111 {
112 case CIRCLE:
113 {
114 int i = 0;
115 // All calculations are done in units of half-tiles, since that
116 // is the required precision
117 int mid_x = m_Size-1;
118 int mid_y = m_Size-1;
119 for (int y = 0; y < m_Size; ++y)
120 {
121 for (int x = 0; x < m_Size; ++x)
122 {
123 float dist_sq = // scaled to 0 in centre, 1 on edge
124 ((2*x - mid_x)*(2*x - mid_x) +
125 (2*y - mid_y)*(2*y - mid_y)) / (float)(m_Size*m_Size);
126 if (dist_sq <= 1.f)
127 data[i++] = (sqrtf(2.f - dist_sq) - 1.f) / (sqrt(2.f) - 1.f);
128 else
129 data[i++] = 0.f;
130 }
131 }
132 break;
133 }
134
135 case SQUARE:
136 {
137 int i = 0;
138 for (int y = 0; y < height; ++y)
139 for (int x = 0; x < width; ++x)
140 data[i++] = 1.f;
141 break;
142 }
143 }
144
145 return data;
146 }
147
148 float Brush::GetStrength() const
149 {
150 return m_Strength;
151 }
152
153 void Brush::SetStrength(float strength)
154 {
155 m_Strength = strength;
156 }
157
158 void Brush::SetCircle(int size)
159 {
160 m_Shape = CIRCLE;
161 m_Size = size;
162 }
163
164 void Brush::SetSquare(int size)
165 {
166 m_Shape = SQUARE;
167 m_Size = size;
168 }
169
170 //////////////////////////////////////////////////////////////////////////
171
172 class BrushShapeCtrl : public wxRadioBox
173 {
174 public:
175 BrushShapeCtrl(wxWindow* parent, wxArrayString& shapes, Brush& brush)
176 : wxRadioBox(parent, wxID_ANY, _("Shape"), wxDefaultPosition, wxDefaultSize, shapes, 0, wxRA_SPECIFY_ROWS),
177 m_Brush(brush)
178 {
179 SetSelection(m_Brush.m_Shape);
180 }
181
182 private:
183 Brush& m_Brush;
184
185 void OnChange(wxCommandEvent& WXUNUSED(evt))
186 {
187 m_Brush.m_Shape = (Brush::BrushShape)GetSelection();
188 m_Brush.Send();
189 }
190
191 DECLARE_EVENT_TABLE();
192 };
193 BEGIN_EVENT_TABLE(BrushShapeCtrl, wxRadioBox)
194 EVT_RADIOBOX(wxID_ANY, BrushShapeCtrl::OnChange)
195 END_EVENT_TABLE()
196
197
198 class BrushSizeCtrl: public wxSpinCtrl
199 {
200 public:
201 BrushSizeCtrl(wxWindow* parent, Brush& brush)
202 : wxSpinCtrl(parent, wxID_ANY, wxString::Format(_T("%d"), brush.m_Size), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 100, brush.m_Size),
203 m_Brush(brush)
204 {
205 }
206
207 private:
208 Brush& m_Brush;
209
210 void OnChange(wxSpinEvent& WXUNUSED(evt))
211 {
212 m_Brush.m_Size = GetValue();
213 m_Brush.Send();
214 }
215
216 DECLARE_EVENT_TABLE();
217 };
218 BEGIN_EVENT_TABLE(BrushSizeCtrl, wxSpinCtrl)
219 EVT_SPINCTRL(wxID_ANY, BrushSizeCtrl::OnChange)
220 END_EVENT_TABLE()
221
222
223 class BrushStrengthCtrl : public wxSpinCtrl
224 {
225 public:
226 BrushStrengthCtrl(wxWindow* parent, Brush& brush)
227 : wxSpinCtrl(parent, wxID_ANY, wxString::Format(_T("%d"), (int)(10.f*brush.m_Strength)), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 100, (int)(10.f*brush.m_Strength)),
228 m_Brush(brush)
229 {
230 }
231
232 private:
233 Brush& m_Brush;
234
235 void OnChange(wxSpinEvent& WXUNUSED(evt))
236 {
237 m_Brush.m_Strength = GetValue()/10.f;
238 m_Brush.Send();
239 }
240
241 DECLARE_EVENT_TABLE();
242 };
243 BEGIN_EVENT_TABLE(BrushStrengthCtrl, wxSpinCtrl)
244 EVT_SPINCTRL(wxID_ANY, BrushStrengthCtrl::OnChange)
245 END_EVENT_TABLE()
246
247
248
249 void Brush::CreateUI(wxWindow* parent, wxSizer* sizer)
250 {
251 wxArrayString shapes; // Must match order of BrushShape enum
252 shapes.Add(_("Circle"));
253 shapes.Add(_("Square"));
254 // TODO (maybe): get rid of the extra static box, by not using wxRadioBox
255 sizer->Add(new BrushShapeCtrl(parent, shapes, *this), wxSizerFlags().Expand());
256
257 sizer->AddSpacer(5);
258
259 // TODO: These are yucky
260 wxFlexGridSizer* spinnerSizer = new wxFlexGridSizer(2, 5, 5);
261 spinnerSizer->AddGrowableCol(1);
262 spinnerSizer->Add(new wxStaticText(parent, wxID_ANY, _("Size")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT));
263 spinnerSizer->Add(new BrushSizeCtrl(parent, *this), wxSizerFlags().Expand());
264 spinnerSizer->Add(new wxStaticText(parent, wxID_ANY, _("Strength")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT));
265 spinnerSizer->Add(new BrushStrengthCtrl(parent, *this), wxSizerFlags().Expand());
266 sizer->Add(spinnerSizer, wxSizerFlags().Expand());
267 }