From cd8c83532b0d33a3a2e24d9af04dac0455a84625 Mon Sep 17 00:00:00 2001 From: David Mikos Date: Sun, 22 Dec 2013 00:16:27 +1030 Subject: [PATCH] Make it so that sliders can be mouse wheel scrolled. --- changelog | 1 + players_changelog | 1 + src/mouse_events.cpp | 32 ++++++++++++++++++++++++++++++++ src/mouse_events.hpp | 4 ++++ src/mouse_handler_base.cpp | 25 +++++++++++++++++++++++++ src/mouse_handler_base.hpp | 20 ++++++++++++++++++++ src/widgets/slider.cpp | 29 +++++++++++++++++++++++++++-- 7 files changed, 110 insertions(+), 2 deletions(-) diff --git a/changelog b/changelog index 256e8953d44..7ae16230363 100644 --- a/changelog +++ b/changelog @@ -84,6 +84,7 @@ Version 1.11.7+dev: * Introduced side's name in MP Connect. * Middle click scrolling is now based on distance from initial click instead of the centre of the screen. + * Make sliders able to be scrolled with the mouse wheel * WML engine: * WML variable turn_number is set correctly (to 1) in prestart and start events. Previously, it retained its last value from the previous scenario diff --git a/players_changelog b/players_changelog index 276640a5ecf..91a123852f9 100644 --- a/players_changelog +++ b/players_changelog @@ -57,6 +57,7 @@ Version 1.11.7+dev: * Introduced side's name in MP Connect. * Middle click scrolling is now based on distance from initial click instead of the centre of the screen. + * Make sliders able to be scrolled with the mouse wheel Version 1.11.7: diff --git a/src/mouse_events.cpp b/src/mouse_events.cpp index b9791bc4d4f..be9f93e2213 100644 --- a/src/mouse_events.cpp +++ b/src/mouse_events.cpp @@ -459,6 +459,38 @@ void mouse_handler::left_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/) resources::controller->set_button_state(*gui_); } +void mouse_handler::mouse_wheel_up(int /*x*/, int /*y*/, const bool /*browse*/) +{ + gui::slider* s = gui_->find_slider("map-zoom-slider"); + if (s && s->value_change()) + if (gui_->set_zoom(s->value(), true)) + resources::controller->set_button_state(*gui_); +} + +void mouse_handler::mouse_wheel_down(int /*x*/, int /*y*/, const bool /*browse*/) +{ + gui::slider* s = gui_->find_slider("map-zoom-slider"); + if (s && s->value_change()) + if (gui_->set_zoom(s->value(), true)) + resources::controller->set_button_state(*gui_); +} + +void mouse_handler::mouse_wheel_left(int /*x*/, int /*y*/, const bool /*browse*/) +{ + gui::slider* s = gui_->find_slider("map-zoom-slider"); + if (s && s->value_change()) + if (gui_->set_zoom(s->value(), true)) + resources::controller->set_button_state(*gui_); +} + +void mouse_handler::mouse_wheel_right(int /*x*/, int /*y*/, const bool /*browse*/) +{ + gui::slider* s = gui_->find_slider("map-zoom-slider"); + if (s && s->value_change()) + if (gui_->set_zoom(s->value(), true)) + resources::controller->set_button_state(*gui_); +} + void mouse_handler::select_or_action() { if (!resources::game_map->on_board(last_hex_)) diff --git a/src/mouse_events.hpp b/src/mouse_events.hpp index 0184c17004c..e14feb120f3 100644 --- a/src/mouse_events.hpp +++ b/src/mouse_events.hpp @@ -78,6 +78,10 @@ public: void select_or_action(); void left_mouse_up(int x, int y, const bool /*browse*/); + void mouse_wheel_up(int x, int y, const bool /*browse*/); + void mouse_wheel_down(int x, int y, const bool /*browse*/); + void mouse_wheel_left(int x, int y, const bool /*browse*/); + void mouse_wheel_right(int x, int y, const bool /*browse*/); protected: /** diff --git a/src/mouse_handler_base.cpp b/src/mouse_handler_base.cpp index ecac2c68f79..5e20f3d67e6 100644 --- a/src/mouse_handler_base.cpp +++ b/src/mouse_handler_base.cpp @@ -181,12 +181,21 @@ void mouse_handler_base::mouse_press(const SDL_MouseButtonEvent& event, const bo } else if (allow_mouse_wheel_scroll(event.x, event.y)) { if (event.button == SDL_BUTTON_WHEELUP) { scrolly = - preferences::scroll_speed(); + mouse_wheel_up(event.x, event.y, browse); } else if (event.button == SDL_BUTTON_WHEELDOWN) { scrolly = preferences::scroll_speed(); + mouse_wheel_down(event.x, event.y, browse); } else if (event.button == SDL_BUTTON_WHEELLEFT) { scrollx = - preferences::scroll_speed(); + mouse_wheel_left(event.x, event.y, browse); } else if (event.button == SDL_BUTTON_WHEELRIGHT) { scrollx = preferences::scroll_speed(); + mouse_wheel_right(event.x, event.y, browse); + } + // Don't scroll map and map zoom slider at same time + gui::slider* s = gui().find_slider("map-zoom-slider"); + if (point_in_rect(event.x, event.y, s->location())) { + scrollx = 0; scrolly = 0; } } @@ -260,6 +269,22 @@ void mouse_handler_base::left_mouse_up(int /*x*/, int /*y*/, const bool /*browse { } +void mouse_handler_base::mouse_wheel_up(int /*x*/, int /*y*/, const bool /*browse*/) +{ +} + +void mouse_handler_base::mouse_wheel_down(int /*x*/, int /*y*/, const bool /*browse*/) +{ +} + +void mouse_handler_base::mouse_wheel_left(int /*x*/, int /*y*/, const bool /*browse*/) +{ +} + +void mouse_handler_base::mouse_wheel_right(int /*x*/, int /*y*/, const bool /*browse*/) +{ +} + bool mouse_handler_base::right_click(int x, int y, const bool browse) { if (right_click_show_menu(x, y, browse)) { diff --git a/src/mouse_handler_base.hpp b/src/mouse_handler_base.hpp index 357382fa423..cf6baae1b54 100644 --- a/src/mouse_handler_base.hpp +++ b/src/mouse_handler_base.hpp @@ -134,6 +134,26 @@ public: */ virtual void right_mouse_up(int x, int y, const bool browse); + /** + * Called when the mouse wheel is scrolled up + */ + virtual void mouse_wheel_up(int x, int y, const bool browse); + + /** + * Called when the mouse wheel is scrolled down + */ + virtual void mouse_wheel_down(int x, int y, const bool browse); + + /** + * Called when the mouse wheel is scrolled left + */ + virtual void mouse_wheel_left(int x, int y, const bool browse); + + /** + * Called when the mouse wheel is scrolled right + */ + virtual void mouse_wheel_right(int x, int y, const bool browse); + /** * Called when the middle click scrolling */ diff --git a/src/widgets/slider.cpp b/src/widgets/slider.cpp index 73897757182..670633a8167 100644 --- a/src/widgets/slider.cpp +++ b/src/widgets/slider.cpp @@ -209,14 +209,39 @@ void slider::mouse_motion(const SDL_MouseMotionEvent& event) void slider::mouse_down(const SDL_MouseButtonEvent& event) { - if (event.button != SDL_BUTTON_LEFT || !point_in_rect(event.x, event.y, location())) + bool prev_change = value_change_; + + if (!point_in_rect(event.x, event.y, location())) + return; + + if (event.button == SDL_BUTTON_WHEELUP || event.button == SDL_BUTTON_WHEELRIGHT) { + value_change_ = false; + set_focus(true); + set_value(value_ + increment_); + if(value_change_) { + sound::play_UI_sound(game_config::sounds::slider_adjust); + } else { + value_change_ = prev_change; + } + } + if (event.button == SDL_BUTTON_WHEELDOWN || event.button == SDL_BUTTON_WHEELLEFT) { + value_change_ = false; + set_focus(true); + set_value(value_ - increment_); + if(value_change_) { + sound::play_UI_sound(game_config::sounds::slider_adjust); + } else { + value_change_ = prev_change; + } + } + + if (event.button != SDL_BUTTON_LEFT) return; state_ = CLICKED; if (point_in_rect(event.x, event.y, slider_area())) { sound::play_UI_sound(game_config::sounds::button_press); } else { - bool prev_change = value_change_; value_change_ = false; set_focus(true); set_slider_position(event.x);