TGUI  0.10-dev
WidgetRenderer.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_WIDGET_RENDERER_HPP
27#define TGUI_WIDGET_RENDERER_HPP
28
29
30#include <TGUI/Config.hpp>
31#include <TGUI/ObjectConverter.hpp>
32#include <TGUI/Loading/DataIO.hpp>
33#include <unordered_map>
34#include <map>
35
37
38namespace tgui
39{
44 {
45 RendererData() = default;
46
47 static std::shared_ptr<RendererData> create(const std::map<String, ObjectConverter>& init = {})
48 {
49 auto data = std::make_shared<RendererData>();
50 data->propertyValuePairs = init;
51 return data;
52 }
53
55 static std::shared_ptr<RendererData> createFromDataIONode(const DataIO::Node* rendererNode)
56 {
57 auto rendererData = std::make_shared<RendererData>();
58 rendererData->shared = false;
59
60 for (const auto& pair : rendererNode->propertyValuePairs)
61 rendererData->propertyValuePairs[pair.first] = ObjectConverter(pair.second->value); // Did not compile with VS2015 Update 2 when using braces
62
63 for (const auto& nestedProperty : rendererNode->children)
64 {
65 std::stringstream ss;
66 DataIO::emit(nestedProperty, ss);
67 rendererData->propertyValuePairs[nestedProperty->name] = {String("{\n" + ss.str() + "}")};
68 }
69
70 return rendererData;
71 };
72
73 std::map<String, ObjectConverter> propertyValuePairs;
74 std::unordered_map<const void*, std::function<void(const String& property)>> observers;
75 bool shared = true;
76 };
77
78
82 class TGUI_API WidgetRenderer
83 {
84 public:
85
89 WidgetRenderer() = default;
90
91
98 WidgetRenderer(const std::shared_ptr<RendererData>& data)
99 {
100 setData(data);
101 }
102
103
108 virtual ~WidgetRenderer() = default;
109
110
117 void setOpacity(float opacity);
118
119
126 float getOpacity() const;
127
128
136 void setOpacityDisabled(float opacity);
137
138
145 float getOpacityDisabled() const;
146
147
156 void setFont(Font font);
157
158
165 Font getFont() const;
166
167
178 void setTransparentTexture(bool ignoreTransparentParts);
179
180
187
188
199 void setProperty(const String& property, ObjectConverter&& value);
200
201
211 ObjectConverter getProperty(const String& property) const;
212
213
220 const std::map<String, ObjectConverter>& getPropertyValuePairs() const;
221
222
230 void subscribe(const void* id, const std::function<void(const String& property)>& function);
231
232
239 void unsubscribe(const void* id);
240
241
249 void setData(const std::shared_ptr<RendererData>& data);
250
251
260 std::shared_ptr<RendererData> getData() const;
261
262
269 std::shared_ptr<RendererData> clone() const;
270
271
273 protected:
274
275 std::shared_ptr<RendererData> m_data = RendererData::create();
276
277
279 };
280
282}
283
285
286#endif // TGUI_WIDGETS_HPP
Definition: Font.hpp:57
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:48
Wrapper class to store strings.
Definition: String.hpp:79
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:83
virtual ~WidgetRenderer()=default
Virtual destructor.
WidgetRenderer(const std::shared_ptr< RendererData > &data)
Construct the renderer from renderer data.
Definition: WidgetRenderer.hpp:98
void setProperty(const String &property, ObjectConverter &&value)
Changes a property of the renderer.
bool getTransparentTexture() const
Returns whether mouse events should be ignored on transparent parts of the texture of the widget.
std::shared_ptr< RendererData > getData() const
Returns the renderer data.
float getOpacity() const
Returns the opacity of the widget.
Font getFont() const
Returns the font associated with the widget (if any)
void subscribe(const void *id, const std::function< void(const String &property)> &function)
Subscribes a callback function to changes in the renderer.
float getOpacityDisabled() const
Returns the opacity of the widget when it is disabled.
std::shared_ptr< RendererData > clone() const
Gets a clone of the renderer data.
ObjectConverter getProperty(const String &property) const
Retrieves the value of a certain property.
void setOpacityDisabled(float opacity)
Changes the opacity of the widget when it is disabled.
WidgetRenderer()=default
Default constructor.
void setTransparentTexture(bool ignoreTransparentParts)
Sets whether mouse events should be ignored on transparent parts of the texture of the widget in norm...
void setOpacity(float opacity)
Changes the opacity of the widget.
void setFont(Font font)
Changes the font used for the text in the widget.
void unsubscribe(const void *id)
Subscribes a callback function to changes in the renderer.
const std::map< String, ObjectConverter > & getPropertyValuePairs() const
Gets a map with all properties and their values.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Shared data used in renderer classes.
Definition: WidgetRenderer.hpp:44