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: 6 135 4.4 %
Date: 2022-06-14 00:41:00 Functions: 2 50 4.0 %

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

Generated by: LCOV version 1.13