TGUI  0.8.8
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/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&&) noexcept;
138 
143 
147  Texture& operator=(const Texture&);
148 
152  Texture& operator=(Texture&&) noexcept;
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 
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
static void setImageLoader(const ImageLoaderFunc &func)
Sets a different image loader.
bool isTransparentPixel(sf::Vector2u pos) const
Checks if a certain pixel is transparent.
bool isSmooth() const
Tells whether the smooth filter is enabled or not.
static const ImageLoaderFunc & getImageLoader()
Returns the used image loader.
Vector2f getImageSize() const
Returns the size that the loaded image.
Texture()
Default constructor.
Definition: Texture.hpp:54
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
sf::IntRect getMiddleRect() const
Returns the middle rect of the texture which is used for 9-slice scaling.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
bool operator!=(const Texture &right) const
Compares the texture with another one.
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
Wrapper for colors.
Definition: Color.hpp:49
Texture(const sf::Texture &texture, const sf::IntRect &partRect=sf::IntRect(0, 0, 0, 0), const sf::IntRect &middlePart=sf::IntRect(0, 0, 0, 0))
Constructor that created the texture from an existing sf::Texture.
void setSmooth(bool smooth)
Enables or disable the smooth filter.
static void setTextureLoader(const TextureLoaderFunc &func)
Sets a different texture loader.
std::shared_ptr< TextureData > getData() const
Returns the texture data.
Texture(const sf::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.
static const Color White
White predefined color.
Definition: Color.hpp:207
sf::Shader * getShader() const
Returns the shader used to draw the texture.
void setDestructCallback(const CallbackFunc &func)
Sets a callback function for when this texture is destroyed.
Definition: Texture.hpp:42
Texture(Texture &&) noexcept
Move constructor.
void setCopyCallback(const CallbackFunc &func)
Sets a callback function for when this texture is copied.
Wrapper class to store strings.
Definition: String.hpp:119
const Color & getColor() const
Returns the global color of the texture.
bool operator==(const Texture &right) const
Compares the texture with another one.
Definition: Vector2f.hpp:39
void setShader(sf::Shader *shader)
Sets the shader used to draw the texture.
void setColor(const Color &color)
Sets the global color of the texture.
Texture(const Texture &)
Copy constructor.
const sf::String & getId() const
Returns the id that was used to load the texture (for the default loader, the id is the filename)
void load(const sf::Texture &texture, const sf::IntRect &partRect={}, const sf::IntRect &middleRect={})
Creates the texture from an existing sf::Texture.
static const TextureLoaderFunc & getTextureLoader()
Returns the used texture loader.