Skip to content

Commit

Permalink
Add ItemSlider::move_indicator(pos)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vankata453 committed Jul 16, 2024
1 parent 74eb520 commit b9482da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/gui/item_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(((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);
Expand All @@ -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<int>(((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;
Expand Down Expand Up @@ -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<int>(((pos.x - m_slider_x) / SLIDER_WIDTH) * static_cast<float>(m_max_value - m_min_value)) + m_min_value;
}

/* EOF */
3 changes: 3 additions & 0 deletions src/gui/item_slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b9482da

Please sign in to comment.