TGUI  0.8-dev
ObjectConverter.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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_OBJECT_CONVERTER_HPP
27 #define TGUI_OBJECT_CONVERTER_HPP
28 
29 
30 #include <TGUI/TextStyle.hpp>
31 #include <TGUI/Outline.hpp>
32 #include <TGUI/Texture.hpp>
33 #include <TGUI/Color.hpp>
34 #include <TGUI/Font.hpp>
35 
36 #ifdef TGUI_USE_VARIANT
37  #include <variant>
38 #else
39  #include <TGUI/Any.hpp>
40 #endif
41 
43 
44 namespace tgui
45 {
46  struct RendererData;
47 
52  class TGUI_API ObjectConverter
53  {
54  public:
55  enum class Type
56  {
57  None,
58  Bool,
59  Font,
60  Color,
61  String,
62  Number,
63  Outline,
64  Texture,
65  TextStyle,
67  };
68 
69 
75  m_type{Type::None}
76  {
77  }
78 
79 
85  ObjectConverter(const char* string) :
86  ObjectConverter{sf::String{string}}
87  {
88  }
89 
90 
96  ObjectConverter(const std::string& string) :
97  ObjectConverter{sf::String{string}}
98  {
99  }
100 
101 
107  ObjectConverter(const sf::String& string) :
108  m_type {Type::String},
109  m_value {string},
110  m_serialized{true},
111  m_string {string}
112  {
113  }
114 
115 
123  m_type {Type::Font},
124  m_value{font}
125  {
126  }
127 
128 
135  ObjectConverter(sf::Color color) :
136  ObjectConverter(Color{color})
137  {
138  }
139 
140 
148  m_type {Type::Color},
149  m_value{color}
150  {
151  }
152 
153 
159  ObjectConverter(bool value) :
160  m_type {Type::Bool},
161  m_value{value}
162  {
163  }
164 
165 
172  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
173  ObjectConverter(T number) :
174  m_type {Type::Number},
175  m_value{static_cast<float>(number)}
176  {
177  }
178 
179 
186  ObjectConverter(const Outline& outline) :
187  m_type {Type::Outline},
188  m_value{outline}
189  {
190  }
191 
192 
199  ObjectConverter(const Texture& texture) :
200  m_type {Type::Texture},
201  m_value{texture}
202  {
203  }
204 
205 
214  ObjectConverter(sf::Text::Style style) :
215  ObjectConverter(TextStyle(style))
216  {
217  }
218 
219 
227  m_type {Type::TextStyle},
228  m_value{style}
229  {
230  }
231 
232 
239  ObjectConverter(std::shared_ptr<RendererData> data) :
240  m_type {Type::RendererData},
241  m_value{std::move(data)}
242  {
243  }
244 
245 
252  const sf::String& getString();
253 
254 
263  const Font& getFont();
264 
265 
274  const Color& getColor();
275 
276 
285  const Outline& getOutline();
286 
287 
295  bool getBool();
296 
297 
306  float getNumber();
307 
308 
317  Texture& getTexture();
318 
319 
328  const TextStyle& getTextStyle();
329 
330 
339  const std::shared_ptr<RendererData>& getRenderer();
340 
341 
348  Type getType() const;
349 
350 
356  bool operator==(const ObjectConverter& right) const;
357 
358 
364  bool operator!=(const ObjectConverter& right) const;
365 
366 
368  private:
369  Type m_type = Type::None;
370 
371  #ifdef TGUI_USE_VARIANT
372  std::variant<sf::String, Font, Color, Outline, bool, float, Texture, TextStyle, std::shared_ptr<RendererData>> m_value;
373  #else
374  Any m_value;
375  #endif
376 
377  bool m_serialized = false;
378  sf::String m_string;
379  };
380 
382 }
383 
385 
386 #endif // TGUI_OBJECT_CONVERTER_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
ObjectConverter(const Texture &texture)
Stores a texture object for later retrieval.
Definition: ObjectConverter.hpp:199
Shared data used in renderer classes.
Definition: WidgetRenderer.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
ObjectConverter(Color color)
Stores a color object for later retrieval.
Definition: ObjectConverter.hpp:147
Wrapper for text styles.
Definition: TextStyle.hpp:46
ObjectConverter(bool value)
Stores a boolean for later retrieval.
Definition: ObjectConverter.hpp:159
Definition: Font.hpp:41
ObjectConverter(std::shared_ptr< RendererData > data)
Stores render data for later retrieval.
Definition: ObjectConverter.hpp:239
ObjectConverter(sf::Text::Style style)
Stores a text style for later retrieval.
Definition: ObjectConverter.hpp:214
ObjectConverter(const Outline &outline)
Stores an outline object for later retrieval.
Definition: ObjectConverter.hpp:186
Definition: Any.hpp:36
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:52
ObjectConverter(sf::Color color)
Stores a color object for later retrieval.
Definition: ObjectConverter.hpp:135
Definition: Texture.hpp:39
ObjectConverter(const char *string)
Stores a string for later retrieval.
Definition: ObjectConverter.hpp:85
ObjectConverter(TextStyle style)
Stores a text style for later retrieval.
Definition: ObjectConverter.hpp:226
Definition: Outline.hpp:38
ObjectConverter()
Default constructor, to create an empty object.
Definition: ObjectConverter.hpp:74
ObjectConverter(const std::string &string)
Stores a string for later retrieval.
Definition: ObjectConverter.hpp:96
ObjectConverter(Font font)
Stores a font object for later retrieval.
Definition: ObjectConverter.hpp:122
ObjectConverter(const sf::String &string)
Stores a string for later retrieval.
Definition: ObjectConverter.hpp:107
ObjectConverter(T number)
Stores a number for later retrieval.
Definition: ObjectConverter.hpp:173