Skip to content

Commit

Permalink
Added Texture::setMiddleRect
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Nov 1, 2023
1 parent e6fb473 commit 1ef9904
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 47 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
37 changes: 6 additions & 31 deletions include/TGUI/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef TGUI_TEXTURE_HPP
#define TGUI_TEXTURE_HPP

Expand Down Expand Up @@ -62,14 +61,12 @@ TGUI_MODULE_EXPORT namespace tgui
using BackendTextureLoaderFunc = std::function<bool(BackendTexture&, const String&, bool smooth)>;
using TextureLoaderFunc = std::function<std::shared_ptr<TextureData>(Texture&, const String&, bool smooth)>;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Default constructor
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Texture() = default;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor that created the texture
///
Expand All @@ -88,7 +85,6 @@ TGUI_MODULE_EXPORT namespace tgui
{
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor that created the texture
///
Expand Down Expand Up @@ -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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -149,7 +144,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Texture& operator=(Texture&&) noexcept;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Creates the texture
///
Expand Down Expand Up @@ -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)
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand All @@ -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)
///
Expand All @@ -236,7 +226,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD const String& getId() const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the texture data
///
Expand All @@ -245,32 +234,27 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD std::shared_ptr<TextureData> 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
///
/// @return Size of the image like it was when loaded (no scaling applied)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
///
/// @return True if smoothing is enabled, false if it is disabled
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isSmooth() const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets the global color of the texture
///
Expand All @@ -283,7 +267,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setColor(const Color& color);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the global color of the texture
///
Expand Down Expand Up @@ -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
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand All @@ -378,7 +360,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void setDefaultSmooth(bool smooth);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether textures are smoothed by default or not
///
Expand All @@ -389,7 +370,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static bool getDefaultSmooth();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets a different backend texture loader
///
Expand All @@ -401,7 +381,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void setBackendTextureLoader(const BackendTextureLoaderFunc& func);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the used backend texture loader
///
Expand All @@ -411,7 +390,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static const BackendTextureLoaderFunc& getBackendTextureLoader();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets a different texture loader
///
Expand All @@ -424,7 +402,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void setTextureLoader(const TextureLoaderFunc& func);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the used texture loader
///
Expand All @@ -435,7 +412,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static const TextureLoaderFunc& getTextureLoader();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:

Expand All @@ -449,7 +425,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextureData(std::shared_ptr<TextureData> data, const UIntRect& partRect, const UIntRect& middleRect);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:

Expand Down
39 changes: 23 additions & 16 deletions src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions tests/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1ef9904

Please sign in to comment.