2021-05-01 16:04:53 +02:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2010-01-09 20:20:14 +01:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "GUIManager.h"
|
|
|
|
|
2019-07-26 20:57:28 +02:00
|
|
|
#include "gui/CGUI.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "ps/CLogger.h"
|
2019-09-23 01:28:25 +02:00
|
|
|
#include "ps/Filesystem.h"
|
|
|
|
#include "ps/GameSetup/Config.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "ps/Profile.h"
|
2021-09-22 00:44:46 +02:00
|
|
|
#include "ps/VideoMode.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2021-05-01 16:04:53 +02:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2020-11-14 11:57:50 +01:00
|
|
|
#include "scriptinterface/ScriptContext.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2021-05-10 13:51:32 +02:00
|
|
|
#include "scriptinterface/StructuredClone.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
|
2019-09-23 01:28:25 +02:00
|
|
|
CGUIManager* g_GUI = nullptr;
|
2009-12-03 21:17:22 +01:00
|
|
|
|
2020-01-15 17:00:37 +01:00
|
|
|
const CStr CGUIManager::EventNameWindowResized = "WindowResized";
|
|
|
|
|
2009-12-03 21:17:22 +01: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
|
|
|
|
InReaction gui_handler(const SDL_Event_* ev)
|
|
|
|
{
|
2019-09-15 14:16:28 +02:00
|
|
|
if (!g_GUI)
|
|
|
|
return IN_PASS;
|
|
|
|
|
2009-12-03 21:17:22 +01:00
|
|
|
PROFILE("GUI event handler");
|
|
|
|
return g_GUI->HandleEvent(ev);
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:44:39 +02:00
|
|
|
static Status ReloadChangedFileCB(void* param, const VfsPath& path)
|
|
|
|
{
|
|
|
|
return static_cast<CGUIManager*>(param)->ReloadChangedFile(path);
|
|
|
|
}
|
|
|
|
|
2014-01-04 11:14:53 +01:00
|
|
|
CGUIManager::CGUIManager()
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
m_ScriptContext = g_ScriptContext;
|
|
|
|
m_ScriptInterface.reset(new ScriptInterface("Engine", "GUIManager", m_ScriptContext));
|
2014-01-04 11:14:53 +01:00
|
|
|
m_ScriptInterface->SetCallbackData(this);
|
|
|
|
m_ScriptInterface->LoadGlobalScripts();
|
2015-06-07 23:56:52 +02:00
|
|
|
|
|
|
|
if (!CXeromyces::AddValidator(g_VFS, "gui_page", "gui/gui_page.rng"))
|
|
|
|
LOGERROR("CGUIManager: failed to load GUI page grammar file 'gui/gui_page.rng'");
|
|
|
|
if (!CXeromyces::AddValidator(g_VFS, "gui", "gui/gui.rng"))
|
|
|
|
LOGERROR("CGUIManager: failed to load GUI XML grammar file 'gui/gui.rng'");
|
|
|
|
|
2014-07-03 01:44:39 +02:00
|
|
|
RegisterFileReloadFunc(ReloadChangedFileCB, this);
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CGUIManager::~CGUIManager()
|
|
|
|
{
|
2014-07-03 01:44:39 +02:00
|
|
|
UnregisterFileReloadFunc(ReloadChangedFileCB, this);
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:45:48 +02:00
|
|
|
size_t CGUIManager::GetPageCount() const
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2019-09-04 17:45:48 +02:00
|
|
|
return m_PageStack.size();
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 15:54:58 +02:00
|
|
|
void CGUIManager::SwitchPage(const CStrW& pageName, const ScriptInterface* srcScriptInterface, JS::HandleValue initData)
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2014-01-04 11:14:53 +01:00
|
|
|
// The page stack is cleared (including the script context where initData came from),
|
|
|
|
// therefore we have to clone initData.
|
2019-08-01 22:48:05 +02:00
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::StructuredClone initDataClone;
|
2014-08-03 21:32:39 +02:00
|
|
|
if (!initData.isUndefined())
|
2021-05-12 21:25:09 +02:00
|
|
|
{
|
|
|
|
ScriptRequest rq(srcScriptInterface);
|
2021-05-10 13:51:32 +02:00
|
|
|
initDataClone = Script::WriteStructuredClone(rq, initData);
|
2021-05-12 21:25:09 +02:00
|
|
|
}
|
2019-08-01 22:48:05 +02:00
|
|
|
|
2009-12-03 21:17:22 +01:00
|
|
|
m_PageStack.clear();
|
2019-08-01 22:48:05 +02:00
|
|
|
|
2019-08-16 20:46:04 +02:00
|
|
|
PushPage(pageName, initDataClone, JS::UndefinedHandleValue);
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
void CGUIManager::PushPage(const CStrW& pageName, Script::StructuredClone initData, JS::HandleValue callbackFunction)
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2019-08-16 20:46:04 +02:00
|
|
|
// Store the callback handler in the current GUI page before opening the new one
|
|
|
|
if (!m_PageStack.empty() && !callbackFunction.isUndefined())
|
|
|
|
m_PageStack.back().SetCallbackFunction(*m_ScriptInterface, callbackFunction);
|
|
|
|
|
2019-08-01 22:48:05 +02:00
|
|
|
// Push the page prior to loading its contents, because that may push
|
|
|
|
// another GUI page on init which should be pushed 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-10 02:04:17 +02:00
|
|
|
m_PageStack.emplace_back(pageName, initData);
|
2020-11-14 11:57:50 +01:00
|
|
|
m_PageStack.back().LoadPage(m_ScriptContext);
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
void CGUIManager::PopPage(Script::StructuredClone args)
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
|
|
|
if (m_PageStack.size() < 2)
|
|
|
|
{
|
|
|
|
debug_warn(L"Tried to pop GUI page when there's < 2 in the stack");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_PageStack.pop_back();
|
2019-08-16 20:46:04 +02:00
|
|
|
m_PageStack.back().PerformCallbackFunction(args);
|
2014-01-04 11:14:53 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
CGUIManager::SGUIPage::SGUIPage(const CStrW& pageName, const Script::StructuredClone initData)
|
2020-11-26 23:28:50 +01:00
|
|
|
: m_Name(pageName), initData(initData), inputs(), gui(), callbackFunction()
|
2019-08-01 22:48:05 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-22 21:23:03 +02:00
|
|
|
void CGUIManager::SGUIPage::LoadPage(std::shared_ptr<ScriptContext> scriptContext)
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2010-02-28 22:28:09 +01:00
|
|
|
// If we're hotloading then try to grab some data from the previous page
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::StructuredClone hotloadData;
|
2019-08-01 22:48:05 +02:00
|
|
|
if (gui)
|
2014-07-31 21:18:40 +02:00
|
|
|
{
|
2021-05-22 21:23:03 +02: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2020-11-14 09:46:32 +01:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue hotloadDataVal(rq.cx);
|
2021-05-01 16:04:53 +02:00
|
|
|
ScriptFunction::Call(rq, global, "getHotloadData", &hotloadDataVal);
|
2021-05-10 13:51:32 +02:00
|
|
|
hotloadData = Script::WriteStructuredClone(rq, hotloadDataVal);
|
2014-01-04 11:14:53 +01:00
|
|
|
}
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2021-09-22 00:44:46 +02:00
|
|
|
g_VideoMode.ResetCursor();
|
2019-08-01 22:48:05 +02:00
|
|
|
inputs.clear();
|
2020-11-14 11:57:50 +01:00
|
|
|
gui.reset(new CGUI(scriptContext));
|
2014-01-04 11:14:53 +01:00
|
|
|
|
2019-11-24 11:26:48 +01:00
|
|
|
gui->AddObjectTypes();
|
2009-12-03 21:17:22 +01:00
|
|
|
|
2020-11-26 23:28:50 +01:00
|
|
|
VfsPath path = VfsPath("gui") / m_Name;
|
2019-08-01 22:48:05 +02:00
|
|
|
inputs.insert(path);
|
2009-12-03 21:17:22 +01:00
|
|
|
|
|
|
|
CXeromyces xero;
|
2015-06-07 23:56:52 +02:00
|
|
|
if (xero.Load(g_VFS, path, "gui_page") != PSRETURN_OK)
|
2009-12-03 21:17:22 +01: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 23:28:50 +01:00
|
|
|
LOGERROR("GUI page '%s' must have root element <page>", utf8_from_wstring(m_Name));
|
2009-12-03 21:17:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
XERO_ITER_EL(root, node)
|
|
|
|
{
|
|
|
|
if (node.GetNodeName() != elmt_include)
|
|
|
|
{
|
2020-11-26 23:28:50 +01:00
|
|
|
LOGERROR("GUI page '%s' must only have <include> elements inside <page>", utf8_from_wstring(m_Name));
|
2009-12-03 21:17:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-26 23:28:50 +01:00
|
|
|
CStr8 name = node.GetText();
|
|
|
|
CStrW nameW = node.GetText().FromUTF8();
|
2011-11-04 02:35:50 +01:00
|
|
|
|
|
|
|
PROFILE2("load gui xml");
|
2015-06-11 11:08:46 +02:00
|
|
|
PROFILE2_ATTR("name: %s", name.c_str());
|
2011-11-04 02:35:50 +01:00
|
|
|
|
2015-06-11 11:08:46 +02:00
|
|
|
TIMER(nameW.c_str());
|
|
|
|
if (name.back() == '/')
|
|
|
|
{
|
2020-11-26 23:28:50 +01: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 11:08:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-26 23:28:50 +01:00
|
|
|
VfsPath directory = VfsPath("gui") / nameW;
|
|
|
|
gui->LoadXmlFile(directory, inputs);
|
2015-06-11 11:08:46 +02:00
|
|
|
}
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 18:20:50 +02:00
|
|
|
gui->LoadedXmlFiles();
|
2009-12-03 21:17:22 +01:00
|
|
|
|
2021-05-22 21:23:03 +02: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue initDataVal(rq.cx);
|
|
|
|
JS::RootedValue hotloadDataVal(rq.cx);
|
2020-11-14 09:46:32 +01:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2017-06-13 04:50:41 +02:00
|
|
|
|
2019-08-01 22:48:05 +02:00
|
|
|
if (initData)
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::ReadStructuredClone(rq, initData, &initDataVal);
|
2017-06-13 04:50:41 +02:00
|
|
|
|
2014-01-04 11:14:53 +01:00
|
|
|
if (hotloadData)
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::ReadStructuredClone(rq, hotloadData, &hotloadDataVal);
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2021-05-13 19:23:52 +02:00
|
|
|
if (Script::HasProperty(rq, global, "init") &&
|
2021-05-01 16:04:53 +02:00
|
|
|
!ScriptFunction::CallVoid(rq, global, "init", initDataVal, hotloadDataVal))
|
2020-11-26 23:28:50 +01:00
|
|
|
LOGERROR("GUI page '%s': Failed to call init() function", utf8_from_wstring(m_Name));
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-16 20:46:04 +02:00
|
|
|
void CGUIManager::SGUIPage::SetCallbackFunction(ScriptInterface& scriptInterface, JS::HandleValue callbackFunc)
|
|
|
|
{
|
|
|
|
if (!callbackFunc.isObject())
|
|
|
|
{
|
|
|
|
LOGERROR("Given callback handler is not an object!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2020-11-13 14:18:22 +01:00
|
|
|
|
2020-11-30 10:03:20 +01:00
|
|
|
if (!JS_ObjectIsFunction(&callbackFunc.toObject()))
|
2019-08-16 20:46:04 +02:00
|
|
|
{
|
|
|
|
LOGERROR("Given callback handler is not a function!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-18 15:39:04 +01:00
|
|
|
callbackFunction = std::make_shared<JS::PersistentRootedValue>(scriptInterface.GetGeneralJSContext(), callbackFunc);
|
2019-08-16 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
void CGUIManager::SGUIPage::PerformCallbackFunction(Script::StructuredClone args)
|
2019-08-16 20:46:04 +02:00
|
|
|
{
|
|
|
|
if (!callbackFunction)
|
|
|
|
return;
|
|
|
|
|
2021-05-22 21:23:03 +02: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2019-08-16 20:46:04 +02:00
|
|
|
|
2020-11-14 09:46:32 +01:00
|
|
|
JS::RootedObject globalObj(rq.cx, rq.glob);
|
2019-08-16 20:46:04 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue funcVal(rq.cx, *callbackFunction);
|
2019-08-16 20:46:04 +02:00
|
|
|
|
|
|
|
// Delete the callback function, so that it is not called again
|
|
|
|
callbackFunction.reset();
|
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue argVal(rq.cx);
|
2019-08-16 20:46:04 +02:00
|
|
|
if (args)
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::ReadStructuredClone(rq, args, &argVal);
|
2019-08-16 20:46:04 +02:00
|
|
|
|
2020-11-30 10:03:20 +01:00
|
|
|
JS::RootedValueVector paramData(rq.cx);
|
2020-12-15 10:03:44 +01:00
|
|
|
ignore_result(paramData.append(argVal));
|
2019-08-16 20:46:04 +02:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue result(rq.cx);
|
2019-08-16 20:46:04 +02:00
|
|
|
|
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 19:29:17 +01:00
|
|
|
if(!JS_CallFunctionValue(rq.cx, globalObj, funcVal, paramData, &result))
|
|
|
|
ScriptException::CatchPending(rq);
|
2019-08-16 20:46:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 01:44:39 +02:00
|
|
|
Status CGUIManager::ReloadChangedFile(const VfsPath& path)
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2015-08-21 19:08:41 +02:00
|
|
|
for (SGUIPage& p : m_PageStack)
|
2019-09-23 01:28:25 +02:00
|
|
|
if (p.inputs.find(path) != p.inputs.end())
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2020-11-26 23:28:50 +01:00
|
|
|
LOGMESSAGE("GUI file '%s' changed - reloading page '%s'", path.string8(), utf8_from_wstring(p.m_Name));
|
2020-11-14 11:57:50 +01:00
|
|
|
p.LoadPage(m_ScriptContext);
|
2010-05-30 15:42:56 +02:00
|
|
|
// TODO: this can crash if LoadPage runs an init script which modifies the page stack and breaks our iterators
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
2014-11-09 17:58:14 +01: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 19:08:41 +02:00
|
|
|
for (SGUIPage& p : m_PageStack)
|
2020-11-14 11:57:50 +01:00
|
|
|
p.LoadPage(m_ScriptContext);
|
2014-11-09 17:58:14 +01:00
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
InReaction CGUIManager::HandleEvent(const SDL_Event_* ev)
|
|
|
|
{
|
|
|
|
// 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 19:08:41 +02:00
|
|
|
|
2019-08-25 00:25:26 +02:00
|
|
|
bool handled = false;
|
2010-08-22 01:58:08 +02:00
|
|
|
|
2010-01-09 20:20:14 +01: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(*top()->GetScriptInterface());
|
2020-11-13 14:18:22 +01:00
|
|
|
|
2020-11-14 09:46:32 +01:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2021-05-01 16:04:53 +02:00
|
|
|
if (ScriptFunction::Call(rq, global, "handleInputBeforeGui", handled, *ev, top()->FindObjectUnderMouse()))
|
2010-01-09 20:20:14 +01: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 21:03:10 +02: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(*top()->GetScriptInterface());
|
2020-11-14 09:46:32 +01:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2014-08-05 21:03:10 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
PROFILE("handleInputAfterGui");
|
2021-05-01 16:04:53 +02:00
|
|
|
if (ScriptFunction::Call(rq, global, "handleInputAfterGui", handled, *ev))
|
2014-07-31 21:18:40 +02:00
|
|
|
if (handled)
|
2010-01-09 20:20:14 +01:00
|
|
|
return IN_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IN_PASS;
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
void CGUIManager::SendEventToAll(const CStr& eventName) const
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2019-11-06 01:30:18 +01:00
|
|
|
// Save an immutable copy so iterators aren't invalidated by handlers
|
|
|
|
PageStackType pageStack = m_PageStack;
|
|
|
|
|
|
|
|
for (const SGUIPage& p : pageStack)
|
|
|
|
p.gui->SendEventToAll(eventName);
|
|
|
|
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 23:15:04 +02:00
|
|
|
void CGUIManager::SendEventToAll(const CStr& eventName, JS::HandleValueArray paramData) const
|
|
|
|
{
|
2019-11-06 01:30:18 +01:00
|
|
|
// Save an immutable copy so iterators aren't invalidated by handlers
|
|
|
|
PageStackType pageStack = m_PageStack;
|
|
|
|
|
|
|
|
for (const SGUIPage& p : pageStack)
|
|
|
|
p.gui->SendEventToAll(eventName, paramData);
|
2019-07-19 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
2009-12-03 21:17:22 +01:00
|
|
|
void CGUIManager::TickObjects()
|
|
|
|
{
|
2011-11-04 02:35:50 +01:00
|
|
|
PROFILE3("gui tick");
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
// We share the script context with everything else that runs in the same thread.
|
2014-09-22 22:13:04 +02:00
|
|
|
// This call makes sure we trigger GC regularly even if the simulation is not running.
|
2020-11-14 11:57:50 +01:00
|
|
|
m_ScriptInterface->GetContext()->MaybeIncrementalGC(1.0f);
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2010-06-30 23:20:08 +02:00
|
|
|
// Save an immutable copy so iterators aren't invalidated by tick handlers
|
|
|
|
PageStackType pageStack = m_PageStack;
|
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
for (const SGUIPage& p : pageStack)
|
|
|
|
p.gui->TickObjects();
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-23 01:51:10 +02:00
|
|
|
void CGUIManager::Draw() const
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2011-11-09 14:09:01 +01:00
|
|
|
PROFILE3_GPU("gui");
|
2011-11-04 02:35:50 +01:00
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
for (const SGUIPage& p : m_PageStack)
|
|
|
|
p.gui->Draw();
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGUIManager::UpdateResolution()
|
|
|
|
{
|
2017-06-30 02:04:29 +02:00
|
|
|
// Save an immutable copy so iterators aren't invalidated by event handlers
|
|
|
|
PageStackType pageStack = m_PageStack;
|
|
|
|
|
|
|
|
for (const SGUIPage& p : pageStack)
|
|
|
|
{
|
2015-08-21 19:08:41 +02:00
|
|
|
p.gui->UpdateResolution();
|
2020-01-15 17:00:37 +01:00
|
|
|
p.gui->SendEventToAll(EventNameWindowResized);
|
2017-06-30 02:04:29 +02:00
|
|
|
}
|
2009-12-03 21:17:22 +01:00
|
|
|
}
|
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
bool CGUIManager::TemplateExists(const std::string& templateName) const
|
2015-04-15 11:10:05 +02:00
|
|
|
{
|
|
|
|
return m_TemplateLoader.TemplateExists(templateName);
|
|
|
|
}
|
|
|
|
|
2014-12-28 17:21:07 +01:00
|
|
|
const CParamNode& CGUIManager::GetTemplate(const std::string& templateName)
|
|
|
|
{
|
|
|
|
const CParamNode& templateRoot = m_TemplateLoader.GetTemplateFileData(templateName).GetChild("Entity");
|
|
|
|
if (!templateRoot.IsOk())
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid template found for '%s'", templateName.c_str());
|
2014-12-28 17:21:07 +01:00
|
|
|
|
|
|
|
return templateRoot;
|
|
|
|
}
|
|
|
|
|
2009-12-03 21:17:22 +01: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 21:23:03 +02:00
|
|
|
std::shared_ptr<CGUI> CGUIManager::top() const
|
2009-12-03 21:17:22 +01:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(m_PageStack.size());
|
2009-12-03 21:17:22 +01:00
|
|
|
return m_PageStack.back().gui;
|
|
|
|
}
|