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