From 10c0f7f255b72b9769d847eb82b9b0c41a7ce8af Mon Sep 17 00:00:00 2001 From: YuSanka Date: Thu, 18 Jul 2024 14:05:22 +0200 Subject: [PATCH] DoubleSlider: Fixed a click on hovered tick, when object is height enough. (layer step is less then 2px) SPE-2421 --- src/slic3r/GUI/DoubleSliderForLayers.cpp | 9 ++++++++- src/slic3r/GUI/ImGuiDoubleSlider.cpp | 25 +++++++++++++++++++++++- src/slic3r/GUI/ImGuiDoubleSlider.hpp | 6 ++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/slic3r/GUI/DoubleSliderForLayers.cpp b/src/slic3r/GUI/DoubleSliderForLayers.cpp index 158629be0c..ff2ff6c119 100644 --- a/src/slic3r/GUI/DoubleSliderForLayers.cpp +++ b/src/slic3r/GUI/DoubleSliderForLayers.cpp @@ -207,6 +207,7 @@ void DSForLayers::draw_ticks(const ImRect& slideable_region) }; std::set::const_iterator tick_it = m_ticks.ticks.begin(); + bool is_hovered_tick = false; while (tick_it != m_ticks.ticks.end()) { float tick_pos = get_tick_pos(tick_it->tick); @@ -221,11 +222,17 @@ void DSForLayers::draw_ticks(const ImRect& slideable_region) m_focus = fiTick; ImGuiPureWrap::tooltip(get_tooltip(tick_it->tick), ImGui::GetFontSize() * 20.f); } + is_hovered_tick = true; + m_ctrl.SetHoveredRegion(tick_hover_box); + if (m_ctrl.IsLClickOnHoveredPos()) + m_ctrl.IsActiveHigherThumb() ? SetHigherPos(tick_it->tick) : SetLowerPos(tick_it->tick); break; } ++tick_it; } - + if (!is_hovered_tick) + m_ctrl.InvalidateHoveredRegion(); + auto active_tick_it = m_ticks.ticks.find(TickCode{ m_ctrl.GetActivePos() }); tick_it = m_ticks.ticks.begin(); diff --git a/src/slic3r/GUI/ImGuiDoubleSlider.cpp b/src/slic3r/GUI/ImGuiDoubleSlider.cpp index 50201cad2f..10fa392531 100644 --- a/src/slic3r/GUI/ImGuiDoubleSlider.cpp +++ b/src/slic3r/GUI/ImGuiDoubleSlider.cpp @@ -316,6 +316,16 @@ bool ImGuiControl::IsLClickOnThumb() return false; } +bool ImGuiControl::IsLClickOnHoveredPos() +{ + if (m_lclick_on_hovered_pos) { + // Discard left mouse click at hovered tick to avoud reuse it on next frame + m_lclick_on_hovered_pos = false; + return true; + } + return false; +} + void ImGuiControl::draw_scroll_line(const ImRect& scroll_line, const ImRect& slideable_region) { if (m_cb_draw_scroll_line) @@ -542,8 +552,8 @@ bool ImGuiControl::draw_slider( int* higher_pos, int* lower_pos, ImGui::ItemHoverable(m_regions.lower_thumb, id) && context.IO.MouseClicked[0]) m_selection = ssLower; - // detect left click on selected thumb { + // detect left click on selected thumb const ImRect& active_thumb = m_selection == ssHigher ? m_regions.higher_thumb : m_regions.lower_thumb; if (ImGui::ItemHoverable(active_thumb, id) && context.IO.MouseClicked[0]) { m_active_thumb = active_thumb; @@ -561,6 +571,19 @@ bool ImGuiControl::draw_slider( int* higher_pos, int* lower_pos, // invalidate active thumb clicking m_active_thumb = ImRect(0.f, 0.f, 0.f, 0.f); } + + // detect left click on hovered region + if (ImGui::ItemHoverable(m_hovered_region, id) && context.IO.MouseClicked[0]) { + // clear active ID to avoid a process of behavior() + if (context.ActiveId == id && context.ActiveIdSource == ImGuiInputSource_Mouse) + ImGui::ClearActiveID(); + } + else if (ImGui::ItemHoverable(m_hovered_region, id) && context.IO.MouseReleased[0]) { + const ImRect& slideable_region = m_selection == ssHigher ? m_regions.higher_slideable_region : m_regions.lower_slideable_region; + if (lclicked_on_thumb(id, slideable_region, m_min_pos, m_max_pos, m_hovered_region, m_flags)) { + m_lclick_on_hovered_pos = true; + } + } } // update thumb position diff --git a/src/slic3r/GUI/ImGuiDoubleSlider.hpp b/src/slic3r/GUI/ImGuiDoubleSlider.hpp index e747139c99..53b6951dae 100644 --- a/src/slic3r/GUI/ImGuiDoubleSlider.hpp +++ b/src/slic3r/GUI/ImGuiDoubleSlider.hpp @@ -90,6 +90,7 @@ public: bool IsRClickOnThumb() const { return m_rclick_on_selected_thumb; } bool IsLClickOnThumb(); + bool IsLClickOnHoveredPos(); bool is_horizontal() const { return !(m_flags & ImGuiSliderFlags_Vertical); } bool render(); @@ -98,6 +99,9 @@ public: float rounding() const { return m_draw_opts.rounding(); } ImVec2 left_dummy_sz() const { return m_draw_opts.text_dummy_sz() + m_draw_opts.text_padding(); } + void SetHoveredRegion(ImRect region) { m_hovered_region = region; } + void InvalidateHoveredRegion() { m_hovered_region = ImRect(0.f, 0.f, 0.f, 0.f); } + void set_get_label_on_move_cb(std::function cb) { m_cb_get_label_on_move = cb; } void set_get_label_cb(std::function cb) { m_cb_get_label = cb; } void set_draw_scroll_line_cb(std::function cb) { m_cb_draw_scroll_line = cb; } @@ -148,8 +152,10 @@ private: bool m_rclick_on_selected_thumb{ false }; bool m_lclick_on_selected_thumb{ false }; + bool m_lclick_on_hovered_pos { false }; bool m_suppress_process_behavior{ false }; ImRect m_active_thumb; + ImRect m_hovered_region; bool m_draw_lower_thumb{ true }; bool m_combine_thumbs { false };