TGUI 1.10
Loading...
Searching...
No Matches
Texture.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#ifndef TGUI_TEXTURE_HPP
26#define TGUI_TEXTURE_HPP
27
29
30#include <TGUI/TextureData.hpp>
31#include <TGUI/Vector2.hpp>
32#include <TGUI/String.hpp>
33#include <TGUI/Global.hpp>
34#include <TGUI/Color.hpp>
35#include <TGUI/Rect.hpp>
36
37#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
38 #include <functional>
39#endif
40
41#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !TGUI_BUILD_AS_CXX_MODULE
42 namespace sf
43 {
44 class Texture;
45 class Shader;
46 }
47#endif
48
50
51TGUI_MODULE_EXPORT namespace tgui
52{
56 class TGUI_API Texture
57 {
58 public:
59
60 using CallbackFunc = std::function<void(std::shared_ptr<TextureData>)>;
61 using BackendTextureLoaderFunc = std::function<bool(BackendTexture&, const String&, bool smooth)>;
62 using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, bool smooth)>;
63
67 Texture() = default;
68
79 Texture(const char* id,
80 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
81 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
82 bool smooth = m_defaultSmooth)
83 : Texture(String{id}, partRect, middlePart, smooth)
84 {
85 }
86
99 Texture(const String& id,
100 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
101 const UIntRect& middlePart = UIntRect(0, 0, 0, 0),
102 bool smooth = m_defaultSmooth);
103
104#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
119 TGUI_DEPRECATED("Use Texture() and loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
120 Texture(const sf::Texture& texture,
121 const UIntRect& partRect = UIntRect(0, 0, 0, 0),
122 const UIntRect& middlePart = UIntRect(0, 0, 0, 0));
123#endif
128
132 Texture(Texture&&) noexcept;
133
138
142 Texture& operator=(const Texture&);
143
147 Texture& operator=(Texture&&) noexcept;
148
159 void load(const String& id,
160 const UIntRect& partRect = {},
161 const UIntRect& middleRect = {},
162 bool smooth = m_defaultSmooth);
163
164#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS && !defined(TGUI_REMOVE_DEPRECATED_CODE)
179 TGUI_DEPRECATED("Use loadFromPixelData(texture.getSize(), texture.copyToImage().getPixelsPtr()) instead")
180 void load(const sf::Texture& texture,
181 const UIntRect& partRect = {},
182 const UIntRect& middleRect = {});
183#endif
195 void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
196
210 void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
211
224 void loadFromBase64(CharStringView imageAsBase64, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth);
225
231 TGUI_NODISCARD const String& getId() const;
232
238 TGUI_NODISCARD std::shared_ptr<TextureData> getData() const;
239
245 TGUI_NODISCARD Vector2u getImageSize() const;
246
252 TGUI_NODISCARD UIntRect getPartRect() const;
253
259 TGUI_NODISCARD bool isSmooth() const;
260
271 void setColor(const Color& color);
272
283 TGUI_NODISCARD const Color& getColor() const;
284
285#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
290 void setShader(sf::Shader* shader);
291
296 TGUI_NODISCARD sf::Shader* getShader() const;
297#endif
305 void setMiddleRect(const UIntRect& middleRect);
306
312 TGUI_NODISCARD UIntRect getMiddleRect() const;
313
328 void setScaledNineSlice(bool scaled);
329
339 TGUI_NODISCARD bool getScaledNineSlice() const;
340
348 TGUI_NODISCARD bool isTransparentPixel(Vector2u pos) const;
349
357 void setCopyCallback(const CallbackFunc& func);
358
366 void setDestructCallback(const CallbackFunc& func);
367
371 TGUI_NODISCARD bool operator==(const Texture& right) const;
372
376 TGUI_NODISCARD bool operator!=(const Texture& right) const;
377
388 static void setDefaultSmooth(bool smooth);
389
398 TGUI_NODISCARD static bool getDefaultSmooth();
399
409 static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);
410
418 TGUI_NODISCARD static const BackendTextureLoaderFunc& getBackendTextureLoader();
419
429 static void setTextureLoader(const TextureLoaderFunc& func);
430
438 TGUI_NODISCARD static const TextureLoaderFunc& getTextureLoader();
439
441 private:
442
450 void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);
451
453 private:
454
455#if TGUI_HAS_RENDERER_BACKEND_SFML_GRAPHICS
456 sf::Shader* m_shader = nullptr;
457#endif
458
459 std::shared_ptr<TextureData> m_data = nullptr;
460 Color m_color = Color::White;
461
462 UIntRect m_partRect;
463 UIntRect m_middleRect;
464 String m_id;
465 bool m_scaledNineSlice = false;
466
467 CallbackFunc m_copyCallback;
468 CallbackFunc m_destructCallback;
469
470 static bool m_defaultSmooth;
471
472 static TextureLoaderFunc m_textureLoader;
473 static BackendTextureLoaderFunc m_backendTextureLoader;
474 };
475
477}
478
480
481#endif // TGUI_TEXTURE_HPP
Base class for texture implementations that depend on the backend.
Definition BackendTexture.hpp:43
Wrapper for colors.
Definition Color.hpp:73
static const Color White
White predefined color.
Definition Color.hpp:260
Wrapper class to store strings.
Definition String.hpp:96
void setScaledNineSlice(bool scaled)
Sets whether the corners of the image can still scale with the image size when 9-slice scaling is use...
UIntRect getMiddleRect() const
Returns the middle rect of the texture which is used for 9-slice scaling.
static void setTextureLoader(const TextureLoaderFunc &func)
Sets a different texture loader.
Texture(const Texture &)
Copy constructor.
static void setDefaultSmooth(bool smooth)
Changes whether textures are smoothed by default or not.
void setMiddleRect(const UIntRect &middleRect)
Sets the middle part of the image for 9-slice scaling (relative to the part defined by partRect)
bool isTransparentPixel(Vector2u pos) const
Checks if a certain pixel is transparent.
std::shared_ptr< TextureData > getData() const
Returns the texture data.
sf::Shader * getShader() const
Returns the shader used to draw the texture.
Texture(Texture &&) noexcept
Move constructor.
void load(const String &id, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Creates the texture.
static const BackendTextureLoaderFunc & getBackendTextureLoader()
Returns the used backend texture loader.
static bool getDefaultSmooth()
Returns whether textures are smoothed by default or not.
bool operator==(const Texture &right) const
Compares the texture with another one.
Texture(const char *id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=m_defaultSmooth)
Constructor that created the texture.
Definition Texture.hpp:79
void setShader(sf::Shader *shader)
Sets the shader used to draw the texture.
bool operator!=(const Texture &right) const
Compares the texture with another one.
void loadFromBase64(CharStringView imageAsBase64, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from a base64 string.
static const TextureLoaderFunc & getTextureLoader()
Returns the used texture loader.
void setColor(const Color &color)
Sets the global color of the texture.
UIntRect getPartRect() const
Returns which part of the image was loaded.
bool isSmooth() const
Tells whether the smooth filter is enabled or not.
void loadFromPixelData(Vector2u size, const std::uint8_t *pixels, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from an array of 32-bits RGBA pixels.
bool getScaledNineSlice() const
Returns whether the corners of the image can still scale with the image size when 9-slice scaling is ...
void loadFromMemory(const std::uint8_t *data, std::size_t dataSize, const UIntRect &partRect={}, const UIntRect &middleRect={}, bool smooth=m_defaultSmooth)
Loads the texture from memory (data in memory should contain the entire file, not just the pixels)
static void setBackendTextureLoader(const BackendTextureLoaderFunc &func)
Sets a different backend texture loader.
void setCopyCallback(const CallbackFunc &func)
Sets a callback function for when this texture is copied.
const Color & getColor() const
Returns the global color of the texture.
const String & getId() const
Returns the id that was used to load the texture (for the default loader, the id is the filename)
void setDestructCallback(const CallbackFunc &func)
Sets a callback function for when this texture is destroyed.
Texture(const String &id, const UIntRect &partRect=UIntRect(0, 0, 0, 0), const UIntRect &middlePart=UIntRect(0, 0, 0, 0), bool smooth=m_defaultSmooth)
Constructor that created the texture.
Vector2u getImageSize() const
Returns the size that the loaded image, or the size of the part if only a part of the image is loaded...
Texture()=default
Default constructor.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38