Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "JSInterface_GUIManager.h" 21 : 22 : #include "gui/CGUI.h" 23 : #include "gui/GUIManager.h" 24 : #include "ps/CStr.h" 25 : #include "ps/VideoMode.h" 26 : #include "scriptinterface/FunctionWrapper.h" 27 : #include "scriptinterface/Interface.h" 28 : #include "scriptinterface/StructuredClone.h" 29 : #include "simulation2/system/Component.h" 30 : 31 : #include <js/RootingAPI.h> 32 : #include <js/TypeDecls.h> 33 : #include <js/Value.h> 34 : #include <string> 35 : 36 : namespace JSI_GUIManager 37 : { 38 : // Note that the initData argument may only contain clonable data. 39 : // Functions aren't supported for example! 40 : // It returns a promise. 41 4 : JS::Value OpenChildPage(const Script::Request& rq, const std::wstring& name, JS::HandleValue initData) 42 : { 43 16 : return g_GUI->OpenChildPage(name, Script::WriteStructuredClone(rq, initData)); 44 : } 45 : 46 0 : void SetCursor(const std::wstring& name) 47 : { 48 0 : g_VideoMode.SetCursor(name); 49 0 : } 50 : 51 0 : void ResetCursor() 52 : { 53 0 : g_VideoMode.ResetCursor(); 54 0 : } 55 : 56 0 : bool TemplateExists(const std::string& templateName) 57 : { 58 0 : return g_GUI->TemplateExists(templateName); 59 : } 60 : 61 0 : CParamNode GetTemplate(const std::string& templateName) 62 : { 63 0 : return g_GUI->GetTemplate(templateName); 64 : } 65 : 66 : 67 26 : void RegisterScriptFunctions(const Script::Request& rq) 68 : { 69 30 : Script::Function::Register<&OpenChildPage>(rq, "OpenChildPage"); 70 26 : Script::Function::Register<&SetCursor>(rq, "SetCursor"); 71 26 : Script::Function::Register<&ResetCursor>(rq, "ResetCursor"); 72 26 : Script::Function::Register<&TemplateExists>(rq, "TemplateExists"); 73 26 : Script::Function::Register<&GetTemplate>(rq, "GetTemplate"); 74 : 75 26 : Script::Function::Register<&CGUI::TryFindObjectByName, &Script::Interface::ObjectFromCBData<CGUI>>(rq, "TryGetGUIObjectByName"); 76 31 : Script::Function::Register<&CGUI::FindObjectByName, &Script::Interface::ObjectFromCBData<CGUI>>(rq, "GetGUIObjectByName"); 77 26 : Script::Function::Register<&CGUI::SetGlobalHotkey, &Script::Interface::ObjectFromCBData<CGUI>>(rq, "SetGlobalHotkey"); 78 26 : Script::Function::Register<&CGUI::UnsetGlobalHotkey, &Script::Interface::ObjectFromCBData<CGUI>>(rq, "UnsetGlobalHotkey"); 79 26 : } 80 : }