Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CGUISetting.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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#ifndef INCLUDED_CGUISETTINGS
19#define INCLUDED_CGUISETTINGS
20
21#include "ps/CStr.h"
23
24
25class IGUIObject;
26
27/**
28 * This setting interface allows GUI objects to call setting function functions without having to know the setting type.
29 * This is fact is used for setting the value from a JS value or XML value (string) and when deleting the setting,
30 * when the type of the setting value is not known in advance.
31 */
33{
34public:
36 IGUISetting(const CStr& name, IGUIObject* owner);
37
38 /**
39 * Parses the given string and assigns to the setting value. Used for parsing XML attributes.
40 */
41 bool FromString(const CStrW& value, const bool sendMessage);
42
43 /**
44 * Parses the given JS::Value using Script::FromJSVal and assigns it to the setting data.
45 */
46 bool FromJSVal(const ScriptRequest& rq, JS::HandleValue value, const bool sendMessage);
47
48 /**
49 * Converts the setting data to a JS::Value using Script::ToJSVal.
50 */
51 virtual void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) = 0;
52
53protected:
54 IGUISetting(IGUISetting&& other);
55 IGUISetting& operator=(IGUISetting&& other) = delete;
56
57 virtual ~IGUISetting() = default;
58
59 virtual bool DoFromString(const CStrW& value) = 0;
60 virtual bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) = 0;
61
62 /**
63 * Triggers the IGUIObject logic when a setting changes.
64 * This should be called by derived classes when something externally visible changes,
65 * unless overloaded to provide similar behaviour.
66 */
67 virtual void OnSettingChange(const CStr& setting, bool sendMessage);
68
69 /**
70 * Return the name of the setting, from JS.
71 */
72 const CStr& GetName() const
73 {
74 return m_Name;
75 }
76
77 /**
78 * The object that stores this setting.
79 */
81
82private:
83 CStr m_Name;
84};
85
86/**
87 * Wraps a T. Makes sure the appropriate setting functions are called when modifying T,
88 * and likewise makes sure that JS/xml settings affect T appropriately,
89 * while being as transparent as possible to use from C++ code.
90 */
91template<typename T>
93{
94public:
95 template<typename... Args>
96 CGUISimpleSetting(IGUIObject* pObject, const CStr& name, Args&&... args)
97 : IGUISetting(name, pObject), m_Setting(args...)
98 {}
102
103 operator const T&() const { return m_Setting; }
104 const T& operator*() const { return m_Setting; }
105 const T* operator->() const { return &m_Setting; }
106
107 /**
108 * 'Uglified' getter when you want direct access without triggering messages.
109 */
110 T& GetMutable() { return m_Setting; }
111
112 /**
113 * 'Uglified' operator=, so that SendMessage is explicit.
114 */
115 void Set(T value, bool sendMessage)
116 {
117 m_Setting = std::move(value);
118 OnSettingChange(GetName(), sendMessage);
119 }
120
121protected:
122 bool DoFromString(const CStrW& value) override;
123 bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) override;
124 void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) override;
125
127};
128
129#endif // INCLUDED_CGUISETTINGS
Wraps a T.
Definition: CGUISetting.h:93
CGUISimpleSetting(IGUIObject *pObject, const CStr &name, Args &&... args)
Definition: CGUISetting.h:96
void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue value) override
Converts the setting data to a JS::Value using Script::ToJSVal.
Definition: CGUISetting.cpp:102
T m_Setting
Definition: CGUISetting.h:126
NONCOPYABLE(CGUISimpleSetting)
CGUISimpleSetting & operator=(CGUISimpleSetting &&)=delete
CGUISimpleSetting(CGUISimpleSetting &&)=default
T & GetMutable()
'Uglified' getter when you want direct access without triggering messages.
Definition: CGUISetting.h:110
const T * operator->() const
Definition: CGUISetting.h:105
void Set(T value, bool sendMessage)
'Uglified' operator=, so that SendMessage is explicit.
Definition: CGUISetting.h:115
bool DoFromString(const CStrW &value) override
Definition: CGUISetting.cpp:71
const T & operator*() const
Definition: CGUISetting.h:104
bool DoFromJSVal(const ScriptRequest &rq, JS::HandleValue value) override
Definition: CGUISetting.cpp:96
GUI object such as a button or an input-box.
Definition: IGUIObject.h:60
This setting interface allows GUI objects to call setting function functions without having to know t...
Definition: CGUISetting.h:33
IGUISetting & operator=(IGUISetting &&other)=delete
virtual ~IGUISetting()=default
bool FromString(const CStrW &value, const bool sendMessage)
Parses the given string and assigns to the setting value.
Definition: CGUISetting.cpp:44
virtual void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue value)=0
Converts the setting data to a JS::Value using Script::ToJSVal.
NONCOPYABLE(IGUISetting)
virtual bool DoFromJSVal(const ScriptRequest &rq, JS::HandleValue value)=0
IGUIObject & m_Object
The object that stores this setting.
Definition: CGUISetting.h:80
virtual void OnSettingChange(const CStr &setting, bool sendMessage)
Triggers the IGUIObject logic when a setting changes.
Definition: CGUISetting.cpp:65
const CStr & GetName() const
Return the name of the setting, from JS.
Definition: CGUISetting.h:72
virtual bool DoFromString(const CStrW &value)=0
bool FromJSVal(const ScriptRequest &rq, JS::HandleValue value, const bool sendMessage)
Parses the given JS::Value using Script::FromJSVal and assigns it to the setting data.
Definition: CGUISetting.cpp:56
CStr m_Name
Definition: CGUISetting.h:83
IGUISetting(const CStr &name, IGUIObject *owner)
Definition: CGUISetting.cpp:34
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
#define T(string_literal)
Definition: secure_crt.cpp:77