From d2d505d9e381dbe5e51a7a59cc25dfe1168df5b0 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Mon, 18 Sep 2023 17:55:47 +0100 Subject: [PATCH] ext/imgui: Add function to draw image with background color --- .../imgui/imgui_monado/cimgui_monado.h | 5 +++ .../imgui/imgui_monado/imgui_monado.cpp | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/external/imgui/imgui_monado/cimgui_monado.h b/src/external/imgui/imgui_monado/cimgui_monado.h index 55e711af5..d51cf4192 100644 --- a/src/external/imgui/imgui_monado/cimgui_monado.h +++ b/src/external/imgui/imgui_monado/cimgui_monado.h @@ -22,6 +22,11 @@ void igPlotTimings(const char *label, void igToggleButton(const char *str_id, bool *v); +void igImageBg(ImTextureID user_texture_id, + const ImVec2 size, + const ImVec2 uv0, const ImVec2 uv1, + const ImVec4 tint_col, const ImVec4 border_col, const ImVec4 bg_col); + #ifdef __cplusplus } #endif diff --git a/src/external/imgui/imgui_monado/imgui_monado.cpp b/src/external/imgui/imgui_monado/imgui_monado.cpp index 790f91ada..0c41213dc 100644 --- a/src/external/imgui/imgui_monado/imgui_monado.cpp +++ b/src/external/imgui/imgui_monado/imgui_monado.cpp @@ -280,3 +280,43 @@ void igToggleButton(const char *str_id, bool *v) { ImVec2(p.x + radius + t * (width - radius * 2.0f), p.y + radius), radius - 1.5f, IM_COL32(255, 255, 255, 255)); } + +extern "C" +void igImageBg(ImTextureID user_texture_id, + const ImVec2 size, + const ImVec2 uv0, const ImVec2 uv1, + const ImVec4 tint_col, const ImVec4 border_col, const ImVec4 bg_col) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) { + return; + } + + ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + auto bbImg = bb; + if (border_col.w > 0.0f) { + bb.Max += ImVec2(2, 2); + + // Move the image pixel down and right, to be inside of the frame. + bbImg.Min += ImVec2(1, 1); + bbImg.Max += ImVec2(1, 1); + } + + ItemSize(bb); + if (!ItemAdd(bb, 0)) { + return; + } + + // Do we have a border? + if (border_col.w > 0.0f) { + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f); + } + + // Should we clear the background? + if (bg_col.w > 0.0f) { + window->DrawList->AddRectFilled(bbImg.Min, bbImg.Max, GetColorU32(bg_col)); + } + + // Finally the image. + window->DrawList->AddImage(user_texture_id, bbImg.Min, bbImg.Max, uv0, uv1, GetColorU32(tint_col)); +}