LCOV - code coverage report
Current view: top level - source/gui - CGUISetting.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 19 33 57.6 %
Date: 2026-11-26 09:57:45 Functions: 6 50 12.0 %

          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 "CGUISetting.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/CGUISprite.h"
      24             : #include "gui/ObjectBases/IGUIObject.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 "maths/Vector2D.h"
      30             : #include "ps/CLogger.h"
      31             : #include "ps/CStr.h"
      32             : #include "scriptinterface/Conversions.h"
      33             : 
      34             : #include <js/RootingAPI.h>
      35             : 
      36             : enum class EAlign;
      37             : enum class EScrollOrientation;
      38             : enum class EVAlign;
      39             : struct CColor;
      40             : 
      41         353 : IGUISetting::IGUISetting(const CStr& name, IGUIObject* owner) : m_Object(*owner), m_Name(name)
      42             : {
      43         353 :     m_Object.RegisterSetting(m_Name, this);
      44         353 : }
      45             : 
      46           1 : IGUISetting::IGUISetting(IGUISetting&& other) : m_Object(other.m_Object), m_Name(other.m_Name)
      47             : {
      48           1 :     m_Object.ReregisterSetting(m_Name, this);
      49           1 : }
      50             : 
      51           3 : bool IGUISetting::FromString(const CStrW& value, const bool sendMessage)
      52             : {
      53           3 :     if (!DoFromString(value))
      54             :         return false;
      55             : 
      56           3 :     OnSettingChange(GetName(), sendMessage);
      57           3 :     return true;
      58             : }
      59             : 
      60             : /**
      61             :  * Parses the given JS::Value using Script::Interface::FromJSVal and assigns it to the setting data.
      62             :  */
      63           1 : bool IGUISetting::FromJSVal(const Script::Request& rq, JS::HandleValue value, const bool sendMessage)
      64             : {
      65           1 :     if (!DoFromJSVal(rq, value))
      66             :         return false;
      67             : 
      68           1 :     OnSettingChange(GetName(), sendMessage);
      69           1 :     return true;
      70             : }
      71             : 
      72          12 : void IGUISetting::OnSettingChange(const CStr& setting, bool sendMessage)
      73             : {
      74          12 :     m_Object.SettingChanged(setting, sendMessage);
      75          12 : }
      76             : 
      77             : template<typename T>
      78           2 : bool CGUISimpleSetting<T>::DoFromString(const CStrW& value)
      79             : {
      80           2 :     return CGUI::ParseString<T>(&m_Object.GetGUI(), value, m_Setting);
      81             : };
      82             : 
      83             : template<>
      84           0 : bool CGUISimpleSetting<CGUIColor>::DoFromJSVal(const Script::Request& rq, JS::HandleValue value)
      85             : {
      86           0 :     if (value.isString())
      87             :     {
      88           0 :         CStr name;
      89           0 :         if (!Script::FromJSVal(rq, value, name))
      90             :             return false;
      91             : 
      92           0 :         if (!m_Setting.ParseString(m_Object.GetGUI(), name))
      93             :         {
      94           0 :             LOGERROR("Invalid color '{}'", name);
      95           0 :             return false;
      96             :         }
      97             :         return true;
      98           0 :     }
      99           0 :     return Script::FromJSVal<CColor>(rq, value, m_Setting);
     100             : };
     101             : 
     102             : template<typename T>
     103           0 : bool CGUISimpleSetting<T>::DoFromJSVal(const Script::Request& rq, JS::HandleValue value)
     104             : {
     105           0 :     return Script::FromJSVal<T>(rq, value, m_Setting);
     106             : };
     107             : 
     108             : template<typename T>
     109           0 : void CGUISimpleSetting<T>::ToJSVal(const Script::Request& rq, JS::MutableHandleValue value)
     110             : {
     111           0 :     Script::ToJSVal<T>(rq, value, m_Setting);
     112           0 : };
     113             : 
     114             : /**
     115             :  * Explicitly instantiate CGUISimpleSetting for the basic types.
     116             :  */
     117             : #define TYPE(T) \
     118             :     template class CGUISimpleSetting<T>;
     119             : 
     120             : TYPE(bool)
     121             : TYPE(std::int32_t)
     122             : TYPE(std::uint32_t)
     123             : TYPE(float)
     124             : TYPE(CVector2D)
     125             : TYPE(CStr)
     126             : TYPE(CStrW)
     127             : // TODO: make these inherit from CGUISimpleSetting directly.
     128             : TYPE(CGUIColor)
     129             : TYPE(CGUISpriteInstance)
     130             : TYPE(CGUIString)
     131             : TYPE(EAlign)
     132             : TYPE(EVAlign)
     133             : TYPE(CGUIList)
     134             : TYPE(CGUISeries)
     135             : TYPE(EScrollOrientation)
     136             : 
     137             : #undef TYPE

Generated by: LCOV version 1.16