The Battle for Wesnoth  1.17.0-dev
mouse_events.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include "game_display.hpp" // for game_display -> display conversion.
18 #include "map/location.hpp" // for map_location
19 #include "mouse_handler_base.hpp" // for mouse_handler_base
20 #include "pathfind/pathfind.hpp" // for marked_route, paths
21 #include "units/map.hpp" // for unit_map, etc
22 
23 #include <set> // for set
24 #include <vector> // for vector
25 #include <SDL2/SDL_events.h> // for SDL_MouseButtonEvent
26 
27 class game_display;
28 class battle_context; // lines 23-23
29 class play_controller;
30 class team;
31 class unit;
32 
33 namespace events{
34 
36 public:
39  static mouse_handler* get_singleton() { return singleton_ ;}
40  void set_side(int side_number);
41  void cycle_units(const bool browse, const bool reverse = false);
42  void cycle_back_units(const bool browse) { cycle_units(browse, true); }
43 
44  int get_path_turns() const { return path_turns_; }
45 
46  /**
47  * @param loc the location occupied by the enemy
48  * @returns the location from which the selected unit can attack the enemy
49  */
51  const pathfind::paths& current_paths() const { return current_paths_; }
52 
53  const map_location& get_last_hex() const { return last_hex_; }
55  void set_path_turns(const int path_turns) { path_turns_ = path_turns; }
56  void set_current_paths(const pathfind::paths & new_paths);
57  void deselect_hex();
59 
60  void set_gui(game_display* gui) { gui_ = gui; }
61 
63 
64  pathfind::marked_route get_route(const unit* un, map_location go_to, team &team) const;
65 
67 
68  //get visible adjacent enemies of 1-based side around location loc
69  std::set<map_location> get_adj_enemies(const map_location& loc, int side) const;
70 
71  // show the attack dialog and return the choice made
72  // which can be invalid if 'cancel' was used
73  int show_attack_dialog(const map_location& attacker_loc, const map_location& defender_loc);
74  // wrapper to catch bad_alloc so this should be called
75  void attack_enemy(const map_location& attacker_loc, const map_location& defender_loc, int choice);
76 
77  /** Moves a unit across the board for a player. */
78  std::size_t move_unit_along_route(const std::vector<map_location> & steps, bool & interrupted);
79 
80  void select_hex(const map_location& hex, const bool browse,
81  const bool highlight = true,
82  const bool fire_event = true);
83 
84  void move_action(bool browse);
85 
86  void touch_action(const map_location hex, bool browse);
87 
88  void select_or_action(bool browse);
89 
90  /**
91  * Uses SDL and @ref game_display::hex_clicked_on
92  * to fetch the hex the mouse is hovering, if applicable.
93  */
94  const map_location hovered_hex() const;
95 
96  /** Unit exists on the hex, no matter if friend or foe. */
97  bool hex_hosts_unit(const map_location& hex) const;
98 
99  /**
100  * Use this to disable hovering an unit from highlighting its movement
101  * range.
102  *
103  * @see enable_units_highlight()
104  */
106 
107  /**
108  * When unit highlighting is disabled, call this when the mouse no
109  * longer hovers any unit to enable highlighting again.
110  *
111  * @see disable_units_highlight()
112  */
113  void enable_units_highlight();
114 
115 protected:
116  /**
117  * Due to the way this class is constructed we can assume that the
118  * display* gui_ member actually points to a game_display (derived class)
119  */
120  game_display& gui() { return *gui_; }
121  /** Const version */
122  const game_display& gui() const { return *gui_; }
123 
124  int drag_threshold() const;
125  /**
126  * Use update to force an update of the mouse state.
127  */
128  void mouse_motion(int x, int y, const bool browse, bool update=false, map_location loc = map_location::null_location());
129  bool right_click_show_menu(int x, int y, const bool browse);
130 // bool left_click(int x, int y, const bool browse);
132 
133  void touch_motion(int x, int y, const bool browse, bool update=false, map_location loc = map_location::null_location());
134 
135  void save_whiteboard_attack(const map_location& attacker_loc, const map_location& defender_loc, int weapon_choice);
136 
137  // fill weapon choices into bc_vector
138  // return the best weapon choice
139  int fill_weapon_choices(std::vector<battle_context>& bc_vector, unit_map::iterator attacker, unit_map::iterator defender);
140  // the real function but can throw bad_alloc
141  // choice is the attack chosen in the attack dialog
142  void attack_enemy_(const map_location& attacker_loc
143  , const map_location& defender_loc
144  , int choice);
145 
149  /*
150  * These return raw pointers instead of smart pointers.
151  * Useful if you don't want to increase the unit reference count.
152  */
154  const unit* find_unit_nonowning(const map_location& hex) const;
156 private:
157  team& viewing_team();
158  const team& viewing_team() const;
159  team &current_team();
160 
163 
164  // previous highlighted hexes
165  // the hex of the selected unit and empty hex are "free"
176 
180 
182 
184 };
185 
186 }
pathfind::marked_route get_route(const unit *un, map_location go_to, team &team) const
void set_current_paths(const pathfind::paths &new_paths)
int fill_weapon_choices(std::vector< battle_context > &bc_vector, unit_map::iterator attacker, unit_map::iterator defender)
This class represents a single unit of a specific type.
Definition: unit.hpp:120
unit_map::const_iterator find_unit(const map_location &hex) const
const pathfind::marked_route & get_current_route() const
map_location selected_hex_
pathfind::paths current_paths_
const map_location hovered_hex() const
Uses SDL and game_display::hex_clicked_on to fetch the hex the mouse is hovering, if applicable...
const pathfind::paths & current_paths() const
General purpose widgets.
bool move_unit_along_current_route()
Moves a unit along the currently cached route.
bool unit_in_cycle(unit_map::const_iterator it)
void touch_motion(int x, int y, const bool browse, bool update=false, map_location loc=map_location::null_location())
map_location previous_hex_
void mouse_motion(int x, int y, const bool browse, bool update=false, map_location loc=map_location::null_location())
Use update to force an update of the mouse state.
void select_hex(const map_location &hex, const bool browse, const bool highlight=true, const bool fire_event=true)
static mouse_handler * singleton_
void touch_action(const map_location hex, bool browse)
map_location get_selected_hex() const
const map_location & get_last_hex() const
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:71
static mouse_handler * get_singleton()
bool hex_hosts_unit(const map_location &hex) const
Unit exists on the hex, no matter if friend or foe.
void disable_units_highlight()
Use this to disable hovering an unit from highlighting its movement range.
int drag_threshold() const
Minimum dragging distance to fire the drag&drop.
std::size_t move_unit_along_route(const std::vector< map_location > &steps, bool &interrupted)
Moves a unit across the board for a player.
void set_path_turns(const int path_turns)
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:172
std::set< map_location > get_adj_enemies(const map_location &loc, int side) const
void set_gui(game_display *gui)
Structure which holds a single route and marks for special events.
Definition: pathfind.hpp:140
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...
const game_display & gui() const
Const version.
Encapsulates the map of the game.
Definition: location.hpp:37
void cycle_back_units(const bool browse)
void enable_units_highlight()
When unit highlighting is disabled, call this when the mouse no longer hovers any unit to enable high...
bool fire_event(const ui_event event, std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
int get_path_turns() const
map_location current_unit_attacks_from(const map_location &loc) const
void attack_enemy(const map_location &attacker_loc, const map_location &defender_loc, int choice)
unit_map::iterator selected_unit()
void save_whiteboard_attack(const map_location &attacker_loc, const map_location &defender_loc, int weapon_choice)
map_location previous_free_hex_
mouse_handler(game_display *gui, play_controller &pc)
void cycle_units(const bool browse, const bool reverse=false)
play_controller & pc_
Handling of system events.
Definition: manager.hpp:42
map_location last_hex_
last highlighted hex
void show_attack_options(const unit_map::const_iterator &u)
Causes attackable hexes to be highlighted.
pathfind::marked_route current_route_
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:71
static const map_location & null_location()
Definition: location.hpp:80
unit * find_unit_nonowning(const map_location &hex)
int side_number
Definition: game_info.hpp:39
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:203
void set_side(int side_number)
void attack_enemy_(const map_location &attacker_loc, const map_location &defender_loc, int choice)
game_display & gui()
Due to the way this class is constructed we can assume that the display* gui_ member actually points ...
This module contains various pathfinding functions and utilities.
int show_attack_dialog(const map_location &attacker_loc, const map_location &defender_loc)
void move_action(bool browse)
Overridden in derived class.
void select_or_action(bool browse)