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