From 14ceaaf4607f999a5a425dd906e13b4ba81678ad Mon Sep 17 00:00:00 2001 From: omar Date: Tue, 26 Jun 2018 12:37:10 +0200 Subject: [PATCH] ArrowButton: Fixed to honor PushButtonRepeat() setting (and internals' ImGuiItemFlags_ButtonRepeat). Setup current line text baseline so that ArrowButton() + SameLine() + Text() are aligned properly. --- CHANGELOG.txt | 9 +++++++++ imgui.cpp | 21 +++++++++++++++------ imgui_demo.cpp | 45 +++++++++++++++++++++++++++++---------------- imgui_internal.h | 1 + 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 39bcfa50..32e9bdfe 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -29,6 +29,15 @@ HOW TO UPDATE? - Please report any issue! +----------------------------------------------------------------------- + VERSION 1.63 WIP (IN PROGRESS) +----------------------------------------------------------------------- + +Other Changes: + - ArrowButton: Fixed to honor PushButtonRepeat() setting (and internals' ImGuiItemFlags_ButtonRepeat). + - ArrowButton: Setup current line text baseline so that ArrowButton() + SameLine() + Text() are aligned properly. + + ----------------------------------------------------------------------- VERSION 1.62 (Released 2018-06-22) ----------------------------------------------------------------------- diff --git a/imgui.cpp b/imgui.cpp index 51af4877..c33b53ae 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -8072,7 +8072,7 @@ bool ImGui::SmallButton(const char* label) return pressed; } -bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) +bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiButtonFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -8080,24 +8080,33 @@ bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) ImGuiContext& g = *GImGui; const ImGuiID id = window->GetID(str_id); - float sz = ImGui::GetFrameHeight(); - const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(sz, sz)); - ItemSize(bb); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + const float default_size = GetFrameHeight(); + ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); if (!ItemAdd(bb, id)) return false; + if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) + flags |= ImGuiButtonFlags_Repeat; + bool hovered, held; - bool pressed = ButtonBehavior(bb, id, &hovered, &held); + bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); // Render const ImU32 col = GetColorU32((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); RenderNavHighlight(bb, id); RenderFrame(bb.Min, bb.Max, col, true, g.Style.FrameRounding); - RenderArrow(bb.Min + g.Style.FramePadding, dir); + RenderArrow(bb.Min + ImVec2(ImMax(0.0f, size.x - g.FontSize - g.Style.FramePadding.x), ImMax(0.0f, size.y - g.FontSize - g.Style.FramePadding.y)), dir); return pressed; } +bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) +{ + float sz = GetFrameHeight(); + return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), 0); +} + // Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack. // Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id) bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index f8fc547d..a8c41dce 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -12,11 +12,11 @@ // Thank you, // -Your beloved friend, imgui_demo.cpp (that you won't delete) -// Message to beginner C/C++ programmers. About the meaning of 'static': in this demo code, we frequently we use 'static' variables inside functions. -// We do this as a way to gather code and data in the same place, just to make the demo code faster to read, faster to write, and use less code. +// Message to beginner C/C++ programmers about the meaning of the 'static' keyword: in this demo code, we frequently we use 'static' variables inside functions. // A static variable persist across calls, so it is essentially like a global variable but declared inside the scope of the function. +// We do this as a way to gather code and data in the same place, just to make the demo code faster to read, faster to write, and use less code. // It also happens to be a convenient way of storing simple UI related information as long as your function doesn't need to be reentrant or used in threads. -// This might be a pattern you occasionally want to use in your code, but most of the real data you would be editing is likely to be stored outside your function. +// This might be a pattern you occasionally want to use in your code, but most of the real data you would be editing is likely to be stored outside your functions. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS @@ -76,6 +76,7 @@ #if !defined(IMGUI_DISABLE_DEMO_WINDOWS) +// Forward Declarations static void ShowExampleAppConsole(bool* p_open); static void ShowExampleAppLog(bool* p_open); static void ShowExampleAppLayout(bool* p_open); @@ -89,6 +90,7 @@ static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleAppMainMenuBar(); static void ShowExampleMenuFile(); +// Helper to display a little (?) mark which shows a tooltip when hovered. static void ShowHelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); @@ -102,6 +104,7 @@ static void ShowHelpMarker(const char* desc) } } +// Helper to display basic user controls. void ImGui::ShowUserGuide() { ImGui::BulletText("Double-click on title bar to collapse window."); @@ -124,10 +127,11 @@ void ImGui::ShowUserGuide() ImGui::Unindent(); } -// Demonstrate most ImGui features (big function!) +// Demonstrate most Dear ImGui features (this is big function!) +// You may execute this function to experiment with the UI and understand what it does. You may then search for keywords in the code when you are interested by a specific feature. void ImGui::ShowDemoWindow(bool* p_open) { - // Examples apps + // Examples Apps (accessible from the "Examples" menu) static bool show_app_main_menu_bar = false; static bool show_app_console = false; static bool show_app_log = false; @@ -139,10 +143,6 @@ void ImGui::ShowDemoWindow(bool* p_open) static bool show_app_simple_overlay = false; static bool show_app_window_titles = false; static bool show_app_custom_rendering = false; - static bool show_app_style_editor = false; - - static bool show_app_metrics = false; - static bool show_app_about = false; if (show_app_main_menu_bar) ShowExampleAppMainMenuBar(); if (show_app_console) ShowExampleAppConsole(&show_app_console); @@ -156,6 +156,11 @@ void ImGui::ShowDemoWindow(bool* p_open) if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles); if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); + // Dear ImGui Apps (accessible from the "Help" menu) + static bool show_app_metrics = false; + static bool show_app_style_editor = false; + static bool show_app_about = false; + if (show_app_metrics) { ImGui::ShowMetricsWindow(&show_app_metrics); } if (show_app_style_editor) { ImGui::Begin("Style Editor", &show_app_style_editor); ImGui::ShowStyleEditor(); ImGui::End(); } if (show_app_about) @@ -168,6 +173,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::End(); } + // Demonstrate the various window flags. Typically you would just use the default! static bool no_titlebar = false; static bool no_scrollbar = false; static bool no_menu = false; @@ -177,7 +183,6 @@ void ImGui::ShowDemoWindow(bool* p_open) static bool no_close = false; static bool no_nav = false; - // Demonstrate the various window flags. Typically you would just use the default. ImGuiWindowFlags window_flags = 0; if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar; if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar; @@ -188,19 +193,22 @@ void ImGui::ShowDemoWindow(bool* p_open) if (no_nav) window_flags |= ImGuiWindowFlags_NoNav; if (no_close) p_open = NULL; // Don't pass our bool* to Begin + // We specify a default size in case there's no data in the .ini file. Typically this isn't required! We only do it to make the Demo applications a little more welcoming. ImGui::SetNextWindowSize(ImVec2(550,680), ImGuiCond_FirstUseEver); + + // Main body of the Demo window starts here. if (!ImGui::Begin("ImGui Demo", p_open, window_flags)) { // Early out if the window is collapsed, as an optimization. ImGui::End(); return; } - - //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); // 2/3 of the space for widget and 1/3 for labels - ImGui::PushItemWidth(-140); // Right align, keep 140 pixels for labels - ImGui::Text("dear imgui says hello. (%s)", IMGUI_VERSION); + // Most "big" widgets share a common width settings by default. + //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); // Use 2/3 of the space for widgets and 1/3 for labels (default) + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // Use fixed width for labels (by passing a negative value), the rest goes to widgets. We choose a width proportional to our font size. + // Menu if (ImGui::BeginMenuBar()) { @@ -302,10 +310,15 @@ void ImGui::ShowDemoWindow(bool* p_open) } // Arrow buttons + static int counter = 0; float spacing = ImGui::GetStyle().ItemInnerSpacing.x; - if (ImGui::ArrowButton("##left", ImGuiDir_Left)) {} + ImGui::PushButtonRepeat(true); + if (ImGui::ArrowButton("##left", ImGuiDir_Left)) { counter--; } ImGui::SameLine(0.0f, spacing); - if (ImGui::ArrowButton("##left", ImGuiDir_Right)) {} + if (ImGui::ArrowButton("##right", ImGuiDir_Right)) { counter++; } + ImGui::PopButtonRepeat(); + ImGui::SameLine(); + ImGui::Text("%d", counter); ImGui::Text("Hover over me"); if (ImGui::IsItemHovered()) diff --git a/imgui_internal.h b/imgui_internal.h index ec04672a..db4429e9 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1154,6 +1154,7 @@ namespace ImGui IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0); IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0); IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos, float radius); + IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags); IMGUI_API bool DragBehavior(ImGuiID id, ImGuiDataType data_type, void* v, float v_speed, const void* v_min, const void* v_max, const char* format, float power); IMGUI_API bool SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format, float power, ImGuiSliderFlags flags = 0);