LCOV - code coverage report
Current view: top level - source/gui/Scripting - GuiScriptConversions.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 45 220 20.5 %
Date: 2026-12-03 01:19:39 Functions: 3 29 10.3 %

          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 "scriptinterface/Conversions.h"
      21             : 
      22             : #include "gui/CGUISprite.h"
      23             : #include "gui/ObjectBases/IGUIObject.h"
      24             : #include "gui/Scripting/JSInterface_GUIProxy.h"
      25             : #include "gui/SettingTypes/CGUIColor.h"
      26             : #include "gui/SettingTypes/CGUIList.h"
      27             : #include "gui/SettingTypes/CGUISeries.h"
      28             : #include "gui/SettingTypes/CGUIString.h"
      29             : #include "gui/SettingTypes/EAlign.h"
      30             : #include "gui/SettingTypes/EScrollOrientation.h"
      31             : #include "lib/code_generation.h"
      32             : #include "lib/external_libraries/libsdl.h"
      33             : #include "maths/Rect.h"
      34             : #include "maths/Size2D.h"
      35             : #include "maths/Vector2D.h"
      36             : #include "ps/CLogger.h"
      37             : #include "ps/Hotkey.h"
      38             : #include "scriptinterface/Object.h"
      39             : #include "scriptinterface/Exceptions.h"
      40             : #include "scriptinterface/Request.h"
      41             : 
      42             : #include <js/CallArgs.h>
      43             : #include <js/PropertyAndElement.h>
      44             : #include <js/RootingAPI.h>
      45             : #include <js/TypeDecls.h>
      46             : #include <js/Value.h>
      47             : #include <jsapi.h>
      48             : #include <SDL_events.h>
      49             : #include <string>
      50             : #include <vector>
      51             : 
      52             : struct CColor;
      53             : 
      54             : #define SET(obj, name, value) STMT(JS::RootedValue v_(rq.cx); Script::ToJSVal(rq, &v_, (value)); JS_SetProperty(rq.cx, obj, (name), v_))
      55             :     // ignore JS_SetProperty return value, because errors should be impossible
      56             :     // and we can't do anything useful in the case of errors anyway
      57             : 
      58          14 : template<> void Script::ToJSVal<SDL_Event>(const Script::Request& rq, JS::MutableHandleValue ret, SDL_Event const& ev)
      59             : {
      60          14 :     const char* typeName;
      61             : 
      62          14 :     switch (ev.type)
      63             :     {
      64           0 :     case SDL_WINDOWEVENT: typeName = "windowevent"; break;
      65           4 :     case SDL_KEYDOWN: typeName = "keydown"; break;
      66           2 :     case SDL_KEYUP: typeName = "keyup"; break;
      67           0 :     case SDL_MOUSEMOTION: typeName = "mousemotion"; break;
      68           0 :     case SDL_MOUSEBUTTONDOWN: typeName = "mousebuttondown"; break;
      69           0 :     case SDL_MOUSEBUTTONUP: typeName = "mousebuttonup"; break;
      70           0 :     case SDL_QUIT: typeName = "quit"; break;
      71           2 :     case SDL_HOTKEYPRESS: typeName = "hotkeypress"; break;
      72           4 :     case SDL_HOTKEYDOWN: typeName = "hotkeydown"; break;
      73           2 :     case SDL_HOTKEYUP: typeName = "hotkeyup"; break;
      74           0 :     case SDL_HOTKEYPRESS_SILENT: typeName = "hotkeypresssilent"; break;
      75           0 :     case SDL_HOTKEYUP_SILENT: typeName = "hotkeyupsilent"; break;
      76           0 :     default: typeName = "(unknown)"; break;
      77             :     }
      78             : 
      79          28 :     JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
      80          14 :     if (!obj)
      81             :     {
      82           0 :         ret.setUndefined();
      83           0 :         return;
      84             :     }
      85             : 
      86          14 :     SET(obj, "type", typeName);
      87             : 
      88          14 :     switch (ev.type)
      89             :     {
      90           6 :     case SDL_KEYDOWN:
      91           6 :     case SDL_KEYUP:
      92           6 :     {
      93             :         // SET(obj, "which", static_cast<int>(ev.key.which)); // (not in wsdl.h)
      94             :         // SET(obj, "state", static_cast<int>(ev.key.state)); // (not in wsdl.h)
      95             : 
      96           6 :         JS::RootedObject keysym(rq.cx, JS_NewPlainObject(rq.cx));
      97           6 :         if (!keysym)
      98             :         {
      99           0 :             ret.setUndefined();
     100           0 :             return;
     101             :         }
     102           6 :         JS::RootedValue keysymVal(rq.cx, JS::ObjectValue(*keysym));
     103           6 :         JS_SetProperty(rq.cx, obj, "keysym", keysymVal);
     104             : 
     105             :         // SET(keysym, "scancode", static_cast<int>(ev.key.keysym.scancode)); // (not in wsdl.h)
     106           6 :         SET(keysym, "sym", static_cast<int>(ev.key.keysym.sym));
     107             :         // SET(keysym, "mod", static_cast<int>(ev.key.keysym.mod)); // (not in wsdl.h)
     108           6 :         {
     109           6 :             SET(keysym, "unicode", JS::UndefinedHandleValue);
     110             :         }
     111             :         // TODO: scripts have no idea what all the key/mod enum values are;
     112             :         // we should probably expose them as constants if we expect scripts to use them
     113             : 
     114           6 :         break;
     115           6 :     }
     116           0 :     case SDL_MOUSEMOTION:
     117           0 :     {
     118             :         // SET(obj, "which", static_cast<int>(ev.motion.which)); // (not in wsdl.h)
     119             :         // SET(obj, "state", static_cast<int>(ev.motion.state)); // (not in wsdl.h)
     120           0 :         SET(obj, "x", static_cast<int>(ev.motion.x));
     121           0 :         SET(obj, "y", static_cast<int>(ev.motion.y));
     122             :         // SET(obj, "xrel", static_cast<int>(ev.motion.xrel)); // (not in wsdl.h)
     123             :         // SET(obj, "yrel", static_cast<int>(ev.motion.yrel)); // (not in wsdl.h)
     124           0 :         break;
     125             :     }
     126           0 :     case SDL_MOUSEBUTTONDOWN:
     127           0 :     case SDL_MOUSEBUTTONUP:
     128           0 :     {
     129             :         // SET(obj, "which", static_cast<int>(ev.button.which)); // (not in wsdl.h)
     130           0 :         SET(obj, "button", static_cast<int>(ev.button.button));
     131           0 :         SET(obj, "state", static_cast<int>(ev.button.state));
     132           0 :         SET(obj, "x", static_cast<int>(ev.button.x));
     133           0 :         SET(obj, "y", static_cast<int>(ev.button.y));
     134           0 :         SET(obj, "clicks", static_cast<int>(ev.button.clicks));
     135           0 :         break;
     136             :     }
     137           8 :     case SDL_HOTKEYPRESS:
     138           8 :     case SDL_HOTKEYDOWN:
     139           8 :     case SDL_HOTKEYUP:
     140           8 :     case SDL_HOTKEYPRESS_SILENT:
     141           8 :     case SDL_HOTKEYUP_SILENT:
     142           8 :     {
     143           8 :         SET(obj, "hotkey", static_cast<const char*>(ev.user.data1));
     144           8 :         break;
     145             :     }
     146             :     }
     147             : 
     148          14 :     ret.setObject(*obj);
     149             : }
     150             : 
     151          12 : template<> void Script::ToJSVal<IGUIObject*>(const Script::Request&, JS::MutableHandleValue ret,
     152             :     IGUIObject* const& val)
     153             : {
     154          12 :     if (val == nullptr)
     155           7 :         ret.setNull();
     156             :     else
     157           5 :         ret.setObject(*val->GetJSObject());
     158          12 : }
     159             : 
     160           0 : template<> bool Script::FromJSVal<IGUIObject*>(const Script::Request& rq, JS::HandleValue v, IGUIObject*& out)
     161             : {
     162           0 :     if (!v.isObject())
     163             :     {
     164           0 :         Script::Exception::Raise(rq, "Value is not an IGUIObject.");
     165           0 :         return false;
     166             :     }
     167           0 :     out = IGUIProxyObject::FromPrivateSlot<IGUIObject>(v.toObjectOrNull());
     168           0 :     if (!out)
     169             :     {
     170           0 :         Script::Exception::Raise(rq, "Value is not an IGUIObject.");
     171           0 :         return false;
     172             :     }
     173             :     return true;
     174             : }
     175             : 
     176           0 : template<> void Script::ToJSVal<CGUIString>(const Script::Request& rq, JS::MutableHandleValue ret, const CGUIString& val)
     177             : {
     178           0 :     Script::ToJSVal(rq, ret, val.GetOriginalString());
     179           0 : }
     180             : 
     181           0 : template<> bool Script::FromJSVal<CGUIString>(const Script::Request& rq, JS::HandleValue v, CGUIString& out)
     182             : {
     183           0 :     std::wstring val;
     184           0 :     if (!FromJSVal(rq, v, val))
     185             :         return false;
     186           0 :     out.SetValue(val);
     187           0 :     return true;
     188           0 : }
     189             : 
     190           0 : JSVAL_VECTOR(CVector2D)
     191           0 : JSVAL_VECTOR(std::vector<CVector2D>)
     192           0 : JSVAL_VECTOR(CGUIString)
     193             : 
     194           0 : template<> void Script::ToJSVal<CGUIColor>(const Script::Request& rq, JS::MutableHandleValue ret, const CGUIColor& val)
     195             : {
     196           0 :     ToJSVal<CColor>(rq, ret, val);
     197           0 : }
     198             : 
     199             : /**
     200             :  * The color depends on the predefined color database stored in the current GUI page.
     201             :  */
     202             : template<> bool Script::FromJSVal<CGUIColor>(const Script::Request& rq, JS::HandleValue v, CGUIColor& out) = delete;
     203             : 
     204           1 : template<> void Script::ToJSVal<CRect>(const Script::Request& rq, JS::MutableHandleValue ret, const CRect& val)
     205             : {
     206           1 :     Script::CreateObject(
     207             :         rq,
     208             :         ret,
     209           1 :         "left", val.left,
     210           1 :         "right", val.right,
     211           1 :         "top", val.top,
     212           1 :         "bottom", val.bottom);
     213           1 : }
     214             : 
     215           0 : template<> void Script::ToJSVal<CGUIList>(const Script::Request& rq, JS::MutableHandleValue ret, const CGUIList& val)
     216             : {
     217           0 :     ToJSVal(rq, ret, val.m_Items);
     218           0 : }
     219             : 
     220           0 : template<> bool Script::FromJSVal<CGUIList>(const Script::Request& rq, JS::HandleValue v, CGUIList& out)
     221             : {
     222           0 :     return FromJSVal(rq, v, out.m_Items);
     223             : }
     224             : 
     225           0 : template<> void Script::ToJSVal<CGUISeries>(const Script::Request& rq, JS::MutableHandleValue ret, const CGUISeries& val)
     226             : {
     227           0 :     ToJSVal(rq, ret, val.m_Series);
     228           0 : }
     229             : 
     230           0 : template<> bool Script::FromJSVal<CGUISeries>(const Script::Request& rq, JS::HandleValue v, CGUISeries& out)
     231             : {
     232           0 :     return FromJSVal(rq, v, out.m_Series);
     233             : }
     234             : 
     235           0 : template<> void Script::ToJSVal<EVAlign>(const Script::Request& rq, JS::MutableHandleValue ret, const EVAlign& val)
     236             : {
     237           0 :     std::string word;
     238           0 :     switch (val)
     239             :     {
     240           0 :     case EVAlign::TOP:
     241           0 :         word = "top";
     242             :         break;
     243             : 
     244           0 :     case EVAlign::BOTTOM:
     245           0 :         word = "bottom";
     246             :         break;
     247             : 
     248           0 :     case EVAlign::CENTER:
     249           0 :         word = "center";
     250             :         break;
     251             : 
     252           0 :     default:
     253           0 :         word = "error";
     254           0 :         Script::Exception::Raise(rq, "Invalid EVAlign");
     255             :         break;
     256             :     }
     257           0 :     ToJSVal(rq, ret, word);
     258           0 : }
     259             : 
     260           0 : template<> bool Script::FromJSVal<EVAlign>(const Script::Request& rq, JS::HandleValue v, EVAlign& out)
     261             : {
     262           0 :     std::string word;
     263           0 :     FromJSVal(rq, v, word);
     264             : 
     265           0 :     if (word == "top")
     266           0 :         out = EVAlign::TOP;
     267           0 :     else if (word == "bottom")
     268           0 :         out = EVAlign::BOTTOM;
     269           0 :     else if (word == "center")
     270           0 :         out = EVAlign::CENTER;
     271             :     else
     272             :     {
     273           0 :         out = EVAlign::TOP;
     274           0 :         LOGERROR("Invalid alignment (should be 'left', 'right' or 'center')");
     275           0 :         return false;
     276             :     }
     277             :     return true;
     278           0 : }
     279             : 
     280           0 : template<> void Script::ToJSVal<EAlign>(const Script::Request& rq, JS::MutableHandleValue ret, const EAlign& val)
     281             : {
     282           0 :     std::string word;
     283           0 :     switch (val)
     284             :     {
     285           0 :     case EAlign::LEFT:
     286           0 :         word = "left";
     287             :         break;
     288           0 :     case EAlign::RIGHT:
     289           0 :         word = "right";
     290             :         break;
     291           0 :     case EAlign::CENTER:
     292           0 :         word = "center";
     293             :         break;
     294           0 :     default:
     295           0 :         word = "error";
     296           0 :         Script::Exception::Raise(rq, "Invalid alignment (should be 'left', 'right' or 'center')");
     297             :         break;
     298             :     }
     299           0 :     ToJSVal(rq, ret, word);
     300           0 : }
     301             : 
     302           0 : template<> bool Script::FromJSVal<EAlign>(const Script::Request& rq, JS::HandleValue v, EAlign& out)
     303             : {
     304           0 :     std::string word;
     305           0 :     FromJSVal(rq, v, word);
     306             : 
     307           0 :     if (word == "left")
     308           0 :         out = EAlign::LEFT;
     309           0 :     else if (word == "right")
     310           0 :         out = EAlign::RIGHT;
     311           0 :     else if (word == "center")
     312           0 :         out = EAlign::CENTER;
     313             :     else
     314             :     {
     315           0 :         out = EAlign::LEFT;
     316           0 :         LOGERROR("Invalid alignment (should be 'left', 'right' or 'center')");
     317           0 :         return false;
     318             :     }
     319             :     return true;
     320           0 : }
     321             : 
     322           0 : template<> void Script::ToJSVal<EScrollOrientation>(const Script::Request& rq, JS::MutableHandleValue ret, const EScrollOrientation& val)
     323             : {
     324           0 :     std::string word;
     325           0 :     switch (val)
     326             :     {
     327           0 :     case EScrollOrientation::HORIZONTAL:
     328           0 :         word = "horizontal";
     329             :         break;
     330           0 :     case EScrollOrientation::VERTICAL:
     331           0 :         word = "vertical";
     332             :         break;
     333           0 :     case EScrollOrientation::BOTH:
     334           0 :         word = "both";
     335             :         break;
     336           0 :     default:
     337           0 :         word = "error";
     338           0 :         Script::Exception::Raise(rq, "Invalid scroll orientation (should be 'vertical', 'horizontal' or 'both')");
     339             :         break;
     340             :     }
     341           0 :     ToJSVal(rq, ret, word);
     342           0 : }
     343             : 
     344           0 : template <> bool Script::FromJSVal<EScrollOrientation>(const Script::Request& rq, JS::HandleValue v, EScrollOrientation& out)
     345             : {
     346           0 :     std::string word;
     347           0 :     FromJSVal(rq, v, word);
     348             : 
     349           0 :     if (word == "horizontal")
     350           0 :         out = EScrollOrientation::HORIZONTAL;
     351           0 :     else if (word == "vertical")
     352           0 :         out = EScrollOrientation::VERTICAL;
     353           0 :     else if (word == "both")
     354           0 :         out = EScrollOrientation::BOTH;
     355             :     else
     356             :     {
     357           0 :         out = EScrollOrientation::VERTICAL;
     358           0 :         LOGERROR("Invalid scroll orientation (should be 'vertical', 'horizontal' or 'both')");
     359           0 :         return false;
     360             :     }
     361             : 
     362             :     return true;
     363           0 : }
     364             : 
     365           0 : template<> void Script::ToJSVal<CGUISpriteInstance>(const Script::Request& rq, JS::MutableHandleValue ret, const CGUISpriteInstance& val)
     366             : {
     367           0 :     ToJSVal(rq, ret, val.GetName());
     368           0 : }
     369             : 
     370           0 : template<> bool Script::FromJSVal<CGUISpriteInstance>(const Script::Request& rq, JS::HandleValue v, CGUISpriteInstance& out)
     371             : {
     372           0 :     std::string name;
     373           0 :     if (!FromJSVal(rq, v, name))
     374             :         return false;
     375             : 
     376           0 :     out.SetName(name);
     377           0 :     return true;
     378           0 : }
     379             : 
     380           0 : template<> void Script::ToJSVal<CSize2D>(const Script::Request& rq, JS::MutableHandleValue ret, const CSize2D& val)
     381             : {
     382           0 :     Script::CreateObject(rq, ret, "width", val.Width, "height", val.Height);
     383           0 : }
     384             : 
     385           0 : template<> bool Script::FromJSVal<CSize2D>(const Script::Request& rq, JS::HandleValue v, CSize2D& out)
     386             : {
     387           0 :     if (!v.isObject())
     388             :     {
     389           0 :         LOGERROR("CSize2D value must be an object!");
     390           0 :         return false;
     391             :     }
     392             : 
     393           0 :     if (!FromJSProperty(rq, v, "width", out.Width))
     394             :     {
     395           0 :         LOGERROR("Failed to get CSize2D.Width property");
     396           0 :         return false;
     397             :     }
     398             : 
     399           0 :     if (!FromJSProperty(rq, v, "height", out.Height))
     400             :     {
     401           0 :         LOGERROR("Failed to get CSize2D.Height property");
     402           0 :         return false;
     403             :     }
     404             : 
     405             :     return true;
     406             : }
     407             : 
     408           0 : template<> void Script::ToJSVal<CVector2D>(const Script::Request& rq, JS::MutableHandleValue ret, const CVector2D& val)
     409             : {
     410           0 :     Script::CreateObject(rq, ret, "x", val.X, "y", val.Y);
     411           0 : }
     412             : 
     413           0 : template<> bool Script::FromJSVal<CVector2D>(const Script::Request& rq, JS::HandleValue v, CVector2D& out)
     414             : {
     415           0 :     if (!v.isObject())
     416             :     {
     417           0 :         LOGERROR("CVector2D value must be an object!");
     418           0 :         return false;
     419             :     }
     420             : 
     421           0 :     if (!FromJSProperty(rq, v, "x", out.X))
     422             :     {
     423           0 :         LOGERROR("Failed to get CVector2D.X property");
     424           0 :         return false;
     425             :     }
     426             : 
     427           0 :     if (!FromJSProperty(rq, v, "y", out.Y))
     428             :     {
     429           0 :         LOGERROR("Failed to get CVector2D.Y property");
     430           0 :         return false;
     431             :     }
     432             : 
     433             :     return true;
     434             : }
     435             : 
     436             : #undef SET

Generated by: LCOV version 1.16