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 176 : IGUISetting::IGUISetting(const CStr& name, IGUIObject* owner) : m_pObject(*owner)
28 : {
29 176 : m_pObject.RegisterSetting(name, this);
30 176 : }
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 0 : 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 0 : 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 :
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 0 : 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 0 : return false;
82 : }
83 0 : return true;
84 : }
85 0 : return Script::FromJSVal<CColor>(rq, value, m_Setting);
86 : };
87 :
88 : template<typename T>
89 0 : bool CGUISimpleSetting<T>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value)
90 : {
91 0 : return Script::FromJSVal<T>(rq, value, m_Setting);
92 : };
93 :
94 : template<typename T>
95 0 : void CGUISimpleSetting<T>::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value)
96 : {
97 0 : Script::ToJSVal<T>(rq, value, m_Setting);
98 0 : };
99 :
100 : /**
101 : * Explicitly instantiate CGUISimpleSetting for the basic types.
102 : */
103 : #define TYPE(T) \
104 : template class CGUISimpleSetting<T>;
105 :
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)
129 :
130 : #undef TYPE
|