2025-11-04 00:14:18 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-09 11:20:14 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-09 11:20:14 -08:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-09 11:20:14 -08:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-09 11:20:14 -08:00
|
|
|
*/
|
|
|
|
|
|
2009-12-03 12:17:22 -08:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "GUIManager.h"
|
|
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
#include "gui/CGUI.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "gui/SGUIMessage.h"
|
|
|
|
|
#include "lib/debug.h"
|
2025-08-12 11:29:24 -07:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "lib/file/vfs/vfs_util.h"
|
|
|
|
|
#include "lib/utf8.h"
|
2009-12-03 12:17:22 -08:00
|
|
|
#include "ps/CLogger.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/Errors.h"
|
2019-09-22 16:28:25 -07:00
|
|
|
#include "ps/Filesystem.h"
|
2009-12-03 12:17:22 -08:00
|
|
|
#include "ps/Profile.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/Profiler2.h"
|
2021-09-21 15:44:46 -07:00
|
|
|
#include "ps/VideoMode.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/XMB/XMBData.h"
|
2025-08-12 11:29:24 -07:00
|
|
|
#include "ps/XMB/XMBStorage.h"
|
2009-12-03 12:17:22 -08:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/containers/StaticVector.h"
|
2021-05-01 07:04:53 -07:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2023-07-07 13:12:16 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
2020-11-14 02:57:50 -08:00
|
|
|
#include "scriptinterface/ScriptContext.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "scriptinterface/ScriptConversions.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
2021-05-10 04:51:32 -07:00
|
|
|
#include "scriptinterface/StructuredClone.h"
|
2025-08-12 11:29:24 -07:00
|
|
|
#include "simulation2/system/Component.h"
|
2009-12-03 12:17:22 -08:00
|
|
|
|
2026-05-18 12:11:00 -07:00
|
|
|
#include <SDL_events.h>
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <js/Equality.h>
|
|
|
|
|
#include <js/GCVector.h>
|
|
|
|
|
#include <js/Promise.h>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/String.h>
|
|
|
|
|
#include <js/Symbol.h>
|
|
|
|
|
#include <js/Value.h>
|
|
|
|
|
#include <js/ValueArray.h>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <tuple>
|
2025-05-21 04:49:23 -07:00
|
|
|
#include <utility>
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <vector>
|
2025-05-21 04:49:23 -07:00
|
|
|
|
2022-01-01 04:23:24 -08:00
|
|
|
namespace
|
|
|
|
|
{
|
2009-12-03 12:17:22 -08:00
|
|
|
|
2022-01-01 04:23:24 -08:00
|
|
|
const CStr EVENT_NAME_GAME_LOAD_PROGRESS = "GameLoadProgress";
|
|
|
|
|
const CStr EVENT_NAME_WINDOW_RESIZED = "WindowResized";
|
2025-03-12 06:05:27 -07:00
|
|
|
constexpr const char* START_ATLAS{"startAtlas"};
|
2025-04-23 04:01:54 -07:00
|
|
|
constexpr const char* OPEN_REQUEST{"openRequest"};
|
2020-01-15 08:00:37 -08:00
|
|
|
|
2022-01-01 04:23:24 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
CGUIManager* g_GUI = nullptr;
|
2009-12-03 12:17:22 -08:00
|
|
|
|
|
|
|
|
// General TODOs:
|
|
|
|
|
//
|
|
|
|
|
// A lot of the CGUI data could (and should) be shared between
|
|
|
|
|
// multiple pages, instead of treating them as completely independent, to save
|
|
|
|
|
// memory and loading time.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// called from main loop when (input) events are received.
|
|
|
|
|
// event is passed to other handlers if false is returned.
|
|
|
|
|
// trampoline: we don't want to make the HandleEvent implementation static
|
2026-05-18 12:11:00 -07:00
|
|
|
InReaction gui_handler(const SDL_Event& ev)
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2019-09-15 05:16:28 -07:00
|
|
|
if (!g_GUI)
|
|
|
|
|
return IN_PASS;
|
|
|
|
|
|
2009-12-03 12:17:22 -08:00
|
|
|
PROFILE("GUI event handler");
|
|
|
|
|
return g_GUI->HandleEvent(ev);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 16:44:39 -07:00
|
|
|
static Status ReloadChangedFileCB(void* param, const VfsPath& path)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<CGUIManager*>(param)->ReloadChangedFile(path);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 12:09:30 -07:00
|
|
|
CGUIManager::CGUIManager(ScriptContext& scriptContext, ScriptInterface& scriptInterface) :
|
|
|
|
|
m_ScriptContext{scriptContext},
|
|
|
|
|
m_ScriptInterface{scriptInterface}
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2024-06-27 12:09:30 -07:00
|
|
|
m_ScriptInterface.SetCallbackData(this);
|
|
|
|
|
m_ScriptInterface.LoadGlobalScripts();
|
2015-06-07 14:56:52 -07:00
|
|
|
|
2024-09-18 09:17:04 -07:00
|
|
|
if (!g_Xeromyces.AddValidator(g_VFS, "gui_page", "gui/gui_page.rng"))
|
2015-06-07 14:56:52 -07:00
|
|
|
LOGERROR("CGUIManager: failed to load GUI page grammar file 'gui/gui_page.rng'");
|
2024-09-18 09:17:04 -07:00
|
|
|
if (!g_Xeromyces.AddValidator(g_VFS, "gui", "gui/gui.rng"))
|
2015-06-07 14:56:52 -07:00
|
|
|
LOGERROR("CGUIManager: failed to load GUI XML grammar file 'gui/gui.rng'");
|
|
|
|
|
|
2014-07-02 16:44:39 -07:00
|
|
|
RegisterFileReloadFunc(ReloadChangedFileCB, this);
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CGUIManager::~CGUIManager()
|
|
|
|
|
{
|
2014-07-02 16:44:39 -07:00
|
|
|
UnregisterFileReloadFunc(ReloadChangedFileCB, this);
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2019-09-04 08:45:48 -07:00
|
|
|
size_t CGUIManager::GetPageCount() const
|
2010-05-19 17:59:01 -07:00
|
|
|
{
|
2019-09-04 08:45:48 -07:00
|
|
|
return m_PageStack.size();
|
2010-05-19 17:59:01 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-15 06:54:58 -07:00
|
|
|
void CGUIManager::SwitchPage(const CStrW& pageName, const ScriptInterface* srcScriptInterface, JS::HandleValue initData)
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2014-01-04 02:14:53 -08:00
|
|
|
// The page stack is cleared (including the script context where initData came from),
|
|
|
|
|
// therefore we have to clone initData.
|
2019-08-01 13:48:05 -07:00
|
|
|
|
2021-05-10 04:51:32 -07:00
|
|
|
Script::StructuredClone initDataClone;
|
2014-08-03 12:32:39 -07:00
|
|
|
if (!initData.isUndefined())
|
2021-05-12 12:25:09 -07:00
|
|
|
{
|
|
|
|
|
ScriptRequest rq(srcScriptInterface);
|
2021-05-10 04:51:32 -07:00
|
|
|
initDataClone = Script::WriteStructuredClone(rq, initData);
|
2021-05-12 12:25:09 -07:00
|
|
|
}
|
2019-08-01 13:48:05 -07:00
|
|
|
|
2022-03-03 03:36:08 -08:00
|
|
|
if (!m_PageStack.empty())
|
|
|
|
|
{
|
|
|
|
|
// Make sure we unfocus anything on the current page.
|
|
|
|
|
m_PageStack.back().gui->SendFocusMessage(GUIM_LOST_FOCUS);
|
|
|
|
|
m_PageStack.clear();
|
|
|
|
|
}
|
2019-08-01 13:48:05 -07:00
|
|
|
|
2024-10-30 10:56:33 -07:00
|
|
|
OpenChildPage(pageName, initDataClone);
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2024-10-30 10:56:33 -07:00
|
|
|
JS::Value CGUIManager::OpenChildPage(const CStrW& pageName, Script::StructuredClone initData)
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2019-08-16 11:46:04 -07:00
|
|
|
// Store the callback handler in the current GUI page before opening the new one
|
2024-07-08 12:07:04 -07:00
|
|
|
JS::RootedValue promise{m_ScriptInterface.GetGeneralJSContext(), [&]
|
|
|
|
|
{
|
|
|
|
|
if (m_PageStack.empty())
|
|
|
|
|
return JS::UndefinedValue();
|
2019-08-16 11:46:04 -07:00
|
|
|
|
2024-09-21 11:20:35 -07:00
|
|
|
CGUI& currentPage = *m_PageStack.back().gui;
|
2024-07-08 12:07:04 -07:00
|
|
|
// Make sure we unfocus anything on the current page.
|
2024-09-21 11:20:35 -07:00
|
|
|
currentPage.SendFocusMessage(GUIM_LOST_FOCUS);
|
2025-04-23 04:01:54 -07:00
|
|
|
return m_PageStack.back().GetPromise();
|
2024-07-08 12:07:04 -07:00
|
|
|
}()};
|
2022-03-03 03:36:08 -08:00
|
|
|
|
2024-10-30 10:56:33 -07:00
|
|
|
// Emplace the page prior to loading its contents, because that may open
|
|
|
|
|
// another GUI page on init which should be emplaced on top of this new page.
|
Use NONCOPYABLE for most GUI classes and structs to have the compiler indicate unintended copies, refs 3028551b91 / D2163.
That is CChartData, CGUIList, CGUISeries, COListColumn, GUITooltip,
SGUIMessage, SSpriteCall, STextCall, SFeedback, IGUISetting,
CGUISetting, GUI, IGUIObject, IGUIScrollBar.
Drop copying GetSetting and SetSetting template functions for CGUIList,
CGUISeries, CClientArea, CGUIString.
Stop copying COListColumn.
Drop copying GUI<CClientArea>::GetSetting call in
IGUIObject::UpdateCachedSize() and four copying
GUI<CGUIString>::GetSetting calls in SetupText() functions.
Delete unused GUIRenderer IGLState class from 849f50a500 obsolete since
1f5b8f1c9a.
Differential Revision: https://code.wildfiregames.com/D2164
This was SVN commit r22638.
2019-08-09 17:04:17 -07:00
|
|
|
m_PageStack.emplace_back(pageName, initData);
|
2020-11-14 02:57:50 -08:00
|
|
|
m_PageStack.back().LoadPage(m_ScriptContext);
|
2024-07-08 12:07:04 -07:00
|
|
|
|
|
|
|
|
return promise;
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 04:51:32 -07:00
|
|
|
CGUIManager::SGUIPage::SGUIPage(const CStrW& pageName, const Script::StructuredClone initData)
|
2024-09-21 11:20:35 -07:00
|
|
|
: m_Name(pageName), initData(initData)
|
2019-08-01 13:48:05 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 12:09:30 -07:00
|
|
|
void CGUIManager::SGUIPage::LoadPage(ScriptContext& scriptContext)
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2010-02-28 13:28:09 -08:00
|
|
|
// If we're hotloading then try to grab some data from the previous page
|
2021-05-10 04:51:32 -07:00
|
|
|
Script::StructuredClone hotloadData;
|
2019-08-01 13:48:05 -07:00
|
|
|
if (gui)
|
2014-07-31 12:18:40 -07:00
|
|
|
{
|
2021-05-22 12:23:03 -07:00
|
|
|
std::shared_ptr<ScriptInterface> scriptInterface = gui->GetScriptInterface();
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2025-03-09 13:47:03 -07:00
|
|
|
JS::RootedValue hotloadDataVal(rq.cx, gui->GetHotloadData(rq));
|
2021-05-10 04:51:32 -07:00
|
|
|
hotloadData = Script::WriteStructuredClone(rq, hotloadDataVal);
|
2014-01-04 02:14:53 -08:00
|
|
|
}
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2021-09-21 15:44:46 -07:00
|
|
|
g_VideoMode.ResetCursor();
|
2019-08-01 13:48:05 -07:00
|
|
|
inputs.clear();
|
2020-11-14 02:57:50 -08:00
|
|
|
gui.reset(new CGUI(scriptContext));
|
2024-09-21 11:20:35 -07:00
|
|
|
const ScriptRequest rq{gui->GetScriptInterface()};
|
2014-01-04 02:14:53 -08:00
|
|
|
|
2025-04-23 04:01:54 -07:00
|
|
|
for (const char* name : {START_ATLAS, OPEN_REQUEST})
|
2025-03-12 06:05:27 -07:00
|
|
|
{
|
2025-04-23 04:01:54 -07:00
|
|
|
JS::RootedString jsName{rq.cx, JS_NewStringCopyZ(rq.cx, name)};
|
2025-03-12 06:05:27 -07:00
|
|
|
JS::RootedValue symbol{rq.cx, JS::SymbolValue(JS::NewSymbol(rq.cx, jsName))};
|
|
|
|
|
JS::RootedValue nativeScope{rq.cx, JS::ObjectValue(*rq.nativeScope)};
|
2025-04-23 04:01:54 -07:00
|
|
|
Script::SetProperty(rq, nativeScope, name, symbol, true);
|
2025-03-12 06:05:27 -07:00
|
|
|
}
|
2019-11-24 02:26:48 -08:00
|
|
|
gui->AddObjectTypes();
|
2009-12-03 12:17:22 -08:00
|
|
|
|
2020-11-26 14:28:50 -08:00
|
|
|
VfsPath path = VfsPath("gui") / m_Name;
|
2019-08-01 13:48:05 -07:00
|
|
|
inputs.insert(path);
|
2009-12-03 12:17:22 -08:00
|
|
|
|
|
|
|
|
CXeromyces xero;
|
2015-06-07 14:56:52 -07:00
|
|
|
if (xero.Load(g_VFS, path, "gui_page") != PSRETURN_OK)
|
2009-12-03 12:17:22 -08:00
|
|
|
// Fail silently (Xeromyces reported the error)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int elmt_page = xero.GetElementID("page");
|
|
|
|
|
int elmt_include = xero.GetElementID("include");
|
|
|
|
|
|
|
|
|
|
XMBElement root = xero.GetRoot();
|
|
|
|
|
|
|
|
|
|
if (root.GetNodeName() != elmt_page)
|
|
|
|
|
{
|
2020-11-26 14:28:50 -08:00
|
|
|
LOGERROR("GUI page '%s' must have root element <page>", utf8_from_wstring(m_Name));
|
2009-12-03 12:17:22 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-09 13:47:03 -07:00
|
|
|
VfsPath rootModule;
|
2009-12-03 12:17:22 -08:00
|
|
|
XERO_ITER_EL(root, node)
|
|
|
|
|
{
|
|
|
|
|
if (node.GetNodeName() != elmt_include)
|
|
|
|
|
{
|
2020-11-26 14:28:50 -08:00
|
|
|
LOGERROR("GUI page '%s' must only have <include> elements inside <page>", utf8_from_wstring(m_Name));
|
2009-12-03 12:17:22 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 14:28:50 -08:00
|
|
|
CStr8 name = node.GetText();
|
|
|
|
|
CStrW nameW = node.GetText().FromUTF8();
|
2011-11-03 18:35:50 -07:00
|
|
|
|
|
|
|
|
PROFILE2("load gui xml");
|
2015-06-11 02:08:46 -07:00
|
|
|
PROFILE2_ATTR("name: %s", name.c_str());
|
2011-11-03 18:35:50 -07:00
|
|
|
|
2015-06-11 02:08:46 -07:00
|
|
|
if (name.back() == '/')
|
|
|
|
|
{
|
2020-11-26 14:28:50 -08:00
|
|
|
VfsPath currentDirectory = VfsPath("gui") / nameW;
|
|
|
|
|
VfsPaths directories;
|
|
|
|
|
vfs::GetPathnames(g_VFS, currentDirectory, L"*.xml", directories);
|
|
|
|
|
for (const VfsPath& directory : directories)
|
|
|
|
|
gui->LoadXmlFile(directory, inputs);
|
2015-06-11 02:08:46 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-26 14:28:50 -08:00
|
|
|
VfsPath directory = VfsPath("gui") / nameW;
|
|
|
|
|
gui->LoadXmlFile(directory, inputs);
|
2015-06-11 02:08:46 -07:00
|
|
|
}
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-11 09:20:50 -07:00
|
|
|
gui->LoadedXmlFiles();
|
2009-12-03 12:17:22 -08:00
|
|
|
|
2025-03-09 13:47:03 -07:00
|
|
|
scriptContext.RunJobs();
|
|
|
|
|
if (gui->m_LoadModuleResult.has_value())
|
|
|
|
|
{
|
|
|
|
|
gui->m_LoadModuleResult->moduleNamespace = gui->m_LoadModuleResult->iterator->Get();
|
|
|
|
|
++gui->m_LoadModuleResult->iterator;
|
|
|
|
|
}
|
2017-06-12 19:50:41 -07:00
|
|
|
|
2025-03-09 13:47:03 -07:00
|
|
|
JS::RootedValue hotloadDataVal(rq.cx);
|
2017-06-12 19:50:41 -07:00
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
if (hotloadData)
|
2021-05-10 04:51:32 -07:00
|
|
|
Script::ReadStructuredClone(rq, hotloadData, &hotloadDataVal);
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2025-12-14 12:12:22 -08:00
|
|
|
sendingPromise = std::make_shared<JS::PersistentRootedObject>(rq.cx);
|
|
|
|
|
// Assigning to `sendingPromise` isn't possible after `init` has been called because `init` might
|
|
|
|
|
// replace this page. So a local copy has to be made.
|
|
|
|
|
const std::shared_ptr<JS::PersistentRootedObject> localPromise{sendingPromise};
|
|
|
|
|
|
2025-03-09 13:47:03 -07:00
|
|
|
JS::RootedObject returnObject{rq.cx, gui->CallPageInit(rq, initData, hotloadDataVal,
|
|
|
|
|
utf8_from_wstring(m_Name))};
|
2024-09-21 11:20:35 -07:00
|
|
|
|
2025-12-14 12:12:22 -08:00
|
|
|
*localPromise = returnObject ? returnObject : JS::NewPromiseObject(rq.cx, nullptr);
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2025-04-23 04:01:54 -07:00
|
|
|
JS::Value CGUIManager::SGUIPage::GetPromise()
|
2019-08-16 11:46:04 -07:00
|
|
|
{
|
2025-04-23 04:01:54 -07:00
|
|
|
const ScriptRequest rq{gui->GetScriptInterface()};
|
|
|
|
|
if (receivingPromise == nullptr)
|
|
|
|
|
{
|
|
|
|
|
receivingPromise = std::make_shared<JS::PersistentRootedObject>(rq.cx,
|
2024-09-21 11:20:35 -07:00
|
|
|
JS::NewPromiseObject(rq.cx, nullptr));
|
2025-04-23 04:01:54 -07:00
|
|
|
}
|
2024-09-21 11:20:35 -07:00
|
|
|
|
|
|
|
|
return JS::ObjectValue(**receivingPromise);
|
2019-08-16 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-23 04:01:54 -07:00
|
|
|
std::optional<CGUIManager::SGUIPage::CloseResult> CGUIManager::SGUIPage::MaybeClose(const bool isRootPage)
|
2019-08-16 11:46:04 -07:00
|
|
|
{
|
2024-09-21 11:20:35 -07:00
|
|
|
if (JS::GetPromiseState(*sendingPromise) == JS::PromiseState::Pending)
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
|
|
// Make sure we unfocus anything on the current page.
|
|
|
|
|
gui->SendFocusMessage(GUIM_LOST_FOCUS);
|
|
|
|
|
|
|
|
|
|
const ScriptRequest rq{gui->GetScriptInterface()};
|
|
|
|
|
JS::RootedValue arg{rq.cx, JS::GetPromiseResult(*sendingPromise)};
|
2025-03-12 06:05:27 -07:00
|
|
|
const bool rejected{JS::GetPromiseState(*sendingPromise) == JS::PromiseState::Rejected};
|
2025-04-23 04:01:54 -07:00
|
|
|
|
|
|
|
|
JS::RootedValue nativeScope{rq.cx, JS::ObjectValue(*rq.nativeScope)};
|
|
|
|
|
if (arg.isObject())
|
|
|
|
|
{
|
|
|
|
|
JS::RootedObject argObject{rq.cx, &arg.toObject()};
|
|
|
|
|
JS::RootedValue symbol{rq.cx};
|
|
|
|
|
Script::GetProperty(rq, nativeScope, OPEN_REQUEST, &symbol);
|
|
|
|
|
JS::RootedId key{rq.cx, JS::PropertyKey::Symbol(symbol.toSymbol())};
|
|
|
|
|
JS::RootedValue openRequest{rq.cx};
|
|
|
|
|
if (JS_GetPropertyById(rq.cx, argObject, key, &openRequest) &&
|
|
|
|
|
Script::HasProperty(rq, openRequest, "page"))
|
|
|
|
|
{
|
|
|
|
|
std::wstring page;
|
|
|
|
|
Script::GetProperty(rq, openRequest, "page", page);
|
|
|
|
|
JS::RootedValue openArg{rq.cx};
|
|
|
|
|
Script::GetProperty(rq, openRequest, "argument", &openArg);
|
|
|
|
|
return CGUIManager::SGUIPage::OpenRequest{page,
|
|
|
|
|
Script::WriteStructuredClone(rq, openArg)};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isRootPage)
|
2025-03-12 06:05:27 -07:00
|
|
|
{
|
|
|
|
|
JS::RootedValue symbol{rq.cx};
|
|
|
|
|
Script::GetProperty(rq, nativeScope, START_ATLAS, &symbol);
|
|
|
|
|
bool equals;
|
|
|
|
|
if (!JS::StrictlyEqual(rq.cx, arg, symbol, &equals))
|
|
|
|
|
throw std::runtime_error{"Error while comparing return value to a symbol."};
|
|
|
|
|
|
|
|
|
|
if (equals)
|
2025-04-23 04:01:54 -07:00
|
|
|
return CGUIManager::SGUIPage::Close{nullptr, rejected};
|
2025-03-12 06:05:27 -07:00
|
|
|
}
|
2025-04-23 04:01:54 -07:00
|
|
|
return CGUIManager::SGUIPage::Close{Script::WriteStructuredClone(rq, arg), rejected};
|
2024-09-21 11:20:35 -07:00
|
|
|
}
|
|
|
|
|
|
2025-04-23 04:01:54 -07:00
|
|
|
void CGUIManager::SGUIPage::Refocus(const Close& result)
|
2024-09-21 11:20:35 -07:00
|
|
|
{
|
|
|
|
|
ENSURE(receivingPromise);
|
2019-08-16 11:46:04 -07:00
|
|
|
|
2021-05-22 12:23:03 -07:00
|
|
|
std::shared_ptr<ScriptInterface> scriptInterface = gui->GetScriptInterface();
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2019-08-16 11:46:04 -07:00
|
|
|
|
2020-11-14 00:46:32 -08:00
|
|
|
JS::RootedObject globalObj(rq.cx, rq.glob);
|
2019-08-16 11:46:04 -07:00
|
|
|
|
2024-09-21 11:20:35 -07:00
|
|
|
JS::RootedObject recv(rq.cx, *std::exchange(receivingPromise, nullptr));
|
2019-08-16 11:46:04 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue argVal(rq.cx);
|
2024-09-21 11:20:35 -07:00
|
|
|
Script::ReadStructuredClone(rq, result.arg, &argVal);
|
2019-08-16 11:46:04 -07:00
|
|
|
|
2024-07-08 12:07:04 -07:00
|
|
|
// This only resolves the promise, it doesn't call the continuation.
|
2024-09-21 11:20:35 -07:00
|
|
|
(result.rejected ? JS::RejectPromise : JS::ResolvePromise)(rq.cx, recv, argVal);
|
|
|
|
|
|
|
|
|
|
// We return to a page where some object might have been focused.
|
|
|
|
|
gui->SendFocusMessage(GUIM_GOT_FOCUS);
|
2019-08-16 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-02 16:44:39 -07:00
|
|
|
Status CGUIManager::ReloadChangedFile(const VfsPath& path)
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2015-08-21 10:08:41 -07:00
|
|
|
for (SGUIPage& p : m_PageStack)
|
2019-09-22 16:28:25 -07:00
|
|
|
if (p.inputs.find(path) != p.inputs.end())
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2020-11-26 14:28:50 -08:00
|
|
|
LOGMESSAGE("GUI file '%s' changed - reloading page '%s'", path.string8(), utf8_from_wstring(p.m_Name));
|
2020-11-14 02:57:50 -08:00
|
|
|
p.LoadPage(m_ScriptContext);
|
2010-05-30 06:42:56 -07:00
|
|
|
// TODO: this can crash if LoadPage runs an init script which modifies the page stack and breaks our iterators
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-09 08:58:14 -08:00
|
|
|
Status CGUIManager::ReloadAllPages()
|
|
|
|
|
{
|
|
|
|
|
// TODO: this can crash if LoadPage runs an init script which modifies the page stack and breaks our iterators
|
2015-08-21 10:08:41 -07:00
|
|
|
for (SGUIPage& p : m_PageStack)
|
2020-11-14 02:57:50 -08:00
|
|
|
p.LoadPage(m_ScriptContext);
|
2014-11-09 08:58:14 -08:00
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 12:11:00 -07:00
|
|
|
InReaction CGUIManager::HandleEvent(const SDL_Event& ev)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// We want scripts to have access to the raw input events, so they can do complex
|
|
|
|
|
// processing when necessary (e.g. for unit selection and camera movement).
|
|
|
|
|
// Sometimes they'll want to be layered behind the GUI widgets (e.g. to detect mousedowns on the
|
|
|
|
|
// visible game area), sometimes they'll want to intercepts events before the GUI (e.g.
|
|
|
|
|
// to capture all mouse events until a mouseup after dragging).
|
|
|
|
|
// So we call two separate handler functions:
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2019-08-24 15:25:26 -07:00
|
|
|
bool handled = false;
|
2010-08-21 16:58:08 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
PROFILE("handleInputBeforeGui");
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(*top()->GetScriptInterface());
|
2020-11-13 05:18:22 -08:00
|
|
|
|
2020-11-14 00:46:32 -08:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2026-05-18 12:11:00 -07:00
|
|
|
if (ScriptFunction::Call(rq, global, "handleInputBeforeGui", handled, ev, top()->FindObjectUnderMouse()))
|
2010-01-09 11:20:14 -08:00
|
|
|
if (handled)
|
|
|
|
|
return IN_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
PROFILE("handle event in native GUI");
|
|
|
|
|
InReaction r = top()->HandleEvent(ev);
|
|
|
|
|
if (r != IN_PASS)
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2014-08-05 12:03:10 -07:00
|
|
|
// We can't take the following lines out of this scope because top() may be another gui page than it was when calling handleInputBeforeGui!
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(*top()->GetScriptInterface());
|
2020-11-14 00:46:32 -08:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2014-08-05 12:03:10 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
PROFILE("handleInputAfterGui");
|
2026-05-18 12:11:00 -07:00
|
|
|
if (ScriptFunction::Call(rq, global, "handleInputAfterGui", handled, ev))
|
2014-07-31 12:18:40 -07:00
|
|
|
if (handled)
|
2010-01-09 11:20:14 -08:00
|
|
|
return IN_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IN_PASS;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 16:30:18 -08:00
|
|
|
|
2025-11-04 00:14:18 -08:00
|
|
|
void CGUIManager::SendEventToAll(const CStr& eventName,
|
|
|
|
|
std::optional<JS::HandleValueArray> paramData) const
|
2019-07-19 14:15:04 -07:00
|
|
|
{
|
2025-04-20 12:54:40 -07:00
|
|
|
const auto pageStack = GetCopyOfFrozenStack();
|
2019-11-05 16:30:18 -08:00
|
|
|
|
|
|
|
|
for (const SGUIPage& p : pageStack)
|
2025-11-04 00:14:18 -08:00
|
|
|
{
|
|
|
|
|
if (paramData.has_value())
|
|
|
|
|
p.gui->SendEventToAll(eventName, paramData.value());
|
|
|
|
|
else
|
|
|
|
|
p.gui->SendEventToAll(eventName);
|
|
|
|
|
}
|
2019-07-19 14:15:04 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-12 06:05:27 -07:00
|
|
|
std::optional<bool> CGUIManager::TickObjects()
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2011-11-03 18:35:50 -07:00
|
|
|
PROFILE3("gui tick");
|
|
|
|
|
|
2020-11-14 02:57:50 -08:00
|
|
|
// We share the script context with everything else that runs in the same thread.
|
2014-09-22 13:13:04 -07:00
|
|
|
// This call makes sure we trigger GC regularly even if the simulation is not running.
|
2024-11-23 09:36:23 -08:00
|
|
|
m_ScriptContext.MaybeIncrementalGC();
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2025-04-20 12:54:40 -07:00
|
|
|
const auto pageStack = GetCopyOfFrozenStack();
|
2010-06-30 14:20:08 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
for (const SGUIPage& p : pageStack)
|
2025-03-09 13:47:03 -07:00
|
|
|
{
|
|
|
|
|
const ScriptRequest rq{p.gui->GetScriptInterface()};
|
|
|
|
|
JS::RootedObject newSendingPromise{rq.cx, p.gui->TickObjects(rq, p.initData,
|
|
|
|
|
utf8_from_wstring(p.m_Name))};
|
|
|
|
|
if (newSendingPromise)
|
|
|
|
|
(*p.sendingPromise) = newSendingPromise;
|
|
|
|
|
}
|
2024-07-08 12:07:04 -07:00
|
|
|
|
|
|
|
|
m_ScriptContext.RunJobs();
|
2024-09-21 11:20:35 -07:00
|
|
|
|
|
|
|
|
while (!m_PageStack.empty())
|
|
|
|
|
{
|
|
|
|
|
const size_t stackSize{m_PageStack.size()};
|
2025-03-12 06:05:27 -07:00
|
|
|
const std::optional<SGUIPage::CloseResult> result{
|
|
|
|
|
m_PageStack.back().MaybeClose(stackSize == 1)};
|
2024-09-21 11:20:35 -07:00
|
|
|
if (!result.has_value())
|
|
|
|
|
break;
|
|
|
|
|
ENSURE(m_PageStack.size() == stackSize);
|
|
|
|
|
m_PageStack.pop_back();
|
2025-04-23 04:01:54 -07:00
|
|
|
if (const SGUIPage::OpenRequest* request{std::get_if<SGUIPage::OpenRequest>(&result.value())})
|
|
|
|
|
OpenChildPage(request->path, request->arg);
|
|
|
|
|
else if (const SGUIPage::Close& ret{std::get<SGUIPage::Close>(result.value())};
|
|
|
|
|
m_PageStack.empty())
|
|
|
|
|
{
|
|
|
|
|
return !ret.arg;
|
|
|
|
|
}
|
2025-03-12 06:05:27 -07:00
|
|
|
else
|
2025-04-23 04:01:54 -07:00
|
|
|
m_PageStack.back().Refocus(ret);
|
2024-09-21 11:20:35 -07:00
|
|
|
|
|
|
|
|
m_ScriptContext.RunJobs();
|
|
|
|
|
}
|
2025-03-12 06:05:27 -07:00
|
|
|
return std::nullopt;
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-25 00:14:11 -08:00
|
|
|
void CGUIManager::Draw(CCanvas2D& canvas) const
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2025-04-16 13:38:34 -07:00
|
|
|
PROFILE3("gui");
|
2011-11-03 18:35:50 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
for (const SGUIPage& p : m_PageStack)
|
2022-02-25 00:14:11 -08:00
|
|
|
p.gui->Draw(canvas);
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CGUIManager::UpdateResolution()
|
|
|
|
|
{
|
2025-04-20 12:54:40 -07:00
|
|
|
const auto pageStack = GetCopyOfFrozenStack();
|
2017-06-29 17:04:29 -07:00
|
|
|
|
|
|
|
|
for (const SGUIPage& p : pageStack)
|
|
|
|
|
{
|
2015-08-21 10:08:41 -07:00
|
|
|
p.gui->UpdateResolution();
|
2022-01-01 04:23:24 -08:00
|
|
|
p.gui->SendEventToAll(EVENT_NAME_WINDOW_RESIZED);
|
2017-06-29 17:04:29 -07:00
|
|
|
}
|
2009-12-03 12:17:22 -08:00
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
bool CGUIManager::TemplateExists(const std::string& templateName) const
|
2015-04-15 02:10:05 -07:00
|
|
|
{
|
|
|
|
|
return m_TemplateLoader.TemplateExists(templateName);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-28 08:21:07 -08:00
|
|
|
const CParamNode& CGUIManager::GetTemplate(const std::string& templateName)
|
|
|
|
|
{
|
2022-12-29 23:34:23 -08:00
|
|
|
const CParamNode& templateRoot = m_TemplateLoader.GetTemplateFileData(templateName).GetOnlyChild();
|
2014-12-28 08:21:07 -08:00
|
|
|
if (!templateRoot.IsOk())
|
2015-01-22 12:36:24 -08:00
|
|
|
LOGERROR("Invalid template found for '%s'", templateName.c_str());
|
2014-12-28 08:21:07 -08:00
|
|
|
|
|
|
|
|
return templateRoot;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 04:23:24 -08:00
|
|
|
void CGUIManager::DisplayLoadProgress(int percent, const wchar_t* pending_task)
|
|
|
|
|
{
|
|
|
|
|
const ScriptInterface& scriptInterface = *(GetActiveGUI()->GetScriptInterface());
|
|
|
|
|
ScriptRequest rq(scriptInterface);
|
|
|
|
|
|
|
|
|
|
JS::RootedValueVector paramData(rq.cx);
|
|
|
|
|
|
2025-05-21 04:49:23 -07:00
|
|
|
std::ignore = paramData.append(JS::NumberValue(percent));
|
2022-01-01 04:23:24 -08:00
|
|
|
|
|
|
|
|
JS::RootedValue valPendingTask(rq.cx);
|
|
|
|
|
Script::ToJSVal(rq, &valPendingTask, pending_task);
|
2025-05-21 04:49:23 -07:00
|
|
|
std::ignore = paramData.append(valPendingTask);
|
2022-01-01 04:23:24 -08:00
|
|
|
|
|
|
|
|
SendEventToAll(EVENT_NAME_GAME_LOAD_PROGRESS, paramData);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-03 12:17:22 -08:00
|
|
|
// This returns a shared_ptr to make sure the CGUI doesn't get deallocated
|
|
|
|
|
// while we're in the middle of calling a function on it (e.g. if a GUI script
|
|
|
|
|
// calls SwitchPage)
|
2021-05-22 12:23:03 -07:00
|
|
|
std::shared_ptr<CGUI> CGUIManager::top() const
|
2009-12-03 12:17:22 -08:00
|
|
|
{
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(m_PageStack.size());
|
2009-12-03 12:17:22 -08:00
|
|
|
return m_PageStack.back().gui;
|
|
|
|
|
}
|
2025-04-20 12:54:40 -07:00
|
|
|
|
|
|
|
|
PS::StaticVector<CGUIManager::SGUIPage, 16> CGUIManager::GetCopyOfFrozenStack() const
|
|
|
|
|
{
|
|
|
|
|
PS::StaticVector<CGUIManager::SGUIPage, 16> stack;
|
|
|
|
|
std::copy(m_PageStack.begin(), m_PageStack.end(), std::back_inserter(stack));
|
|
|
|
|
return stack;
|
|
|
|
|
}
|