From b9482da8755fc19fe09e342440c2ba4738d9d806 Mon Sep 17 00:00:00 2001 From: Vankata453 <78196474+Vankata453@users.noreply.github.com> Date: Tue, 16 Jul 2024 15:39:04 +0300 Subject: [PATCH] Add `ItemSlider::move_indicator(pos)` --- src/gui/item_slider.cpp | 20 +++++++++++++------- src/gui/item_slider.hpp | 3 +++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/gui/item_slider.cpp b/src/gui/item_slider.cpp index dd0d1d7c94a..c53c76ada41 100644 --- a/src/gui/item_slider.cpp +++ b/src/gui/item_slider.cpp @@ -87,7 +87,7 @@ ItemSlider::event(const SDL_Event& ev) const Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(ev.motion.x, ev.motion.y); if (mouse_pos.x >= m_slider_x && mouse_pos.x <= m_slider_x + SLIDER_WIDTH) { - *m_value = static_cast(((mouse_pos.x - m_slider_x) / SLIDER_WIDTH) * (m_max_value - m_min_value)) + m_min_value; + move_indicator(mouse_pos); m_sliding = true; MenuManager::instance().current_menu()->menu_action(*this); @@ -106,12 +106,7 @@ ItemSlider::event(const SDL_Event& ev) break; const Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(ev.motion.x, ev.motion.y); - if (mouse_pos.x <= m_slider_x) - *m_value = m_min_value; - else if (mouse_pos.x >= m_slider_x + SLIDER_WIDTH) - *m_value = m_max_value; - else - *m_value = static_cast(((mouse_pos.x - m_slider_x) / SLIDER_WIDTH) * (m_max_value - m_min_value)) + m_min_value; + move_indicator(mouse_pos); MenuManager::instance().current_menu()->menu_action(*this); break; @@ -147,4 +142,15 @@ ItemSlider::event(const SDL_Event& ev) } } +void +ItemSlider::move_indicator(const Vector& pos) +{ + if (pos.x <= m_slider_x) + *m_value = m_min_value; + else if (pos.x >= m_slider_x + SLIDER_WIDTH) + *m_value = m_max_value; + else + *m_value = static_cast(((pos.x - m_slider_x) / SLIDER_WIDTH) * static_cast(m_max_value - m_min_value)) + m_min_value; +} + /* EOF */ diff --git a/src/gui/item_slider.hpp b/src/gui/item_slider.hpp index 51329da147f..a85327e87fa 100644 --- a/src/gui/item_slider.hpp +++ b/src/gui/item_slider.hpp @@ -36,6 +36,9 @@ class ItemSlider final : public MenuItem bool changes_width() const override { return false; } bool locks_selection() const override { return m_sliding; } +private: + void move_indicator(const Vector& pos); + private: int m_min_value; int m_max_value;