Index: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_emptyPage.xml
===================================================================
--- ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_emptyPage.xml (nonexistent)
+++ ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_emptyPage.xml (revision 25616)
@@ -0,0 +1,4 @@
+
+
+ common/styles.xml
+
Property changes on: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_emptyPage.xml
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_pushWithPopOnInit.xml
===================================================================
--- ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_pushWithPopOnInit.xml (nonexistent)
+++ ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_pushWithPopOnInit.xml (revision 25616)
@@ -0,0 +1,5 @@
+
+
+ common/styles.xml
+ regainFocus/pushWithPopOnInit.xml
+
Property changes on: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/page_pushWithPopOnInit.xml
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.js
===================================================================
--- ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.js (nonexistent)
+++ ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.js (revision 25616)
@@ -0,0 +1 @@
+Engine.PushGuiPage("regainFocus/page_emptyPage.xml", {}, () => Engine.PopGuiPage());
Property changes on: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.js
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.xml
===================================================================
--- ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.xml (nonexistent)
+++ ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.xml (revision 25616)
@@ -0,0 +1,4 @@
+
+
+
+
Property changes on: ps/trunk/binaries/data/mods/_test.gui/gui/regainFocus/pushWithPopOnInit.xml
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: ps/trunk/source/gui/GUIManager.h
===================================================================
--- ps/trunk/source/gui/GUIManager.h (revision 25615)
+++ ps/trunk/source/gui/GUIManager.h (revision 25616)
@@ -1,180 +1,186 @@
/* Copyright (C) 2021 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_GUIMANAGER
#define INCLUDED_GUIMANAGER
#include "lib/file/vfs/vfs_path.h"
#include "lib/input.h"
#include "ps/CStr.h"
#include "ps/TemplateLoader.h"
#include "scriptinterface/StructuredClone.h"
+#include
#include
#include
class CGUI;
/**
* External interface to the GUI system.
*
* The GUI consists of a set of pages. Each page is constructed from a
* series of XML files, and is independent from any other page.
* Only one page is active at a time. All events and render requests etc
* will go to the active page. This lets the GUI switch between pre-game menu
* and in-game UI.
*/
class CGUIManager
{
NONCOPYABLE(CGUIManager);
public:
CGUIManager();
~CGUIManager();
std::shared_ptr GetScriptInterface()
{
return m_ScriptInterface;
}
std::shared_ptr GetContext() { return m_ScriptContext; }
std::shared_ptr GetActiveGUI() { return top(); }
/**
* Returns the number of currently open GUI pages.
*/
size_t GetPageCount() const;
/**
* Load a new GUI page and make it active. All current pages will be destroyed.
*/
void SwitchPage(const CStrW& name, const ScriptInterface* srcScriptInterface, JS::HandleValue initData);
/**
* Load a new GUI page and make it active. All current pages will be retained,
* and will still be drawn and receive tick events, but will not receive
* user inputs.
* If given, the callbackHandler function will be executed once this page is closed.
*/
void PushPage(const CStrW& pageName, Script::StructuredClone initData, JS::HandleValue callbackFunc);
/**
* Unload the currently active GUI page, and make the previous page active.
* (There must be at least two pages when you call this.)
*/
void PopPage(Script::StructuredClone args);
/**
* Called when a file has been modified, to hotload changes.
*/
Status ReloadChangedFile(const VfsPath& path);
/**
* Called when we should reload all pages (e.g. translation hotloading update).
*/
Status ReloadAllPages();
/**
* Pass input events to the currently active GUI page.
*/
InReaction HandleEvent(const SDL_Event_* ev);
/**
* See CGUI::SendEventToAll; applies to the currently active page.
*/
void SendEventToAll(const CStr& eventName) const;
void SendEventToAll(const CStr& eventName, JS::HandleValueArray paramData) const;
/**
* See CGUI::TickObjects; applies to @em all loaded pages.
*/
void TickObjects();
/**
* See CGUI::Draw; applies to @em all loaded pages.
*/
void Draw() const;
/**
* See CGUI::UpdateResolution; applies to @em all loaded pages.
*/
void UpdateResolution();
/**
* Check if a template with this name exists
*/
bool TemplateExists(const std::string& templateName) const;
/**
* Retrieve the requested template, used for displaying faction specificities.
*/
const CParamNode& GetTemplate(const std::string& templateName);
private:
struct SGUIPage
{
// COPYABLE, because event handlers may invalidate page stack iterators by open or close pages,
// and event handlers need to be called for the entire stack.
/**
* Initializes the data that will be used to create the CGUI page one or multiple times (hotloading).
*/
SGUIPage(const CStrW& pageName, const Script::StructuredClone initData);
/**
* Create the CGUI with it's own ScriptInterface. Deletes the previous CGUI if it existed.
*/
void LoadPage(std::shared_ptr scriptContext);
/**
* Sets the callback handler when a new page is opened that will be performed when the page is closed.
*/
void SetCallbackFunction(ScriptInterface& scriptInterface, JS::HandleValue callbackFunc);
/**
* Execute the stored callback function with the given arguments.
*/
void PerformCallbackFunction(Script::StructuredClone args);
CStrW m_Name;
std::unordered_set inputs; // for hotloading
Script::StructuredClone initData; // data to be passed to the init() function
std::shared_ptr gui; // the actual GUI page
/**
* Function executed by this parent GUI page when the child GUI page it pushed is popped.
* Notice that storing it in the SGUIPage instead of CGUI means that it will survive the hotloading CGUI reset.
*/
std::shared_ptr callbackFunction;
};
const static CStr EventNameWindowResized;
std::shared_ptr top() const;
std::shared_ptr m_ScriptContext;
std::shared_ptr m_ScriptInterface;
- using PageStackType = std::vector;
+ /**
+ * The page stack must not move pointers on push/pop, or pushing a page in a page's init method
+ * may crash (as the pusher page will suddenly have moved, and the stack will be confused).
+ * Therefore use std::deque over std::vector.
+ */
+ using PageStackType = std::deque;
PageStackType m_PageStack;
CTemplateLoader m_TemplateLoader;
};
extern CGUIManager* g_GUI;
extern InReaction gui_handler(const SDL_Event_* ev);
#endif // INCLUDED_GUIMANAGER
Index: ps/trunk/source/gui/tests/test_GuiManager.h
===================================================================
--- ps/trunk/source/gui/tests/test_GuiManager.h (revision 25615)
+++ ps/trunk/source/gui/tests/test_GuiManager.h (revision 25616)
@@ -1,200 +1,225 @@
/* Copyright (C) 2021 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 .
*/
#include "lib/self_test.h"
#include "gui/GUIManager.h"
#include "gui/CGUI.h"
#include "ps/ConfigDB.h"
#include "ps/Filesystem.h"
#include "ps/GameSetup/GameSetup.h"
#include "ps/Hotkey.h"
#include "ps/XML/Xeromyces.h"
#include "scriptinterface/ScriptRequest.h"
#include "scriptinterface/ScriptInterface.h"
#include "scriptinterface/StructuredClone.h"
#include "scriptinterface/Object.h"
#include
class TestGuiManager : public CxxTest::TestSuite
{
std::unique_ptr configDB;
public:
void setUp()
{
g_VFS = CreateVfs();
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.gui" / "", VFS_MOUNT_MUST_EXIST));
TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY));
configDB = std::make_unique();
CXeromyces::Startup();
g_GUI = new CGUIManager();
}
void tearDown()
{
delete g_GUI;
CXeromyces::Terminate();
configDB.reset();
g_VFS.reset();
DeleteDirectory(DataDir()/"_testcache");
}
void test_EventObject()
{
// Load up a test page.
const ScriptInterface& scriptInterface = *(g_GUI->GetScriptInterface());
ScriptRequest rq(scriptInterface);
JS::RootedValue val(rq.cx);
Script::CreateObject(rq, &val);
Script::StructuredClone data = Script::WriteStructuredClone(rq, JS::NullHandleValue);
g_GUI->PushPage(L"event/page_event.xml", data, JS::UndefinedHandleValue);
const ScriptInterface& pageScriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface());
ScriptRequest prq(pageScriptInterface);
JS::RootedValue global(prq.cx, prq.globalValue());
int called_value = 0;
JS::RootedValue js_called_value(prq.cx);
// Ticking will call the onTick handlers of all object. The second
// onTick is configured to disable the onTick handlers of the first and
// third and enable the fourth. So ticking once will only call the
// first and second object. We don't want the fourth object to be
// called, to avoid infinite additions of objects.
g_GUI->TickObjects();
Script::GetProperty(prq, global, "called1", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 1);
Script::GetProperty(prq, global, "called2", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 1);
Script::GetProperty(prq, global, "called3", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 0);
Script::GetProperty(prq, global, "called4", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 0);
// Ticking again will still call the second object, but also the fourth.
g_GUI->TickObjects();
Script::GetProperty(prq, global, "called1", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 1);
Script::GetProperty(prq, global, "called2", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 2);
Script::GetProperty(prq, global, "called3", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 0);
Script::GetProperty(prq, global, "called4", &js_called_value);
Script::FromJSVal(prq, js_called_value, called_value);
TS_ASSERT_EQUALS(called_value, 1);
}
void test_hotkeysState()
{
// Load up a fake test hotkey when pressing 'a'.
const char* test_hotkey_name = "hotkey.test";
configDB->SetValueString(CFG_SYSTEM, test_hotkey_name, "A");
LoadHotkeys(*configDB);
// Load up a test page.
const ScriptInterface& scriptInterface = *(g_GUI->GetScriptInterface());
ScriptRequest rq(scriptInterface);
JS::RootedValue val(rq.cx);
Script::CreateObject(rq, &val);
Script::StructuredClone data = Script::WriteStructuredClone(rq, JS::NullHandleValue);
g_GUI->PushPage(L"hotkey/page_hotkey.xml", data, JS::UndefinedHandleValue);
// Press 'a'.
SDL_Event_ hotkeyNotification;
hotkeyNotification.ev.type = SDL_KEYDOWN;
hotkeyNotification.ev.key.keysym.scancode = SDL_SCANCODE_A;
hotkeyNotification.ev.key.repeat = 0;
// Init input and poll the event.
InitInput();
in_push_priority_event(&hotkeyNotification);
SDL_Event_ ev;
while (in_poll_event(&ev))
in_dispatch_event(&ev);
const ScriptInterface& pageScriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface());
ScriptRequest prq(pageScriptInterface);
JS::RootedValue global(prq.cx, prq.globalValue());
// Ensure that our hotkey state was synchronised with the event itself.
bool hotkey_pressed_value = false;
JS::RootedValue js_hotkey_pressed_value(prq.cx);
Script::GetProperty(prq, global, "state_before", &js_hotkey_pressed_value);
Script::FromJSVal(prq, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
hotkey_pressed_value = false;
Script::GetProperty(prq, global, "state_after", &js_hotkey_pressed_value);
Script::FromJSVal(prq, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
// We are listening to KeyDown events, so repeat shouldn't matter.
hotkeyNotification.ev.key.repeat = 1;
in_push_priority_event(&hotkeyNotification);
while (in_poll_event(&ev))
in_dispatch_event(&ev);
hotkey_pressed_value = false;
Script::GetProperty(prq, global, "state_before", &js_hotkey_pressed_value);
Script::FromJSVal(prq, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
hotkey_pressed_value = false;
Script::GetProperty(prq, global, "state_after", &js_hotkey_pressed_value);
Script::FromJSVal(prq, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
hotkeyNotification.ev.type = SDL_KEYUP;
in_push_priority_event(&hotkeyNotification);
while (in_poll_event(&ev))
in_dispatch_event(&ev);
hotkey_pressed_value = true;
Script::GetProperty(prq, global, "state_before", &js_hotkey_pressed_value);
Script::FromJSVal(prq, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
hotkey_pressed_value = true;
Script::GetProperty(prq, global, "state_after", &js_hotkey_pressed_value);
Script::FromJSVal(prq, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
UnloadHotkeys();
}
+
+ void test_PageRegainedFocusEvent()
+ {
+ // Load up a test page.
+ const ScriptInterface& scriptInterface = *(g_GUI->GetScriptInterface());
+ ScriptRequest rq(scriptInterface);
+ JS::RootedValue val(rq.cx);
+ Script::CreateObject(rq, &val);
+
+ Script::StructuredClone data = Script::WriteStructuredClone(rq, JS::NullHandleValue);
+ g_GUI->PushPage(L"regainFocus/page_emptyPage.xml", data, JS::UndefinedHandleValue);
+
+ const ScriptInterface& pageScriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface());
+ ScriptRequest prq(pageScriptInterface);
+ JS::RootedValue global(prq.cx, prq.globalValue());
+
+ g_GUI->PushPage(L"regainFocus/page_emptyPage.xml", data, JS::UndefinedHandleValue);
+ g_GUI->PopPage(data);
+
+ // This page instantly pushes an empty page with a callback that pops another page again.
+ g_GUI->PushPage(L"regainFocus/page_pushWithPopOnInit.xml", data, JS::UndefinedHandleValue);
+
+ // Pop the empty page and trigger the callback (effectively pops twice).
+ g_GUI->PopPage(data);
+ }
};