The Battle for Wesnoth  1.19.0+dev
mouse_events.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
3  by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #pragma once
18 
19 #include "game_display.hpp" // for game_display -> display conversion.
20 #include "map/location.hpp" // for map_location
21 #include "mouse_handler_base.hpp" // for mouse_handler_base
22 #include "pathfind/pathfind.hpp" // for marked_route, paths
23 #include "units/map.hpp" // for unit_map, etc
24 
25 #include <set> // for set
26 #include <vector> // for vector
27 #include <SDL2/SDL_events.h> // for SDL_MouseButtonEvent
28 
29 class battle_context; // lines 23-23
30 class play_controller;
31 class team;
32 class unit;
33 
34 namespace events{
35 
37 public:
40  static mouse_handler* get_singleton() { return singleton_ ;}
41  void set_side(int side_number);
42  void cycle_units(const bool browse, const bool reverse = false);
43  void cycle_back_units(const bool browse) { cycle_units(browse, true); }
44 
45  int get_path_turns() const { return path_turns_; }
46 
47  /**
48  * @param loc the location occupied by the enemy
49  * @returns the location from which the selected unit can attack the enemy
50  */
52  const pathfind::paths& current_paths() const { return current_paths_; }
53 
54  const map_location& get_last_hex() const { return last_hex_; }
56  void set_path_turns(const int path_turns) { path_turns_ = path_turns; }
57  void set_current_paths(const pathfind::paths & new_paths);
58  void deselect_hex();
60 
62 
64 
65  pathfind::marked_route get_route(const unit* un, map_location go_to, team &team) const;
66 
68 
69  //get visible adjacent enemies of 1-based side around location loc
70  std::set<map_location> get_adj_enemies(const map_location& loc, int side) const;
71 
72  // show the attack dialog and return the choice made
73  // which can be invalid if 'cancel' was used
74  int show_attack_dialog(const map_location& attacker_loc, const map_location& defender_loc);
75  // wrapper to catch bad_alloc so this should be called
76  void attack_enemy(const map_location& attacker_loc, const map_location& defender_loc, int choice);
77 
78  /** Moves a unit across the board for a player. */
79  std::size_t move_unit_along_route(const std::vector<map_location> & steps, bool & interrupted);
80 
81  void select_hex(const map_location& hex, const bool browse,
82  const bool highlight = true,
83  const bool fire_event = true);
84 
85  void move_action(bool browse) override;
86  void teleport_action();
87 
88  void touch_action(const map_location hex, bool browse) override;
89 
90  void select_or_action(bool browse);
91  void select_teleport();
92 
93  /**
94  * Uses SDL and @ref game_display::hex_clicked_on
95  * to fetch the hex the mouse is hovering, if applicable.
96  */
97  const map_location hovered_hex() const;
98 
99  /** Unit exists on the hex, no matter if friend or foe. */
100  bool hex_hosts_unit(const map_location& hex) const;
101 
102  /**
103  * Use this to disable hovering an unit from highlighting its movement
104  * range.
105  *
106  * @see enable_units_highlight()
107  */
109 
110  /**
111  * When unit highlighting is disabled, call this when the mouse no
112  * longer hovers any unit to enable highlighting again.
113  *
114  * @see disable_units_highlight()
115  */
116  void enable_units_highlight();
117 
118 protected:
119  /**
120  * Due to the way this class is constructed we can assume that the
121  * display* gui_ member actually points to a game_display (derived class)
122  */
123  game_display& gui() override { return *gui_; }
124  /** Const version */
125  const game_display& gui() const override { return *gui_; }
126 
127  int drag_threshold() const override;
128  /**
129  * Use update to force an update of the mouse state.
130  */
131  void mouse_motion(int x, int y, const bool browse, bool update=false, map_location loc = map_location::null_location()) override;
132  bool mouse_button_event(const SDL_MouseButtonEvent& event, uint8_t button, map_location loc, bool click = false) override;
133  bool right_click_show_menu(int x, int y, const bool browse) override;
134 // bool left_click(int x, int y, const bool browse);
136 
137  void touch_motion(int x, int y, const bool browse, bool update=false, map_location loc = map_location::null_location()) override;
138 
139  void save_whiteboard_attack(const map_location& attacker_loc, const map_location& defender_loc, int weapon_choice);
140 
141  // fill weapon choices into bc_vector
142  // return the best weapon choice
143  int fill_weapon_choices(std::vector<battle_context>& bc_vector, unit_map::iterator attacker, unit_map::iterator defender);
144  // the real function but can throw bad_alloc
145  // choice is the attack chosen in the attack dialog
146  void attack_enemy_(const map_location& attacker_loc
147  , const map_location& defender_loc
148  , int choice);
149 
153  /*
154  * These return raw pointers instead of smart pointers.
155  * Useful if you don't want to increase the unit reference count.
156  */
158  const unit* find_unit_nonowning(const map_location& hex) const;
160 private:
161  team& viewing_team();
162  const team& viewing_team() const;
163  team &current_team();
164 
165  // Some common code from mouse_motion and touch_motion.
166  /**
167  * Highlight the hexes that a unit can move to.
168  *
169  * Based on the currently selected hex, selected unit and what's being moused-over,
170  * conditionally draw any planned moves for the unit passed as an argument.
171  */
172  void show_reach_for_unit(const unit_ptr& un);
173 
176 
177  // previous highlighted hexes
178  // the hex of the selected unit and empty hex are "free"
184  /**
185  * If non-empty, current_paths_.destinations contains a cache of highlighted
186  * hexes, likely the movement range or attack range of a unit.
187  */
193 
198 
200 
202 };
203 
204 }
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:167
map_location last_hex_
last highlighted hex
void disable_units_highlight()
Use this to disable hovering an unit from highlighting its movement range.
bool move_unit_along_current_route()
Moves a unit along the currently cached route.
bool mouse_button_event(const SDL_MouseButtonEvent &event, uint8_t button, map_location loc, bool click=false) override
int show_attack_dialog(const map_location &attacker_loc, const map_location &defender_loc)
void cycle_back_units(const bool browse)
map_location previous_hex_
void show_reach_for_unit(const unit_ptr &un)
Highlight the hexes that a unit can move to.
game_display & gui() override
Due to the way this class is constructed we can assume that the display* gui_ member actually points ...
void set_gui(game_display *gui)
static mouse_handler * get_singleton()
const pathfind::paths & current_paths() const
void select_or_action(bool browse)
void attack_enemy_(const map_location &attacker_loc, const map_location &defender_loc, int choice)
void save_whiteboard_attack(const map_location &attacker_loc, const map_location &defender_loc, int weapon_choice)
const game_display & gui() const override
Const version.
void set_current_paths(const pathfind::paths &new_paths)
play_controller & pc_
unit_map::iterator selected_unit()
map_location get_selected_hex() const
static mouse_handler * singleton_
int fill_weapon_choices(std::vector< battle_context > &bc_vector, unit_map::iterator attacker, unit_map::iterator defender)
void enable_units_highlight()
When unit highlighting is disabled, call this when the mouse no longer hovers any unit to enable high...
void touch_motion(int x, int y, const bool browse, bool update=false, map_location loc=map_location::null_location()) override
void attack_enemy(const map_location &attacker_loc, const map_location &defender_loc, int choice)
std::set< map_location > get_adj_enemies(const map_location &loc, int side) const
map_location previous_free_hex_
int get_path_turns() const
void select_hex(const map_location &hex, const bool browse, const bool highlight=true, const bool fire_event=true)
unit_map::const_iterator find_unit(const map_location &hex) const
map_location current_unit_attacks_from(const map_location &loc) const
mouse_handler(game_display *gui, play_controller &pc)
bool right_click_show_menu(int x, int y, const bool browse) override
Called in the default right_click when the context menu is about to be shown, can be used for preproc...
map_location selected_hex_
pathfind::marked_route current_route_
const map_location hovered_hex() const
Uses SDL and game_display::hex_clicked_on to fetch the hex the mouse is hovering, if applicable.
void touch_action(const map_location hex, bool browse) override
bool unit_in_cycle(unit_map::const_iterator it)
void set_side(int side_number)
void move_action(bool browse) override
Overridden in derived class.
pathfind::paths current_paths_
If non-empty, current_paths_.destinations contains a cache of highlighted hexes, likely the movement ...
const map_location & get_last_hex() const
bool hex_hosts_unit(const map_location &hex) const
Unit exists on the hex, no matter if friend or foe.
int drag_threshold() const override
Minimum dragging distance to fire the drag&drop.
void set_path_turns(const int path_turns)
unit * find_unit_nonowning(const map_location &hex)
const pathfind::marked_route & get_current_route() const
void cycle_units(const bool browse, const bool reverse=false)
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 show_attack_options(const unit_map::const_iterator &u)
Causes attackable hexes to be highlighted.
void mouse_motion(int x, int y, const bool browse, bool update=false, map_location loc=map_location::null_location()) override
Use update to force an update of the mouse state.
pathfind::marked_route get_route(const unit *un, map_location go_to, team &team) const
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
This class represents a single unit of a specific type.
Definition: unit.hpp:133
int side_number
Definition: game_info.hpp:40
static void update()
Handling of system events.
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
General purpose widgets.
bool click(int mousex, int mousey)
Definition: tooltips.cpp:294
This module contains various pathfinding functions and utilities.
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Encapsulates the map of the game.
Definition: location.hpp:38
static const map_location & null_location()
Definition: location.hpp:81
Structure which holds a single route and marks for special events.
Definition: pathfind.hpp:142
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:73