The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mouse_handler_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Joerg Hinrichs <[email protected]>
3  wesnoth playturn Copyright (C) 2003 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 #ifndef MOUSE_HANDLER_BASE_H_INCLUDED
17 #define MOUSE_HANDLER_BASE_H_INCLUDED
18 
19 #include "map/location.hpp"
20 #include <SDL_events.h>
21 #include <SDL_version.h>
22 
23 class display;
24 
25 namespace events {
26 
28 {
31 };
32 
33 extern int commands_disabled;
34 
36 public:
38  virtual ~mouse_handler_base() {}
39 
40  /**
41  * Reference to the used display objects. Derived classes should ensure
42  * this is always valid. Note the constructor of this class cannot use this.
43  */
44  virtual display& gui() = 0;
45 
46  /**
47  * Const version.
48  */
49  virtual const display& gui() const = 0;
50 
51  /**
52  * @return true when the class in the "dragging" state.
53  */
54  bool is_dragging() const;
55 
56  //minimum dragging distance to fire the drag&drop
57  virtual int drag_threshold() const {return 0;}
58 
59  void mouse_motion_event(const SDL_MouseMotionEvent& event, const bool browse);
60 
61  /** update the mouse with a fake mouse motion */
62  void mouse_update(const bool browse, map_location loc);
63 
64  bool get_show_menu() const { return show_menu_; }
65 
66  /**
67  * This handles minimap scrolling and click-drag.
68  * @returns true when the caller should not process the mouse motion
69  * further (i.e. should return), false otherwise.
70  */
71  bool mouse_motion_default(int x, int y, bool update);
72 
73  /**
74  * Called when a mouse motion event takes place. Derived classes must provide an
75  * implementation, possibly using mouse_motion_default().
76  */
77  virtual void mouse_motion(int x, int y, const bool browse, bool update=false, map_location new_loc = map_location::null_location()) = 0;
78 
79  virtual void mouse_press(const SDL_MouseButtonEvent& event, const bool browse);
80  bool is_left_click(const SDL_MouseButtonEvent& event) const;
81  bool is_middle_click(const SDL_MouseButtonEvent& event) const;
82  bool is_right_click(const SDL_MouseButtonEvent& event) const;
83 
84  /**
85  * Called when scrolling with the mouse wheel.
86  */
87  virtual void mouse_wheel(int xscroll, int yscroll, bool browse);
88 
89  /**
90  * Derived classes can override this to disable mousewheel scrolling under
91  * some circumstances, e.g. when the mouse wheel controls something else,
92  * but the event is also received by this class
93  */
94  virtual bool allow_mouse_wheel_scroll(int x, int y);
95 
96  /**
97  * Overridden in derived classes, called on a left click (mousedown).
98  * Defaults to process (initiate) minimap scrolling.
99  * @returns true when the click should not process the event further.
100  * This means do not treat the call as a start of drag movement.
101  * FIXME: This return value is currently ignored
102  */
103  virtual bool left_click(int x, int y, const bool browse);
104 
105  /**
106  * Overridden in derived class. Called on drag+drop movements.
107  */
108  virtual void move_action(bool browse);
109 
110  /**
111  * Called whenever the left mouse drag has "ended".
112  */
113  virtual void left_drag_end(int x, int y, const bool browse);
114 
115  /**
116  * Called when the left mouse button is up
117  */
118  virtual void left_mouse_up(int x, int y, const bool browse);
119 
120  /**
121  * Overridden in derived classes, called on a right click (mousedown).
122  * Defaults to displaying the menu (by setting the appropriate flag)
123  * if right_click_show_menu returns true.
124  * @returns true when the click should not process the event further.
125  * This means do not treat the call as a start of drag movement.
126  */
127  virtual bool right_click(int x, int y, const bool browse);
128 
129  /**
130  * Called in the default right_click when the context menu is about to
131  * be shown, can be used for preprocessing and preventing the menu from
132  * being displayed without rewriting the right click function.
133  * @returns true when the menu should be displayed and false otherwise
134  * FIXME: This return value is currently ignored
135  */
136  virtual bool right_click_show_menu(int x, int y, const bool browse);
137 
138  /**
139  * Called whenever the right mouse drag has "ended".
140  */
141  virtual void right_drag_end(int x, int y, const bool browse);
142 
143  /**
144  * Called when the right mouse button is up
145  */
146  virtual void right_mouse_up(int x, int y, const bool browse);
147 
148  /**
149  * Called when the mouse wheel is scrolled up
150  */
151  virtual void mouse_wheel_up(int x, int y, const bool browse);
152 
153  /**
154  * Called when the mouse wheel is scrolled down
155  */
156  virtual void mouse_wheel_down(int x, int y, const bool browse);
157 
158  /**
159  * Called when the mouse wheel is scrolled left
160  */
161  virtual void mouse_wheel_left(int x, int y, const bool browse);
162 
163  /**
164  * Called when the mouse wheel is scrolled right
165  */
166  virtual void mouse_wheel_right(int x, int y, const bool browse);
167 
168  /**
169  * Called when the middle click scrolling
170  */
171  void set_scroll_start (int x, int y) { scroll_start_x_ = x; scroll_start_y_ = y; }
173  bool scroll_started() { return scroll_started_; }
174 
175 protected:
176  void cancel_dragging();
177  void clear_dragging(const SDL_MouseButtonEvent& event, bool browse);
178  void init_dragging(bool& dragging_flag);
179 
180  /** MMB click (on game map) state flag */
182  /** minimap scrolling (scroll-drag) state flag */
184  /** LMB drag init flag */
186  /** Actual drag flag */
188  /** RMB drag init flag */
190  /** Drag start position x */
192  /** Drag start position y */
194  /** Drag start map location */
196 
197  /** last highlighted hex */
199 
200  /** Show context menu flag */
202 
203  /** Relative to middle click scrolling */
207 };
208 
209 } // end namespace events
210 
211 #endif
bool mouse_motion_default(int x, int y, bool update)
This handles minimap scrolling and click-drag.
const map_location get_scroll_start()
int drag_from_x_
Drag start position x.
void clear_dragging(const SDL_MouseButtonEvent &event, bool browse)
virtual void left_mouse_up(int x, int y, const bool browse)
Called when the left mouse button is up.
bool is_left_click(const SDL_MouseButtonEvent &event) const
bool dragging_right_
RMB drag init flag.
void set_scroll_start(int x, int y)
Called when the middle click scrolling.
bool dragging_started_
Actual drag flag.
virtual bool right_click_show_menu(int x, int y, const bool browse)
Called in the default right_click when the context menu is about to be shown, can be used for preproc...
virtual void right_drag_end(int x, int y, const bool browse)
Called whenever the right mouse drag has "ended".
virtual void mouse_wheel_down(int x, int y, const bool browse)
Called when the mouse wheel is scrolled down.
virtual bool allow_mouse_wheel_scroll(int x, int y)
Derived classes can override this to disable mousewheel scrolling under some circumstances, e.g.
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
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 mouse_motion_event(const SDL_MouseMotionEvent &event, const bool browse)
virtual void mouse_wheel_up(int x, int y, const bool browse)
Called when the mouse wheel is scrolled up.
virtual void mouse_press(const SDL_MouseButtonEvent &event, const bool browse)
virtual bool left_click(int x, int y, const bool browse)
Overridden in derived classes, called on a left click (mousedown).
virtual bool right_click(int x, int y, const bool browse)
Overridden in derived classes, called on a right click (mousedown).
bool minimap_scrolling_
minimap scrolling (scroll-drag) state flag
virtual void right_mouse_up(int x, int y, const bool browse)
Called when the right mouse button is up.
virtual void mouse_wheel_right(int x, int y, const bool browse)
Called when the mouse wheel is scrolled right.
bool show_menu_
Show context menu flag.
map_location drag_from_hex_
Drag start map location.
static const map_location & null_location()
Definition: location.hpp:195
bool dragging_left_
LMB drag init flag.
Encapsulates the map of the game.
Definition: location.hpp:38
int scroll_start_x_
Relative to middle click scrolling.
virtual void mouse_wheel_left(int x, int y, const bool browse)
Called when the mouse wheel is scrolled left.
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
virtual void mouse_wheel(int xscroll, int yscroll, bool browse)
Called when scrolling with the mouse wheel.
Handling of system events.
Definition: manager.hpp:42
map_location last_hex_
last highlighted hex
cl_event event
Definition: glew.h:3070
void init_dragging(bool &dragging_flag)
virtual display & gui()=0
Reference to the used display objects.
virtual int drag_threshold() const
bool is_right_click(const SDL_MouseButtonEvent &event) const
virtual void move_action(bool browse)
Overridden in derived class.
void mouse_update(const bool browse, map_location loc)
update the mouse with a fake mouse motion
virtual void left_drag_end(int x, int y, const bool browse)
Called whenever the left mouse drag has "ended".
bool simple_warp_
MMB click (on game map) state flag.
bool is_middle_click(const SDL_MouseButtonEvent &event) const
int drag_from_y_
Drag start position y.