Index: ps/trunk/source/gui/CGUI.h =================================================================== --- ps/trunk/source/gui/CGUI.h +++ ps/trunk/source/gui/CGUI.h @@ -61,9 +61,9 @@ ~CGUI(); /** - * Initializes the GUI, needs to be called before the GUI is used + * Informs the GUI page which GUI object types may be constructed from XML. */ - void Initialize(); + void AddObjectTypes(); /** * Performs processing that should happen every frame Index: ps/trunk/source/gui/CGUI.cpp =================================================================== --- ps/trunk/source/gui/CGUI.cpp +++ ps/trunk/source/gui/CGUI.cpp @@ -19,21 +19,8 @@ #include "CGUI.h" +#include "gui/GUIObjectTypes.h" #include "gui/IGUIScrollBar.h" -#include "gui/ObjectTypes/CButton.h" -#include "gui/ObjectTypes/CChart.h" -#include "gui/ObjectTypes/CCheckBox.h" -#include "gui/ObjectTypes/CDropDown.h" -#include "gui/ObjectTypes/CImage.h" -#include "gui/ObjectTypes/CInput.h" -#include "gui/ObjectTypes/CList.h" -#include "gui/ObjectTypes/CMiniMap.h" -#include "gui/ObjectTypes/COList.h" -#include "gui/ObjectTypes/CProgressBar.h" -#include "gui/ObjectTypes/CRadioButton.h" -#include "gui/ObjectTypes/CSlider.h" -#include "gui/ObjectTypes/CText.h" -#include "gui/ObjectTypes/CTooltip.h" #include "gui/Scripting/ScriptFunctions.h" #include "i18n/L10n.h" #include "lib/bits.h" @@ -59,6 +46,28 @@ const double SELECT_DBLCLICK_RATE = 0.5; const u32 MAX_OBJECT_DEPTH = 100; // Max number of nesting for GUI includes. Used to detect recursive inclusion +CGUI::CGUI(const shared_ptr& runtime) + : m_BaseObject(*this), + m_FocusedObject(nullptr), + m_InternalNameNumber(0), + m_MouseButtons(0) +{ + m_ScriptInterface.reset(new ScriptInterface("Engine", "GUIPage", runtime)); + m_ScriptInterface->SetCallbackData(this); + + GuiScriptingInit(*m_ScriptInterface); + m_ScriptInterface->LoadGlobalScripts(); +} + +CGUI::~CGUI() +{ + for (const std::pair& p : m_pAllObjects) + delete p.second; + + for (const std::pair& p : m_Sprites) + delete p.second; +} + InReaction CGUI::HandleEvent(const SDL_Event_* ev) { InReaction ret = IN_PASS; @@ -272,57 +281,6 @@ m_BaseObject.RecurseObject(nullptr, &IGUIObject::ScriptEvent, EventNameLower, paramData); } -CGUI::CGUI(const shared_ptr& runtime) - : m_MouseButtons(0), m_FocusedObject(nullptr), m_InternalNameNumber(0), m_BaseObject(*this) -{ - m_ScriptInterface.reset(new ScriptInterface("Engine", "GUIPage", runtime)); - m_ScriptInterface->SetCallbackData(this); - - GuiScriptingInit(*m_ScriptInterface); - m_ScriptInterface->LoadGlobalScripts(); -} - -CGUI::~CGUI() -{ - for (const std::pair& p : m_pAllObjects) - delete p.second; - - for (const std::pair& p : m_Sprites) - delete p.second; -} - -IGUIObject* CGUI::ConstructObject(const CStr& str) -{ - std::map::iterator it = m_ObjectTypes.find(str); - if (it != m_ObjectTypes.end()) - return (*it->second)(*this); - - // Error reporting will be handled with the nullptr return. - return nullptr; -} - -void CGUI::Initialize() -{ - // Add base types! - // You can also add types outside the GUI to extend the flexibility of the GUI. - // Pyrogenesis though will have all the object types inserted from here. - AddObjectType("empty", &CGUIDummyObject::ConstructObject); - AddObjectType("button", &CButton::ConstructObject); - AddObjectType("image", &CImage::ConstructObject); - AddObjectType("text", &CText::ConstructObject); - AddObjectType("checkbox", &CCheckBox::ConstructObject); - AddObjectType("radiobutton", &CRadioButton::ConstructObject); - AddObjectType("progressbar", &CProgressBar::ConstructObject); - AddObjectType("minimap", &CMiniMap::ConstructObject); - AddObjectType("input", &CInput::ConstructObject); - AddObjectType("list", &CList::ConstructObject); - AddObjectType("olist", &COList::ConstructObject); - AddObjectType("dropdown", &CDropDown::ConstructObject); - AddObjectType("tooltip", &CTooltip::ConstructObject); - AddObjectType("chart", &CChart::ConstructObject); - AddObjectType("slider", &CSlider::ConstructObject); -} - void CGUI::Draw() { // Clear the depth buffer, so the GUI is @@ -348,6 +306,16 @@ m_BaseObject.RecurseObject(nullptr, &IGUIObject::UpdateCachedSize); } +IGUIObject* CGUI::ConstructObject(const CStr& str) +{ + std::map::iterator it = m_ObjectTypes.find(str); + + if (it == m_ObjectTypes.end()) + return nullptr; + + return (*it->second)(*this); +} + bool CGUI::AddObject(IGUIObject& parent, IGUIObject& child) { if (child.m_Name.empty()) Index: ps/trunk/source/gui/GUIManager.cpp =================================================================== --- ps/trunk/source/gui/GUIManager.cpp +++ ps/trunk/source/gui/GUIManager.cpp @@ -144,7 +144,7 @@ inputs.clear(); gui.reset(new CGUI(scriptRuntime)); - gui->Initialize(); + gui->AddObjectTypes(); VfsPath path = VfsPath("gui") / name; inputs.insert(path); Index: ps/trunk/source/gui/GUIObjectTypes.h =================================================================== --- ps/trunk/source/gui/GUIObjectTypes.h +++ ps/trunk/source/gui/GUIObjectTypes.h @@ -0,0 +1,54 @@ +/* Copyright (C) 2019 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 . + */ +#ifndef INCLUDED_GUIOBJECTTYPES +#define INCLUDED_GUIOBJECTTYPES + +#include "gui/ObjectTypes/CButton.h" +#include "gui/ObjectTypes/CChart.h" +#include "gui/ObjectTypes/CCheckBox.h" +#include "gui/ObjectTypes/CDropDown.h" +#include "gui/ObjectTypes/CImage.h" +#include "gui/ObjectTypes/CInput.h" +#include "gui/ObjectTypes/CList.h" +#include "gui/ObjectTypes/CMiniMap.h" +#include "gui/ObjectTypes/COList.h" +#include "gui/ObjectTypes/CProgressBar.h" +#include "gui/ObjectTypes/CRadioButton.h" +#include "gui/ObjectTypes/CSlider.h" +#include "gui/ObjectTypes/CText.h" +#include "gui/ObjectTypes/CTooltip.h" + +void CGUI::AddObjectTypes() +{ + AddObjectType("button", &CButton::ConstructObject); + AddObjectType("chart", &CChart::ConstructObject); + AddObjectType("checkbox", &CCheckBox::ConstructObject); + AddObjectType("dropdown", &CDropDown::ConstructObject); + AddObjectType("empty", &CGUIDummyObject::ConstructObject); + AddObjectType("image", &CImage::ConstructObject); + AddObjectType("input", &CInput::ConstructObject); + AddObjectType("list", &CList::ConstructObject); + AddObjectType("minimap", &CMiniMap::ConstructObject); + AddObjectType("olist", &COList::ConstructObject); + AddObjectType("progressbar", &CProgressBar::ConstructObject); + AddObjectType("radiobutton", &CRadioButton::ConstructObject); + AddObjectType("slider", &CSlider::ConstructObject); + AddObjectType("text", &CText::ConstructObject); + AddObjectType("tooltip", &CTooltip::ConstructObject); +} + +#endif // INCLUDED_GUIOBJECTTYPES