From 48f1df2fd63ea7ec600a75d7ee224f887b847de4 Mon Sep 17 00:00:00 2001 From: Bruno Herbelin Date: Sun, 29 Jan 2023 10:54:57 +0100 Subject: [PATCH] ImGuiToolkit to render a Disabled Button --- src/ImGuiToolkit.cpp | 27 +++++++++++++++++++++++++++ src/ImGuiToolkit.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/ImGuiToolkit.cpp b/src/ImGuiToolkit.cpp index dedb542..77318ee 100644 --- a/src/ImGuiToolkit.cpp +++ b/src/ImGuiToolkit.cpp @@ -72,6 +72,33 @@ bool ImGuiToolkit::ButtonToggle( const char* label, bool* toggle, const char *to return action; } +void ImGuiToolkit::ButtonDisabled(const char* label, const ImVec2 &size_arg) +{ + ImGuiWindow* window = ImGui::GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true); + + ImVec2 pos = window->DC.CursorPos; + ImVec2 size = ImGui::CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f); + + const ImRect bb(pos, pos + size); + ImGui::ItemSize(size, style.FramePadding.y); + if (!ImGui::ItemAdd(bb, id)) + return; + + // Render + const ImU32 col = ImGui::GetColorU32(ImGuiCol_FrameBg); + ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); + + ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetColorU32(ImGuiCol_TextDisabled)); + ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, NULL, &label_size, style.ButtonTextAlign, &bb); + ImGui::PopStyleColor(1); +} bool ImGuiToolkit::ButtonSwitch(const char* label, bool* toggle, const char* shortcut) { diff --git a/src/ImGuiToolkit.h b/src/ImGuiToolkit.h index a840921..93f4546 100644 --- a/src/ImGuiToolkit.h +++ b/src/ImGuiToolkit.h @@ -35,6 +35,7 @@ namespace ImGuiToolkit bool ButtonToggle (const char* label, bool* toggle, const char *tooltip = nullptr); bool ButtonSwitch (const char* label, bool* toggle, const char *shortcut = nullptr); void ButtonOpenUrl (const char* label, const char* url, const ImVec2& size_arg = ImVec2(0,0)); + void ButtonDisabled(const char* label, const ImVec2& size_arg = ImVec2(0,0)); // tooltip and mouse over help void setToolTipsEnabled (bool on);