TGUI  0.7.8
Texture.hpp
1
2//
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
40namespace 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
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 override;
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
Definition: Texture.hpp:45
sf::IntRect getMiddleRect() const
Returns the middle rect of the texture which is used for 9-slice scaling.
Definition: Texture.hpp:293
bool isTransparentPixel(float x, float y) const
Check if a certain pixel is transparent.
static void setTextureLoader(const TextureLoaderFunc &func)
Sets a different texture loader.
void load(const sf::String &id, const sf::IntRect &partRect={}, const sf::IntRect &middleRect={})
Creates the texture.
Texture & operator=(const Texture &right)
Overload of assignment operator.
bool isLoaded() const
Check if the texture has been correctly loaded.
Definition: Texture.hpp:358
void setDestructCallback(const std::function< void(std::shared_ptr< TextureData >)> func)
Sets a callback function for when this texture is destroyed.
sf::FloatRect getTextureRect() const
Returns the part of the texture that is drawn.
Definition: Texture.hpp:281
void load(const sf::Texture &texture, const sf::IntRect &partRect={}, const sf::IntRect &middleRect={})
Creates the texture from an existing sf::Texture.
static void setImageLoader(const ImageLoaderFunc &func)
Sets a different image loader.
void setColor(const sf::Color &color)
Set the global color of the sprite.
std::shared_ptr< const TextureData > getData() const
Returns the constant texture data.
const sf::Color & getColor() const
Returns the global color of the sprite.
void setTexture(std::shared_ptr< TextureData > data, const sf::IntRect &middleRect={})
Changes the texture.
Texture(const Texture &texture)
Copy constructor.
static ImageLoaderFunc getImageLoader()
Returns the used image loader.
sf::Vector2f getSize() const
Returns the size that the image has when drawing.
Definition: Texture.hpp:215
std::string getId() const
Returns the id that was used to load the texture (for the default loader, the id is the filename)
sf::Vector2f getImageSize() const
Returns the size that the loaded image.
Definition: Texture.hpp:227
void setTextureRect(const sf::FloatRect &textureRect)
Changes the part of the texture that should be drawn.
Definition: Texture.hpp:269
void setSmooth(bool smooth)
Enable or disable the smooth filter.
Texture()
Default constructor.
Definition: Texture.hpp:72
bool isSmooth() const
Tell whether the smooth filter is enabled or not.
Definition: Texture.hpp:322
ScalingType
The way the image should be scaled.
Definition: Texture.hpp:57
static TextureLoaderFunc getTextureLoader()
Returns the used texture loader.
void setCopyCallback(const std::function< void(std::shared_ptr< TextureData >)> func)
Sets a callback function for when this texture is copied.
std::shared_ptr< TextureData > & getData()
Returns a reference to 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))
Constructor that created the texture.
ScalingType getScalingType() const
Returns the way in which the image is being scaled.
Definition: Texture.hpp:346
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 setSize(const sf::Vector2f &size)
Changes the size that the image will have when drawing.
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34