LCOV - code coverage report
Current view: top level - source/gui/Scripting - JSInterface_GUIManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 18 33 54.5 %
Date: 2023-01-19 00:18:29 Functions: 3 8 37.5 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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 "gui/ObjectBases/IGUIObject.h"
      25             : #include "ps/GameSetup/Config.h"
      26             : #include "ps/VideoMode.h"
      27             : #include "scriptinterface/FunctionWrapper.h"
      28             : #include "scriptinterface/ScriptInterface.h"
      29             : #include "scriptinterface/StructuredClone.h"
      30             : 
      31             : namespace JSI_GUIManager
      32             : {
      33             : // Note that the initData argument may only contain clonable data.
      34             : // Functions aren't supported for example!
      35           1 : void PushGuiPage(const ScriptRequest& rq, const std::wstring& name, JS::HandleValue initData, JS::HandleValue callbackFunction)
      36             : {
      37           1 :     g_GUI->PushPage(name, Script::WriteStructuredClone(rq, initData), callbackFunction);
      38           1 : }
      39             : 
      40           0 : void SwitchGuiPage(const ScriptInterface& scriptInterface, const std::wstring& name, JS::HandleValue initData)
      41             : {
      42           0 :     g_GUI->SwitchPage(name, &scriptInterface, initData);
      43           0 : }
      44             : 
      45           1 : void PopGuiPage(const ScriptRequest& rq, JS::HandleValue args)
      46             : {
      47           1 :     if (g_GUI->GetPageCount() < 2)
      48             :     {
      49           0 :         ScriptException::Raise(rq, "Can't pop GUI pages when less than two pages are opened!");
      50           0 :         return;
      51             :     }
      52             : 
      53           1 :     g_GUI->PopPage(Script::WriteStructuredClone(rq, args));
      54             : }
      55             : 
      56           0 : void SetCursor(const std::wstring& name)
      57             : {
      58           0 :     g_VideoMode.SetCursor(name);
      59           0 : }
      60             : 
      61           0 : void ResetCursor()
      62             : {
      63           0 :     g_VideoMode.ResetCursor();
      64           0 : }
      65             : 
      66           0 : bool TemplateExists(const std::string& templateName)
      67             : {
      68           0 :     return g_GUI->TemplateExists(templateName);
      69             : }
      70             : 
      71           0 : CParamNode GetTemplate(const std::string& templateName)
      72             : {
      73           0 :     return g_GUI->GetTemplate(templateName);
      74             : }
      75             : 
      76             : 
      77          12 : void RegisterScriptFunctions(const ScriptRequest& rq)
      78             : {
      79          14 :     ScriptFunction::Register<&PushGuiPage>(rq, "PushGuiPage");
      80          12 :     ScriptFunction::Register<&SwitchGuiPage>(rq, "SwitchGuiPage");
      81          14 :     ScriptFunction::Register<&PopGuiPage>(rq, "PopGuiPage");
      82          12 :     ScriptFunction::Register<&SetCursor>(rq, "SetCursor");
      83          12 :     ScriptFunction::Register<&ResetCursor>(rq, "ResetCursor");
      84          12 :     ScriptFunction::Register<&TemplateExists>(rq, "TemplateExists");
      85          12 :     ScriptFunction::Register<&GetTemplate>(rq, "GetTemplate");
      86             : 
      87          12 :     ScriptFunction::Register<&CGUI::FindObjectByName, &ScriptInterface::ObjectFromCBData<CGUI>>(rq, "GetGUIObjectByName");
      88          12 :     ScriptFunction::Register<&CGUI::SetGlobalHotkey, &ScriptInterface::ObjectFromCBData<CGUI>>(rq, "SetGlobalHotkey");
      89          12 :     ScriptFunction::Register<&CGUI::UnsetGlobalHotkey, &ScriptInterface::ObjectFromCBData<CGUI>>(rq, "UnsetGlobalHotkey");
      90          12 : }
      91             : }

Generated by: LCOV version 1.13