Pyrogenesis  trunk
CGUISetting.h
Go to the documentation of this file.
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 #ifndef INCLUDED_CGUISETTINGS
19 #define INCLUDED_CGUISETTINGS
20 
21 #include "ps/CStr.h"
23 
24 
25 class 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 {
34 public:
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 
53 protected:
55  virtual ~IGUISetting() = default;
56 
57  virtual bool DoFromString(const CStrW& value) = 0;
58  virtual bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) = 0;
59 
60  /**
61  * Triggers the IGUIObject logic when a setting changes.
62  * This should be called by derived classes when something externally visible changes,
63  * unless overloaded to provide similar behaviour.
64  */
65  virtual void OnSettingChange(const CStr& setting, bool sendMessage);
66 
67  /**
68  * Return the name of the setting, from JS.
69  */
70  virtual CStr GetName() const = 0;
71 
72  /**
73  * The object that stores this setting.
74  */
76 };
77 
78 /**
79  * Wraps a T. Makes sure the appropriate setting functions are called when modifying T,
80  * and likewise makes sure that JS/xml settings affect T appropriately,
81  * while being as transparent as possible to use from C++ code.
82  */
83 template<typename T>
85 {
86 public:
87  template<typename... Args>
88  CGUISimpleSetting(IGUIObject* pObject, const CStr& Name, Args&&... args)
89  : IGUISetting(Name, pObject), m_Name(Name), m_Setting(args...)
90  {}
93 
94  operator const T&() const { return m_Setting; }
95  const T& operator*() const { return m_Setting; }
96  const T* operator->() const { return &m_Setting; }
97 
98  /**
99  * 'Uglified' getter when you want direct access without triggering messages.
100  */
101  T& GetMutable() { return m_Setting; }
102 
103  /**
104  * 'Uglified' operator=, so that SendMessage is explicit.
105  */
106  void Set(T value, bool sendMessage)
107  {
108  m_Setting = std::move(value);
109  OnSettingChange(m_Name, sendMessage);
110  }
111 
112 protected:
113  CStr GetName() const override
114  {
115  return m_Name;
116  }
117 
118  bool DoFromString(const CStrW& value) override;
119  bool DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) override;
120  void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) override;
121 
122  const CStr m_Name;
124 };
125 
126 #endif // INCLUDED_CGUISETTINGS
T & GetMutable()
&#39;Uglified&#39; getter when you want direct access without triggering messages.
Definition: CGUISetting.h:101
T m_Setting
Definition: CGUISetting.h:123
virtual ~IGUISetting()=default
virtual void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue value)=0
Converts the setting data to a JS::Value using Script::ToJSVal.
NONCOPYABLE(IGUISetting)
#define MOVABLE(className)
Indicates that move semantics can be used, so that a NONCOPYABLE class can still be assigned by takin...
Definition: code_annotation.h:235
const T * operator->() const
Definition: CGUISetting.h:96
virtual CStr GetName() const =0
Return the name of the setting, from JS.
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:49
virtual void OnSettingChange(const CStr &setting, bool sendMessage)
Triggers the IGUIObject logic when a setting changes.
Definition: CGUISetting.cpp:58
GUI object such as a button or an input-box.
Definition: IGUIObject.h:59
void Set(T value, bool sendMessage)
&#39;Uglified&#39; operator=, so that SendMessage is explicit.
Definition: CGUISetting.h:106
Wraps a T.
Definition: CGUISetting.h:84
virtual bool DoFromJSVal(const ScriptRequest &rq, JS::HandleValue value)=0
virtual bool DoFromString(const CStrW &value)=0
#define T(string_literal)
Definition: secure_crt.cpp:77
This setting interface allows GUI objects to call setting function functions without having to know t...
Definition: CGUISetting.h:32
IGUISetting(const CStr &name, IGUIObject *owner)
Definition: CGUISetting.cpp:27
CStr GetName() const override
Return the name of the setting, from JS.
Definition: CGUISetting.h:113
const CStr m_Name
Definition: CGUISetting.h:122
IGUIObject & m_pObject
The object that stores this setting.
Definition: CGUISetting.h:75
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59
const T & operator*() const
Definition: CGUISetting.h:95
bool FromString(const CStrW &value, const bool sendMessage)
Parses the given string and assigns to the setting value.
Definition: CGUISetting.cpp:37
CGUISimpleSetting(IGUIObject *pObject, const CStr &Name, Args &&... args)
Definition: CGUISetting.h:88