Skip to content

Commit

Permalink
Added definitions for ImageButton with explicit ID (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinan-Karakaya authored Dec 7, 2023
1 parent 286d34c commit 125081b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
with:
repository: ocornut/imgui
path: imgui
ref: v1.87
ref: v1.89

- name: Checkout SFML
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if(NOT IMGUI_DIR)
endif()

# This uses FindImGui.cmake provided in ImGui-SFML repo for now
find_package(ImGui 1.87 REQUIRED)
find_package(ImGui 1.89 REQUIRED)

# These headers will be installed alongside ImGui-SFML
set(IMGUI_PUBLIC_HEADERS
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Dependencies
-----

* [SFML](https://github.com/SFML/SFML) >= 2.5.0
* [Dear ImGui](https://github.com/ocornut/imgui) >= 1.87
* [Dear ImGui](https://github.com/ocornut/imgui) >= 1.89

Contributing
-----
Expand Down
35 changes: 9 additions & 26 deletions imgui-SFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,47 +726,30 @@ void Image(const sf::Sprite& sprite, const sf::Vector2f& size, const sf::Color&

/////////////// Image Button Overloads for sf::Texture

bool ImageButton(const sf::Texture& texture, const int framePadding, const sf::Color& bgColor,
const sf::Color& tintColor) {
return ImageButton(texture, sf::Vector2f(texture.getSize()), framePadding, bgColor, tintColor);
}

bool ImageButton(const sf::Texture& texture, const sf::Vector2f& size, const int framePadding,
bool ImageButton(const char* id, const sf::Texture& texture, const sf::Vector2f& size,
const sf::Color& bgColor, const sf::Color& tintColor) {
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());

return ImGui::ImageButton(textureID, ImVec2(size.x, size.y), ImVec2(0, 0), ImVec2(1, 1),
framePadding, toImColor(bgColor), toImColor(tintColor));
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), ImVec2(0, 0), ImVec2(1, 1),
toImColor(bgColor), toImColor(tintColor));
}

/////////////// Image Button Overloads for sf::RenderTexture

bool ImageButton(const sf::RenderTexture& texture, const int framePadding, const sf::Color& bgColor,
const sf::Color& tintColor) {
return ImageButton(texture, sf::Vector2f(texture.getSize()), framePadding, bgColor, tintColor);
}

bool ImageButton(const sf::RenderTexture& texture, const sf::Vector2f& size, const int framePadding,
bool ImageButton(const char* id, const sf::RenderTexture& texture, const sf::Vector2f& size,
const sf::Color& bgColor, const sf::Color& tintColor) {
ImTextureID textureID =
convertGLTextureHandleToImTextureID(texture.getTexture().getNativeHandle());

return ImGui::ImageButton(textureID, ImVec2(size.x, size.y), ImVec2(0, 1),
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), ImVec2(0, 1),
ImVec2(1, 0), // flipped vertically, because textures in
// sf::RenderTexture are stored this way
framePadding, toImColor(bgColor), toImColor(tintColor));
toImColor(bgColor), toImColor(tintColor));
}

/////////////// Image Button Overloads for sf::Sprite

bool ImageButton(const sf::Sprite& sprite, const int framePadding, const sf::Color& bgColor,
const sf::Color& tintColor) {
const sf::FloatRect 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,
bool ImageButton(const char* id, const sf::Sprite& sprite, const sf::Vector2f& size,
const sf::Color& bgColor, const sf::Color& tintColor) {
const sf::Texture* texturePtr = sprite.getTexture();
// sprite without texture cannot be drawn
Expand All @@ -781,8 +764,8 @@ bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size, const int f
(textureRect.top + textureRect.height) / textureSize.y);

ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
return ImGui::ImageButton(textureID, ImVec2(size.x, size.y), uv0, uv1, framePadding,
toImColor(bgColor), toImColor(tintColor));
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), uv0, uv1, toImColor(bgColor),
toImColor(tintColor));
}

/////////////// Draw_list Overloads
Expand Down
20 changes: 5 additions & 15 deletions imgui-SFML.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,19 @@ IMGUI_SFML_API void Image(const sf::Sprite& sprite, const sf::Vector2f& size,
const sf::Color& borderColor = sf::Color::Transparent);

// ImageButton overloads for sf::Texture
IMGUI_SFML_API bool ImageButton(const sf::Texture& texture, int framePadding = -1,
const sf::Color& bgColor = sf::Color::Transparent,
const sf::Color& tintColor = sf::Color::White);
IMGUI_SFML_API bool ImageButton(const sf::Texture& texture, const sf::Vector2f& size,
int framePadding = -1,
IMGUI_SFML_API bool ImageButton(const char* id, const sf::Texture& texture,
const sf::Vector2f& size,
const sf::Color& bgColor = sf::Color::Transparent,
const sf::Color& tintColor = sf::Color::White);

// ImageButton overloads for sf::RenderTexture
IMGUI_SFML_API bool ImageButton(const sf::RenderTexture& texture, int framePadding = -1,
const sf::Color& bgColor = sf::Color::Transparent,
const sf::Color& tintColor = sf::Color::White);
IMGUI_SFML_API bool ImageButton(const sf::RenderTexture& texture, const sf::Vector2f& size,
int framePadding = -1,
IMGUI_SFML_API bool ImageButton(const char* id, const sf::RenderTexture& texture,
const sf::Vector2f& size,
const sf::Color& bgColor = sf::Color::Transparent,
const sf::Color& tintColor = sf::Color::White);

// ImageButton overloads for sf::Sprite
IMGUI_SFML_API bool ImageButton(const sf::Sprite& sprite, int framePadding = -1,
const sf::Color& bgColor = sf::Color::Transparent,
const sf::Color& tintColor = sf::Color::White);
IMGUI_SFML_API bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size,
int framePadding = -1,
IMGUI_SFML_API bool ImageButton(const char* id, const sf::Sprite& sprite, const sf::Vector2f& size,
const sf::Color& bgColor = sf::Color::Transparent,
const sf::Color& tintColor = sf::Color::White);

Expand Down

0 comments on commit 125081b

Please sign in to comment.