Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
31 changes: 31 additions & 0 deletions rlImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions rlImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ RLIMGUIAPI void rlImGuiImageSizeV(const Texture* image, Vector2 size);
/// <param name="sourceRect">The portion of the texture to draw as an image. Negative values for the width and height will flip the image</param>
RLIMGUIAPI void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Rectangle sourceRect);

/// <summary>
/// Draws a texture as an image an ImGui Context
/// Fits the texture to the available content area
/// </summary>
/// <param name="image">The texture to draw</param>
/// <param name="center">When true the image will be centered in the content area</param>
RLIMGUIAPI void rlImGuiImageFit(const Texture* image, bool center);

/// <summary>
/// Draws a render texture as an image an ImGui Context, automatically flipping the Y axis so it will show correctly on screen
/// </summary>
Expand Down