The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mouse_action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Tomasz Sniatowski <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef EDITOR_MOUSE_ACTION_HPP
16 #define EDITOR_MOUSE_ACTION_HPP
17 
20 #include "theme.hpp"
25 
26 #include <SDL_video.h>
27 
28 class CKey;
29 
30 namespace editor {
31 
32 /**
33  * A mouse action receives events from the controller, and responds to them by creating
34  * an appropriate editor_action object. Mouse actions may store some temporary data
35  * such as the last clicked hex for better handling of click-drag. They should *not* modify
36  * the map or trigger refreshes, but may set brush locations and similar overlays that
37  * should be visible around the mouse cursor, hence the display references are not const.
38  */
40 {
41 public:
44  , key_(key)
45  , toolbar_button_(nullptr)
46  , palette_(palette)
47  {
48  }
49 
50  virtual ~mouse_action() {}
51 
52  virtual bool has_context_menu() const;
53 
54  /**
55  * Mouse move (not a drag). Never changes anything (other than temporary highlights and similar)
56  */
57  virtual void move(editor_display& disp, const map_location& hex);
58 
59  /**
60  * Unconditionally update the brush highlights for the current tool when hex is the center location
61  */
62  void update_brush_highlights(editor_display& disp, const map_location& hex);
63 
64  /**
65  * Locations that would be affected by a click, used by move to update highlights. Defaults to highlight the mouseover hex.
66  * Maybe also used for actually performing the action in click() or drag().
67  */
68  virtual std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
69 
70  /**
71  * A click, possibly the beginning of a drag. Must be overridden.
72  */
73  virtual editor_action* click_left(editor_display& disp, int x, int y) = 0;
74 
75  /**
76  * A click, possibly the beginning of a drag. Must be overridden.
77  */
78  virtual editor_action* click_right(editor_display& disp, int x, int y) = 0;
79 
80  /**
81  * Drag operation. A click should have occurred earlier. Defaults to no action.
82  */
83  virtual editor_action* drag_left(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
84 
85  /**
86  * Drag operation. A click should have occurred earlier. Defaults to no action.
87  */
88  virtual editor_action* drag_right(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
89 
90  /**
91  * The end of dragging. Defaults to no action.
92  */
93  virtual editor_action* drag_end_left(editor_display& disp, int x, int y);
94 
95  virtual editor_action* drag_end_right(editor_display& disp, int x, int y);
96 
97  virtual editor_action* up_left(editor_display& disp, int x, int y);
98 
99  virtual editor_action* up_right(editor_display& disp, int x, int y);
100 
101  /**
102  * Function called by the controller on a key event for the current mouse action.
103  * Defaults to starting position processing.
104  */
105  virtual editor_action* key_event(editor_display& disp, const SDL_Event& e);
106 
107  /**
108  * Helper variable setter - pointer to a toolbar menu/button used for highlighting
109  * the current action. Should always be nullptr or point to a valid menu.
110  */
112 
113  /**
114  * Getter for the (possibly nullptr) associated menu/button.
115  */
116  const theme::menu* toolbar_button() const { return toolbar_button_; }
117 
118  /**
119  * Getter for the associated palette.
120  */
122 
123  /** Whether we need the brush bar, is used to grey it out.*/
124  virtual bool supports_brushes() { return false; }
125 
126  /**
127  * Set the mouse overlay for this action. Defaults to an empty overlay.
128  */
129  virtual void set_mouse_overlay(editor_display& disp);
130 
131 
132 protected:
133  bool has_alt_modifier() const;
134  bool has_shift_modifier() const;
135  bool has_ctrl_modifier() const;
136 
137  /**
138  * Helper function for derived classes that need a active-terrain mouse overlay
139  */
141  const t_translation::t_terrain & bg);
142 
143  /**
144  * The hex previously used in move operations
145  */
147 
148  /**
149  * Key presses, used for modifiers (alt, shift) in some operations
150  */
151  const CKey& key_;
152 
153 private:
154  /**
155  * Pointer to an associated menu/button, if such exists
156  */
158 
159  /**
160  * Pointer to an associated palette, if such exists
161  */
163 };
164 
165 /**
166  * A brush-drag mouse action base class which adds brush and drag processing to a basic mouse action
167  */
169 {
170 public:
171  brush_drag_mouse_action(common_palette& palette, const brush* const * const brush, const CKey& key)
172  : mouse_action(palette, key)
174  , brush_(brush)
175  {
176  }
177 
178  /**
179  * The affected hexes of a brush action are the result of projecting the current brush on the mouseover hex
180  */
181  std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
182 
183  /**
184  * The actual action function which is called by click() and drag(). Derived classes override this instead of click() and drag().
185  */
186  virtual editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes) = 0;
187 
188  /**
189  * The actual action function which is called by click() and drag(). Derived classes override this instead of click() and drag().
190  */
191  virtual editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes) = 0;
192 
193  /**
194  * Calls click_perform_left()
195  */
196  editor_action* click_left(editor_display& disp, int x, int y);
197 
198  /**
199  * Calls click_perform_right()
200  */
201  editor_action* click_right(editor_display& disp, int x, int y);
202 
203  /**
204  * Calls click_perform() for every new hex the mouse is dragged into.
205  * @todo partial actions support and merging of many drag actions into one
206  */
207  editor_action* drag_left(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
208 
209  /**
210  * Calls click_perform for every new hex the mouse is dragged into.
211  * @todo partial actions support and merging of many drag actions into one
212  */
213  editor_action* drag_right(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
214 
215  /**
216  * End of dragging.
217  * @todo partial actions (the entire drag should end up as one action)
218  */
219  editor_action* drag_end(editor_display& disp, int x, int y);
220 
221 protected:
222  /** Brush accessor */
223  const brush& get_brush();
224 
225  /**
226  * The previous hex dragged into.
227  * @todo keep a set of all "visited" locations to reduce action count in long drags that hit the same hexes multiple times?
228  */
230 
231 private:
232  /**
233  * Template helper gathering actions common for both drag_right and drag_left.
234  * The drags differ only in the worker function called, which should be
235  * passed as the template parameter. This exists only to avoid copy-pasting code.
236  */
237  template <editor_action* (brush_drag_mouse_action::*perform_func)(editor_display&, const std::set<map_location>&)>
238  editor_action* drag_generic(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
239 
240  /**
241  * Current brush handle. Currently a pointer-to-pointer with full constness.
242  * The mouse action does not modify the brush, does not modify the pointer
243  * to the current brush, and we allow setting this pointr only once, hence
244  * the three "consts".
245  */
246  const brush* const * const brush_;
247 };
248 
249 /**
250  * Brush paint mouse action. Uses keyboard modifiers for one-layer painting.
251  */
253 {
254 public:
256  const brush* const * const brush, const CKey& key, terrain_palette& palette)
257  : brush_drag_mouse_action(palette, brush, key)
258  , terrain_palette_(palette)
259  {
260  }
261 
262  /**
263  * Handle terrain sampling before calling generic handler
264  */
265  editor_action* click_left(editor_display& disp, int x, int y);
266 
267  /**
268  * Handle terrain sampling before calling generic handler
269  */
270  editor_action* click_right(editor_display& disp, int x, int y);
271 
272  /**
273  * Create an appropriate editor_action and return it
274  */
275  editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes);
276 
277  /**
278  * Create an appropriate editor_action and return it
279  */
280  editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes);
281 
282  void set_mouse_overlay(editor_display& disp);
283 
284  bool supports_brushes() { return true; }
285 
286 protected:
287 
289 
290 };
291 
292 
293 
294 /**
295  * Paste action. No dragging capabilities.
296  */
298 {
299 public:
301  : mouse_action(palette, key), paste_(paste)
302  {
303  }
304 
305  bool has_context_menu() const;
306 
307  /**
308  * Show an outline of where the paste will go
309  */
310  std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
311 
312  /**
313  * Return a paste with offset action
314  */
315  editor_action* click_left(editor_display& disp, int x, int y);
316 
317  /**
318  * Right click does nothing for now
319  */
320  editor_action* click_right(editor_display& disp, int x, int y);
321 
322  virtual void set_mouse_overlay(editor_display& disp);
323 
324 protected:
325  /**
326  * Reference to the buffer used for pasting (e.g. the clipboard)
327  */
329 };
330 
331 /**
332  * Fill action. No dragging capabilities. Uses keyboard modifiers for one-layer painting.
333  */
335 {
336 public:
339  : mouse_action(terrain_palette, key)
340  , terrain_palette_(terrain_palette)
341  {
342  }
343 
344  /**
345  * Tiles that will be painted to, possibly use modifier keys here
346  */
347  std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
348 
349  /**
350  * Left / right click fills with the respective terrain
351  */
352  editor_action* click_left(editor_display& disp, int x, int y);
353 
354  /**
355  * Left / right click fills with the respective terrain
356  */
357  editor_action* click_right(editor_display& disp, int x, int y);
358 
359  virtual void set_mouse_overlay(editor_display& disp);
360 
361 protected:
363 };
364 
365 /**
366  * Set starting position action.
367  */
369 {
370 public:
372  : mouse_action(palette, key), click_(false), location_palette_(palette)
373  {
374  }
375 
376  /**
377  * Left click displays a player-number-selector dialog and then creates an action
378  * or returns nullptr if cancel was pressed or there would be no change.
379  * Do this on mouse up to avoid drag issue.
380  */
381  editor_action* up_left(editor_display& disp, int x, int y);
382 
383  editor_action* click_left(editor_display& disp, int x, int y);
384  /**
385  * Right click only erases the starting position if there is one.
386  * Do this on mouse up to avoid drag issue,
387  */
388  editor_action* up_right(editor_display& disp, int x, int y);
389 
390  editor_action* click_right(editor_display& disp, int x, int y);
391 
392  virtual void set_mouse_overlay(editor_display& disp);
393 
394 private:
395  bool click_;
397 };
398 
399 
400 
401 } //end namespace editor
402 
403 #endif
Brush paint mouse action.
editor_action * drag_end(editor_display &disp, int x, int y)
End of dragging.
editor_action * click_left(editor_display &disp, int x, int y)
A click, possibly the beginning of a drag.
terrain_palette & terrain_palette_
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
virtual editor_action * click_perform_left(editor_display &disp, const std::set< map_location > &hexes)=0
The actual action function which is called by click() and drag().
virtual editor_action * click_left(editor_display &disp, int x, int y)=0
A click, possibly the beginning of a drag.
void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
bool has_shift_modifier() const
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
const CKey & key_
Key presses, used for modifiers (alt, shift) in some operations.
A map fragment – a collection of locations and information abut them.
editor_action * click_right(editor_display &disp, int x, int y)
A click, possibly the beginning of a drag.
virtual bool has_context_menu() const
const brush & get_brush()
Brush accessor.
map_location previous_move_hex_
The hex previously used in move operations.
const theme::menu * toolbar_button() const
Getter for the (possibly nullptr) associated menu/button.
bool supports_brushes()
Whether we need the brush bar, is used to grey it out.
const brush *const *const brush_
Current brush handle.
editor_action * click_perform_right(editor_display &disp, const std::set< map_location > &hexes)
Create an appropriate editor_action and return it.
mouse_action_fill(const CKey &key, terrain_palette &terrain_palette)
Palette where the terrain to be drawn can be selected.
editor_action * click_right(editor_display &disp, int x, int y)
Right click does nothing for now.
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
common_palette & palette_
Pointer to an associated palette, if such exists.
void set_terrain_mouse_overlay(editor_display &disp, const t_translation::t_terrain &fg, const t_translation::t_terrain &bg)
Helper function for derived classes that need a active-terrain mouse overlay.
editor_action * up_left(editor_display &disp, int x, int y)
Left click displays a player-number-selector dialog and then creates an action or returns nullptr if ...
Base class for editor actions.
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
virtual editor_action * drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Drag operation.
bool has_ctrl_modifier() const
mouse_action_paste(const map_fragment &paste, const CKey &key, common_palette &palette)
A brush-drag mouse action base class which adds brush and drag processing to a basic mouse action...
map_location previous_drag_hex_
The previous hex dragged into.
editor_action * click_left(editor_display &disp, int x, int y)
Handle terrain sampling before calling generic handler.
virtual editor_action * up_left(editor_display &disp, int x, int y)
virtual editor_action * key_event(editor_display &disp, const SDL_Event &e)
Function called by the controller on a key event for the current mouse action.
const map_fragment & paste_
Reference to the buffer used for pasting (e.g.
brush_drag_mouse_action(common_palette &palette, const brush *const *const brush, const CKey &key)
editor_action * click_perform_left(editor_display &disp, const std::set< map_location > &hexes)
Create an appropriate editor_action and return it.
mouse_action_starting_position(const CKey &key, location_palette &palette)
common_palette & get_palette()
Getter for the associated palette.
virtual editor_action * drag_right(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Drag operation.
GLsizei const GLfloat * value
Definition: glew.h:1817
virtual bool supports_brushes()
Whether we need the brush bar, is used to grey it out.
editor_action * click_left(editor_display &disp, int x, int y)
Left / right click fills with the respective terrain.
editor_action * up_right(editor_display &disp, int x, int y)
Right click only erases the starting position if there is one.
virtual editor_action * click_right(editor_display &disp, int x, int y)=0
A click, possibly the beginning of a drag.
mouse_action_paint(const brush *const *const brush, const CKey &key, terrain_palette &palette)
void update_brush_highlights(editor_display &disp, const map_location &hex)
Unconditionally update the brush highlights for the current tool when hex is the center location...
The brush class represents a single brush – a set of relative locations around a "hotspot"...
Definition: brush.hpp:26
editor_action * click_left(editor_display &disp, int x, int y)
Calls click_perform_left()
std::set< map_location > affected_hexes(editor_display &disp, const map_location &hex)
Show an outline of where the paste will go.
virtual editor_action * up_right(editor_display &disp, int x, int y)
Manage the empty-palette in the editor.
Definition: action.cpp:28
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
terrain_palette & terrain_palette_
Encapsulates the map of the game.
Definition: location.hpp:38
void set_toolbar_button(const theme::menu *value)
Helper variable setter - pointer to a toolbar menu/button used for highlighting the current action...
bool has_alt_modifier() const
Base class for all editor actions.
Definition: action_base.hpp:41
std::set< map_location > affected_hexes(editor_display &disp, const map_location &hex)
Tiles that will be painted to, possibly use modifier keys here.
Definitions related to theme-support.
editor_action * drag_right(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Calls click_perform for every new hex the mouse is dragged into.
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
editor_action * click_right(editor_display &disp, int x, int y)
Handle terrain sampling before calling generic handler.
editor_action * drag_generic(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Template helper gathering actions common for both drag_right and drag_left.
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
editor_action * click_left(editor_display &disp, int x, int y)
Return a paste with offset action.
editor_action * drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Calls click_perform() for every new hex the mouse is dragged into.
const theme::menu * toolbar_button_
Pointer to an associated menu/button, if such exists.
virtual editor_action * drag_end_left(editor_display &disp, int x, int y)
The end of dragging.
editor_action * click_right(editor_display &disp, int x, int y)
Left / right click fills with the respective terrain.
virtual void move(editor_display &disp, const map_location &hex)
Mouse move (not a drag).
Set starting position action.
mouse_action(common_palette &palette, const CKey &key)
editor_action * click_right(editor_display &disp, int x, int y)
Calls click_perform_right()
#define e
A mouse action receives events from the controller, and responds to them by creating an appropriate e...
virtual std::set< map_location > affected_hexes(editor_display &disp, const map_location &hex)
Locations that would be affected by a click, used by move to update highlights.
virtual editor_action * click_perform_right(editor_display &disp, const std::set< map_location > &hexes)=0
The actual action function which is called by click() and drag().
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:27
std::vector< Uint32 > palette(color_range cr)
Creates a reference color palette from a color range.
virtual editor_action * drag_end_right(editor_display &disp, int x, int y)
std::set< map_location > affected_hexes(editor_display &disp, const map_location &hex)
The affected hexes of a brush action are the result of projecting the current brush on the mouseover ...