From d3fd2630b77967e326b3a1e8759087e19788bd21 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 20 Jun 2022 18:06:34 +0200 Subject: [PATCH] Sliders: An initial click within the knob/grab doesn't shift its position. (#1946, #5328) + Adjust default GrabMinSize. --- docs/CHANGELOG.txt | 2 ++ imgui.cpp | 2 +- imgui.h | 2 +- imgui_internal.h | 2 ++ imgui_widgets.cpp | 11 ++++++++++- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index f6674d8ae..27e8022eb 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -78,6 +78,7 @@ Other Changes: - Layout: Fixed mixing up SameLine() and SetCursorPos() together from creating situations where line height would be emitted from the wrong location (e.g. 'ItemA+SameLine()+SetCursorPos()+ItemB' would emit ItemA worth of height from the position of ItemB, which is not necessarily aligned with ItemA). +- Sliders: An initial click within the knob/grab doesn't shift its position. (#1946, #5328) - Sliders, Drags: Fixed dragging when using hexadecimal display format string. (#5165, #3133) - Sliders, Drags: Fixed manual input when using hexadecimal display format string. (#5165, #3133) - InputScalar: Fixed manual input when using %03d style width in display format string. (#5165, #3133) @@ -109,6 +110,7 @@ Other Changes: level of a popup with a child menu opened. - Menus: Menus emitted from the main/scrolling layer are not part of the same menuset as menus emitted from the menu-bar, avoiding accidental hovering from one to the other. (#3496, #4797) [@rokups] +- Style: Adjust default value of GrabMinSize from 10.0f to 12.0f. - Stack Tool: Added option to copy item path to clipboard. (#4631) - Settings: Fixed out-of-bounds read when .ini file on disk is empty. (#5351) [@quantum5] - Settings: Fixed some SetNextWindowPos/SetNextWindowSize API calls not marking settings as dirty. diff --git a/imgui.cpp b/imgui.cpp index ceb589d20..443373605 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1062,7 +1062,7 @@ ImGuiStyle::ImGuiStyle() ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar - GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar + GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. diff --git a/imgui.h b/imgui.h index 0265f833f..c923f1564 100644 --- a/imgui.h +++ b/imgui.h @@ -65,7 +65,7 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) #define IMGUI_VERSION "1.88 WIP" -#define IMGUI_VERSION_NUM 18731 +#define IMGUI_VERSION_NUM 18732 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_HAS_TABLE diff --git a/imgui_internal.h b/imgui_internal.h index 555022cf8..26d1a8f46 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1773,6 +1773,7 @@ struct ImGuiContext ImU32 ColorEditLastColor; // RGB value with alpha set to 0. ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. ImGuiComboPreviewData ComboPreviewData; + float SliderGrabClickOffset; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? bool DragCurrentAccumDirty; @@ -1944,6 +1945,7 @@ struct ImGuiContext ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; ColorEditLastHue = ColorEditLastSat = 0.0f; ColorEditLastColor = 0; + SliderGrabClickOffset = 0.0f; SliderCurrentAccum = 0.0f; SliderCurrentAccumDirty = false; DragCurrentAccumDirty = false; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index d552bec4f..d07df3abb 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -2760,8 +2760,17 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ else { const float mouse_abs_pos = g.IO.MousePos[axis]; + if (g.ActiveIdIsJustActivated) + { + float grab_t = ScaleRatioFromValueT(data_type, *v, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + if (axis == ImGuiAxis_Y) + grab_t = 1.0f - grab_t; + const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t); + const bool clicked_around_grab = (mouse_abs_pos >= grab_pos - grab_sz * 0.5f - 1.0f) && (mouse_abs_pos <= grab_pos + grab_sz * 0.5f + 1.0f); // No harm being extra generous here. + g.SliderGrabClickOffset = (clicked_around_grab && is_floating_point) ? mouse_abs_pos - grab_pos : 0.0f; + } if (slider_usable_sz > 0.0f) - clicked_t = ImSaturate((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz); + clicked_t = ImSaturate((mouse_abs_pos - g.SliderGrabClickOffset - slider_usable_pos_min) / slider_usable_sz); if (axis == ImGuiAxis_Y) clicked_t = 1.0f - clicked_t; set_new_value = true;