From 1ddc4afc14d371fe6fa7e1147b0f40ca9fd9d12d Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 7 Mar 2015 20:56:44 +0100 Subject: [PATCH 1/4] Sliders react to mouse --- src/guiengine/skin.cpp | 14 +++++++++ src/guiengine/widgets/spinner_widget.cpp | 40 ++++++++++++++++++++++++ src/guiengine/widgets/spinner_widget.hpp | 8 +++++ 3 files changed, 62 insertions(+) diff --git a/src/guiengine/skin.cpp b/src/guiengine/skin.cpp index f948ea28d..7834f6b46 100644 --- a/src/guiengine/skin.cpp +++ b/src/guiengine/skin.cpp @@ -1378,6 +1378,20 @@ void Skin::drawSpinnerBody(const core::recti &rect, Widget* widget, 0 /* no clipping */, 0, true /* alpha */); + // ---- Set value by mouse position + const float max_value = (float)(w->getMax() - w->getMin()) / + (w->getMax() - w->getMin()); + + const core::recti mouse_area(rect.UpperLeftCorner.X + handle_size, + rect.UpperLeftCorner.Y, + rect.UpperLeftCorner.X + handle_size + + (int)((widget->m_w + - 2*handle_size)*max_value), + rect.UpperLeftCorner.Y + widget->m_h); + + const core::position2di mouse_position + = irr_driver->getDevice()->getCursorControl()->getPosition(); + q->setValuesByMouse(mouse_position, mouse_area); } if (focused && widget->hasTooltip()) diff --git a/src/guiengine/widgets/spinner_widget.cpp b/src/guiengine/widgets/spinner_widget.cpp index 1a740830f..bbb21522d 100644 --- a/src/guiengine/widgets/spinner_widget.cpp +++ b/src/guiengine/widgets/spinner_widget.cpp @@ -46,6 +46,7 @@ SpinnerWidget::SpinnerWidget(const bool gauge) : Widget(WTYPE_SPINNER) m_supports_multiplayer = true; m_value = -1; m_use_background_color=false; + m_allow_mouse_change = false; m_spinner_widget_player_id=-1; m_min = 0; m_max = 999; @@ -432,3 +433,42 @@ void SpinnerWidget::setCustomText(const core::stringw& text) } } +// ----------------------------------------------------------------------------- + +void SpinnerWidget::setValuesByMouse(const core::position2di & mouse_position, + const core::recti & body_rect) +{ + if(body_rect.isPointInside(mouse_position)) + { + if (m_allow_mouse_change) + { + float exact_hover = (float)((mouse_position.X - + body_rect.UpperLeftCorner.X) / + (float)body_rect.getWidth()) * (m_max-m_min) + 0.5f; + + int new_value = ((exact_hover * (m_max-m_min)) / + (m_max-m_min))+m_min; + //Log::info("SpinnerWidget", "mouse_value: %d (%f)", + // mouse_value, exact_hover); + + if (new_value > m_max) new_value = m_max; + if (new_value < m_min) new_value = m_min; + + setValue(new_value); + EventHandler::get()->processGUIAction( + PlayerAction::PA_MENU_SELECT, + 1, Input::MAX_VALUE, + Input::IT_MOUSEBUTTON, false); //process the changes + m_allow_mouse_change = false; + } + } +} + +// ----------------------------------------------------------------------------- + +void SpinnerWidget::onClick() +{ + m_allow_mouse_change = true; +} + +// ----------------------------------------------------------------------------- diff --git a/src/guiengine/widgets/spinner_widget.hpp b/src/guiengine/widgets/spinner_widget.hpp index 02a908788..12bd61bae 100644 --- a/src/guiengine/widgets/spinner_widget.hpp +++ b/src/guiengine/widgets/spinner_widget.hpp @@ -59,6 +59,7 @@ namespace GUIEngine int m_spinner_widget_player_id; bool m_use_background_color; + bool m_allow_mouse_change; /** If each value the spinner can take has an associated text, this vector will be non-empty */ std::vector m_labels; @@ -91,6 +92,9 @@ namespace GUIEngine /** \brief implementing method from base class Widget */ virtual EventPropagation leftPressed(const int playerID); + + /** \brief implementing method from base class Widget */ + virtual void onClick(); /** When inferring widget size from its label length, this method will be called to * if/how much space must be added to the raw label's size for the widget to be large enough */ @@ -197,6 +201,10 @@ namespace GUIEngine /** Display custom text in spinner */ void setCustomText(const core::stringw& text); + + /** Set m_value based on mouse position */ + void setValuesByMouse(const core::position2di & mouse_position, + const core::recti & body_rect); }; } From acd1628528e2d080221bef60ace1af59f0d1568f Mon Sep 17 00:00:00 2001 From: Kacper Date: Mon, 9 Mar 2015 19:22:10 +0100 Subject: [PATCH 2/4] Moved slider's code to onClick() --- src/guiengine/skin.cpp | 14 ------ src/guiengine/widgets/spinner_widget.cpp | 61 +++++++++++++----------- src/guiengine/widgets/spinner_widget.hpp | 5 -- 3 files changed, 33 insertions(+), 47 deletions(-) diff --git a/src/guiengine/skin.cpp b/src/guiengine/skin.cpp index 7834f6b46..f948ea28d 100644 --- a/src/guiengine/skin.cpp +++ b/src/guiengine/skin.cpp @@ -1378,20 +1378,6 @@ void Skin::drawSpinnerBody(const core::recti &rect, Widget* widget, 0 /* no clipping */, 0, true /* alpha */); - // ---- Set value by mouse position - const float max_value = (float)(w->getMax() - w->getMin()) / - (w->getMax() - w->getMin()); - - const core::recti mouse_area(rect.UpperLeftCorner.X + handle_size, - rect.UpperLeftCorner.Y, - rect.UpperLeftCorner.X + handle_size + - (int)((widget->m_w - - 2*handle_size)*max_value), - rect.UpperLeftCorner.Y + widget->m_h); - - const core::position2di mouse_position - = irr_driver->getDevice()->getCursorControl()->getPosition(); - q->setValuesByMouse(mouse_position, mouse_area); } if (focused && widget->hasTooltip()) diff --git a/src/guiengine/widgets/spinner_widget.cpp b/src/guiengine/widgets/spinner_widget.cpp index bbb21522d..9b9670d18 100644 --- a/src/guiengine/widgets/spinner_widget.cpp +++ b/src/guiengine/widgets/spinner_widget.cpp @@ -46,7 +46,6 @@ SpinnerWidget::SpinnerWidget(const bool gauge) : Widget(WTYPE_SPINNER) m_supports_multiplayer = true; m_value = -1; m_use_background_color=false; - m_allow_mouse_change = false; m_spinner_widget_player_id=-1; m_min = 0; m_max = 999; @@ -435,40 +434,46 @@ void SpinnerWidget::setCustomText(const core::stringw& text) // ----------------------------------------------------------------------------- -void SpinnerWidget::setValuesByMouse(const core::position2di & mouse_position, - const core::recti & body_rect) +void SpinnerWidget::onClick() { + int x = m_x; + int y = m_y; + + if (m_parent != NULL) + { + x += m_parent->getAbsolutePosition().UpperLeftCorner.X; + y += m_parent->getAbsolutePosition().UpperLeftCorner.Y; + } + + if (m_children[1].m_deactivated) return; + + if (m_children[1].m_properties[PROP_ID] != "spinnerbody" + || !isGauge()) { return; } + + const core::position2di mouse_position + = irr_driver->getDevice()->getCursorControl()->getPosition(); + + const int handle_size = m_h*110/128; //need params->m_left_border and params->getImage()->getSize().Height + + rect body_rect = rect(x+handle_size, + y, + x+handle_size + (m_w-2*handle_size), + y+handle_size); + if(body_rect.isPointInside(mouse_position)) { - if (m_allow_mouse_change) - { - float exact_hover = (float)((mouse_position.X - - body_rect.UpperLeftCorner.X) / - (float)body_rect.getWidth()) * (m_max-m_min) + 0.5f; + float exact_hover = (float)((mouse_position.X - + body_rect.UpperLeftCorner.X) / + (float)body_rect.getWidth()) * (m_max-m_min) + 0.5f; - int new_value = ((exact_hover * (m_max-m_min)) / - (m_max-m_min))+m_min; - //Log::info("SpinnerWidget", "mouse_value: %d (%f)", - // mouse_value, exact_hover); + int new_value = ((exact_hover * (m_max-m_min)) / + (m_max-m_min))+m_min; - if (new_value > m_max) new_value = m_max; - if (new_value < m_min) new_value = m_min; + if (new_value > m_max) new_value = m_max; + if (new_value < m_min) new_value = m_min; - setValue(new_value); - EventHandler::get()->processGUIAction( - PlayerAction::PA_MENU_SELECT, - 1, Input::MAX_VALUE, - Input::IT_MOUSEBUTTON, false); //process the changes - m_allow_mouse_change = false; - } + setValue(new_value); } } // ----------------------------------------------------------------------------- - -void SpinnerWidget::onClick() -{ - m_allow_mouse_change = true; -} - -// ----------------------------------------------------------------------------- diff --git a/src/guiengine/widgets/spinner_widget.hpp b/src/guiengine/widgets/spinner_widget.hpp index 12bd61bae..2347ab3d0 100644 --- a/src/guiengine/widgets/spinner_widget.hpp +++ b/src/guiengine/widgets/spinner_widget.hpp @@ -59,7 +59,6 @@ namespace GUIEngine int m_spinner_widget_player_id; bool m_use_background_color; - bool m_allow_mouse_change; /** If each value the spinner can take has an associated text, this vector will be non-empty */ std::vector m_labels; @@ -201,10 +200,6 @@ namespace GUIEngine /** Display custom text in spinner */ void setCustomText(const core::stringw& text); - - /** Set m_value based on mouse position */ - void setValuesByMouse(const core::position2di & mouse_position, - const core::recti & body_rect); }; } From 17f7f84597eb887408f7a5abb64a04c9ee5cb6c2 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 10 Mar 2015 16:52:07 +0100 Subject: [PATCH 3/4] Replaced hardcoded values --- src/guiengine/widgets/spinner_widget.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/guiengine/widgets/spinner_widget.cpp b/src/guiengine/widgets/spinner_widget.cpp index 9b9670d18..6423df23f 100644 --- a/src/guiengine/widgets/spinner_widget.cpp +++ b/src/guiengine/widgets/spinner_widget.cpp @@ -445,20 +445,18 @@ void SpinnerWidget::onClick() y += m_parent->getAbsolutePosition().UpperLeftCorner.Y; } - if (m_children[1].m_deactivated) return; - - if (m_children[1].m_properties[PROP_ID] != "spinnerbody" - || !isGauge()) { return; } + if (m_children[1].m_deactivated || + m_children[1].m_properties[PROP_ID] != "spinnerbody" || + !isGauge()) + { + return; + } const core::position2di mouse_position = irr_driver->getDevice()->getCursorControl()->getPosition(); - const int handle_size = m_h*110/128; //need params->m_left_border and params->getImage()->getSize().Height - - rect body_rect = rect(x+handle_size, - y, - x+handle_size + (m_w-2*handle_size), - y+handle_size); + core::recti body_rect + = m_children[1].getIrrlichtElement()->getAbsolutePosition(); if(body_rect.isPointInside(mouse_position)) { From a3105509047a7ec202e9128fe5a16f0707e5e5fe Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Tue, 10 Mar 2015 19:42:56 -0400 Subject: [PATCH 4/4] Small tweaks to pull request, remove unused code, use roundf instead of adding +0.5 --- src/guiengine/widgets/spinner_widget.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/guiengine/widgets/spinner_widget.cpp b/src/guiengine/widgets/spinner_widget.cpp index 6423df23f..568bd5958 100644 --- a/src/guiengine/widgets/spinner_widget.cpp +++ b/src/guiengine/widgets/spinner_widget.cpp @@ -436,15 +436,6 @@ void SpinnerWidget::setCustomText(const core::stringw& text) void SpinnerWidget::onClick() { - int x = m_x; - int y = m_y; - - if (m_parent != NULL) - { - x += m_parent->getAbsolutePosition().UpperLeftCorner.X; - y += m_parent->getAbsolutePosition().UpperLeftCorner.Y; - } - if (m_children[1].m_deactivated || m_children[1].m_properties[PROP_ID] != "spinnerbody" || !isGauge()) @@ -458,14 +449,15 @@ void SpinnerWidget::onClick() core::recti body_rect = m_children[1].getIrrlichtElement()->getAbsolutePosition(); - if(body_rect.isPointInside(mouse_position)) + if (body_rect.isPointInside(mouse_position)) { float exact_hover = (float)((mouse_position.X - body_rect.UpperLeftCorner.X) / - (float)body_rect.getWidth()) * (m_max-m_min) + 0.5f; + (float)body_rect.getWidth()) * (m_max-m_min); - int new_value = ((exact_hover * (m_max-m_min)) / - (m_max-m_min))+m_min; + float new_value_f = ((exact_hover * (m_max - m_min)) / + (m_max - m_min)) + m_min; + int new_value = (int)roundf(new_value_f); if (new_value > m_max) new_value = m_max; if (new_value < m_min) new_value = m_min;