The Battle for Wesnoth  1.15.1+dev
mouse_handler_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2018 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
3  wesnoth playturn Copyright (C) 2003 by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "map/location.hpp"
19 
20 #include <SDL2/SDL_events.h>
21 
22 class display;
23 
24 namespace events
25 {
27 {
30 };
31 
32 extern int commands_disabled;
33 
35 {
36 public:
38 
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  bool is_left_click(const SDL_MouseButtonEvent& event) const;
99  bool is_middle_click(const SDL_MouseButtonEvent& event) const;
100  bool is_right_click(const SDL_MouseButtonEvent& event) const;
101  bool is_touch_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 
205  const SDL_Point get_scroll_start() const
206  {
207  return {scroll_start_x_, scroll_start_y_};
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 init_dragging(bool& dragging_flag);
219 
220  /** MMB click (on game map) state flag */
222 
223  /** minimap scrolling (scroll-drag) state flag */
225 
226  /** LMB drag init flag */
228 
229  /** Finger drag init flag */
231 
232  /** Actual drag flag */
234 
235  /** RMB drag init flag */
237 
238  /** Drag start position x */
240 
241  /** Drag start position y */
243 
244  /** Drag start map location */
246 
247  /** last highlighted hex */
249 
250  /** Show context menu flag */
252 
253  /** Relative to middle click scrolling */
257 };
258 
259 } // 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:42
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:41
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:85
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.