From f9b529f2fb294faa3658fdb20304abfccb261406 Mon Sep 17 00:00:00 2001 From: wraitii Date: Sat, 20 Apr 2019 15:49:42 +0000 Subject: [PATCH] Remove workaround in GetGUIObjectByName f0d9806b3f added a problem: Engine.GetGuiObjectByName is unaware of the caller GUI page. So GUI pages in the background that still run the onTick and other event code tried to look on the topmost GUI page, rather than their own GUI page. 9674c3c0fe added a workaround that has to be copied to any place that can call JS code. If developers don't know about the reason for this workaround and add a new place that can call JS code (#5369), they won't be able to implement anything. This removes this workaround by passing the pointer to the correct GUI page as callback data. Patch By: elexis Reviewed By: wraitii Refs #5369 Trac Tickets: #5369 Differential Revision: https://code.wildfiregames.com/D1701 This was SVN commit r22200. --- source/gui/CGUI.cpp | 2 ++ source/gui/GUIManager.cpp | 22 ------------------- source/gui/GUIManager.h | 6 ----- .../gui/scripting/JSInterface_GUIManager.cpp | 7 ++++-- 4 files changed, 7 insertions(+), 30 deletions(-) diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 5807427be0..c5f21697b2 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -282,6 +282,8 @@ CGUI::CGUI(const shared_ptr& runtime) : m_MouseButtons(0), m_FocusedObject(NULL), m_InternalNameNumber(0) { m_ScriptInterface.reset(new ScriptInterface("Engine", "GUIPage", runtime)); + m_ScriptInterface->SetCallbackData(this); + GuiScriptingInit(*m_ScriptInterface); m_ScriptInterface->LoadGlobalScripts(); m_BaseObject = new CGUIDummyObject; diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index 4ae8d8aedd..4b170e22d3 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -231,10 +231,6 @@ void CGUIManager::LoadPage(SGUIPage& page) } } - // Remember this GUI page, in case the scripts call FindObjectByName - shared_ptr oldGUI = m_CurrentGUI; - m_CurrentGUI = page.gui; - page.gui->SendEventToAll("load"); shared_ptr scriptInterface = page.gui->GetScriptInterface(); @@ -254,8 +250,6 @@ void CGUIManager::LoadPage(SGUIPage& page) if (scriptInterface->HasProperty(global, "init") && !scriptInterface->CallFunctionVoid(global, "init", initDataVal, hotloadDataVal)) LOGERROR("GUI page '%s': Failed to call init() function", utf8_from_wstring(page.name)); - - m_CurrentGUI = oldGUI; } Status CGUIManager::ReloadChangedFile(const VfsPath& path) @@ -358,16 +352,6 @@ bool CGUIManager::GetPreDefinedColor(const CStr& name, CColor& output) const return top()->GetPreDefinedColor(name, output); } -IGUIObject* CGUIManager::FindObjectByName(const CStr& name) const -{ - // This can be called from scripts run by TickObjects, - // and we want to return it the same GUI page as is being ticked - if (m_CurrentGUI) - return m_CurrentGUI->FindObjectByName(name); - else - return top()->FindObjectByName(name); -} - void CGUIManager::SendEventToAll(const CStr& eventName) const { top()->SendEventToAll(eventName); @@ -385,11 +369,7 @@ void CGUIManager::TickObjects() PageStackType pageStack = m_PageStack; for (const SGUIPage& p : pageStack) - { - m_CurrentGUI = p.gui; p.gui->TickObjects(); - } - m_CurrentGUI.reset(); } void CGUIManager::Draw() @@ -407,11 +387,9 @@ void CGUIManager::UpdateResolution() for (const SGUIPage& p : pageStack) { - m_CurrentGUI = p.gui; p.gui->UpdateResolution(); p.gui->SendEventToAll("WindowResized"); } - m_CurrentGUI.reset(); } bool CGUIManager::TemplateExists(const std::string& templateName) const diff --git a/source/gui/GUIManager.h b/source/gui/GUIManager.h index 80353a0ff1..d00ee7fc7c 100644 --- a/source/gui/GUIManager.h +++ b/source/gui/GUIManager.h @@ -107,11 +107,6 @@ public: */ bool GetPreDefinedColor(const CStr& name, CColor& output) const; - /** - * See CGUI::FindObjectByName; applies to the currently active page. - */ - IGUIObject* FindObjectByName(const CStr& name) const; - /** * See CGUI::SendEventToAll; applies to the currently active page. */ @@ -166,7 +161,6 @@ private: shared_ptr top() const; - shared_ptr m_CurrentGUI; // used to latch state during TickObjects/LoadPage (this is kind of ugly) shared_ptr m_ScriptRuntime; shared_ptr m_ScriptInterface; diff --git a/source/gui/scripting/JSInterface_GUIManager.cpp b/source/gui/scripting/JSInterface_GUIManager.cpp index c4a70be79f..33d868559b 100644 --- a/source/gui/scripting/JSInterface_GUIManager.cpp +++ b/source/gui/scripting/JSInterface_GUIManager.cpp @@ -19,6 +19,7 @@ #include "JSInterface_GUIManager.h" +#include "gui/CGUI.h" #include "gui/GUIManager.h" #include "gui/IGUIObject.h" #include "ps/GameSetup/Config.h" @@ -46,9 +47,11 @@ void JSI_GUIManager::PopGuiPageCB(ScriptInterface::CxPrivate* pCxPrivate, JS::Ha g_GUI->PopPageCB(pCxPrivate->pScriptInterface->WriteStructuredClone(args)); } -JS::Value JSI_GUIManager::GetGUIObjectByName(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& name) +JS::Value JSI_GUIManager::GetGUIObjectByName(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name) { - IGUIObject* guiObj = g_GUI->FindObjectByName(name); + CGUI* guiPage = static_cast(pCxPrivate->pCBData); + + IGUIObject* guiObj = guiPage->FindObjectByName(name); if (!guiObj) return JS::UndefinedValue();