The Battle for Wesnoth  1.19.18+dev
mouse_handler_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2025
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 "gui/widgets/settings.hpp"
22 #include "log.hpp"
24 #include "sdl/rect.hpp"
25 #include "tooltips.hpp"
26 #include "sdl/input.hpp" // get_mouse_state
27 
28 static lg::log_domain log_display("display");
29 #define WRN_DP LOG_STREAM(warn, log_display)
30 
31 namespace events
32 {
34 {
36 }
37 
39 {
41 }
42 
44 
45 static bool command_active()
46 {
47 #ifdef __APPLE__
48  return (SDL_GetModState() & KMOD_CTRL) != 0;
49 #else
50  return false;
51 #endif
52 }
53 
55 {
56  return dragging_started_;
57 }
58 
60 {
62 }
63 
64 void mouse_handler_base::mouse_motion_event(const SDL_MouseMotionEvent& event, const bool browse)
65 {
66  mouse_motion(event.x, event.y, browse);
67 }
68 
69 void mouse_handler_base::touch_motion_event(const SDL_TouchFingerEvent& event, const bool browse)
70 {
71  // This is wrong (needs to be scaled from -1..1 to screen size), but it's discarded in touch_motion anyway.
72  // Let's not waste CPU cycles.
73  touch_motion(event.x, event.y, browse);
74 }
75 
77 {
78  auto [x, y] = sdl::get_mouse_location();
79  mouse_motion(x, y, browse, true, loc);
80 }
81 
82 bool mouse_handler_base::mouse_motion_default(int x, int y, bool /*update*/)
83 {
84  tooltips::process(x, y);
85 
86  if(simple_warp_) {
87  return true;
88  }
89 
90  if(minimap_scrolling_) {
91  // if the game is run in a window, we could miss a LMB/MMB up event
92  // if it occurs outside our window.
93  // thus, we need to check if the LMB/MMB is still down
94  minimap_scrolling_ = ((sdl::get_mouse_button_mask() & (SDL_BUTTON(SDL_BUTTON_LEFT) | SDL_BUTTON(SDL_BUTTON_MIDDLE))) != 0);
95  if(minimap_scrolling_) {
96  const map_location& loc = gui().minimap_location_on(x, y);
97  if(loc.valid()) {
98  if(loc != last_hex_) {
99  last_hex_ = loc;
100  gui().scroll_to_tile(loc, display::WARP, false);
101  }
102  } else {
103  // clicking outside of the minimap will end minimap scrolling
104  minimap_scrolling_ = false;
105  }
106  }
107 
108  if(minimap_scrolling_) {
109  return true;
110  }
111  }
112 
113  // Fire the drag & drop only after minimal drag distance
114  // While we check the mouse buttons state, we also grab fresh position data.
115 
116  if(is_dragging() && !dragging_started_) {
117  point pos = drag_from_; // some default value to prevent unlikely SDL bug
118  uint32_t mouse_state = dragging_left_ || dragging_right_ ? sdl::get_mouse_state(&pos.x, &pos.y) : 0;
119 
120 #ifdef MOUSE_TOUCH_EMULATION
121  if(dragging_left_ && (mouse_state & SDL_BUTTON(SDL_BUTTON_RIGHT))) {
122  // Monkey-patch touch controls again to make them look like left button.
123  mouse_state = SDL_BUTTON(SDL_BUTTON_LEFT);
124  }
125 #endif
126  if((dragging_left_ && (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0) ||
127  (dragging_right_ && (mouse_state & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0))
128  {
129  const double drag_distance =
130  std::pow(static_cast<double>(drag_from_.x - pos.x), 2) +
131  std::pow(static_cast<double>(drag_from_.y - pos.y), 2);
132 
133  if(drag_distance > drag_threshold() * drag_threshold()) {
134  dragging_started_ = true;
135  cursor::set_dragging(true);
136  }
137  }
138  }
139 
140  return false;
141 }
142 
144  const SDL_MouseButtonEvent& /*event*/, uint8_t /*button*/, map_location /*loc*/, bool /*click*/)
145 {
146  return false;
147 }
148 
149 void mouse_handler_base::mouse_press(const SDL_MouseButtonEvent& event, const bool browse)
150 {
151  if(is_middle_click(event) && !prefs::get().middle_click_scrolls()) {
152  simple_warp_ = true;
153  }
154 
155  show_menu_ = false;
156  map_location loc = gui().hex_clicked_on(event.x, event.y);
157  mouse_update(browse, loc);
158 
159  if(events::is_touch(event)) {
160  static std::chrono::steady_clock::time_point touch_timestamp;
161  const auto touch_time = gui2::settings::popup_show_delay;
162  const auto now = std::chrono::steady_clock::now();
163 
164  if(event.state == SDL_PRESSED) {
165  cancel_dragging();
166  touch_timestamp = now;
168  if (!mouse_button_event(event, SDL_BUTTON_LEFT, loc, true)) {
169  left_click(event.x, event.y, browse);
170  }
171  } else if(event.state == SDL_RELEASED) {
172  minimap_scrolling_ = false;
173 
174  if (!dragging_started_ && touch_timestamp != std::chrono::steady_clock::time_point{}) {
175  auto dt = now - touch_timestamp;
176  if (dt > touch_time) {
177  if (!mouse_button_event(event, SDL_BUTTON_RIGHT, loc, true)) {
178  // BUG: This function won't do anything in the game, need right_mouse_up()
179  right_click(event.x, event.y, browse); // show_menu_ = true;
180  }
181  }
182  } else {
183  touch_timestamp = {};
184  }
185 
186  clear_dragging(event, browse);
187  mouse_button_event(event, SDL_BUTTON_LEFT, loc);
188  left_mouse_up(event.x, event.y, browse);
190  }
191  } else if(is_left_click(event)) {
192  if(event.state == SDL_PRESSED) {
193  cancel_dragging();
195  if (!mouse_button_event(event, SDL_BUTTON_LEFT, loc, true)) {
196  left_click(event.x, event.y, browse);
197  }
198  } else if(event.state == SDL_RELEASED) {
199  minimap_scrolling_ = false;
200  clear_dragging(event, browse);
201  mouse_button_event(event, SDL_BUTTON_LEFT, loc);
202  left_mouse_up(event.x, event.y, browse);
204  }
205  } else if(is_right_click(event)) {
206  if(event.state == SDL_PRESSED) {
207  mouse_button_event(event, SDL_BUTTON_RIGHT, loc);
208  cancel_dragging();
210  right_click(event.x, event.y, browse);
211  } else if(event.state == SDL_RELEASED) {
212  minimap_scrolling_ = false;
213  clear_dragging(event, browse);
214  if (!mouse_button_event(event, SDL_BUTTON_RIGHT, loc, true)) {
215  right_mouse_up(event.x, event.y, browse);
216  }
218  }
219  } else if(is_middle_click(event)) {
220  if(event.state == SDL_PRESSED) {
222  set_scroll_start(event.x, event.y);
223  scroll_started_ = true;
224 
225  map_location minimap_loc = gui().minimap_location_on(event.x, event.y);
226  minimap_scrolling_ = false;
227  if(minimap_loc.valid()) {
228  simple_warp_ = false;
229  minimap_scrolling_ = true;
230  last_hex_ = minimap_loc;
231  gui().scroll_to_tile(minimap_loc, display::WARP, false);
232  } else if(mouse_button_event(event, SDL_BUTTON_MIDDLE, loc, true)) {
233  scroll_started_ = false;
234  simple_warp_ = false;
235  } else if(simple_warp_) {
236  // middle click not on minimap, check gamemap instead
237  if(loc.valid()) {
238  last_hex_ = loc;
239  gui().scroll_to_tile(loc, display::WARP, false);
240  }
241  } else {
242  // Deselect the current tile as we're scrolling
243  gui().highlight_hex({-1,-1});
244  }
245  } else if(event.state == SDL_RELEASED) {
246  minimap_scrolling_ = false;
247  simple_warp_ = false;
248  scroll_started_ = false;
249  mouse_button_event(event, SDL_BUTTON_MIDDLE, loc);
251  }
252  } else if(event.button == SDL_BUTTON_X1 || event.button == SDL_BUTTON_X2) {
253  if(event.state == SDL_PRESSED) {
254  cancel_dragging();
255  // record mouse-down hex in drag_from_hex_
257  mouse_button_event(event, event.button, loc);
258  } else {
259  mouse_button_event(event, event.button, loc, true);
261  }
262  }
264  dragging_started_ = false;
265  cursor::set_dragging(false);
266  }
267 
268  mouse_update(browse, loc);
269 }
270 
271 bool mouse_handler_base::is_left_click(const SDL_MouseButtonEvent& event) const
272 {
273 #ifdef MOUSE_TOUCH_EMULATION
274  if(event.button == SDL_BUTTON_RIGHT) {
275  return true;
276  }
277 #endif
278  if(events::is_touch(event)) {
279  return false;
280  }
281  return event.button == SDL_BUTTON_LEFT && !command_active();
282 }
283 
284 bool mouse_handler_base::is_middle_click(const SDL_MouseButtonEvent& event) const
285 {
286  return event.button == SDL_BUTTON_MIDDLE;
287 }
288 
289 bool mouse_handler_base::is_right_click(const SDL_MouseButtonEvent& event) const
290 {
291 #ifdef MOUSE_TOUCH_EMULATION
292  (void) event;
293  return false;
294 #else
295  if(events::is_touch(event)) {
296  return false;
297  }
298  return event.button == SDL_BUTTON_RIGHT
299  || (event.button == SDL_BUTTON_LEFT && command_active());
300 #endif
301 }
302 
303 bool mouse_handler_base::left_click(int x, int y, const bool /*browse*/)
304 {
305  if(gui().view_locked()) {
306  return false;
307  }
308 
309  // clicked on a hex on the minimap? then initiate minimap scrolling
310  const map_location& loc = gui().minimap_location_on(x, y);
311  minimap_scrolling_ = false;
312  if(loc.valid()) {
313  minimap_scrolling_ = true;
314  last_hex_ = loc;
315  gui().scroll_to_tile(loc, display::WARP, false);
316  return true;
317  }
318 
319  return false;
320 }
321 
322 void mouse_handler_base::touch_action(const map_location /*hex*/, bool /*browse*/)
323 {
324 }
325 
326 void mouse_handler_base::left_drag_end(int /*x*/, int /*y*/, const bool browse)
327 {
328  move_action(browse);
329 }
330 
331 void mouse_handler_base::mouse_wheel(int scrollx, int scrolly, bool browse)
332 {
333  auto [x, y] = sdl::get_mouse_location();
334 
335  int movex = scrollx * prefs::get().scroll_speed();
336  int movey = scrolly * prefs::get().scroll_speed();
337 
338  // Don't scroll map if cursor is not in gamemap area
339  if(!gui().map_area().contains(x, y)) {
340  return;
341  }
342 
343  if(movex != 0 || movey != 0) {
344  CKey pressed;
345  // Alt + mousewheel do an 90° rotation on the scroll direction
346  if(pressed[SDLK_LALT] || pressed[SDLK_RALT]) {
347  gui().scroll(point{movey, movex});
348  } else {
349  gui().scroll(point{movex, movey});
350  }
351  }
352 
353  if(scrollx < 0) {
354  mouse_wheel_left(x, y, browse);
355  } else if(scrollx > 0) {
356  mouse_wheel_right(x, y, browse);
357  }
358 
359  if(scrolly < 0) {
360  mouse_wheel_up(x, y, browse);
361  } else if(scrolly > 0) {
362  mouse_wheel_down(x, y, browse);
363  }
364 }
365 
366 void mouse_handler_base::right_mouse_up(int x, int y, const bool browse)
367 {
368  if(!right_click_show_menu(x, y, browse)) {
369  return;
370  }
371 
372  const theme::menu* const m = gui().get_theme().context_menu();
373  if(m != nullptr) {
374  show_menu_ = true;
375  } else {
376  WRN_DP << "no context menu found...";
377  }
378 }
379 
380 void mouse_handler_base::init_dragging(bool& dragging_flag)
381 {
382  dragging_flag = true;
385 }
386 
388 {
389  dragging_started_ = false;
390  dragging_left_ = false;
391  dragging_touch_ = false;
392  dragging_right_ = false;
393  cursor::set_dragging(false);
394 }
395 
396 void mouse_handler_base::clear_dragging(const SDL_MouseButtonEvent& event, bool browse)
397 {
398  // we reset dragging info before calling functions
399  // because they may take time to return, and we
400  // could have started other drag&drop before that
401  cursor::set_dragging(false);
402 
403  if(dragging_started_) {
404  dragging_started_ = false;
405 
406  if(dragging_touch_) {
407  dragging_touch_ = false;
408  // Maybe to do: create touch_drag_end(). Do panning and what else there. OTOH, it's fine now.
409  left_drag_end(event.x, event.y, browse);
410  }
411 
412  if(dragging_left_) {
413  dragging_left_ = false;
414  left_drag_end(event.x, event.y, browse);
415  }
416 
417  if(dragging_right_) {
418  dragging_right_ = false;
419  right_drag_end(event.x, event.y, browse);
420  }
421  } else {
422  dragging_left_ = false;
423  dragging_right_ = false;
424  dragging_touch_ = false;
425  }
426 }
427 
429 {
431 }
432 
433 } // end namespace events
map_location loc
Definition: move.cpp:172
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:29
virtual void highlight_hex(map_location hex)
Definition: display.cpp:1380
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:1855
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:683
theme & get_theme()
Definition: display.hpp:381
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:527
bool scroll(const point &amount, bool force=false)
Scrolls the display by amount pixels.
Definition: display.cpp:1580
virtual bool mouse_button_event(const SDL_MouseButtonEvent &event, uint8_t button, map_location loc, bool click=false)
bool show_menu_
Show context menu flag.
bool dragging_right_
RMB drag init flag.
virtual void left_drag_end(int, int, const bool)
Called whenever the left mouse drag has "ended".
bool minimap_scrolling_
minimap scrolling (scroll-drag) state flag
bool dragging_left_
LMB drag init flag.
map_location last_hex_
last highlighted hex
virtual void mouse_wheel_right(int, int, const bool)
Called when the mouse wheel is scrolled right.
bool scroll_started_
Scroll start flag.
virtual void mouse_wheel_down(int, int, const bool)
Called when the mouse wheel is scrolled down.
map_location drag_from_hex_
Drag start or mouse-down map location.
virtual int drag_threshold() const
Minimum dragging distance to fire the drag&drop.
virtual void right_drag_end(int, int, const bool)
Called whenever the right mouse drag has "ended".
void touch_motion_event(const SDL_TouchFingerEvent &event, const bool browse)
bool is_left_click(const SDL_MouseButtonEvent &event) const
virtual void mouse_press(const SDL_MouseButtonEvent &event, const bool browse)
bool dragging_started() const
If mouse/finger has moved far enough to consider it move/swipe, and not a click/touch.
virtual void mouse_wheel_up(int, int, const bool)
Called when the mouse wheel is scrolled up.
bool simple_warp_
MMB click (on game map) state flag.
bool is_middle_click(const SDL_MouseButtonEvent &event) const
void mouse_update(const bool browse, map_location loc)
Update the mouse with a fake mouse motion.
void init_dragging(bool &dragging_flag)
virtual void left_mouse_up(int, int, const bool)
Called when the left mouse button is up.
bool is_right_click(const SDL_MouseButtonEvent &event) const
bool mouse_motion_default(int x, int y, bool update)
This handles minimap scrolling and click-drag.
bool dragging_started_
Actual drag flag.
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 display & gui()=0
Reference to the used display objects.
virtual bool right_click(int x, int y, const bool browse)
Overridden in derived classes, called on a right click (mousedown).
void mouse_motion_event(const SDL_MouseMotionEvent &event, const bool browse)
virtual void right_mouse_up(int, int, const bool)
Called when the right mouse button is up.
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 touch_action(const map_location hex, bool browse)
bool dragging_touch_
Finger drag init flag.
virtual void mouse_wheel(int xscroll, int yscroll, bool browse)
Called when scrolling with the mouse wheel.
virtual void move_action(bool)
Overridden in derived class.
virtual bool left_click(int x, int y, const bool browse)
Overridden in derived classes, called on a left click (mousedown).
point drag_from_
Drag start position.
virtual void mouse_wheel_left(int, int, const bool)
Called when the mouse wheel is scrolled left.
void clear_dragging(const SDL_MouseButtonEvent &event, 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 set_scroll_start(int x, int y)
Called when the middle click scrolling.
static prefs & get()
int scroll_speed()
const menu * context_menu() const
Definition: theme.hpp:260
map_display and display: classes which take care of displaying the map and game-data on the screen.
Contains functions for cleanly handling SDL input.
Standard logging facilities (interface).
#define WRN_DP
static lg::log_domain log_display("display")
void set_dragging(bool drag)
Definition: cursor.cpp:198
Handling of system events.
static bool command_active()
bool is_touch(const SDL_MouseButtonEvent &event)
Check if this mouse button event is caused by a touch.
Definition: events.cpp:768
std::chrono::milliseconds popup_show_delay
Delay before a popup shows.
Definition: settings.cpp:38
uint32_t get_mouse_button_mask()
Returns the current mouse button mask.
Definition: input.cpp:49
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
Definition: input.cpp:27
point get_mouse_location()
Returns the current mouse location in draw space.
Definition: input.cpp:54
void process(int mousex, int mousey)
Definition: tooltips.cpp:334
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:87
Contains the SDL_Rect helper code.
This file contains the settings handling of the widget library.
Encapsulates the map of the game.
Definition: location.hpp:46
bool valid() const
Definition: location.hpp:111
static const map_location & null_location()
Definition: location.hpp:103
Holds a 2D point.
Definition: point.hpp:25