#include "precompiled.h" #include "wx/filename.h" #include "wx/wfstream.h" #include "SectionLayout.h" #include "SnapSplitterWindow/SnapSplitterWindow.h" #include "ScenarioEditor.h" #include "AtlasScript/ScriptInterface.h" #include "Sections/Terrain/Terrain.h" #include "Sections/Object/Object.h" #include "Sections/Environment/Environment.h" #include "Sections/Cinematic/Cinematic.h" #include "Sections/Trigger/Trigger.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(28, 28)) , m_Book(book), m_Id(id) { SetSelectedAppearance(false); } void OnClick(wxCommandEvent& event); void SetSelectedAppearance(bool selected) { if (selected) SetBackgroundColour(wxColour(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, wxButton) 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, 5, 5); 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); // If this is the first page, make it selected by default if (m_Pages.size() == 1) SetSelection(0); 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]); } } 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 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); } ////////////////////////////////////////////////////////////////////////// class ScriptedSidebar : public Sidebar { public: ScriptedSidebar(const wxString& name, ScenarioEditor& scenarioEditor, wxWindow* sidebarContainer, wxWindow* bottomBarContainer) : Sidebar(scenarioEditor, sidebarContainer, bottomBarContainer) { wxPanel* panel = m_ScenarioEditor.GetScriptInterface().LoadScriptAsPanel(_T("section/") + name, this); m_MainSizer->Add(panel, wxSizerFlags(1).Expand()); } }; 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)); #define ADD_SIDEBAR_SCRIPT(name, icon, tooltip) \ sidebar = new ScriptedSidebar(name, 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(name, (int)m_SidebarBook->GetPageCount()-1)); ADD_SIDEBAR_SCRIPT(_T("map"), _T("map.png"), _("Map")); ADD_SIDEBAR_SCRIPT(_T("terrain"), _T("terrain.png"), _("Terrain")); ADD_SIDEBAR(TerrainSidebar, _T("terrain.png"), _("Terrain")); ADD_SIDEBAR(ObjectSidebar, _T("object.png"), _("Object")); ADD_SIDEBAR(EnvironmentSidebar, _T("environment.png"), _("Environment")); #ifndef ATLAS_PUBLIC_RELEASE ADD_SIDEBAR(CinematicSidebar, _T("cinematic.png"), _("Cinema")); ADD_SIDEBAR(TriggerSidebar, _T("trigger.png"), _("Trigger")); #endif #undef ADD_SIDEBAR m_VertSplitter->SetDefaultSashPosition(-165); m_VertSplitter->Initialize(m_Canvas); m_HorizSplitter->SetDefaultSashPosition(200); m_HorizSplitter->SplitVertically(m_SidebarBook, m_VertSplitter); } void SectionLayout::SelectPage(const wxString& classname) { std::map::iterator it = m_PageMappings.find(classname.c_str()); if (it != m_PageMappings.end()) m_SidebarBook->SetSelection(it->second); }