Debian Codegraph @

source/tools/atlas/AtlasUI/ScenarioEditor/SectionLayout.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 "SectionLayout.h"
21
22 #include "tools/atlas/AtlasUI/CustomControls/SnapSplitterWindow/SnapSplitterWindow.h"
23 #include "tools/atlas/AtlasUI/General/Datafile.h"
24 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Cinema/Cinema.h"
25 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Common/Sidebar.h"
26 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Environment/Environment.h"
27 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Map/Map.h"
28 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Object/Object.h"
29 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Player/Player.h"
30 #include "tools/atlas/AtlasUI/ScenarioEditor/Sections/Terrain/Terrain.h"
31
32 #include <cstddef>
33 #include <utility>
34 #include <vector>
35 #include <wx/bitmap.h>
36 #include <wx/bmpbndl.h>
37 #include <wx/bmpbuttn.h>
38 #include <wx/chartype.h>
39 #include <wx/colour.h>
40 #include <wx/defs.h>
41 #include <wx/event.h>
42 #include <wx/filename.h>
43 #include <wx/gdicmn.h>
44 #include <wx/image.h>
45 #include <wx/log.h>
46 #include <wx/panel.h>
47 #include <wx/settings.h>
48 #include <wx/sizer.h>
49 #include <wx/splitter.h>
50 #include <wx/string.h>
51 #include <wx/toolbar.h>
52 #include <wx/translation.h>
53 #include <wx/wfstream.h>
54 #include <wx/window.h>
55
56 //////////////////////////////////////////////////////////////////////////
57
58 class SidebarButton : public wxBitmapButton
59 {
60 public:
61 SidebarButton(wxWindow* parent, const wxBitmap& bitmap, SidebarBook* book, size_t id)
62 : wxBitmapButton(parent, wxID_ANY, bitmap, wxDefaultPosition, wxSize(34, 32))
63 , m_Book(book), m_Id(id)
64 {
65 SetSelectedAppearance(false);
66 }
67
68 void OnClick(wxCommandEvent& event);
69
70 void SetSelectedAppearance(bool selected)
71 {
72 if (selected)
73 SetBackgroundColour(wxColor(0xee, 0xcc, 0x55));
74 else
75 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
76 }
77
78 private:
79 SidebarBook* m_Book;
80 size_t m_Id;
81
82 DECLARE_EVENT_TABLE();
83 };
84
85 BEGIN_EVENT_TABLE(SidebarButton, wxBitmapButton)
86 EVT_BUTTON(wxID_ANY, SidebarButton::OnClick)
87 END_EVENT_TABLE();
88
89
90 class SidebarBook : public wxPanel
91 {
92 private:
93 struct SidebarPage
94 {
95 SidebarPage() : button(NULL), bar(NULL) {}
96 SidebarPage(SidebarButton* button, Sidebar* bar) : button(button), bar(bar) {}
97 SidebarButton* button;
98 Sidebar* bar;
99 };
100
101 public:
102 SidebarBook(wxWindow *parent, SnapSplitterWindow* splitter)
103 : wxPanel(parent), m_Splitter(splitter), m_SelectedPage(-1)
104 {
105 m_ButtonsSizer = new wxGridSizer(6, 0, 0);
106
107 wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
108
109 mainSizer->Add(m_ButtonsSizer);
110
111 m_ContentWindow = new wxPanel(this);
112 mainSizer->Add(m_ContentWindow, wxSizerFlags().Expand().Proportion(1).Border(wxALL, 4));
113
114 SetSizer(mainSizer);
115 }
116
117 // Only allow Sidebar objects to be added
118 bool AddPage(Sidebar* sidebar, const wxString& iconPNGFilename, const wxString& tooltip)
119 {
120 wxImage img (1, 1, true);
121
122 // Load the icon
123 wxFileName iconPath (_T("tools/atlas/toolbar/"));
124 iconPath.MakeAbsolute(Datafile::GetDataDirectory());
125 iconPath.SetFullName(iconPNGFilename);
126 wxFFileInputStream fstr (iconPath.GetFullPath());
127 if (! fstr.Ok())
128 {
129 wxLogError(_("Failed to open toolbar icon file '%s'"), iconPath.GetFullPath().c_str());
130 }
131 else
132 {
133 img = wxImage(fstr, wxBITMAP_TYPE_PNG);
134 if (! img.Ok())
135 {
136 wxLogError(_("Failed to load toolbar icon image '%s'"), iconPath.GetFullPath().c_str());
137 img = wxImage (1, 1, true);
138 }
139 }
140
141 // Create the button for the sidebar toolbar
142 SidebarButton* button = new SidebarButton(this, img, this, m_Pages.size());
143 button->SetToolTip(tooltip);
144
145 m_ButtonsSizer->Add(button);
146
147 m_Pages.push_back(SidebarPage(button, sidebar));
148
149 sidebar->Show(false);
150
151 return true;
152 }
153
154 size_t GetPageCount()
155 {
156 return m_Pages.size();
157 }
158
159 wxWindow* GetContentWindow()
160 {
161 return m_ContentWindow;
162 }
163
164 void RepositionSelectedPage()
165 {
166 if (m_SelectedPage != -1 && m_Pages[m_SelectedPage].bar)
167 {
168 m_Pages[m_SelectedPage].bar->SetSize(m_ContentWindow->GetSize());
169 }
170 }
171
172 void OnSize(wxSizeEvent& event)
173 {
174 Layout();
175 RepositionSelectedPage();
176 event.Skip();
177 }
178
179 void SetSelection(size_t page)
180 {
181 if (page < m_Pages.size())
182 {
183 // If selecting the same one twice, don't do anything
184 if ((ssize_t)page == m_SelectedPage)
185 return;
186
187 SidebarPage oldPage;
188 if (m_SelectedPage != -1)
189 oldPage = m_Pages[m_SelectedPage];
190
191 if (oldPage.bar)
192 oldPage.bar->Show(false);
193
194 m_SelectedPage = (ssize_t)page;
195 RepositionSelectedPage();
196 m_Pages[m_SelectedPage].bar->Show(true);
197
198 OnPageChanged(oldPage, m_Pages[m_SelectedPage]);
199 }
200 }
201
202 void OnMapReload()
203 {
204 for (size_t i = 0; i < m_Pages.size(); ++i)
205 m_Pages[i].bar->OnMapReload();
206 }
207
208 void OnShutdown()
209 {
210 for (size_t i = 0; i < m_Pages.size(); ++i)
211 m_Pages[i].bar->OnShutdown();
212 }
213
214 protected:
215
216 void OnPageChanged(SidebarPage oldPage, SidebarPage newPage)
217 {
218 if (oldPage.bar)
219 {
220 oldPage.bar->OnSwitchAway();
221 oldPage.button->SetSelectedAppearance(false);
222 }
223
224 if (newPage.bar)
225 {
226 newPage.bar->OnSwitchTo();
227 newPage.button->SetSelectedAppearance(true);
228 }
229
230 if (m_Splitter->IsSplit())
231 {
232 wxWindow* bottom;
233 if (newPage.bar && NULL != (bottom = newPage.bar->GetBottomBar()))
234 {
235 m_Splitter->ReplaceWindow(m_Splitter->GetWindow2(), bottom);
236 }
237 else
238 {
239 m_Splitter->Unsplit();
240 }
241 }
242 else
243 {
244 wxWindow* bottom;
245 if (newPage.bar && NULL != (bottom = newPage.bar->GetBottomBar()))
246 {
247 m_Splitter->SplitHorizontally(m_Splitter->GetWindow1(), bottom);
248 }
249 }
250 }
251
252 private:
253 wxSizer* m_ButtonsSizer;
254 wxWindow* m_ContentWindow;
255 SnapSplitterWindow* m_Splitter;
256
257 std::vector<SidebarPage> m_Pages;
258 ssize_t m_SelectedPage;
259
260 DECLARE_EVENT_TABLE();
261 };
262
263 BEGIN_EVENT_TABLE(SidebarBook, wxPanel)
264 EVT_SIZE(SidebarBook::OnSize)
265 END_EVENT_TABLE();
266
267 void SidebarButton::OnClick(wxCommandEvent& WXUNUSED(event))
268 {
269 m_Book->SetSelection(m_Id);
270 }
271
272 //////////////////////////////////////////////////////////////////////////
273
274 SectionLayout::SectionLayout()
275 {
276 }
277
278 SectionLayout::~SectionLayout()
279 {
280 }
281
282 void SectionLayout::SetWindow(wxWindow* window)
283 {
284 m_HorizSplitter = new SnapSplitterWindow(
285 window, wxSP_NOBORDER, _T("Scenario Editor/Layout/HorizontalSplitter/"));
286 m_VertSplitter = new SnapSplitterWindow(
287 m_HorizSplitter, wxSP_3D, _T("Scenario Editor/Layout/VerticalSplitter/"));
288 }
289
290 wxWindow* SectionLayout::GetCanvasParent()
291 {
292 return m_VertSplitter;
293 }
294
295 void SectionLayout::SetCanvas(wxWindow* canvas)
296 {
297 m_Canvas = canvas;
298 }
299
300 void SectionLayout::Build(ScenarioEditor& scenarioEditor)
301 {
302 // TODO: wxWidgets bug (http://sourceforge.net/tracker/index.php?func=detail&aid=1298803&group_id=9863&atid=109863)
303 // - pressing menu keys (e.g. alt+f) with notebook tab focussed causes application to freeze
304
305 m_SidebarBook = new SidebarBook(m_HorizSplitter, m_VertSplitter);
306 Sidebar* sidebar;
307
308 #define ADD_SIDEBAR(classname, icon, tooltip) \
309 sidebar = new classname(scenarioEditor, m_SidebarBook->GetContentWindow(), m_VertSplitter); \
310 if (sidebar->GetBottomBar()) \
311 sidebar->GetBottomBar()->Show(false); \
312 m_SidebarBook->AddPage(sidebar, icon, tooltip); \
313 m_PageMappings.insert(std::make_pair(L###classname, (int)m_SidebarBook->GetPageCount()-1));
314
315 ADD_SIDEBAR(MapSidebar, _T("map.png"), _("Map"));
316 ADD_SIDEBAR(PlayerSidebar, _T("player.png"), _("Player"));
317 ADD_SIDEBAR(TerrainSidebar, _T("terrain.png"), _("Terrain"));
318 ADD_SIDEBAR(ObjectSidebar, _T("object.png"), _("Object"));
319 ADD_SIDEBAR(EnvironmentSidebar, _T("environment.png"), _("Environment"));
320 ADD_SIDEBAR(CinemaSidebar, _T("cinematic.png"), _("Cinema"));
321
322 #undef ADD_SIDEBAR
323
324 m_VertSplitter->SetDefaultSashPosition(-BOTTOMBAR_SIZE);
325 m_VertSplitter->Initialize(m_Canvas);
326
327 m_HorizSplitter->SetDefaultSashPosition(SIDEBAR_SIZE);
328 m_HorizSplitter->SplitVertically(m_SidebarBook, m_VertSplitter);
329 }
330
331 void SectionLayout::SelectPage(const wxString& classname)
332 {
333 std::map<std::wstring, int>::iterator it = m_PageMappings.find((std::wstring)classname.wc_str());
334 if (it != m_PageMappings.end())
335 m_SidebarBook->SetSelection(it->second);
336 }
337
338 void SectionLayout::OnMapReload()
339 {
340 m_SidebarBook->OnMapReload();
341 }
342
343 void SectionLayout::OnShutdown()
344 {
345 m_SidebarBook->OnShutdown();
346 }