Sliders react to mouse

This commit is contained in:
Kacper 2015-03-07 20:56:44 +01:00
parent bc0d7a5ef7
commit 1ddc4afc14
3 changed files with 62 additions and 0 deletions

View File

@ -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())

View File

@ -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;
}
// -----------------------------------------------------------------------------

View File

@ -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<irr::core::stringw> m_labels;
@ -92,6 +93,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 */
virtual int getWidthNeededAroundLabel() const { return 25; }
@ -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);
};
}