Index: binaries/data/mods/public/gui/pregame/MainMenuPage.js =================================================================== --- binaries/data/mods/public/gui/pregame/MainMenuPage.js +++ binaries/data/mods/public/gui/pregame/MainMenuPage.js @@ -9,6 +9,46 @@ this.menuHandler = new MainMenuItemHandler(mainMenuItems); this.splashScreenHandler = new SplashScreenHandler(data, hotloadData && hotloadData.splashScreenHandler); + let toto = Engine.GetGUIObjectByName("mainMenuPage"); + let doText = (caption, size) => ({ + "@type": "text", + "@size": size, + "@style": "ModernLabelText", + "@caption": caption, + }); + let added = toto.createChild({ + "@type": "image", + "@size": "200 200 500 500", + "@style": "ModernDialog", + "@z": 200, + "object": [ + doText("turlututu", "0 0 200 200"), + { + "@type": "text", + "@size": "0 200 200 400", + "@style": "ModernLabelText", + "@caption": "ChapoPointu", + "object": { + "@type": "image", + "@size": "600 300 100% 600+40", + "@style": "ModernDialog", + "@z": 250, + } + }] + }); + added.deleteChild(added.children[0]); + + toto.createChild({ + "script": { + "@file": "not_a_file.js", + }, + }); + + + toto.createChild({ + "script": "warn('Hey you')", + }); + new MusicHandler(); new ProjectInformationHandler(projectInformation); new CommunityButtonHandler(communityButtons); Index: source/gui/CGUI.h =================================================================== --- source/gui/CGUI.h +++ source/gui/CGUI.h @@ -251,6 +251,18 @@ shared_ptr GetScriptInterface() { return m_ScriptInterface; }; + /** + * The CGUI creates the object and adds it as a child to parent. + * To the created object, this is the same as being created on page open (same events, etc.) + */ + IGUIObject* CreateChild(const XMBFile& file, const XMBElement& element, IGUIObject* parent); + + /** + * Detach the object from its parent and delete it. + * The object must have a parent - use PopPage to delete the root page object. + */ + void DetachAndDeleteChild(IGUIObject* child); + private: /** * The CGUI takes ownership of the child object and links the parent with the child. @@ -358,7 +370,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile, std::unordered_set& Paths); + void Xeromyces_ReadRootObjects(const XMBFile& file, XMBElement element, std::unordered_set& Paths); /** * Reads in the root element \ (the DOMElement). @@ -369,7 +381,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadRootSprites(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadRootSprites(const XMBFile& file, XMBElement element); /** * Reads in the root element \ (the DOMElement). @@ -380,7 +392,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadRootStyles(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadRootStyles(const XMBFile& file, XMBElement element); /** * Reads in the root element \ (the DOMElement). @@ -391,7 +403,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadRootSetup(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadRootSetup(const XMBFile& file, XMBElement element); // Read Subs @@ -417,7 +429,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject* pParent, std::vector >& NameSubst, std::unordered_set& Paths, u32 nesting_depth); + IGUIObject* Xeromyces_ReadObject(const XMBFile& file, XMBElement element, IGUIObject* pParent, std::vector >& NameSubst, std::unordered_set& Paths, u32 nesting_depth); /** * Reads in the element \, which repeats its child \s @@ -425,7 +437,7 @@ * 'var' enclosed in square brackets) in its descendants' names with "[0]", * "[1]", etc. */ - void Xeromyces_ReadRepeat(XMBElement Element, CXeromyces* pFile, IGUIObject* pParent, std::vector >& NameSubst, std::unordered_set& Paths, u32 nesting_depth); + void Xeromyces_ReadRepeat(const XMBFile& file, XMBElement element, IGUIObject* pParent, std::vector >& NameSubst, std::unordered_set& Paths, u32 nesting_depth); /** * Reads in the element \ (the XMBElement) and executes @@ -438,7 +450,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadScript(XMBElement Element, CXeromyces* pFile, std::unordered_set& Paths); + void Xeromyces_ReadScript(const XMBFile& file, XMBElement element, std::unordered_set& Paths); /** * Reads in the element \ (the XMBElement) and stores the @@ -450,7 +462,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadSprite(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadSprite(const XMBFile& file, XMBElement element); /** * Reads in the element \ (the XMBElement) and stores the @@ -463,7 +475,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite& parent); + void Xeromyces_ReadImage(const XMBFile& file, XMBElement element, CGUISprite& parent); /** * Reads in the element \ (the XMBElement) and stores the @@ -476,7 +488,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadEffects(XMBElement Element, CXeromyces* pFile, SGUIImageEffects& effects); + void Xeromyces_ReadEffects(const XMBFile& file, XMBElement element, SGUIImageEffects& effects); /** * Reads in the element \ (the XMBElement) and stores the @@ -488,7 +500,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadStyle(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadStyle(const XMBFile& file, XMBElement element); /** * Reads in the element \ (the XMBElement) and stores the @@ -500,7 +512,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadScrollBarStyle(const XMBFile& file, XMBElement element); /** * Reads in the element \ (the XMBElement) and stores the @@ -512,7 +524,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadIcon(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadIcon(const XMBFile& file, XMBElement element); /** * Reads in the element \ (the XMBElement) and stores the @@ -524,7 +536,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadTooltip(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadTooltip(const XMBFile& file, XMBElement element); /** * Reads in the element \ (the XMBElement) and stores the @@ -536,7 +548,7 @@ * * @see LoadXmlFile() */ - void Xeromyces_ReadColor(XMBElement Element, CXeromyces* pFile); + void Xeromyces_ReadColor(const XMBFile& file, XMBElement element); //@} Index: source/gui/CGUI.cpp =================================================================== --- source/gui/CGUI.cpp +++ source/gui/CGUI.cpp @@ -371,6 +371,37 @@ return (*it->second)(*this); } + +IGUIObject* CGUI::CreateChild(const XMBFile& file, const XMBElement& element, IGUIObject* parent) +{ + std::unordered_set paths; + std::vector> nameSubst; + IGUIObject* child = Xeromyces_ReadObject(file, element, parent, nameSubst, paths, 0); + if (!child) + return nullptr; + + child->RecurseObject(nullptr, &IGUIObject::UpdateCachedSize); + SGUIMessage msg(GUIM_LOAD); + child->RecurseObject(nullptr, &IGUIObject::HandleMessage, msg); + child->ScriptEvent(EventNameLoad); + + return child; +} + +void CGUI::DetachAndDeleteChild(IGUIObject* child) +{ + if (!child) + return; + if (!child->m_pParent) + { + LOGERROR("Cannot delete the root object of a page"); + return; + } + child->m_pParent->RemoveChild(child); + m_pAllObjects.erase(child->m_Name); + delete child; +} + bool CGUI::AddObject(IGUIObject& parent, IGUIObject& child) { if (child.m_Name.empty()) @@ -386,7 +417,7 @@ } m_pAllObjects[child.m_Name] = &child; - parent.AddChild(child); + parent.AddChild(&child); return true; } @@ -527,21 +558,21 @@ { Paths.insert(Filename); - CXeromyces XeroFile; - if (XeroFile.Load(g_VFS, Filename, "gui") != PSRETURN_OK) + CXeromyces xeroFile; + if (xeroFile.Load(g_VFS, Filename, "gui") != PSRETURN_OK) return; - XMBElement node = XeroFile.GetRoot(); - CStr root_name(XeroFile.GetElementString(node.GetNodeName())); + XMBElement node = xeroFile.GetRoot(); + CStr root_name(xeroFile.GetElementString(node.GetNodeName())); if (root_name == "objects") - Xeromyces_ReadRootObjects(node, &XeroFile, Paths); + Xeromyces_ReadRootObjects(xeroFile, node, Paths); else if (root_name == "sprites") - Xeromyces_ReadRootSprites(node, &XeroFile); + Xeromyces_ReadRootSprites(xeroFile, node); else if (root_name == "styles") - Xeromyces_ReadRootStyles(node, &XeroFile); + Xeromyces_ReadRootStyles(xeroFile, node); else if (root_name == "setup") - Xeromyces_ReadRootSetup(node, &XeroFile); + Xeromyces_ReadRootSetup(xeroFile, node); else LOGERROR("CGUI::LoadXmlFile encountered an unknown XML root node type: %s", root_name.c_str()); } @@ -560,63 +591,63 @@ // XML Reading Xeromyces Specific Sub-Routines //=================================================================== -void CGUI::Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile, std::unordered_set& Paths) +void CGUI::Xeromyces_ReadRootObjects(const XMBFile& file, XMBElement element, std::unordered_set& Paths) { - int el_script = pFile->GetElementID("script"); + int el_script = file.GetElementID("script"); std::vector > subst; // Iterate main children // they should all be or