TGUI  0.8-beta
WidgetRenderer.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
35 
36 namespace tgui
37 {
41  struct RendererData
42  {
43  RendererData() = default;
44 
45  static std::shared_ptr<RendererData> create(const std::map<std::string, ObjectConverter>& init = {})
46  {
47  auto data = std::make_shared<RendererData>();
48  data->propertyValuePairs = init;
49  return data;
50  }
51 
53  static std::shared_ptr<RendererData> createFromDataIONode(const DataIO::Node* rendererNode)
54  {
55  auto rendererData = std::make_shared<RendererData>();
56  rendererData->shared = false;
57 
58  for (const auto& pair : rendererNode->propertyValuePairs)
59  rendererData->propertyValuePairs[pair.first] = ObjectConverter(pair.second->value); // Did not compile with VS2015 Update 2 when using braces
60 
61  for (const auto& nestedProperty : rendererNode->children)
62  {
63  std::stringstream ss;
64  DataIO::emit(nestedProperty, ss);
65  rendererData->propertyValuePairs[toLower(nestedProperty->name)] = {sf::String{"{\n" + ss.str() + "}"}};
66  }
67 
68  return rendererData;
69  };
70 
71  std::map<std::string, ObjectConverter> propertyValuePairs;
72  std::map<const void*, std::function<void(const std::string& property)>> observers;
73  bool shared = true;
74  };
75 
76 
80  class TGUI_API WidgetRenderer
81  {
82  public:
83 
87  WidgetRenderer() = default;
88 
89 
96  WidgetRenderer(const std::shared_ptr<RendererData>& data)
97  {
98  setData(data);
99  }
100 
101 
106  virtual ~WidgetRenderer() = default;
107 
108 
115  void setOpacity(float opacity);
116 
117 
124  float getOpacity() const;
125 
126 
135  void setFont(Font font);
136 
137 
144  Font getFont() const;
145 
146 
157  void setTransparentTexture(bool ignoreTransparentParts);
158 
159 
165  bool getTransparentTexture() const;
166 
167 
178  void setProperty(const std::string& property, ObjectConverter&& value);
179 
180 
190  ObjectConverter getProperty(const std::string& property) const;
191 
192 
199  const std::map<std::string, ObjectConverter>& getPropertyValuePairs() const;
200 
201 
209  void subscribe(const void* id, const std::function<void(const std::string& property)>& function);
210 
211 
218  void unsubscribe(const void* id);
219 
220 
228  void setData(const std::shared_ptr<RendererData>& data);
229 
230 
239  std::shared_ptr<RendererData> getData() const;
240 
241 
248  std::shared_ptr<RendererData> clone() const;
249 
250 
252  protected:
253 
254  std::shared_ptr<RendererData> m_data = RendererData::create();
255 
256 
258  };
259 
261 }
262 
264 
265 #endif // TGUI_WIDGETS_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Shared data used in renderer classes.
Definition: WidgetRenderer.hpp:41
Definition: Font.hpp:42
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:52
WidgetRenderer(const std::shared_ptr< RendererData > &data)
Construct the renderer from renderer data.
Definition: WidgetRenderer.hpp:96
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:80