The Battle for Wesnoth  1.19.18+dev
mouse_handler_base.hpp
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 #pragma once
18 
19 #include "map/location.hpp"
20 #include "sdl/point.hpp"
21 
22 #include <SDL2/SDL_events.h>
23 
24 class display;
25 
26 namespace events
27 {
29 {
32 };
33 
34 extern int commands_disabled;
35 
37 {
38 public:
40  {
41  }
42 
43  /**
44  * Reference to the used display objects. Derived classes should ensure
45  * this is always valid. Note the constructor of this class cannot use this.
46  */
47  virtual display& gui() = 0;
48 
49  /** Const version of @ref gui */
50  virtual const display& gui() const = 0;
51 
52  /** If mouse/finger has moved far enough to consider it move/swipe, and not a click/touch */
53  bool dragging_started() const;
54 
55  /**
56  * @return true when the class in the "dragging" state.
57  */
58  bool is_dragging() const;
59 
60  /** Minimum dragging distance to fire the drag&drop */
61  virtual int drag_threshold() const
62  {
63  return 0;
64  }
65 
66  void mouse_motion_event(const SDL_MouseMotionEvent& event, const bool browse);
67 
68  void touch_motion_event(const SDL_TouchFingerEvent& event, const bool browse);
69 
70  /** Update the mouse with a fake mouse motion */
71  void mouse_update(const bool browse, map_location loc);
72 
73  bool get_show_menu() const
74  {
75  return show_menu_;
76  }
77 
78  /**
79  * This handles minimap scrolling and click-drag.
80  * @returns true when the caller should not process the mouse motion
81  * further (i.e. should return), false otherwise.
82  */
83  bool mouse_motion_default(int x, int y, bool update);
84 
85  /**
86  * Called when a mouse motion event takes place. Derived classes must provide an
87  * implementation, possibly using mouse_motion_default().
88  */
89  virtual void mouse_motion(
90  int x, int y, const bool browse, bool update = false, map_location new_loc = map_location::null_location())
91  = 0;
92 
93  virtual void touch_motion(
94  int x, int y, const bool browse, bool update = false, map_location new_loc = map_location::null_location())
95  = 0;
96 
97  virtual void mouse_press(const SDL_MouseButtonEvent& event, const bool browse);
98  virtual bool mouse_button_event(const SDL_MouseButtonEvent& event, uint8_t button, map_location loc, bool click = false);
99  bool is_left_click(const SDL_MouseButtonEvent& event) const;
100  bool is_middle_click(const SDL_MouseButtonEvent& event) const;
101  bool is_right_click(const SDL_MouseButtonEvent& event) const;
102 
103  /** Called when scrolling with the mouse wheel. */
104  virtual void mouse_wheel(int xscroll, int yscroll, bool browse);
105 
106  /**
107  * Derived classes can override this to disable mousewheel scrolling under
108  * some circumstances, e.g. when the mouse wheel controls something else,
109  * but the event is also received by this class
110  */
111  virtual bool allow_mouse_wheel_scroll(int /*x*/, int /*y*/)
112  {
113  return true;
114  }
115 
116  /**
117  * Overridden in derived classes, called on a left click (mousedown).
118  * Defaults to process (initiate) minimap scrolling.
119  *
120  * @returns true when the click should not process the event further.
121  * This means do not treat the call as a start of drag movement.
122  * FIXME: This return value is currently ignored
123  */
124  virtual bool left_click(int x, int y, const bool browse);
125 
126  /** Overridden in derived class. Called on drag + drop movements. */
127  virtual void move_action(bool /*browse*/)
128  {
129  // Overridden with unit move code elsewhere
130  }
131 
132  virtual void touch_action(const map_location hex, bool browse);
133 
134  /** Called whenever the left mouse drag has "ended". */
135  virtual void left_drag_end(int /*x*/, int /*y*/, const bool /*browse*/);
136 
137  /** Called when the left mouse button is up */
138  virtual void left_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/)
139  {
140  }
141 
142  /**
143  * Overridden in derived classes, called on a right click (mousedown).
144  * Defaults to displaying the menu (by setting the appropriate flag)
145  * if right_click_show_menu returns true.
146  *
147  * @returns true when the click should not process the event further.
148  * This means do not treat the call as a start of drag movement.
149  */
150  virtual bool right_click(int x, int y, const bool browse)
151  {
152  return right_click_show_menu(x, y, browse);
153  }
154 
155  /**
156  * Called in the default right_click when the context menu is about to
157  * be shown, can be used for preprocessing and preventing the menu from
158  * being displayed without rewriting the right click function.
159  *
160  * @returns true when the menu should be displayed and false otherwise
161  * FIXME: This return value is currently ignored
162  */
163  virtual bool right_click_show_menu(int /*x*/, int /*y*/, const bool /*browse*/)
164  {
165  return true;
166  }
167 
168  /** Called whenever the right mouse drag has "ended". */
169  virtual void right_drag_end(int /*x*/, int /*y*/, const bool /*browse*/)
170  {
171  // FIXME: This is called when we select an option in context-menu,
172  // which is bad because that was not a real dragging
173  }
174 
175  /** Called when the right mouse button is up. */
176  virtual void right_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/);
177 
178  /** Called when the mouse wheel is scrolled up. */
179  virtual void mouse_wheel_up(int /*x*/, int /*y*/, const bool /*browse*/)
180  {
181  }
182 
183  /** Called when the mouse wheel is scrolled down. */
184  virtual void mouse_wheel_down(int /*x*/, int /*y*/, const bool /*browse*/)
185  {
186  }
187 
188  /** Called when the mouse wheel is scrolled left. */
189  virtual void mouse_wheel_left(int /*x*/, int /*y*/, const bool /*browse*/)
190  {
191  }
192 
193  /** Called when the mouse wheel is scrolled right. */
194  virtual void mouse_wheel_right(int /*x*/, int /*y*/, const bool /*browse*/)
195  {
196  }
197 
198  /** Called when the middle click scrolling. */
199  void set_scroll_start(int x, int y)
200  {
201  scroll_start_.x = x;
202  scroll_start_.y = y;
203  }
204 
206  {
207  return scroll_start_;
208  }
209 
210  bool scroll_started() const
211  {
212  return scroll_started_;
213  }
214 
215 protected:
216  void cancel_dragging();
217  void clear_dragging(const SDL_MouseButtonEvent& event, bool browse);
218  void clear_drag_from_hex();
219  void init_dragging(bool& dragging_flag);
220 
221  /** Show context menu flag */
222  bool show_menu_{false};
223 
224  /** MMB click (on game map) state flag */
225  bool simple_warp_{false};
226 
227  /** minimap scrolling (scroll-drag) state flag */
228  bool minimap_scrolling_{false};
229 
230  /** LMB drag init flag */
231  bool dragging_left_{false};
232 
233  /** Finger drag init flag */
234  bool dragging_touch_{false};
235 
236  /** Actual drag flag */
237  bool dragging_started_{false};
238 
239  /** RMB drag init flag */
240  bool dragging_right_{false};
241 
242  /** Scroll start flag */
243  bool scroll_started_{false};
244 
245  /** Relative to middle click scrolling */
247 
248  /** Drag start position */
250 
251  /** Drag start or mouse-down map location */
253 
254  /** last highlighted hex */
256 };
257 
258 } // end namespace events
map_location loc
Definition: move.cpp:172
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:88
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.
virtual bool allow_mouse_wheel_scroll(int, int)
Derived classes can override this to disable mousewheel scrolling under some circumstances,...
point scroll_start_
Relative to middle click scrolling.
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 const display & gui() const =0
Const version of gui.
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 void update()
Handling of system events.
bool click(int mousex, int mousey)
Definition: tooltips.cpp:350
Encapsulates the map of the game.
Definition: location.hpp:46
static const map_location & null_location()
Definition: location.hpp:103
Holds a 2D point.
Definition: point.hpp:25