TGUI  0.7.4
Texture.hpp
1 //
3 // TGUI - Texus's 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_TEXTURE_HPP
27 #define TGUI_TEXTURE_HPP
28 
30 
31 #include <TGUI/TextureData.hpp>
32 #include <TGUI/TextureManager.hpp>
33 
34 #include <TGUI/Global.hpp>
35 
36 #include <functional>
37 
39 
40 namespace tgui
41 {
43 
44  class TGUI_API Texture : public sf::Transformable, public sf::Drawable
45  {
46  public:
47 
48  using ImageLoaderFunc = std::function<std::shared_ptr<sf::Image>(const sf::String&)>;
49  using TextureLoaderFunc = std::function<bool(Texture&, const sf::String&, const sf::IntRect&)>;
50 
51 
56  enum class ScalingType
57  {
58  Normal,
59  Horizontal,
60  Vertical,
61  NineSlice
62  };
63 
64 
66  public:
67 
72  Texture() {}
73 
74 
85  Texture(const sf::String& id,
86  const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
87  const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0));
88 
89 
102  Texture(const sf::Texture& texture,
103  const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
104  const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0));
105 
106 
113  Texture(const Texture& texture);
114 
115 
117  // Virtual destructor
119  ~Texture();
120 
121 
130  Texture& operator=(const Texture& right);
131 
132 
143  void load(const sf::Texture& texture,
144  const sf::IntRect& partRect = {},
145  const sf::IntRect& middleRect = {});
146 
147 
156  void load(const sf::String& id,
157  const sf::IntRect& partRect = {},
158  const sf::IntRect& middleRect = {});
159 
160 
170  void setTexture(std::shared_ptr<TextureData> data, const sf::IntRect& middleRect = {});
171 
172 
179  std::string getId() const;
180 
181 
188  std::shared_ptr<TextureData>& getData();
189 
190 
197  std::shared_ptr<const TextureData> getData() const;
198 
199 
206  void setSize(const sf::Vector2f& size);
207 
208 
215  sf::Vector2f getSize() const
216  {
217  return m_size;
218  }
219 
220 
227  sf::Vector2f getImageSize() const
228  {
229  return sf::Vector2f{m_data->texture.getSize()};
230  }
231 
232 
244  void setColor(const sf::Color& color);
245 
246 
258  const sf::Color& getColor() const;
259 
260 
269  void setTextureRect(const sf::FloatRect& textureRect)
270  {
271  m_textureRect = textureRect;
272  }
273 
274 
281  sf::FloatRect getTextureRect() const
282  {
283  return m_textureRect;
284  }
285 
286 
293  sf::IntRect getMiddleRect() const
294  {
295  return m_middleRect;
296  }
297 
298 
311  void setSmooth(bool smooth);
312 
313 
322  bool isSmooth() const
323  {
324  return m_data->texture.isSmooth();
325  }
326 
327 
337  bool isTransparentPixel(float x, float y) const;
338 
339 
347  {
348  return m_scalingType;
349  }
350 
351 
358  bool isLoaded() const
359  {
360  return m_loaded;
361  }
362 
363 
374  static void setImageLoader(const ImageLoaderFunc& func);
375 
376 
387  static void setTextureLoader(const TextureLoaderFunc& func);
388 
389 
398  static ImageLoaderFunc getImageLoader();
399 
400 
409  static TextureLoaderFunc getTextureLoader();
410 
411 
420  void setCopyCallback(const std::function<void(std::shared_ptr<TextureData>)> func);
421 
422 
431  void setDestructCallback(const std::function<void(std::shared_ptr<TextureData>)> func);
432 
433 
434 
436  private:
437 
439  // Update the vertices of the internal vertex array
441  void updateVertices();
442 
443 
445  // Draws the texture
447  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
448 
449 
451 
452  private:
453  std::shared_ptr<TextureData> m_data = std::make_shared<TextureData>();
454  std::vector<sf::Vertex> m_vertices;
455 
456  sf::Vector2f m_size;
457  sf::IntRect m_middleRect;
458  sf::FloatRect m_textureRect;
459  sf::Color m_vertexColor = sf::Color::White;
460 
461  ScalingType m_scalingType = ScalingType::Normal;
462 
463  bool m_loaded = false;
464  std::string m_id;
465 
466  std::function<void(std::shared_ptr<TextureData>)> m_copyCallback;
467  std::function<void(std::shared_ptr<TextureData>)> m_destructCallback;
468  static TextureLoaderFunc m_textureLoader;
469  static ImageLoaderFunc m_imageLoader;
470  };
471 
473 }
474 
476 
477 #endif // TGUI_TEXTURE_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
ScalingType getScalingType() const
Returns the way in which the image is being scaled.
Definition: Texture.hpp:346
sf::Vector2f getImageSize() const
Returns the size that the loaded image.
Definition: Texture.hpp:227
Texture()
Default constructor.
Definition: Texture.hpp:72
sf::FloatRect getTextureRect() const
Returns the part of the texture that is drawn.
Definition: Texture.hpp:281
bool isLoaded() const
Check if the texture has been correctly loaded.
Definition: Texture.hpp:358
ScalingType
The way the image should be scaled.
Definition: Texture.hpp:56
sf::IntRect getMiddleRect() const
Returns the middle rect of the texture which is used for 9-slice scaling.
Definition: Texture.hpp:293
Definition: Texture.hpp:44
sf::Vector2f getSize() const
Returns the size that the image has when drawing.
Definition: Texture.hpp:215
void setTextureRect(const sf::FloatRect &textureRect)
Changes the part of the texture that should be drawn.
Definition: Texture.hpp:269
bool isSmooth() const
Tell whether the smooth filter is enabled or not.
Definition: Texture.hpp:322