TGUI  0.8.5
Texture.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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/Vector2f.hpp>
33 #include <TGUI/Color.hpp>
34 #include <SFML/System/String.hpp>
35 #include <functional>
36 
38 
39 namespace tgui
40 {
41  class TGUI_API Texture
42  {
43  public:
44 
45  using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
46  using ImageLoaderFunc = std::function<std::unique_ptr<sf::Image>(const sf::String&)>;
47  using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const sf::String&, const sf::IntRect&)>;
48 
49 
54  Texture() {}
55 
56 
66  Texture(const char* id,
67  const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
68  const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0),
69  bool smooth = false)
70  : Texture(sf::String{id}, partRect, middlePart, smooth)
71  {
72  }
73 
74 
86  Texture(const std::string& id,
87  const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
88  const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0),
89  bool smooth = false)
90  : Texture(sf::String{id}, partRect, middlePart, smooth)
91  {
92  }
93 
94 
106  Texture(const sf::String& id,
107  const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
108  const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0),
109  bool smooth = false);
110 
111 
124  Texture(const sf::Texture& texture,
125  const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
126  const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0));
127 
128 
132  Texture(const Texture&);
133 
137  Texture(Texture&&);
138 
142  ~Texture();
143 
147  Texture& operator=(const Texture&);
148 
152  Texture& operator=(Texture&&);
153 
154 
164  void load(const sf::String& id,
165  const sf::IntRect& partRect = {},
166  const sf::IntRect& middleRect = {},
167  bool smooth = false);
168 
169 
180  void load(const sf::Texture& texture,
181  const sf::IntRect& partRect = {},
182  const sf::IntRect& middleRect = {});
183 
184 
191  const sf::String& getId() const;
192 
193 
200  std::shared_ptr<TextureData> getData() const;
201 
202 
209  Vector2f getImageSize() const;
210 
211 
224  void setSmooth(bool smooth);
225 
226 
235  bool isSmooth() const;
236 
237 
248  void setColor(const Color& color);
249 
250 
262  const Color& getColor() const;
263 
264 
269  void setShader(sf::Shader* shader);
270 
271 
276  sf::Shader* getShader() const;
277 
278 
285  sf::IntRect getMiddleRect() const;
286 
287 
296  bool isTransparentPixel(sf::Vector2u pos) const;
297 
298 
307  void setCopyCallback(const CallbackFunc& func);
308 
309 
318  void setDestructCallback(const CallbackFunc& func);
319 
320 
324  bool operator==(const Texture& right) const;
325 
326 
330  bool operator!=(const Texture& right) const;
331 
332 
343  static void setImageLoader(const ImageLoaderFunc& func);
344 
345 
354  static const ImageLoaderFunc& getImageLoader();
355 
356 
367  static void setTextureLoader(const TextureLoaderFunc& func);
368 
369 
378  static const TextureLoaderFunc& getTextureLoader();
379 
380 
382  private:
383 
391  void setTextureData(std::shared_ptr<TextureData> data, const sf::IntRect& middleRect = {});
392 
393 
395  private:
396 
397  std::shared_ptr<TextureData> m_data = nullptr;
398  Color m_color = Color::White;
399  sf::Shader* m_shader = nullptr;
400  sf::IntRect m_middleRect;
401  sf::String m_id;
402 
403  CallbackFunc m_copyCallback;
404  CallbackFunc m_destructCallback;
405 
406  static TextureLoaderFunc m_textureLoader;
407  static ImageLoaderFunc m_imageLoader;
408  };
409 
411 }
412 
414 
415 #endif // TGUI_TEXTURE_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Texture()
Default constructor.
Definition: Texture.hpp:54
TGUI_CONSTEXPR bool operator!=(const Vector2f &left, const Vector2f &right)
Overload of binary operator ==.
Definition: Vector2f.hpp:224
TGUI_CONSTEXPR bool operator==(const Vector2f &left, const Vector2f &right)
Overload of binary operator ==.
Definition: Vector2f.hpp:216
Texture(const char *id, const sf::IntRect &partRect=sf::IntRect(0, 0, 0, 0), const sf::IntRect &middlePart=sf::IntRect(0, 0, 0, 0), bool smooth=false)
Constructor that created the texture.
Definition: Texture.hpp:66
Definition: Texture.hpp:41
static const Color White
White predefined color.
Definition: Color.hpp:207
Texture(const std::string &id, const sf::IntRect &partRect=sf::IntRect(0, 0, 0, 0), const sf::IntRect &middlePart=sf::IntRect(0, 0, 0, 0), bool smooth=false)
Constructor that created the texture.
Definition: Texture.hpp:86