TGUI  0.9-dev
Texture.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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_TEXTURE_HPP
27 #define TGUI_TEXTURE_HPP
28 
30 
31 #include <TGUI/TextureData.hpp>
32 #include <TGUI/Vector2.hpp>
33 #include <TGUI/String.hpp>
34 #include <TGUI/Color.hpp>
35 #include <TGUI/Rect.hpp>
36 #include <functional>
37 
39 
40 namespace tgui
41 {
42  class TGUI_API Texture
43  {
44  public:
45 
46  using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
47  using ImageLoaderFunc = std::function<std::shared_ptr<sf::Image>(const String&)>;
48  using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, const UIntRect&, bool smooth)>;
49 
50 
55  Texture() {}
56 
57 
67  Texture(const char* id,
68  const UIntRect& partRect = UIntRect(0, 0, 0, 0),
69  const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
70  bool smooth = false)
71  : Texture(String{id}, partRect, middlePart, smooth)
72  {
73  }
74 
75 
87  Texture(const String& id,
88  const UIntRect& partRect = UIntRect(0, 0, 0, 0),
89  const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
90  bool smooth = false);
91 
92 
105  Texture(const sf::Texture& texture,
106  const UIntRect& partRect = UIntRect(0, 0, 0, 0),
107  const UIntRect& middlePart = UIntRect(0, 0, 0, 0));
108 
109 
113  Texture(const Texture&);
114 
118  Texture(Texture&&) noexcept;
119 
123  ~Texture();
124 
128  Texture& operator=(const Texture&);
129 
133  Texture& operator=(Texture&&) noexcept;
134 
135 
145  void load(const String& id,
146  const UIntRect& partRect = {},
147  const UIntRect& middleRect = {},
148  bool smooth = false);
149 
150 
161  void load(const sf::Texture& texture,
162  const UIntRect& partRect = {},
163  const UIntRect& middleRect = {});
164 
165 
172  const String& getId() const;
173 
174 
181  std::shared_ptr<TextureData> getData() const;
182 
183 
189  Vector2u getImageSize() const;
190 
191 
198  UIntRect getPartRect() const;
199 
200 
206  bool isSmooth() const;
207 
208 
219  void setColor(const Color& color);
220 
221 
233  const Color& getColor() const;
234 
235 
240  void setShader(sf::Shader* shader);
241 
242 
247  sf::Shader* getShader() const;
248 
249 
256  UIntRect getMiddleRect() const;
257 
258 
267  bool isTransparentPixel(Vector2u pos) const;
268 
269 
278  void setCopyCallback(const CallbackFunc& func);
279 
280 
289  void setDestructCallback(const CallbackFunc& func);
290 
291 
295  bool operator==(const Texture& right) const;
296 
297 
301  bool operator!=(const Texture& right) const;
302 
303 
314  static void setImageLoader(const ImageLoaderFunc& func);
315 
316 
325  static const ImageLoaderFunc& getImageLoader();
326 
327 
338  static void setTextureLoader(const TextureLoaderFunc& func);
339 
340 
349  static const TextureLoaderFunc& getTextureLoader();
350 
351 
353  private:
354 
363  void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
364 
365 
367  private:
368 
369  std::shared_ptr<TextureData> m_data = nullptr;
370  Color m_color = Color::White;
371  sf::Shader* m_shader = nullptr;
372  UIntRect m_partRect;
373  UIntRect m_middleRect;
374  String m_id;
375 
376  CallbackFunc m_copyCallback;
377  CallbackFunc m_destructCallback;
378 
379  static TextureLoaderFunc m_textureLoader;
380  static ImageLoaderFunc m_imageLoader;
381  };
382 
384 }
385 
387 
388 #endif // TGUI_TEXTURE_HPP
Texture()
Default constructor.
Definition: Texture.hpp:55
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
static const Color White
White predefined color.
Definition: Color.hpp:222
Definition: Texture.hpp:42
Texture(const char *id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=false)
Constructor that created the texture.
Definition: Texture.hpp:67
Wrapper class to store strings.
Definition: String.hpp:70