diff --git a/README.md b/README.md index fc217fd..c3259c0 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ void rlImGuiImage(const Texture *image); void rlImGuiImageSize(const Texture *image, int width, int height); void rlImGuiImageSizeV(const Texture* image, Vector2 size); void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect); +void rlImGuiImageFit(const Texture* image, bool center); void rlImGuiImageRenderTexture(const RenderTexture* image); void rlImGuiImageRenderTextureFit(const RenderTexture* image, bool center); diff --git a/rlImGui.cpp b/rlImGui.cpp index 0c2f151..a004cdb 100644 --- a/rlImGui.cpp +++ b/rlImGui.cpp @@ -609,6 +609,37 @@ void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Recta ImGui::Image((ImTextureID)image->id, ImVec2(float(destWidth), float(destHeight)), uv0, uv1); } +void rlImGuiImageFit(const Texture* image, bool center) +{ + if (!image) + return; + + if (GlobalContext) + ImGui::SetCurrentContext(GlobalContext); + + ImVec2 area = ImGui::GetContentRegionAvail(); + + float scale = area.x / image->width; + + float y = image->height * scale; + if (y > area.y) + { + scale = area.y / image->height; + } + + int sizeX = int(image->width * scale); + int sizeY = int(image->height * scale); + + if (center) + { + ImGui::SetCursorPosX(0); + ImGui::SetCursorPosX(area.x/2 - sizeX/2); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (area.y / 2 - sizeY / 2)); + } + + rlImGuiImageRect(image, sizeX, sizeY, Rectangle{ 0,0, float(image->width), float(image->height) }); +} + void rlImGuiImageRenderTexture(const RenderTexture* image) { if (!image) diff --git a/rlImGui.h b/rlImGui.h index 92d2e2c..f7029d9 100644 --- a/rlImGui.h +++ b/rlImGui.h @@ -159,6 +159,14 @@ RLIMGUIAPI void rlImGuiImageSizeV(const Texture* image, Vector2 size); /// The portion of the texture to draw as an image. Negative values for the width and height will flip the image RLIMGUIAPI void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect); +/// +/// Draws a texture as an image an ImGui Context +/// Fits the texture to the available content area +/// +/// The texture to draw +/// When true the image will be centered in the content area +RLIMGUIAPI void rlImGuiImageFit(const Texture* image, bool center); + /// /// Draws a render texture as an image an ImGui Context, automatically flipping the Y axis so it will show correctly on screen ///