Update clean-workspaces.sh after SpiderMonkey's upgrade to 78.6.0 and 91.13.1.
[0ad.git] / source / gui / CGUISetting.cpp
blob41b504309dc8514a3e1127f35bd5e8232aff50cd
1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
20 #include "CGUISetting.h"
22 #include "gui/CGUI.h"
23 #include "gui/ObjectBases/IGUIObject.h"
24 #include "ps/CLogger.h"
25 #include "scriptinterface/ScriptConversions.h"
27 IGUISetting::IGUISetting(const CStr& name, IGUIObject* owner) : m_pObject(*owner)
29 m_pObject.RegisterSetting(name, this);
32 IGUISetting::IGUISetting(IGUISetting&& o) : m_pObject(o.m_pObject)
34 m_pObject.ReregisterSetting(o.GetName(), this);
37 bool IGUISetting::FromString(const CStrW& value, const bool sendMessage)
39 if (!DoFromString(value))
40 return false;
42 OnSettingChange(GetName(), sendMessage);
43 return true;
46 /**
47 * Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data.
49 bool IGUISetting::FromJSVal(const ScriptRequest& rq, JS::HandleValue value, const bool sendMessage)
51 if (!DoFromJSVal(rq, value))
52 return false;
54 OnSettingChange(GetName(), sendMessage);
55 return true;
58 void IGUISetting::OnSettingChange(const CStr& setting, bool sendMessage)
60 m_pObject.SettingChanged(setting, sendMessage);
63 template<typename T>
64 bool CGUISimpleSetting<T>::DoFromString(const CStrW& value)
66 return CGUI::ParseString<T>(&m_pObject.GetGUI(), value, m_Setting);
69 template<>
70 bool CGUISimpleSetting<CGUIColor>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value)
72 if (value.isString())
74 CStr name;
75 if (!Script::FromJSVal(rq, value, name))
76 return false;
78 if (!m_Setting.ParseString(m_pObject.GetGUI(), name))
80 LOGERROR("Invalid color '%s'", name.c_str());
81 return false;
83 return true;
85 return Script::FromJSVal<CColor>(rq, value, m_Setting);
88 template<typename T>
89 bool CGUISimpleSetting<T>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value)
91 return Script::FromJSVal<T>(rq, value, m_Setting);
94 template<typename T>
95 void CGUISimpleSetting<T>::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value)
97 Script::ToJSVal<T>(rq, value, m_Setting);
101 * Explicitly instantiate CGUISimpleSetting for the basic types.
103 #define TYPE(T) \
104 template class CGUISimpleSetting<T>;
106 TYPE(bool)
107 TYPE(i32)
108 TYPE(u32)
109 TYPE(float)
110 TYPE(CVector2D)
111 #include "ps/CStr.h"
112 TYPE(CStr)
113 TYPE(CStrW)
114 // TODO: make these inherit from CGUISimpleSetting directly.
115 #include "gui/SettingTypes/CGUISize.h"
116 TYPE(CGUISize)
117 TYPE(CGUIColor)
118 #include "gui/CGUISprite.h"
119 TYPE(CGUISpriteInstance)
120 #include "gui/SettingTypes/CGUIString.h"
121 TYPE(CGUIString)
122 #include "gui/SettingTypes/EAlign.h"
123 TYPE(EAlign)
124 TYPE(EVAlign)
125 #include "gui/SettingTypes/CGUIList.h"
126 TYPE(CGUIList)
127 #include "gui/SettingTypes/CGUISeries.h"
128 TYPE(CGUISeries)
130 #undef TYPE