The Battle for Wesnoth  1.19.25+dev
game_display.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 class config;
19 class game_board;
20 
21 #include "display.hpp"
22 #include "display_chat_manager.hpp"
23 #include "pathfind/pathfind.hpp"
24 
25 
26 // This needs to be separate from display.h because of the static
27 // singleton member, which will otherwise trigger link failure
28 // when building the editor.
29 
30 class game_display : public display
31 {
32 public:
33  game_display(game_board& board,
34  std::weak_ptr<wb::manager> wb,
35  reports & reports_object,
36  const std::string& theme_id,
37  const config& level);
38 
39  ~game_display();
40 
41  game_display(const game_display&) = delete;
42  game_display& operator=(const game_display&) = delete;
43 
45  {
46  return static_cast<game_display*>(singleton_);
47  }
48 
49  /**
50  * Update lighting settings.
51  *
52  * Should be called on every new turn.
53  */
54  void new_turn();
55 
56  virtual const std::set<std::string>& observers() const override { return chat_man_->observers(); }
57  /**
58  * Scrolls to the leader of a certain side.
59  *
60  * This will normally be the playing team.
61  */
62  void scroll_to_leader(int side, SCROLL_TYPE scroll_type = ONSCREEN,bool force = true);
63 
64  /**
65  * Function to display a location as selected.
66  *
67  * If a unit is in the location, and there is no unit in the currently
68  * highlighted hex, the unit will be displayed in the sidebar.
69  */
70  virtual void select_hex(map_location hex) override;
71 
72  /**
73  * Function to highlight a location.
74  *
75  * If a unit is in the location, it will be displayed in the sidebar.
76  * Selection is used when a unit has been clicked on, while highlighting is
77  * used when a location has been moused over.
78  */
79  virtual void highlight_hex(map_location hex) override;
80 
81  /**
82  * Change the unit to be displayed in the sidebar.
83  *
84  * This is used when selecting or highlighting is not wanted.
85  */
87 
88  /**
89  * Sets the paths that are currently displayed as available
90  * for the unit to move along.
91  * All other paths will be grayed out.
92  */
93  void highlight_reach(const pathfind::paths &paths_list);
94 
95  /**
96  * Add more paths to highlight. Print numbers where they overlap.
97  * Used by Show Enemy Moves. If @a goal is not @c null_location, highlight
98  * enemy units that can reach @a goal.
99  */
100  void highlight_another_reach(const pathfind::paths &paths_list,
101  const map_location& goal = map_location::null_location());
102  /**
103  * Return the locations of units that can reach @a goal (@see highlight_another_reach()).
104  */
105  const std::set<map_location>& units_that_can_reach_goal() const { return units_that_can_reach_goal_; }
106 
107  /** Reset highlighting of paths. */
108  bool unhighlight_reach();
109 
110  /**
111  * Sets the route along which footsteps are drawn to show movement of a
112  * unit. If nullptr, no route is displayed. @a route does not have to remain
113  * valid after being set.
114  */
115  void set_route(const pathfind::marked_route *route);
116 
117  /**
118  * Gets the route along which footsteps are drawn to show movement of a
119  * unit. If no route is currently being shown, the array get_route().steps
120  * will be empty.
121  */
123 
124  /**
125  * Visualizes a path by flashing it white then fade out over 2 seconds.
126  * Typically used to provide feedback when a unit is assigned
127  * a multi-turn move that will be executed on future turns.
128  * @param route The route path that should flash fade.
129  */
130  void flash_fade_route(const pathfind::marked_route& route);
131 
132  static constexpr std::chrono::milliseconds FLASH_FADE_ROUTE_FLASH_DURATION{ 500 };
133  static constexpr std::chrono::milliseconds FLASH_FADE_ROUTE_FADE_DURATION{ 1500 };
134  static constexpr std::chrono::milliseconds FLASH_FADE_ROUTE_TOTAL_DURATION{ 2000 };
135 
136  /** Function to float a label above a tile */
137  void float_label(const map_location& loc, const std::string& text, const color_t& color);
138 
139  /** Draws the movement info (turns available) for a given location. */
140  void draw_movement_info(const map_location& loc);
141 
142  /** Function to invalidate that unit status displayed on the sidebar. */
144 
145  /** Same as invalidate_unit() if moving the displayed unit. */
147 
148  virtual const time_of_day& get_time_of_day(const map_location& loc) const override;
149 
150  virtual bool has_time_area() const override;
151 
152  /**
153  * TLD update() override. Replaces old pre_draw(). Be sure to call
154  * the base class method as well.
155  *
156  * game_display does specific things related e.g. to unit rendering
157  * and calls the whiteboard pre-draw method here.
158  */
159  virtual void update() override;
160 
161  /**
162  * TLD layout() override. Replaces old refresh_reports(). Be sure to
163  * call the base class method as well.
164  *
165  * This updates some reports, like clock, that need to be refreshed
166  * every frame.
167  */
168  virtual void layout() override;
169 
170  /**
171  * TLD render() override. Replaces old post_draw(). Be sure to call
172  * the base class method as well.
173  *
174  * This calls the whiteboard's post-draw method after rendering.
175  */
176  virtual void render() override;
177 
178 protected:
179  virtual void draw_invalidated() override;
180 
181  virtual void draw_hex(const map_location& loc) override;
182 
183  /** Inherited from display. */
184  virtual overlay_map& get_overlays() override;
185 
186  std::set<map_location> units_that_can_reach_goal_;
187 
188  std::vector<texture> get_reachmap_images(const map_location& loc) const;
189 
190 public:
191  /** Set the attack direction indicator. */
193  void clear_attack_indicator();
194  // TODO: compare reports::context::mhb()->current_unit_attacks_from()
196 
197  /** Function to get attack direction suffix. */
198  std::string attack_indicator_direction() const {
201  }
202 
203  virtual const map_location &displayed_unit_hex() const override { return displayedUnitHex_; }
204 
206 
207  void begin_game();
208 
209  virtual bool in_game() const override { return in_game_; }
210 
211  /**
212  * Sets the linger mode for the display.
213  * There have been some discussions on what to do with fog and shroud
214  * the extra variables make it easier to modify the behavior. There
215  * might even be a split between victory and defeat.
216  *
217  * @todo if the current implementation is wanted we can change
218  * the stuff back to a boolean
219  */
220  enum game_mode {
221  RUNNING, /**< no linger overlay, show fog and shroud. */
222  LINGER }; /**< linger overlay, show fog and shroud. */
223 
224  void set_game_mode(const game_mode mode);
225 
226  /** Sets whether the screen (map visuals) needs to be rebuilt. This is typically after the map has been changed by wml. */
227  void needs_rebuild(bool b);
228 
229  /** Rebuilds the screen if needs_rebuild(true) was previously called, and resets the flag. */
230  bool maybe_rebuild();
231 
232 private:
234 
235  // Locations of the attack direction indicator's parts
238 
240 
241  void invalidate_route();
242 
244 
246 
247  const std::unique_ptr<display_chat_manager> chat_man_;
248 
250 
252 
253  // Flash fading route for multi-turn movement feedback
255  std::chrono::steady_clock::time_point flash_fade_route_start_time_;
257 
259 
260 };
map_location loc
Definition: move.cpp:172
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:88
std::map< map_location, std::vector< overlay > > overlay_map
Definition: display.hpp:883
@ ONSCREEN
Definition: display.hpp:494
static display * singleton_
Definition: display.hpp:940
bool invalidateGameStatus_
Definition: display.hpp:738
Game board class.
Definition: game_board.hpp:47
virtual void draw_invalidated() override
Only called when there's actual redrawing to do.
void draw_movement_info(const map_location &loc)
Draws the movement info (turns available) for a given location.
game_display(const game_display &)=delete
void display_unit_hex(map_location hex)
Change the unit to be displayed in the sidebar.
virtual bool in_game() const override
void invalidate_unit_after_move(const map_location &src, const map_location &dst)
Same as invalidate_unit() if moving the displayed unit.
std::string attack_indicator_direction() const
Function to get attack direction suffix.
const map_location & get_attack_indicator_src()
const pathfind::marked_route & get_route()
Gets the route along which footsteps are drawn to show movement of a unit.
game_mode mode_
void flash_fade_route(const pathfind::marked_route &route)
Visualizes a path by flashing it white then fade out over 2 seconds.
virtual void highlight_hex(map_location hex) override
Function to highlight a location.
virtual const map_location & displayed_unit_hex() const override
Virtual functions shadowed in game_display.
map_location displayedUnitHex_
void invalidate_route()
std::vector< texture > get_reachmap_images(const map_location &loc) const
void scroll_to_leader(int side, SCROLL_TYPE scroll_type=ONSCREEN, bool force=true)
Scrolls to the leader of a certain side.
static constexpr std::chrono::milliseconds FLASH_FADE_ROUTE_FLASH_DURATION
void set_game_mode(const game_mode mode)
virtual void draw_hex(const map_location &loc) override
Redraws a single gamemap location.
const std::set< map_location > & units_that_can_reach_goal() const
Return the locations of units that can reach goal (.
void set_attack_indicator(const map_location &src, const map_location &dst)
Set the attack direction indicator.
const std::unique_ptr< display_chat_manager > chat_man_
pathfind::marked_route route_
virtual void render() override
TLD render() override.
void set_route(const pathfind::marked_route *route)
Sets the route along which footsteps are drawn to show movement of a unit.
virtual overlay_map & get_overlays() override
Inherited from display.
static constexpr std::chrono::milliseconds FLASH_FADE_ROUTE_TOTAL_DURATION
void update_flash_fade_route()
static constexpr std::chrono::milliseconds FLASH_FADE_ROUTE_FADE_DURATION
bool flash_fade_route_active_
std::set< map_location > units_that_can_reach_goal_
void invalidate_unit()
Function to invalidate that unit status displayed on the sidebar.
virtual const std::set< std::string > & observers() const override
virtual void select_hex(map_location hex) override
Function to display a location as selected.
pathfind::marked_route flash_fade_route_
static game_display * get_singleton()
void highlight_reach(const pathfind::paths &paths_list)
Sets the paths that are currently displayed as available for the unit to move along.
void new_turn()
Update lighting settings.
void clear_attack_indicator()
virtual const time_of_day & get_time_of_day(const map_location &loc) const override
void highlight_another_reach(const pathfind::paths &paths_list, const map_location &goal=map_location::null_location())
Add more paths to highlight.
map_location attack_indicator_dst_
game_mode
Sets the linger mode for the display.
@ RUNNING
no linger overlay, show fog and shroud.
std::chrono::steady_clock::time_point flash_fade_route_start_time_
bool unhighlight_reach()
Reset highlighting of paths.
void needs_rebuild(bool b)
Sets whether the screen (map visuals) needs to be rebuilt.
overlay_map overlay_map_
bool maybe_rebuild()
Rebuilds the screen if needs_rebuild(true) was previously called, and resets the flag.
virtual void layout() override
TLD layout() override.
virtual bool has_time_area() const override
display_chat_manager & get_chat_manager()
game_display(game_board &board, std::weak_ptr< wb::manager > wb, reports &reports_object, const std::string &theme_id, const config &level)
virtual void update() override
TLD update() override.
map_location attack_indicator_src_
game_display & operator=(const game_display &)=delete
void float_label(const map_location &loc, const std::string &text, const color_t &color)
Function to float a label above a tile.
map_display and display: classes which take care of displaying the map and game-data on the screen.
Unit and team statistics.
Definition: display.hpp:45
This module contains various pathfinding functions and utilities.
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:51
Encapsulates the map of the game.
Definition: location.hpp:46
static std::string write_direction(direction dir)
Definition: location.cpp:154
direction get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:238
static const map_location & null_location()
Definition: location.hpp:103
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
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
#define b