editor2: mouse actions cleanup and doc comments
This commit is contained in:
parent
7117f272ff
commit
50bbdb55fb
3 changed files with 133 additions and 52 deletions
|
@ -855,7 +855,7 @@ void editor_controller::mouse_motion(int x, int y, const bool browse, bool updat
|
|||
refresh_after_action(true);
|
||||
}
|
||||
} else {
|
||||
get_mouse_action()->move(*gui_, x, y);
|
||||
get_mouse_action()->move(*gui_, hex_clicked);
|
||||
}
|
||||
gui().highlight_hex(hex_clicked);
|
||||
}
|
||||
|
|
|
@ -25,9 +25,19 @@
|
|||
|
||||
namespace editor2 {
|
||||
|
||||
void mouse_action::move(editor_display& disp, int x, int y)
|
||||
void mouse_action::move(editor_display& disp, const gamemap::location& hex)
|
||||
{
|
||||
previous_move_hex_ = disp.hex_clicked_on(x, y);
|
||||
if (hex != previous_move_hex_) {
|
||||
disp.set_brush_locs(affected_hexes(disp, hex));
|
||||
previous_move_hex_ = hex;
|
||||
}
|
||||
}
|
||||
|
||||
std::set<gamemap::location> mouse_action::affected_hexes(editor_display& disp, const gamemap::location& hex)
|
||||
{
|
||||
std::set<gamemap::location> res;
|
||||
res.insert(hex);
|
||||
return res;
|
||||
}
|
||||
|
||||
editor_action* mouse_action::drag(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo)
|
||||
|
@ -45,23 +55,6 @@ editor_action* mouse_action::key_event(editor_display& disp, const SDL_Event& e)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void brush_drag_mouse_action::move(editor_display& disp, int x, int y)
|
||||
{
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
move(disp, hex);
|
||||
}
|
||||
|
||||
void brush_drag_mouse_action::move(editor_display& disp, const gamemap::location& hex)
|
||||
{
|
||||
LOG_ED << "Move" << hex << previous_move_hex_ << "\n";
|
||||
if (hex != previous_move_hex_) {
|
||||
LOG_ED << "setMove\n";
|
||||
disp.set_brush_locs(affected_hexes(disp, hex));
|
||||
previous_move_hex_ = hex;
|
||||
}
|
||||
}
|
||||
|
||||
std::set<gamemap::location> brush_drag_mouse_action::affected_hexes(
|
||||
editor_display& disp, const gamemap::location& hex)
|
||||
{
|
||||
|
@ -77,8 +70,8 @@ editor_action* brush_drag_mouse_action::click(editor_display& disp, int x, int y
|
|||
|
||||
editor_action* brush_drag_mouse_action::drag(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo)
|
||||
{
|
||||
move(disp, x, y);
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
move(disp, hex);
|
||||
if (hex != previous_drag_hex_) {
|
||||
editor_action* a = click_perform(disp, affected_hexes(disp, hex));
|
||||
previous_drag_hex_ = hex;
|
||||
|
@ -107,6 +100,7 @@ editor_action* mouse_action_paint::click_perform(editor_display& disp, const std
|
|||
return new editor_action_paint_area(hexes, terrain_, one_layer);
|
||||
}
|
||||
|
||||
|
||||
editor_action* mouse_action_select::click(editor_display& disp, int x, int y)
|
||||
{
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
|
@ -145,11 +139,10 @@ editor_action* mouse_action_select::click_perform(editor_display& disp, const st
|
|||
}
|
||||
|
||||
|
||||
void mouse_action_paste::move(editor_display& disp, int x, int y)
|
||||
std::set<gamemap::location> mouse_action_paste::affected_hexes(
|
||||
editor_display& disp, const gamemap::location& hex)
|
||||
{
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
std::set<gamemap::location> affected = paste_.get_offset_area(hex);
|
||||
disp.set_brush_locs(affected);
|
||||
return paste_.get_offset_area(hex);
|
||||
}
|
||||
|
||||
editor_action* mouse_action_paste::click(editor_display& disp, int x, int y)
|
||||
|
@ -160,11 +153,10 @@ editor_action* mouse_action_paste::click(editor_display& disp, int x, int y)
|
|||
}
|
||||
|
||||
|
||||
void mouse_action_fill::move(editor_display& disp, int x, int y)
|
||||
std::set<gamemap::location> mouse_action_fill::affected_hexes(
|
||||
editor_display& disp, const gamemap::location& hex)
|
||||
{
|
||||
std::set<gamemap::location> affected =
|
||||
disp.map().get_contigious_terrain_tiles(disp.hex_clicked_on(x, y));
|
||||
disp.set_brush_locs(affected);
|
||||
return disp.map().get_contigious_terrain_tiles(hex);
|
||||
}
|
||||
|
||||
editor_action* mouse_action_fill::click(editor_display& disp, int x, int y)
|
||||
|
@ -178,16 +170,6 @@ editor_action* mouse_action_fill::click(editor_display& disp, int x, int y)
|
|||
}
|
||||
|
||||
|
||||
void mouse_action_starting_position::move(editor_display& disp, int x, int y)
|
||||
{
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
if (hex != previous_move_hex_) {
|
||||
disp.clear_brush_locs();
|
||||
disp.add_brush_loc(disp.hex_clicked_on(x, y));
|
||||
previous_move_hex_ = disp.hex_clicked_on(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
editor_action* mouse_action_starting_position::click(editor_display& disp, int x, int y)
|
||||
{
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
|
|
|
@ -24,11 +24,12 @@ class CKey;
|
|||
|
||||
namespace editor2 {
|
||||
|
||||
|
||||
/**
|
||||
* A mouse action receives events from the controller, and responds to them by creating
|
||||
* an appropriate editor_action object. Mouse actions may store some temporary data
|
||||
* such as the last clicked hex for better handling of click-drag.
|
||||
* such as the last clicked hex for better handling of click-drag. They should *not* modify
|
||||
* the map or trigger refreshes, but may set brush locations and similar overlays that
|
||||
* should be visible around the mouse cursor, hence the display references are not const.
|
||||
*/
|
||||
class mouse_action
|
||||
{
|
||||
|
@ -40,36 +41,63 @@ public:
|
|||
|
||||
virtual ~mouse_action() {}
|
||||
|
||||
virtual void move(editor_display& disp, int x, int y);
|
||||
/**
|
||||
* Mouse move (not a drag). Never changes anything (other than temporary highlihts and similar)
|
||||
*/
|
||||
void move(editor_display& disp, const gamemap::location& hex);
|
||||
|
||||
/**
|
||||
* Locations that would be affected by a click, used by move to update highlights. Defauts to higlight the mouseover hex.
|
||||
* Maybe also used for actually performing the action in click() or drag().
|
||||
*/
|
||||
virtual std::set<gamemap::location> affected_hexes(editor_display& disp, const gamemap::location& hex);
|
||||
|
||||
/**
|
||||
* A click, possibly the beginning of a drag
|
||||
* A click, possibly the beginning of a drag. Must be overriden.
|
||||
*/
|
||||
virtual editor_action* click(editor_display& disp, int x, int y) = 0;
|
||||
|
||||
/**
|
||||
* Drag operation. A click should have occured earlier.
|
||||
* Drag operation. A click should have occured earlier. Defaults to no action.
|
||||
*/
|
||||
virtual editor_action* drag(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
|
||||
|
||||
/**
|
||||
* The end of dragging.
|
||||
* The end of dragging. Defaults to no action.
|
||||
*/
|
||||
virtual editor_action* drag_end(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* Function called by the controller on a key event for the current mouse action.
|
||||
* Defaults to no action.
|
||||
*/
|
||||
virtual editor_action* key_event(editor_display& disp, const SDL_Event& e);
|
||||
|
||||
/**
|
||||
* Helper variable setter and getter - pointer to a toolbar menu/button used for highlighting
|
||||
* the current action. Should always be NULL or point to a valid menu.
|
||||
*/
|
||||
void set_toolbar_button(const theme::menu* value) { toolbar_button_ = value; }
|
||||
const theme::menu* toolbar_button() const { return toolbar_button_; }
|
||||
|
||||
protected:
|
||||
/**
|
||||
* The hex previously used in move operations
|
||||
*/
|
||||
gamemap::location previous_move_hex_;
|
||||
|
||||
/**
|
||||
* Key presses, used for modifiers (alt, shift) in some operations
|
||||
*/
|
||||
const CKey& key_;
|
||||
|
||||
private:
|
||||
const theme::menu* toolbar_button_;
|
||||
};
|
||||
|
||||
/**
|
||||
* A brush-drag mouse action base class which adds brush and drag processing to a basic mouse action
|
||||
*/
|
||||
class brush_drag_mouse_action : public mouse_action
|
||||
{
|
||||
public:
|
||||
|
@ -77,20 +105,49 @@ public:
|
|||
: mouse_action(key), brush_(brush)
|
||||
{
|
||||
}
|
||||
void move(editor_display& disp, int x, int y);
|
||||
void move(editor_display& disp, const gamemap::location& hex);
|
||||
virtual std::set<gamemap::location> affected_hexes(editor_display& disp, const gamemap::location& hex);
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* The affected hexes of a brush action are the result of projecting the current brush on the mouseover hex
|
||||
*/
|
||||
std::set<gamemap::location> affected_hexes(editor_display& disp, const gamemap::location& hex);
|
||||
|
||||
/**
|
||||
* The actual action function which is called by click() and drag(). Derived classes override this instead of click() and drag().
|
||||
*/
|
||||
virtual editor_action* click_perform(editor_display& disp, const std::set<gamemap::location>& hexes) = 0;
|
||||
|
||||
/**
|
||||
* Calls click_perform()
|
||||
*/
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* Calls click_perform() for every new hex the mouse is dragged into.
|
||||
* @todo partial actions support and merging of many drag actions into one
|
||||
*/
|
||||
editor_action* drag(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo);
|
||||
|
||||
editor_action* drag_end(editor_display& disp, int x, int y);
|
||||
|
||||
protected:
|
||||
/** Brush accessor */
|
||||
const brush& get_brush();
|
||||
|
||||
/**
|
||||
* The previous hex dragged into.
|
||||
* @todo keep a set of all "visited" locations to reduce action count in long drags that hit the same hexes multiple times?
|
||||
*/
|
||||
gamemap::location previous_drag_hex_;
|
||||
private:
|
||||
/**
|
||||
* Current brush handle
|
||||
*/
|
||||
const brush* const * const brush_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Brush paint mouse action. Uses keyboard modifiers for one-layer painting.
|
||||
*/
|
||||
class mouse_action_paint : public brush_drag_mouse_action
|
||||
{
|
||||
public:
|
||||
|
@ -98,11 +155,18 @@ public:
|
|||
: brush_drag_mouse_action(brush, key), terrain_(terrain)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an appropriate editor_action and return it
|
||||
*/
|
||||
editor_action* click_perform(editor_display& disp, const std::set<gamemap::location>& hexes);
|
||||
protected:
|
||||
const t_translation::t_terrain& terrain_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Select (and deselect) action, by brush or "magic wand" (via keyboard modifier)
|
||||
*/
|
||||
class mouse_action_select : public brush_drag_mouse_action
|
||||
{
|
||||
public:
|
||||
|
@ -110,14 +174,36 @@ public:
|
|||
: brush_drag_mouse_action(brush, key), selecting_(true)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Overriden to allow special behaviour based on modifier keys
|
||||
*/
|
||||
std::set<gamemap::location> affected_hexes(editor_display& disp, const gamemap::location& hex);
|
||||
|
||||
/**
|
||||
* Force a fake "move" event to update brush overkay on key event
|
||||
*/
|
||||
editor_action* key_event(editor_display& disp, const SDL_Event& e);
|
||||
|
||||
/**
|
||||
* Click is overriden to set selecting_ according to the whether the mouseover hex is selecte, and call base class' click*()
|
||||
*/
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* Return a select or deselect action based on the state of selecting_ flag
|
||||
*/
|
||||
editor_action* click_perform(editor_display& disp, const std::set<gamemap::location>& hexes);
|
||||
protected:
|
||||
/**
|
||||
* Selecting (true) or deselecting (false) flag used in dragging
|
||||
*/
|
||||
bool selecting_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Paste action. No dragging capabilities.
|
||||
*/
|
||||
class mouse_action_paste : public mouse_action
|
||||
{
|
||||
public:
|
||||
|
@ -125,12 +211,15 @@ public:
|
|||
: mouse_action(key), paste_(paste)
|
||||
{
|
||||
}
|
||||
void move(editor_display& disp, int x, int y);
|
||||
std::set<gamemap::location> affected_hexes(editor_display& disp, const gamemap::location& hex);
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
protected:
|
||||
const map_fragment& paste_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fill action. No dragging capabilities. Uses keyboard modifiers for one-layer painting.
|
||||
*/
|
||||
class mouse_action_fill : public mouse_action
|
||||
{
|
||||
public:
|
||||
|
@ -138,12 +227,15 @@ public:
|
|||
: mouse_action(key), terrain_(terrain)
|
||||
{
|
||||
}
|
||||
void move(editor_display& disp, int x, int y);
|
||||
std::set<gamemap::location> affected_hexes(editor_display& disp, const gamemap::location& hex);
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
protected:
|
||||
const t_translation::t_terrain& terrain_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set starting position action.
|
||||
*/
|
||||
class mouse_action_starting_position : public mouse_action
|
||||
{
|
||||
public:
|
||||
|
@ -151,8 +243,15 @@ public:
|
|||
: mouse_action(key)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows setting/clearing the starting positions in the mouseover hexes via keyboard
|
||||
*/
|
||||
editor_action* key_event(editor_display& disp, const SDL_Event& e);
|
||||
void move(editor_display& disp, int x, int y);
|
||||
|
||||
/**
|
||||
* Click displays a player-number-selector dialog and then creates an action (or not, if cancel was pressed).
|
||||
*/
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue