editor/action/mouse/mouse_action.hpp

Go to the documentation of this file.
00001 /* $Id: mouse_action.hpp 53573 2012-03-20 20:27:56Z fendrin $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Tomasz Sniatowski <kailoran@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef EDITOR_MOUSE_ACTION_HPP
00017 #define EDITOR_MOUSE_ACTION_HPP
00018 
00019 #include "editor/action/action_base.hpp"
00020 #include "editor/map/editor_map.hpp"
00021 #include "theme.hpp"
00022 #include "editor/palette/editor_palettes.hpp"
00023 #include "editor/palette/terrain_palettes.hpp"
00024 #include "editor/palette/empty_palette.hpp"
00025 
00026 class CKey;
00027 
00028 namespace editor {
00029 
00030 /**
00031  * A mouse action receives events from the controller, and responds to them by creating
00032  * an appropriate editor_action object. Mouse actions may store some temporary data
00033  * such as the last clicked hex for better handling of click-drag. They should *not* modify
00034  * the map or trigger refreshes, but may set brush locations and similar overlays that
00035  * should be visible around the mouse cursor, hence the display references are not const.
00036  */
00037 class mouse_action
00038 {
00039 public:
00040     mouse_action(common_palette& palette, const CKey& key)
00041         : previous_move_hex_()
00042         , key_(key)
00043         , toolbar_button_(NULL)
00044         , palette_(palette)
00045     {
00046     }
00047 
00048     virtual ~mouse_action() {}
00049 
00050     virtual bool has_context_menu() const;
00051 
00052     /**
00053      * Mouse move (not a drag). Never changes anything (other than temporary highlihts and similar)
00054      */
00055     void move(editor_display& disp, const map_location& hex);
00056 
00057     /**
00058      * Unconditionally update the brush highlights for the current tool when hex is the center location
00059      */
00060     void update_brush_highlights(editor_display& disp, const map_location& hex);
00061 
00062     /**
00063      * Locations that would be affected by a click, used by move to update highlights. Defauts to higlight the mouseover hex.
00064      * Maybe also used for actually performing the action in click() or drag().
00065      */
00066     virtual std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
00067 
00068     /**
00069      * A click, possibly the beginning of a drag. Must be overridden.
00070      */
00071     virtual editor_action* click_left(editor_display& disp, int x, int y) = 0;
00072 
00073     /**
00074      * A click, possibly the beginning of a drag. Must be overridden.
00075      */
00076     virtual editor_action* click_right(editor_display& disp, int x, int y) = 0;
00077 
00078     /**
00079      * Drag operation. A click should have occurred earlier. Defaults to no action.
00080      */
00081     virtual editor_action* drag_left(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
00082 
00083     /**
00084      * Drag operation. A click should have occurred earlier. Defaults to no action.
00085      */
00086     virtual editor_action* drag_right(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
00087 
00088     /**
00089      * The end of dragging. Defaults to no action.
00090      */
00091     virtual editor_action* drag_end(editor_display& disp, int x, int y);
00092 
00093 
00094     virtual editor_action* up_left(editor_display& disp, int x, int y);
00095 
00096     virtual editor_action* up_right(editor_display& disp, int x, int y);
00097 
00098     /**
00099      * Function called by the controller on a key event for the current mouse action.
00100      * Defaults to starting position processing.
00101      */
00102     virtual editor_action* key_event(editor_display& disp, const SDL_Event& e);
00103 
00104     /**
00105      * Helper variable setter - pointer to a toolbar menu/button used for highlighting
00106      * the current action. Should always be NULL or point to a valid menu.
00107      */
00108     void set_toolbar_button(const theme::menu* value) { toolbar_button_ = value; }
00109 
00110     /**
00111      * Getter for the (possibly NULL) associated menu/button.
00112      */
00113     const theme::menu* toolbar_button() const { return toolbar_button_; }
00114 
00115     /**
00116      * Getter for the associated palette.
00117      */
00118     common_palette& get_palette() { return palette_; }
00119 
00120     /**
00121      * Set the mouse overlay for this action. Defaults to an empty overlay.
00122      */
00123     virtual void set_mouse_overlay(editor_display& disp);
00124 
00125 
00126 protected:
00127     bool has_alt_modifier() const;
00128     bool has_shift_modifier() const;
00129     bool has_ctrl_modifier() const;
00130 
00131     /**
00132      * Helper function for derived classes that need a active-terrain mouse overlay
00133      */
00134     void set_terrain_mouse_overlay(editor_display& disp, t_translation::t_terrain fg,
00135         t_translation::t_terrain bg);
00136 
00137     /**
00138      * The hex previously used in move operations
00139      */
00140     map_location previous_move_hex_;
00141 
00142     /**
00143      * Key presses, used for modifiers (alt, shift) in some operations
00144      */
00145     const CKey& key_;
00146 
00147 private:
00148     /**
00149      * Pointer to an associated menu/button, if such exists
00150      */
00151     const theme::menu* toolbar_button_;
00152 
00153     /**
00154      * Pointer to an associated palette, if such exists
00155      */
00156     common_palette& palette_;
00157 };
00158 
00159 /**
00160  * A brush-drag mouse action base class which adds brush and drag processing to a basic mouse action
00161  */
00162 class brush_drag_mouse_action : public mouse_action
00163 {
00164 public:
00165     brush_drag_mouse_action(common_palette& palette, const brush* const * const brush, const CKey& key)
00166         : mouse_action(palette, key)
00167         , previous_drag_hex_()
00168         , brush_(brush)
00169     {
00170     }
00171 
00172     /**
00173      * The affected hexes of a brush action are the result of projecting the current brush on the mouseover hex
00174      */
00175     std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
00176 
00177     /**
00178      * The actual action function which is called by click() and drag(). Derived classes override this instead of click() and drag().
00179      */
00180     virtual editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes) = 0;
00181 
00182     /**
00183      * The actual action function which is called by click() and drag(). Derived classes override this instead of click() and drag().
00184      */
00185     virtual editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes) = 0;
00186 
00187     /**
00188      * Calls click_perform_left()
00189      */
00190     editor_action* click_left(editor_display& disp, int x, int y);
00191 
00192     /**
00193      * Calls click_perform_right()
00194      */
00195     editor_action* click_right(editor_display& disp, int x, int y);
00196 
00197     /**
00198      * Calls click_perform() for every new hex the mouse is dragged into.
00199      * @todo partial actions support and merging of many drag actions into one
00200      */
00201     editor_action* drag_left(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
00202 
00203     /**
00204      * Calls click_perform for every new hex the mouse is dragged into.
00205      * @todo partial actions support and merging of many drag actions into one
00206      */
00207     editor_action* drag_right(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
00208 
00209     /**
00210      * End of dragging.
00211      * @todo partial actions (the entire drag should end up as one action)
00212      */
00213     editor_action* drag_end(editor_display& disp, int x, int y);
00214 
00215 protected:
00216     /** Brush accessor */
00217     const brush& get_brush();
00218 
00219     /**
00220      * The previous hex dragged into.
00221      * @todo keep a set of all "visited" locations to reduce action count in long drags that hit the same hexes multiple times?
00222      */
00223     map_location previous_drag_hex_;
00224 
00225 private:
00226     /**
00227      * Template helper gathering actions common for both drag_right and drag_left.
00228      * The drags differ only in the worker function called, which should be
00229      * passed as the template parameter. This exists only to avoid copy-pasting code.
00230      */
00231     template <editor_action* (brush_drag_mouse_action::*perform_func)(editor_display&, const std::set<map_location>&)>
00232     editor_action* drag_generic(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
00233 
00234     /**
00235      * Current brush handle. Currently a pointer-to-pointer with full constness.
00236      * The mouse action does not modify the brush, does not modify the pointer
00237      * to the current brush, and we allow setting this pointr only once, hence
00238      * the three "consts".
00239      */
00240     const brush* const * const brush_;
00241 };
00242 
00243 /**
00244  * Brush paint mouse action. Uses keyboard modifiers for one-layer painting.
00245  */
00246 class mouse_action_paint : public brush_drag_mouse_action
00247 {
00248 public:
00249     mouse_action_paint(
00250         const brush* const * const brush, const CKey& key, terrain_palette& palette)
00251     : brush_drag_mouse_action(palette, brush, key)
00252     , terrain_palette_(palette)
00253     {
00254     }
00255 
00256     /**
00257      * Handle terrain sampling before calling generic handler
00258      */
00259     editor_action* click_left(editor_display& disp, int x, int y);
00260 
00261     /**
00262      * Handle terrain sampling before calling generic handler
00263      */
00264     editor_action* click_right(editor_display& disp, int x, int y);
00265 
00266     /**
00267      * Create an appropriate editor_action and return it
00268      */
00269     editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes);
00270 
00271     /**
00272      * Create an appropriate editor_action and return it
00273      */
00274     editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes);
00275 
00276     void set_mouse_overlay(editor_display& disp);
00277 
00278 protected:
00279 
00280     const terrain_palette& terrain_palette_;
00281 
00282     //t_translation::t_terrain& terrain_left_;
00283     //t_translation::t_terrain& terrain_right_;
00284 };
00285 
00286 /**
00287  * Select (and deselect) action, by brush or "magic wand" (via keyboard modifier)
00288  */
00289 class mouse_action_select : public brush_drag_mouse_action
00290 {
00291 public:
00292     mouse_action_select(const brush* const * const brush, const CKey& key, empty_palette& palette)
00293     : brush_drag_mouse_action(palette, brush, key)
00294     {
00295     }
00296 
00297     /**
00298      * Overridden to allow special behaviour based on modifier keys
00299      */
00300     std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
00301 
00302     /**
00303      * Force a fake "move" event to update brush overlay on key event
00304      */
00305     editor_action* key_event(editor_display& disp, const SDL_Event& e);
00306 
00307     /**
00308      * Left click/drag selects
00309      */
00310     editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes);
00311 
00312     /**
00313      * Right click/drag deselects
00314      */
00315     editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes);
00316 
00317     virtual void set_mouse_overlay(editor_display& disp);
00318 };
00319 
00320 /**
00321  * Paste action. No dragging capabilities.
00322  */
00323 class mouse_action_paste : public mouse_action
00324 {
00325 public:
00326     mouse_action_paste(const map_fragment& paste, const CKey& key, common_palette& palette)
00327     : mouse_action(palette, key), paste_(paste)
00328     {
00329     }
00330 
00331     bool has_context_menu() const;
00332 
00333     /**
00334      * Show an outline of where the paste will go
00335      */
00336     std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
00337 
00338     /**
00339      * Return a paste with offset action
00340      */
00341     editor_action* click_left(editor_display& disp, int x, int y);
00342 
00343     /**
00344      * Right click does nothing for now
00345      */
00346     editor_action* click_right(editor_display& disp, int x, int y);
00347 
00348     virtual void set_mouse_overlay(editor_display& disp);
00349 
00350 protected:
00351     /**
00352      * Reference to the buffer used for pasting (e.g. the clipboard)
00353      */
00354     const map_fragment& paste_;
00355 };
00356 
00357 /**
00358  * Fill action. No dragging capabilities. Uses keyboard modifiers for one-layer painting.
00359  */
00360 class mouse_action_fill : public mouse_action
00361 {
00362 public:
00363     mouse_action_fill(
00364         //  t_translation::t_terrain& terrain_left, t_translation::t_terrain& terrain_right,
00365             const CKey& key, terrain_palette& terrain_palette)
00366     : mouse_action(terrain_palette, key)
00367     , terrain_palette_(terrain_palette)
00368 //  , terrain_left_(terrain_left)
00369 //  , terrain_right_(terrain_right)
00370     {
00371     }
00372 
00373     /**
00374      * Tiles that will be painted to, possibly use modifier keys here
00375      */
00376     std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
00377 
00378     /**
00379      * Left / right click fills with the respective terrain
00380      */
00381     editor_action* click_left(editor_display& disp, int x, int y);
00382 
00383     /**
00384      * Left / right click fills with the respective terrain
00385      */
00386     editor_action* click_right(editor_display& disp, int x, int y);
00387 
00388     virtual void set_mouse_overlay(editor_display& disp);
00389 
00390 protected:
00391     const terrain_palette& terrain_palette_;
00392     //t_translation::t_terrain& terrain_left_;
00393     //t_translation::t_terrain& terrain_right_;
00394 };
00395 
00396 /**
00397  * Set starting position action.
00398  */
00399 class mouse_action_starting_position : public mouse_action
00400 {
00401 public:
00402     mouse_action_starting_position(const CKey& key, empty_palette& palette)
00403     : mouse_action(palette, key), click_(false)
00404     {
00405     }
00406 
00407     /**
00408      * Left click displays a player-number-selector dialog and then creates an action
00409      * or returns NULL if cancel was pressed or there would be no change.
00410      * Do this on mouse up to avoid drag issue.
00411      */
00412     editor_action* up_left(editor_display& disp, int x, int y);
00413 
00414     editor_action* click_left(editor_display& disp, int x, int y);
00415     /**
00416      * Right click only erases the starting position if there is one.
00417      * Do this on mouse up to avoid drag issue,
00418      */
00419     editor_action* up_right(editor_display& disp, int x, int y);
00420 
00421     editor_action* click_right(editor_display& disp, int x, int y);
00422 
00423     virtual void set_mouse_overlay(editor_display& disp);
00424 
00425 private:
00426     bool click_;
00427 };
00428 
00429 
00430 
00431 } //end namespace editor
00432 
00433 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:49 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs