Mouse Handler Base: formatting cleanup

# Conflicts:
#	src/mouse_handler_base.cpp
This commit is contained in:
Charles Dang 2017-07-21 19:13:57 +11:00
parent 79ba6ea55f
commit cc3c9be5b0
2 changed files with 184 additions and 181 deletions

View file

@ -19,14 +19,14 @@
#include "display.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include "tooltips.hpp"
#include "sdl/rect.hpp"
#include "tooltips.hpp"
static lg::log_domain log_display("display");
#define WRN_DP LOG_STREAM(warn, log_display)
namespace events {
namespace events
{
command_disabler::command_disabler()
{
++commands_disabled;
@ -37,31 +37,31 @@ command_disabler::~command_disabler()
--commands_disabled;
}
int commands_disabled= 0;
int commands_disabled = 0;
static bool command_active()
{
#ifdef __APPLE__
return (SDL_GetModState()&KMOD_CTRL) != 0;
return (SDL_GetModState() & KMOD_CTRL) != 0;
#else
return false;
#endif
}
mouse_handler_base::mouse_handler_base() :
simple_warp_(false),
minimap_scrolling_(false),
dragging_left_(false),
dragging_started_(false),
dragging_right_(false),
drag_from_x_(0),
drag_from_y_(0),
drag_from_hex_(),
last_hex_(),
show_menu_(false),
scroll_start_x_(0),
scroll_start_y_(0),
scroll_started_(false)
mouse_handler_base::mouse_handler_base()
: simple_warp_(false)
, minimap_scrolling_(false)
, dragging_left_(false)
, dragging_started_(false)
, dragging_right_(false)
, drag_from_x_(0)
, drag_from_y_(0)
, drag_from_hex_()
, last_hex_()
, show_menu_(false)
, scroll_start_x_(0)
, scroll_start_y_(0)
, scroll_started_(false)
{
}
@ -72,13 +72,13 @@ bool mouse_handler_base::is_dragging() const
void mouse_handler_base::mouse_motion_event(const SDL_MouseMotionEvent& event, const bool browse)
{
mouse_motion(event.x,event.y, browse);
mouse_motion(event.x, event.y, browse);
}
void mouse_handler_base::mouse_update(const bool browse, map_location loc)
{
int x, y;
SDL_GetMouseState(&x,&y);
SDL_GetMouseState(&x, &y);
mouse_motion(x, y, browse, true, loc);
}
@ -91,40 +91,49 @@ bool mouse_handler_base::mouse_motion_default(int x, int y, bool /*update*/)
}
if(minimap_scrolling_) {
//if the game is run in a window, we could miss a LMB/MMB up event
// if the game is run in a window, we could miss a LMB/MMB up event
// if it occurs outside our window.
// thus, we need to check if the LMB/MMB is still down
minimap_scrolling_ = ((SDL_GetMouseState(nullptr,nullptr) & (SDL_BUTTON(1) | SDL_BUTTON(2))) != 0);
minimap_scrolling_ = ((SDL_GetMouseState(nullptr, nullptr) & (SDL_BUTTON(1) | SDL_BUTTON(2))) != 0);
if(minimap_scrolling_) {
const map_location& loc = gui().minimap_location_on(x,y);
const map_location& loc = gui().minimap_location_on(x, y);
if(loc.valid()) {
if(loc != last_hex_) {
last_hex_ = loc;
gui().scroll_to_tile(loc,display::WARP,false);
gui().scroll_to_tile(loc, display::WARP, false);
}
} else {
// clicking outside of the minimap will end minimap scrolling
minimap_scrolling_ = false;
}
}
if(minimap_scrolling_) return true;
if(minimap_scrolling_) {
return true;
}
}
// Fire the drag & drop only after minimal drag distance
// While we check the mouse buttons state, we also grab fresh position data.
int mx = drag_from_x_; // some default value to prevent unlikely SDL bug
int my = drag_from_y_;
if (is_dragging() && !dragging_started_) {
if ((dragging_left_ && (SDL_GetMouseState(&mx,&my) & SDL_BUTTON_LEFT) != 0)
|| (dragging_right_ && (SDL_GetMouseState(&mx,&my) & SDL_BUTTON_RIGHT) != 0)) {
const double drag_distance = std::pow(static_cast<double>(drag_from_x_- mx), 2)
+ std::pow(static_cast<double>(drag_from_y_- my), 2);
if (drag_distance > drag_threshold()*drag_threshold()) {
if(is_dragging() && !dragging_started_) {
if((dragging_left_ && (SDL_GetMouseState(&mx, &my) & SDL_BUTTON_LEFT) != 0) ||
(dragging_right_ && (SDL_GetMouseState(&mx, &my) & SDL_BUTTON_RIGHT) != 0))
{
const double drag_distance =
std::pow(static_cast<double>(drag_from_x_ - mx), 2) +
std::pow(static_cast<double>(drag_from_y_ - my), 2);
if(drag_distance > drag_threshold() * drag_threshold()) {
dragging_started_ = true;
cursor::set_dragging(true);
}
}
}
return false;
}
@ -133,94 +142,82 @@ void mouse_handler_base::mouse_press(const SDL_MouseButtonEvent& event, const bo
if(is_middle_click(event) && !preferences::middle_click_scrolls()) {
simple_warp_ = true;
}
show_menu_ = false;
map_location loc = gui().hex_clicked_on(event.x,event.y);
map_location loc = gui().hex_clicked_on(event.x, event.y);
mouse_update(browse, loc);
if (is_left_click(event)) {
if (event.state == SDL_PRESSED) {
if(is_left_click(event)) {
if(event.state == SDL_PRESSED) {
cancel_dragging();
init_dragging(dragging_left_);
left_click(event.x, event.y, browse);
} else if (event.state == SDL_RELEASED) {
} else if(event.state == SDL_RELEASED) {
minimap_scrolling_ = false;
clear_dragging(event, browse);
left_mouse_up(event.x, event.y, browse);
}
} else if (is_right_click(event)) {
if (event.state == SDL_PRESSED) {
} else if(is_right_click(event)) {
if(event.state == SDL_PRESSED) {
cancel_dragging();
init_dragging(dragging_right_);
right_click(event.x, event.y, browse);
} else if (event.state == SDL_RELEASED) {
} else if(event.state == SDL_RELEASED) {
minimap_scrolling_ = false;
clear_dragging(event, browse);
right_mouse_up(event.x, event.y, browse);
}
} else if (is_middle_click(event)) {
if (event.state == SDL_PRESSED) {
} else if(is_middle_click(event)) {
if(event.state == SDL_PRESSED) {
set_scroll_start(event.x, event.y);
scroll_started_ = true;
map_location minimap_loc = gui().minimap_location_on(event.x,event.y);
map_location minimap_loc = gui().minimap_location_on(event.x, event.y);
minimap_scrolling_ = false;
if(minimap_loc.valid()) {
simple_warp_ = false;
minimap_scrolling_ = true;
last_hex_ = minimap_loc;
gui().scroll_to_tile(minimap_loc,display::WARP,false);
gui().scroll_to_tile(minimap_loc, display::WARP, false);
} else if(simple_warp_) {
// middle click not on minimap, check gamemap instead
if(loc.valid()) {
last_hex_ = loc;
gui().scroll_to_tile(loc,display::WARP,false);
gui().scroll_to_tile(loc, display::WARP, false);
}
}
} else if (event.state == SDL_RELEASED) {
} else if(event.state == SDL_RELEASED) {
minimap_scrolling_ = false;
simple_warp_ = false;
scroll_started_ = false;
}
}
if (!dragging_left_ && !dragging_right_ && dragging_started_) {
if(!dragging_left_ && !dragging_right_ && dragging_started_) {
dragging_started_ = false;
cursor::set_dragging(false);
}
mouse_update(browse, loc);
}
bool mouse_handler_base::is_left_click(
const SDL_MouseButtonEvent& event) const
bool mouse_handler_base::is_left_click(const SDL_MouseButtonEvent& event) const
{
return event.button == SDL_BUTTON_LEFT && !command_active();
}
bool mouse_handler_base::is_middle_click(
const SDL_MouseButtonEvent& event) const
bool mouse_handler_base::is_middle_click(const SDL_MouseButtonEvent& event) const
{
return event.button == SDL_BUTTON_MIDDLE;
}
bool mouse_handler_base::is_right_click(
const SDL_MouseButtonEvent& event) const
bool mouse_handler_base::is_right_click(const SDL_MouseButtonEvent& event) const
{
return event.button == SDL_BUTTON_RIGHT
|| (event.button == SDL_BUTTON_LEFT && command_active());
}
bool mouse_handler_base::allow_mouse_wheel_scroll(int /*x*/, int /*y*/)
{
return true;
}
bool mouse_handler_base::right_click_show_menu(int /*x*/, int /*y*/, const bool /*browse*/)
{
return true;
return event.button == SDL_BUTTON_RIGHT || (event.button == SDL_BUTTON_LEFT && command_active());
}
bool mouse_handler_base::left_click(int x, int y, const bool /*browse*/)
{
if(gui().view_locked()) {
return false;
}
@ -231,15 +228,11 @@ bool mouse_handler_base::left_click(int x, int y, const bool /*browse*/)
if(loc.valid()) {
minimap_scrolling_ = true;
last_hex_ = loc;
gui().scroll_to_tile(loc,display::WARP, false);
gui().scroll_to_tile(loc, display::WARP, false);
return true;
}
return false;
}
void mouse_handler_base::move_action(const bool /*browse*/)
{
// Overridden with unit move code elsewhere
return false;
}
void mouse_handler_base::left_drag_end(int /*x*/, int /*y*/, const bool browse)
@ -247,10 +240,6 @@ void mouse_handler_base::left_drag_end(int /*x*/, int /*y*/, const bool browse)
move_action(browse);
}
void mouse_handler_base::left_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/)
{
}
void mouse_handler_base::mouse_wheel(int scrollx, int scrolly, bool browse)
{
int x, y;
@ -264,68 +253,45 @@ void mouse_handler_base::mouse_wheel(int scrollx, int scrolly, bool browse)
return;
}
if (movex != 0 || movey != 0) {
if(movex != 0 || movey != 0) {
CKey pressed;
// Alt + mousewheel do an 90° rotation on the scroll direction
if (pressed[SDLK_LALT] || pressed[SDLK_RALT]) {
gui().scroll(-movey,-movex);
if(pressed[SDLK_LALT] || pressed[SDLK_RALT]) {
gui().scroll(-movey, -movex);
} else {
gui().scroll(-movex,-movey);
gui().scroll(-movex, -movey);
}
}
if (scrollx < 0) {
if(scrollx < 0) {
mouse_wheel_left(x, y, browse);
} else if (scrollx > 0) {
} else if(scrollx > 0) {
mouse_wheel_right(x, y, browse);
}
if (scrolly < 0) {
if(scrolly < 0) {
mouse_wheel_down(x, y, browse);
} else if (scrolly > 0) {
} else if(scrolly > 0) {
mouse_wheel_up(x, y, 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)) {
gui().draw(); // redraw highlight (and maybe some more)
const theme::menu* const m = gui().get_theme().context_menu();
if (m != nullptr) {
show_menu_ = true;
} else {
WRN_DP << "no context menu found..." << std::endl;
}
return true;
if(!right_click_show_menu(x, y, browse)) {
return false;
}
return false;
}
void mouse_handler_base::right_drag_end(int /*x*/, int /*y*/, const bool /*browse*/)
{
//FIXME: This is called when we select an option in context-menu,
// which is bad because that was not a real dragging
}
gui().draw(); // redraw highlight (and maybe some more)
void mouse_handler_base::right_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/)
{
const theme::menu* const m = gui().get_theme().context_menu();
if(m != nullptr) {
show_menu_ = true;
} else {
WRN_DP << "no context menu found..." << std::endl;
}
return true;
}
void mouse_handler_base::init_dragging(bool& dragging_flag)
@ -349,13 +315,15 @@ void mouse_handler_base::clear_dragging(const SDL_MouseButtonEvent& event, bool
// because they may take time to return, and we
// could have started other drag&drop before that
cursor::set_dragging(false);
if (dragging_started_) {
if(dragging_started_) {
dragging_started_ = false;
if (dragging_left_) {
if(dragging_left_) {
dragging_left_ = false;
left_drag_end(event.x, event.y, browse);
}
if (dragging_right_) {
if(dragging_right_) {
dragging_right_ = false;
right_drag_end(event.x, event.y, browse);
}
@ -365,5 +333,4 @@ void mouse_handler_base::clear_dragging(const SDL_MouseButtonEvent& event, bool
}
}
} //end namespace events
} // end namespace events

View file

@ -16,12 +16,13 @@
#pragma once
#include "map/location.hpp"
#include <SDL_events.h>
class display;
namespace events {
namespace events
{
struct command_disabler
{
command_disabler();
@ -30,10 +31,14 @@ struct command_disabler
extern int commands_disabled;
class mouse_handler_base {
class mouse_handler_base
{
public:
mouse_handler_base();
virtual ~mouse_handler_base() {}
virtual ~mouse_handler_base()
{
}
/**
* Reference to the used display objects. Derived classes should ensure
@ -41,9 +46,7 @@ public:
*/
virtual display& gui() = 0;
/**
* Const version.
*/
/** Const version of @ref gui */
virtual const display& gui() const = 0;
/**
@ -51,15 +54,21 @@ public:
*/
bool is_dragging() const;
//minimum dragging distance to fire the drag&drop
virtual int drag_threshold() const {return 0;}
/** Minimum dragging distance to fire the drag&drop */
virtual int drag_threshold() const
{
return 0;
}
void mouse_motion_event(const SDL_MouseMotionEvent& event, const bool browse);
/** update the mouse with a fake mouse motion */
/** Update the mouse with a fake mouse motion */
void mouse_update(const bool browse, map_location loc);
bool get_show_menu() const { return show_menu_; }
bool get_show_menu() const
{
return show_menu_;
}
/**
* This handles minimap scrolling and click-drag.
@ -72,16 +81,16 @@ public:
* Called when a mouse motion event takes place. Derived classes must provide an
* implementation, possibly using mouse_motion_default().
*/
virtual void mouse_motion(int x, int y, const bool browse, bool update=false, map_location new_loc = map_location::null_location()) = 0;
virtual void mouse_motion(
int x, int y, const bool browse, bool update = false, map_location new_loc = map_location::null_location())
= 0;
virtual void mouse_press(const SDL_MouseButtonEvent& event, const bool browse);
bool is_left_click(const SDL_MouseButtonEvent& event) const;
bool is_middle_click(const SDL_MouseButtonEvent& event) const;
bool is_right_click(const SDL_MouseButtonEvent& event) const;
/**
* Called when scrolling with the mouse wheel.
*/
/** Called when scrolling with the mouse wheel. */
virtual void mouse_wheel(int xscroll, int yscroll, bool browse);
/**
@ -89,36 +98,40 @@ public:
* some circumstances, e.g. when the mouse wheel controls something else,
* but the event is also received by this class
*/
virtual bool allow_mouse_wheel_scroll(int x, int y);
virtual bool allow_mouse_wheel_scroll(int /*x*/, int /*y*/)
{
return true;
}
/**
* Overridden in derived classes, called on a left click (mousedown).
* Defaults to process (initiate) minimap scrolling.
*
* @returns true when the click should not process the event further.
* This means do not treat the call as a start of drag movement.
* FIXME: This return value is currently ignored
*/
virtual bool left_click(int x, int y, const bool browse);
/**
* Overridden in derived class. Called on drag+drop movements.
*/
virtual void move_action(bool browse);
/** Overridden in derived class. Called on drag + drop movements. */
virtual void move_action(bool /*browse*/)
{
// Overridden with unit move code elsewhere
}
/**
* Called whenever the left mouse drag has "ended".
*/
virtual void left_drag_end(int x, int y, const bool browse);
/** Called whenever the left mouse drag has "ended". */
virtual void left_drag_end(int /*x*/, int /*y*/, const bool /*browse*/);
/**
* Called when the left mouse button is up
*/
virtual void left_mouse_up(int x, int y, const bool browse);
/** Called when the left mouse button is up */
virtual void left_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/)
{
}
/**
* Overridden in derived classes, called on a right click (mousedown).
* Defaults to displaying the menu (by setting the appropriate flag)
* if right_click_show_menu returns true.
*
* @returns true when the click should not process the event further.
* This means do not treat the call as a start of drag movement.
*/
@ -128,47 +141,63 @@ public:
* Called in the default right_click when the context menu is about to
* be shown, can be used for preprocessing and preventing the menu from
* being displayed without rewriting the right click function.
*
* @returns true when the menu should be displayed and false otherwise
* FIXME: This return value is currently ignored
*/
virtual bool right_click_show_menu(int x, int y, const bool browse);
virtual bool right_click_show_menu(int /*x*/, int /*y*/, const bool /*browse*/)
{
return true;
}
/**
* Called whenever the right mouse drag has "ended".
*/
virtual void right_drag_end(int x, int y, const bool browse);
/** Called whenever the right mouse drag has "ended". */
virtual void right_drag_end(int /*x*/, int /*y*/, const bool /*browse*/)
{
// FIXME: This is called when we select an option in context-menu,
// which is bad because that was not a real dragging
}
/**
* Called when the right mouse button is up
*/
virtual void right_mouse_up(int x, int y, const bool browse);
/** Called when the right mouse button is up. */
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 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 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 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 mouse wheel is scrolled right. */
virtual void mouse_wheel_right(int /*x*/, int /*y*/, const bool /*browse*/)
{
}
/**
* Called when the middle click scrolling
*/
void set_scroll_start (int x, int y) { scroll_start_x_ = x; scroll_start_y_ = y; }
const SDL_Point get_scroll_start() const { return{ scroll_start_x_, scroll_start_y_ }; }
bool scroll_started() const { return scroll_started_; }
/** Called when the middle click scrolling. */
void set_scroll_start(int x, int y)
{
scroll_start_x_ = x;
scroll_start_y_ = y;
}
const SDL_Point get_scroll_start() const
{
return {scroll_start_x_, scroll_start_y_};
}
bool scroll_started() const
{
return scroll_started_;
}
protected:
void cancel_dragging();
@ -177,18 +206,25 @@ protected:
/** MMB click (on game map) state flag */
bool simple_warp_;
/** minimap scrolling (scroll-drag) state flag */
bool minimap_scrolling_;
/** LMB drag init flag */
bool dragging_left_;
/** Actual drag flag */
bool dragging_started_;
/** RMB drag init flag */
bool dragging_right_;
/** Drag start position x */
int drag_from_x_;
/** Drag start position y */
int drag_from_y_;
/** Drag start map location */
map_location drag_from_hex_;