From 95bcfd2da3f8ecded9433927a87218213a8f2ca8 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 29 May 2016 17:19:06 +0200 Subject: [PATCH] Added sf::Sprite and sf::Texture overload for ImageButton --- imgui-SFML.cpp | 67 ++++++++++++++++++++++++++++++++++++++++---------- imgui-SFML.h | 21 ++++++++++++---- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/imgui-SFML.cpp b/imgui-SFML.cpp index 8c06256..fe4c238 100644 --- a/imgui-SFML.cpp +++ b/imgui-SFML.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -19,7 +20,7 @@ static sf::Texture* s_fontTexture = nullptr; static sf::Clock s_deltaClock; static bool s_mousePressed[5] = { false, false, false, false, false }; -namespace +namespace { ImVec2 getDisplaySize() @@ -31,6 +32,10 @@ ImVec2 getDisplaySize() void RenderDrawLists(ImDrawData* draw_data); // rendering callback function prototype +// Implementation of ImageButton overload +bool imageButtonImpl(const sf::Texture& texture, const sf::FloatRect& textureRect, const sf::Vector2f& size, const int framePadding, + const sf::Color& bgColor, const sf::Color& tintColor); + } // anonymous namespace for helper / "private" functions namespace ImGui @@ -76,7 +81,7 @@ void Init(sf::Window& window, sf::RenderTarget& target) io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); if (s_fontTexture) { // was already created, delete it - delete s_fontTexture; + delete s_fontTexture; } s_fontTexture = new sf::Texture; @@ -176,27 +181,50 @@ void Image(const sf::Texture& texture, const sf::Vector2f& size) void Image(const sf::Sprite& sprite) { - auto textureRect = static_cast(sprite.getTextureRect()); - Image(sprite, sf::Vector2f(textureRect.width, textureRect.height)); + auto bounds = sprite.getGlobalBounds(); + Image(sprite, sf::Vector2f(bounds.width, bounds.height)); } void Image(const sf::Sprite& sprite, const sf::Vector2f& size) { auto texturePtr = sprite.getTexture(); - if (!texturePtr) { return; } // sprite without texture cannot be drawn - Image_impl(*texturePtr, static_cast(sprite.getTextureRect()), size); -} + // sprite without texture cannot be drawn + if (!texturePtr) + return; -void Image_impl(const sf::Texture& texture, const sf::FloatRect& textureRect, const sf::Vector2f& size) -{ - auto textureSize = static_cast(texture.getSize()); + auto textureSize = static_cast(texturePtr->getSize()); + auto textureRect = sprite.getTextureRect(); ImVec2 uv0(textureRect.left / textureSize.x, textureRect.top / textureSize.y); ImVec2 uv1((textureRect.left + textureRect.width) / textureSize.x, - (textureRect.top + textureRect.height) / textureSize.y); + (textureRect.top + textureRect.height) / textureSize.y); + + ImGui::Image((void*)texturePtr, size, uv0, uv1); +} - ImGui::Image((void*)&texture, size, uv0, uv1); +bool ImageButton(const sf::Texture& texture, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) +{ + return ImageButton(texture, static_cast(texture.getSize()), framePadding, bgColor, tintColor); } +bool ImageButton(const sf::Texture& texture, const sf::Vector2f& size, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) +{ + auto textureSize = texture.getSize(); + return ::imageButtonImpl(texture, sf::FloatRect(0,0,textureSize.x, textureSize.y), size, framePadding, bgColor, tintColor); +} + +bool ImageButton(const sf::Sprite& sprite, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) +{ + auto spriteSize = sprite.getGlobalBounds(); + return ImageButton(sprite, sf::Vector2f(spriteSize.width, spriteSize.height), framePadding, bgColor, tintColor); +} + +bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) +{ + auto texturePtr = sprite.getTexture(); + if (!texturePtr) + return false; + return ::imageButtonImpl(*texturePtr, static_cast(sprite.getTextureRect()), size, framePadding, bgColor, tintColor); +} } // end of namespace ImGui @@ -276,4 +304,17 @@ void RenderDrawLists(ImDrawData* draw_data) s_renderTarget->resetGLStates(); } -} // end of anonymous namespace \ No newline at end of file +bool imageButtonImpl(const sf::Texture& texture, const sf::FloatRect& textureRect, const sf::Vector2f& size, const int framePadding, + const sf::Color& bgColor, const sf::Color& tintColor) +{ + auto textureSize = texture.getSize(); + + ImVec2 uv0(textureRect.left / textureSize.x, textureRect.top / textureSize.y); + ImVec2 uv1((textureRect.left + textureRect.width) / textureSize.x, + (textureRect.top + textureRect.height) / textureSize.y); + + return ImGui::ImageButton((void*)&texture, size, uv0, uv1, framePadding, ImVec4(bgColor.r, bgColor.g, bgColor.b, bgColor.a), + ImVec4(tintColor.r, tintColor.g, tintColor.b, tintColor.a)); +} + +} // end of anonymous namespace diff --git a/imgui-SFML.h b/imgui-SFML.h index b845a09..1d34436 100644 --- a/imgui-SFML.h +++ b/imgui-SFML.h @@ -1,5 +1,6 @@ #include #include +#include namespace sf { @@ -28,10 +29,20 @@ namespace SFML // custom ImGui widgets for SFML stuff void Image(const sf::Texture& texture); void Image(const sf::Texture& texture, const sf::Vector2f& size); - + void Image(const sf::Sprite& sprite); - void Image(const sf::Sprite& sprite, const sf::Vector2f& size); + void Image(const sf::Sprite& sprite, const sf::Vector2f& size); + + bool ImageButton(const sf::Texture& texture, const int framePadding = -1, + const sf::Color& bgColor = sf::Color::Transparent, + const sf::Color& tintColor = sf::Color::White); + bool ImageButton(const sf::Texture& texture, const sf::Vector2f& size, const int framePadding = -1, + const sf::Color& bgColor = sf::Color::Transparent, const sf::Color& tintColor = sf::Color::White); - void Image_impl(const sf::Texture& texture, const sf::FloatRect& textureRect, - const sf::Vector2f& size); -} \ No newline at end of file + bool ImageButton(const sf::Sprite& sprite, const int framePadding = -1, + const sf::Color& bgColor = sf::Color::Transparent, + const sf::Color& tintColor = sf::Color::White); + bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size, const int framePadding = -1, + const sf::Color& bgColor = sf::Color::Transparent, + const sf::Color& tintColor = sf::Color::White); +}