The Battle for Wesnoth  1.17.0-dev
mouse_handler_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2021
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  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  bool is_touch_click(const SDL_MouseButtonEvent& event) const;
103 
104  /** Called when scrolling with the mouse wheel. */
105  virtual void mouse_wheel(int xscroll, int yscroll, bool browse);
106 
107  /**
108  * Derived classes can override this to disable mousewheel scrolling under
109  * some circumstances, e.g. when the mouse wheel controls something else,
110  * but the event is also received by this class
111  */
112  virtual bool allow_mouse_wheel_scroll(int /*x*/, int /*y*/)
113  {
114  return true;
115  }
116 
117  /**
118  * Overridden in derived classes, called on a left click (mousedown).
119  * Defaults to process (initiate) minimap scrolling.
120  *
121  * @returns true when the click should not process the event further.
122  * This means do not treat the call as a start of drag movement.
123  * FIXME: This return value is currently ignored
124  */
125  virtual bool left_click(int x, int y, const bool browse);
126 
127  /** Overridden in derived class. Called on drag + drop movements. */
128  virtual void move_action(bool /*browse*/)
129  {
130  // Overridden with unit move code elsewhere
131  }
132 
133  virtual void touch_action(const map_location hex, bool browse);
134 
135  /** Called whenever the left mouse drag has "ended". */
136  virtual void left_drag_end(int /*x*/, int /*y*/, const bool /*browse*/);
137 
138  /** Called when the left mouse button is up */
139  virtual void left_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/)
140  {
141  }
142 
143  /**
144  * Overridden in derived classes, called on a right click (mousedown).
145  * Defaults to displaying the menu (by setting the appropriate flag)
146  * if right_click_show_menu returns true.
147  *
148  * @returns true when the click should not process the event further.
149  * This means do not treat the call as a start of drag movement.
150  */
151  virtual bool right_click(int x, int y, const bool browse)
152  {
153  return right_click_show_menu(x, y, browse);
154  }
155 
156  /**
157  * Called in the default right_click when the context menu is about to
158  * be shown, can be used for preprocessing and preventing the menu from
159  * being displayed without rewriting the right click function.
160  *
161  * @returns true when the menu should be displayed and false otherwise
162  * FIXME: This return value is currently ignored
163  */
164  virtual bool right_click_show_menu(int /*x*/, int /*y*/, const bool /*browse*/)
165  {
166  return true;
167  }
168 
169  /** Called whenever the right mouse drag has "ended". */
170  virtual void right_drag_end(int /*x*/, int /*y*/, const bool /*browse*/)
171  {
172  // FIXME: This is called when we select an option in context-menu,
173  // which is bad because that was not a real dragging
174  }
175 
176  /** Called when the right mouse button is up. */
177  virtual void right_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/);
178 
179  /** Called when the mouse wheel is scrolled up. */
180  virtual void mouse_wheel_up(int /*x*/, int /*y*/, const bool /*browse*/)
181  {
182  }
183 
184  /** Called when the mouse wheel is scrolled down. */
185  virtual void mouse_wheel_down(int /*x*/, int /*y*/, const bool /*browse*/)
186  {
187  }
188 
189  /** Called when the mouse wheel is scrolled left. */
190  virtual void mouse_wheel_left(int /*x*/, int /*y*/, const bool /*browse*/)
191  {
192  }
193 
194  /** Called when the mouse wheel is scrolled right. */
195  virtual void mouse_wheel_right(int /*x*/, int /*y*/, const bool /*browse*/)
196  {
197  }
198 
199  /** Called when the middle click scrolling. */
200  void set_scroll_start(int x, int y)
201  {
202  scroll_start_x_ = x;
203  scroll_start_y_ = y;
204  }
205 
206  const SDL_Point get_scroll_start() const
207  {
208  return {scroll_start_x_, scroll_start_y_};
209  }
210 
211  bool scroll_started() const
212  {
213  return scroll_started_;
214  }
215 
216 protected:
217  void cancel_dragging();
218  void clear_dragging(const SDL_MouseButtonEvent& event, bool browse);
219  void init_dragging(bool& dragging_flag);
220 
221  /** MMB click (on game map) state flag */
223 
224  /** minimap scrolling (scroll-drag) state flag */
226 
227  /** LMB drag init flag */
229 
230  /** Finger drag init flag */
232 
233  /** Actual drag flag */
235 
236  /** RMB drag init flag */
238 
239  /** Drag start position x */
241 
242  /** Drag start position y */
244 
245  /** Drag start 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
int drag_from_x_
Drag start position x.
const SDL_Point get_scroll_start() const
bool dragging_right_
RMB drag init flag.
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.
bool dragging_started_
Actual drag flag.
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).
General purpose widgets.
virtual void mouse_wheel_down(int, int, const bool)
Called when the mouse wheel is scrolled down.
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...
bool minimap_scrolling_
minimap scrolling (scroll-drag) state flag
virtual bool allow_mouse_wheel_scroll(int, int)
Derived classes can override this to disable mousewheel scrolling under some circumstances, e.g.
bool show_menu_
Show context menu flag.
virtual void mouse_wheel_up(int, int, const bool)
Called when the mouse wheel is scrolled up.
map_location drag_from_hex_
Drag start map location.
bool dragging_left_
LMB drag init flag.
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".
int scroll_start_x_
Relative to middle click scrolling.
Handling of system events.
Definition: manager.hpp:43
map_location last_hex_
last highlighted hex
virtual void mouse_wheel_left(int, int, const bool)
Called when the mouse wheel is scrolled left.
static const map_location & null_location()
Definition: location.hpp:81
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.