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