The Battle for Wesnoth  1.17.10+dev
mouse_handler_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2022
3  by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #include "mouse_handler_base.hpp"
18 
19 #include "cursor.hpp"
20 #include "display.hpp"
21 #include "log.hpp"
22 #include "preferences/general.hpp"
23 #include "sdl/rect.hpp"
24 #include "tooltips.hpp"
25 #include "sdl/input.hpp" // get_mouse_state
26 
27 static lg::log_domain log_display("display");
28 #define WRN_DP LOG_STREAM(warn, log_display)
29 
30 namespace events
31 {
33 {
35 }
36 
38 {
40 }
41 
43 
44 static bool command_active()
45 {
46 #ifdef __APPLE__
47  return (SDL_GetModState() & KMOD_CTRL) != 0;
48 #else
49  return false;
50 #endif
51 }
52 
54  : simple_warp_(false)
55  , minimap_scrolling_(false)
56  , dragging_left_(false)
57  , dragging_touch_(false)
58  , dragging_started_(false)
59  , dragging_right_(false)
60  , drag_from_x_(0)
61  , drag_from_y_(0)
62  , drag_from_hex_()
63  , last_hex_()
64  , show_menu_(false)
65  , scroll_start_x_(0)
66  , scroll_start_y_(0)
67  , scroll_started_(false)
68 {
69 }
70 
72 {
73  return dragging_started_;
74 }
75 
77 {
79 }
80 
81 void mouse_handler_base::mouse_motion_event(const SDL_MouseMotionEvent& event, const bool browse)
82 {
83  mouse_motion(event.x, event.y, browse);
84 }
85 
86 void mouse_handler_base::touch_motion_event(const SDL_TouchFingerEvent& event, const bool browse)
87 {
88  // This is wrong (needs to be scaled from -1..1 to screen size), but it's discarded in touch_motion anyway.
89  // Let's not waste CPU cycles.
90  touch_motion(event.x, event.y, browse);
91 }
92 
93 void mouse_handler_base::mouse_update(const bool browse, map_location loc)
94 {
95  int x, y;
96  sdl::get_mouse_state(&x, &y);
97  mouse_motion(x, y, browse, true, loc);
98 }
99 
100 bool mouse_handler_base::mouse_motion_default(int x, int y, bool /*update*/)
101 {
102  tooltips::process(x, y);
103 
104  if(simple_warp_) {
105  return true;
106  }
107 
108  if(minimap_scrolling_) {
109  // if the game is run in a window, we could miss a LMB/MMB up event
110  // if it occurs outside our window.
111  // thus, we need to check if the LMB/MMB is still down
112  minimap_scrolling_ = ((sdl::get_mouse_button_mask() & (SDL_BUTTON(SDL_BUTTON_LEFT) | SDL_BUTTON(SDL_BUTTON_MIDDLE))) != 0);
113  if(minimap_scrolling_) {
114  const map_location& loc = gui().minimap_location_on(x, y);
115  if(loc.valid()) {
116  if(loc != last_hex_) {
117  last_hex_ = loc;
118  gui().scroll_to_tile(loc, display::WARP, false);
119  }
120  } else {
121  // clicking outside of the minimap will end minimap scrolling
122  minimap_scrolling_ = false;
123  }
124  }
125 
126  if(minimap_scrolling_) {
127  return true;
128  }
129  }
130 
131  // Fire the drag & drop only after minimal drag distance
132  // While we check the mouse buttons state, we also grab fresh position data.
133  int mx = drag_from_x_; // some default value to prevent unlikely SDL bug
134  int my = drag_from_y_;
135 
136  if(is_dragging() && !dragging_started_) {
137  Uint32 mouse_state = dragging_left_ || dragging_right_ ? sdl::get_mouse_state(&mx, &my) : 0;
138 #ifdef MOUSE_TOUCH_EMULATION
139  if(dragging_left_ && (mouse_state & SDL_BUTTON(SDL_BUTTON_RIGHT))) {
140  // Monkey-patch touch controls again to make them look like left button.
141  mouse_state = SDL_BUTTON(SDL_BUTTON_LEFT);
142  }
143 #endif
144  if((dragging_left_ && (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0) ||
145  (dragging_right_ && (mouse_state & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0))
146  {
147  const double drag_distance =
148  std::pow(static_cast<double>(drag_from_x_- mx), 2) +
149  std::pow(static_cast<double>(drag_from_y_- my), 2);
150 
151  if(drag_distance > drag_threshold() * drag_threshold()) {
152  dragging_started_ = true;
153  cursor::set_dragging(true);
154  }
155  }
156  }
157 
158  return false;
159 }
160 
161 void mouse_handler_base::mouse_press(const SDL_MouseButtonEvent& event, const bool browse)
162 {
164  simple_warp_ = true;
165  }
166 
167  show_menu_ = false;
168  map_location loc = gui().hex_clicked_on(event.x, event.y);
169  mouse_update(browse, loc);
170 
171  static clock_t touch_timestamp = 0;
172 
173  if(is_touch_click(event)) {
174  if (event.state == SDL_PRESSED) {
175  cancel_dragging();
176  touch_timestamp = clock();
178  left_click(event.x, event.y, browse);
179  } else if (event.state == SDL_RELEASED) {
180  minimap_scrolling_ = false;
181 
182  if (!dragging_started_ && touch_timestamp > 0) {
183  clock_t dt = clock() - touch_timestamp;
184  if (dt > CLOCKS_PER_SEC * 3 / 10) {
185  right_click(event.x, event.y, browse); // show_menu_ = true;
186  }
187  } else {
188  touch_timestamp = 0;
189  }
190 
191  clear_dragging(event, browse);
192  left_mouse_up(event.x, event.y, browse);
193  }
194  } else if(is_left_click(event)) {
195  if(event.state == SDL_PRESSED) {
196  cancel_dragging();
198  left_click(event.x, event.y, browse);
199  } else if(event.state == SDL_RELEASED) {
200  minimap_scrolling_ = false;
201  clear_dragging(event, browse);
202  left_mouse_up(event.x, event.y, browse);
203  }
204  } else if(is_right_click(event)) {
205  if(event.state == SDL_PRESSED) {
206  cancel_dragging();
208  right_click(event.x, event.y, browse);
209  } else if(event.state == SDL_RELEASED) {
210  minimap_scrolling_ = false;
211  clear_dragging(event, browse);
212  right_mouse_up(event.x, event.y, browse);
213  }
214  } else if(is_middle_click(event)) {
215  if(event.state == SDL_PRESSED) {
216  set_scroll_start(event.x, event.y);
217  scroll_started_ = true;
218 
219  map_location minimap_loc = gui().minimap_location_on(event.x, event.y);
220  minimap_scrolling_ = false;
221  if(minimap_loc.valid()) {
222  simple_warp_ = false;
223  minimap_scrolling_ = true;
224  last_hex_ = minimap_loc;
225  gui().scroll_to_tile(minimap_loc, display::WARP, false);
226  } else if(simple_warp_) {
227  // middle click not on minimap, check gamemap instead
228  if(loc.valid()) {
229  last_hex_ = loc;
230  gui().scroll_to_tile(loc, display::WARP, false);
231  }
232  } else {
233  // Deselect the current tile as we're scrolling
234  gui().highlight_hex({-1,-1});
235  }
236  } else if(event.state == SDL_RELEASED) {
237  minimap_scrolling_ = false;
238  simple_warp_ = false;
239  scroll_started_ = false;
240  }
241  }
243  dragging_started_ = false;
244  cursor::set_dragging(false);
245  }
246 
247  mouse_update(browse, loc);
248 }
249 
250 bool mouse_handler_base::is_left_click(const SDL_MouseButtonEvent& event) const
251 {
252 #ifdef MOUSE_TOUCH_EMULATION
253  if(event.button == SDL_BUTTON_RIGHT) {
254  return true;
255  }
256 #endif
257  if(event.which == SDL_TOUCH_MOUSEID) {
258  return false;
259  }
260  return event.button == SDL_BUTTON_LEFT && !command_active();
261 }
262 
263 bool mouse_handler_base::is_middle_click(const SDL_MouseButtonEvent& event) const
264 {
265  return event.button == SDL_BUTTON_MIDDLE;
266 }
267 
268 bool mouse_handler_base::is_right_click(const SDL_MouseButtonEvent& event) const
269 {
270 #ifdef MOUSE_TOUCH_EMULATION
271  (void) event;
272  return false;
273 #else
274  if(event.which == SDL_TOUCH_MOUSEID) {
275  return false;
276  }
277  return event.button == SDL_BUTTON_RIGHT
278  || (event.button == SDL_BUTTON_LEFT && command_active());
279 #endif
280 }
281 
282 bool mouse_handler_base::is_touch_click(const SDL_MouseButtonEvent& event) const
283 {
284  return event.which == SDL_TOUCH_MOUSEID;
285 }
286 
287 bool mouse_handler_base::left_click(int x, int y, const bool /*browse*/)
288 {
289  if(gui().view_locked()) {
290  return false;
291  }
292 
293  // clicked on a hex on the minimap? then initiate minimap scrolling
294  const map_location& loc = gui().minimap_location_on(x, y);
295  minimap_scrolling_ = false;
296  if(loc.valid()) {
297  minimap_scrolling_ = true;
298  last_hex_ = loc;
299  gui().scroll_to_tile(loc, display::WARP, false);
300  return true;
301  }
302 
303  return false;
304 }
305 
306 void mouse_handler_base::touch_action(const map_location /*hex*/, bool /*browse*/)
307 {
308 }
309 
310 void mouse_handler_base::left_drag_end(int /*x*/, int /*y*/, const bool browse)
311 {
312  move_action(browse);
313 }
314 
315 void mouse_handler_base::mouse_wheel(int scrollx, int scrolly, bool browse)
316 {
317  int x, y;
318  sdl::get_mouse_state(&x, &y);
319 
320  int movex = scrollx * preferences::scroll_speed();
321  int movey = scrolly * preferences::scroll_speed();
322 
323  // Don't scroll map if cursor is not in gamemap area
324  if(!gui().map_area().contains(x, y)) {
325  return;
326  }
327 
328  if(movex != 0 || movey != 0) {
329  CKey pressed;
330  // Alt + mousewheel do an 90° rotation on the scroll direction
331  if(pressed[SDLK_LALT] || pressed[SDLK_RALT]) {
332  gui().scroll(-movey, -movex);
333  } else {
334  gui().scroll(-movex, -movey);
335  }
336  }
337 
338  if(scrollx < 0) {
339  mouse_wheel_left(x, y, browse);
340  } else if(scrollx > 0) {
341  mouse_wheel_right(x, y, browse);
342  }
343 
344  if(scrolly < 0) {
345  mouse_wheel_down(x, y, browse);
346  } else if(scrolly > 0) {
347  mouse_wheel_up(x, y, browse);
348  }
349 }
350 
351 void mouse_handler_base::right_mouse_up(int x, int y, const bool browse)
352 {
353  if(!right_click_show_menu(x, y, browse)) {
354  return;
355  }
356 
357  const theme::menu* const m = gui().get_theme().context_menu();
358  if(m != nullptr) {
359  show_menu_ = true;
360  } else {
361  WRN_DP << "no context menu found...";
362  }
363 }
364 
365 void mouse_handler_base::init_dragging(bool& dragging_flag)
366 {
367  dragging_flag = true;
370 }
371 
373 {
374  dragging_started_ = false;
375  dragging_left_ = false;
376  dragging_touch_ = false;
377  dragging_right_ = false;
378  cursor::set_dragging(false);
379 }
380 
381 void mouse_handler_base::clear_dragging(const SDL_MouseButtonEvent& event, bool browse)
382 {
383  // we reset dragging info before calling functions
384  // because they may take time to return, and we
385  // could have started other drag&drop before that
386  cursor::set_dragging(false);
387 
388  if(dragging_started_) {
389  dragging_started_ = false;
390 
391  if(dragging_touch_) {
392  dragging_touch_ = false;
393  // Maybe to do: create touch_drag_end(). Do panning and what else there. OTOH, it's fine now.
394  left_drag_end(event.x, event.y, browse);
395  }
396 
397  if(dragging_left_) {
398  dragging_left_ = false;
399  left_drag_end(event.x, event.y, browse);
400  }
401 
402  if(dragging_right_) {
403  dragging_right_ = false;
404  right_drag_end(event.x, event.y, browse);
405  }
406  } else {
407  dragging_left_ = false;
408  dragging_right_ = false;
409  dragging_touch_ = false;
410  }
411 }
412 
413 } // end namespace events
bool mouse_motion_default(int x, int y, bool update)
This handles minimap scrolling and click-drag.
int drag_from_x_
Drag start position x.
void clear_dragging(const SDL_MouseButtonEvent &event, bool browse)
#define WRN_DP
bool dragging_right_
RMB drag init flag.
theme & get_theme()
Definition: display.hpp:375
void set_scroll_start(int x, int y)
Called when the middle click scrolling.
bool dragging_touch_
Finger drag init flag.
virtual int drag_threshold() const
Minimum dragging distance to fire the drag&drop.
const map_location hex_clicked_on(int x, int y) const
given x,y co-ordinates of an onscreen pixel, will return the location of the hex that this pixel corr...
Definition: display.cpp:581
bool dragging_started_
Actual drag flag.
bool is_middle_click(const SDL_MouseButtonEvent &event) const
virtual void mouse_wheel_right(int, int, const bool)
Called when the mouse wheel is scrolled right.
virtual bool right_click(int x, int y, const bool browse)
Overridden in derived classes, called on a right click (mousedown).
int scroll_speed()
Definition: general.cpp:794
map_location minimap_location_on(int x, int y)
given x,y co-ordinates of the mouse, will return the location of the hex in the minimap that the mous...
Definition: display.cpp:749
void touch_motion_event(const SDL_TouchFingerEvent &event, const bool browse)
virtual void mouse_motion(int x, int y, const bool browse, bool update=false, map_location new_loc=map_location::null_location())=0
Called when a mouse motion event takes place.
void mouse_motion_event(const SDL_MouseMotionEvent &event, const bool browse)
virtual void mouse_wheel_down(int, int, const bool)
Called when the mouse wheel is scrolled down.
bool is_right_click(const SDL_MouseButtonEvent &event) const
Contains functions for cleanly handling SDL input.
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:84
virtual bool right_click_show_menu(int, int, const bool)
Called in the default right_click when the context menu is about to be shown, can be used for preproc...
virtual void mouse_press(const SDL_MouseButtonEvent &event, const bool browse)
virtual bool left_click(int x, int y, const bool browse)
Overridden in derived classes, called on a left click (mousedown).
void process(int mousex, int mousey)
Definition: tooltips.cpp:278
bool minimap_scrolling_
minimap scrolling (scroll-drag) state flag
bool is_left_click(const SDL_MouseButtonEvent &event) const
void set_dragging(bool drag)
Definition: cursor.cpp:196
bool show_menu_
Show context menu flag.
bool valid() const
Definition: location.hpp:89
virtual void mouse_wheel_up(int, int, const bool)
Called when the mouse wheel is scrolled up.
bool dragging_started() const
If mouse/finger has moved far enough to consider it move/swipe, and not a click/touch.
map_location drag_from_hex_
Drag start map location.
virtual void left_drag_end(int, int, const bool)
Called whenever the left mouse drag has "ended".
virtual void right_mouse_up(int, int, const bool)
Called when the right mouse button is up.
map_display and display: classes which take care of displaying the map and game-data on the screen...
uint32_t get_mouse_button_mask()
Returns the current mouse button mask.
Definition: input.cpp:49
bool dragging_left_
LMB drag init flag.
static lg::log_domain log_display("display")
virtual void move_action(bool)
Overridden in derived class.
Encapsulates the map of the game.
Definition: location.hpp:38
virtual void right_drag_end(int, int, const bool)
Called whenever the right mouse drag has "ended".
void scroll_to_tile(const map_location &loc, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true, bool force=true)
Scroll such that location loc is on-screen.
Definition: display.cpp:2056
virtual void highlight_hex(map_location hex)
Definition: display.cpp:1542
bool is_touch_click(const SDL_MouseButtonEvent &event) const
bool middle_click_scrolls()
Definition: general.cpp:808
virtual void touch_action(const map_location hex, bool browse)
static bool command_active()
virtual void mouse_wheel(int xscroll, int yscroll, bool browse)
Called when scrolling with the mouse wheel.
Handling of system events.
Definition: manager.hpp:43
map_location last_hex_
last highlighted hex
Contains the SDL_Rect helper code.
virtual void touch_motion(int x, int y, const bool browse, bool update=false, map_location new_loc=map_location::null_location())=0
virtual void mouse_wheel_left(int, int, const bool)
Called when the mouse wheel is scrolled left.
void init_dragging(bool &dragging_flag)
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
Definition: input.cpp:27
virtual display & gui()=0
Reference to the used display objects.
Standard logging facilities (interface).
const menu * context_menu() const
Definition: theme.hpp:260
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:28
void mouse_update(const bool browse, map_location loc)
Update the mouse with a fake mouse motion.
bool scroll(int xmov, int ymov, bool force=false)
Scrolls the display by xmov,ymov pixels.
Definition: display.cpp:1772
bool simple_warp_
MMB click (on game map) state flag.
int drag_from_y_
Drag start position y.
virtual void left_mouse_up(int, int, const bool)
Called when the left mouse button is up.