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