From 1ef9904b6652558f269491ddd22e1e54eb40fb9f Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Wed, 1 Nov 2023 22:29:37 +0100 Subject: [PATCH] Added Texture::setMiddleRect --- changelog.md | 1 + include/TGUI/Texture.hpp | 37 ++++++------------------------------- src/Texture.cpp | 39 +++++++++++++++++++++++---------------- tests/Texture.cpp | 3 +++ 4 files changed, 33 insertions(+), 47 deletions(-) diff --git a/changelog.md b/changelog.md index 7d89f8973..d639d5725 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ TGUI 1.1 (TBD) - Added methods for arrow key navigation between widgets - Added getColumnDesignWidth function to ListView - Added TextOutlineColor and TextOutlineThickness to ProgressBar renderer +- MiddleRect of Texture can now be changed after loading - Hover state is now reset when mouse leaves the window diff --git a/include/TGUI/Texture.hpp b/include/TGUI/Texture.hpp index 5182d3272..96a7db7ea 100644 --- a/include/TGUI/Texture.hpp +++ b/include/TGUI/Texture.hpp @@ -22,7 +22,6 @@ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - #ifndef TGUI_TEXTURE_HPP #define TGUI_TEXTURE_HPP @@ -62,14 +61,12 @@ TGUI_MODULE_EXPORT namespace tgui using BackendTextureLoaderFunc = std::function; using TextureLoaderFunc = std::function(Texture&, const String&, bool smooth)>; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Default constructor /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Texture() = default; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Constructor that created the texture /// @@ -88,7 +85,6 @@ TGUI_MODULE_EXPORT namespace tgui { } - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Constructor that created the texture /// @@ -123,7 +119,6 @@ TGUI_MODULE_EXPORT namespace tgui const UIntRect& partRect = UIntRect(0, 0, 0, 0), const UIntRect& middlePart = UIntRect(0, 0, 0, 0)); #endif - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Copy constructor ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -149,7 +144,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Texture& operator=(Texture&&) noexcept; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Creates the texture /// @@ -182,7 +176,6 @@ TGUI_MODULE_EXPORT namespace tgui const UIntRect& partRect = {}, const UIntRect& middleRect = {}); #endif - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Loads the texture from memory (data in memory should contain the entire file, not just the pixels) /// @@ -196,7 +189,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loadFromMemory(const std::uint8_t* data, std::size_t dataSize, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Loads the texture from an array of 32-bits RGBA pixels /// @@ -212,7 +204,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loadFromPixelData(Vector2u size, const std::uint8_t* pixels, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Loads the texture from a base64 string /// @@ -227,7 +218,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loadFromBase64(CharStringView imageAsBase64, const UIntRect& partRect = {}, const UIntRect& middleRect = {}, bool smooth = m_defaultSmooth); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the id that was used to load the texture (for the default loader, the id is the filename) /// @@ -236,7 +226,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD const String& getId() const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the texture data /// @@ -245,7 +234,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD std::shared_ptr getData() const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the size that the loaded image, or the size of the part if only a part of the image is loaded /// @@ -253,16 +241,13 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD Vector2u getImageSize() const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns which part of the image was loaded /// /// @return Part of the image that was loaded - /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD UIntRect getPartRect() const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Tells whether the smooth filter is enabled or not /// @@ -270,7 +255,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD bool isSmooth() const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Sets the global color of the texture /// @@ -283,7 +267,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setColor(const Color& color); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the global color of the texture /// @@ -311,16 +294,20 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD sf::Shader* getShader() const; #endif + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Sets the middle part of the image for 9-slice scaling (relative to the part defined by partRect) + /// + /// @param middleRect Middle rect of the texture + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void setMiddleRect(const UIntRect& middleRect); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the middle rect of the texture which is used for 9-slice scaling /// /// @return Middle rect of the texture - /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD UIntRect getMiddleRect() const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Checks if a certain pixel is transparent /// @@ -331,7 +318,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD bool isTransparentPixel(Vector2u pos) const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Sets a callback function for when this texture is copied /// @@ -342,7 +328,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setCopyCallback(const CallbackFunc& func); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Sets a callback function for when this texture is destroyed /// @@ -353,19 +338,16 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setDestructCallback(const CallbackFunc& func); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Compares the texture with another one ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD bool operator==(const Texture& right) const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Compares the texture with another one ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD bool operator!=(const Texture& right) const; - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes whether textures are smoothed by default or not /// @@ -378,7 +360,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static void setDefaultSmooth(bool smooth); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns whether textures are smoothed by default or not /// @@ -389,7 +370,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD static bool getDefaultSmooth(); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Sets a different backend texture loader /// @@ -401,7 +381,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static void setBackendTextureLoader(const BackendTextureLoaderFunc& func); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the used backend texture loader /// @@ -411,7 +390,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD static const BackendTextureLoaderFunc& getBackendTextureLoader(); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Sets a different texture loader /// @@ -424,7 +402,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static void setTextureLoader(const TextureLoaderFunc& func); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns the used texture loader /// @@ -435,7 +412,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD static const TextureLoaderFunc& getTextureLoader(); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private: @@ -449,7 +425,6 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setTextureData(std::shared_ptr data, const UIntRect& partRect, const UIntRect& middleRect); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private: diff --git a/src/Texture.cpp b/src/Texture.cpp index 2c272e987..418b7d67c 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -343,6 +343,28 @@ TGUI_IGNORE_DEPRECATED_WARNINGS_END #endif ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void Texture::setMiddleRect(const UIntRect& middleRect) + { + if (middleRect == UIntRect{}) + m_middleRect = {0, 0, m_partRect.width, m_partRect.height}; + else + { + m_middleRect = middleRect; + + // If the middle rect was only partially provided then we need to calculate the width and height ourselves + if (((middleRect.left > 0) || (middleRect.top > 0)) && (middleRect.width == 0) && (middleRect.height == 0)) + { + if (m_partRect.width > 2 * middleRect.left) + m_middleRect.width = m_partRect.width - (2 * middleRect.left); + + if (m_partRect.height > 2 * middleRect.top) + m_middleRect.height = m_partRect.height - (2 * middleRect.top); + } + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + UIntRect Texture::getMiddleRect() const { return m_middleRect; @@ -464,22 +486,7 @@ TGUI_IGNORE_DEPRECATED_WARNINGS_END else m_partRect = partRect; - if (middleRect == UIntRect{}) - m_middleRect = {0, 0, m_partRect.width, m_partRect.height}; - else - { - m_middleRect = middleRect; - - // If the middle rect was only partially provided then we need to calculate the width and height ourselves - if (((middleRect.left > 0) || (middleRect.top > 0)) && (middleRect.width == 0) && (middleRect.height == 0)) - { - if (m_partRect.width > 2 * middleRect.left) - m_middleRect.width = m_partRect.width - (2 * middleRect.left); - - if (m_partRect.height > 2 * middleRect.top) - m_middleRect.height = m_partRect.height - (2 * middleRect.top); - } - } + setMiddleRect(middleRect); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/Texture.cpp b/tests/Texture.cpp index 81275864a..00959b177 100644 --- a/tests/Texture.cpp +++ b/tests/Texture.cpp @@ -221,6 +221,9 @@ TEST_CASE("[Texture]") texture.load("resources/image.png"); REQUIRE(texture.getMiddleRect() == tgui::UIntRect(0, 0, 50, 50)); + + texture.setMiddleRect({10, 15, 30, 25}); + REQUIRE(texture.getMiddleRect() == tgui::UIntRect(10, 15, 30, 25)); } SECTION("Smooth")