The Battle for Wesnoth  1.15.12+dev
game_display.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
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 class config;
18 class team;
19 class game_board;
20 
21 #include "chat_events.hpp"
22 #include "display.hpp"
23 #include "display_chat_manager.hpp"
24 #include "pathfind/pathfind.hpp"
25 
26 #include <deque>
27 
28 // This needs to be separate from display.h because of the static
29 // singleton member, which will otherwise trigger link failure
30 // when building the editor.
31 
32 class game_display : public display
33 {
34 public:
35  game_display(game_board& board,
36  std::weak_ptr<wb::manager> wb,
37  reports & reports_object,
38  const config& theme_cfg,
39  const config& level);
40 
41  ~game_display();
43  {
44  return static_cast<game_display*>(singleton_);
45  }
46 
47  /**
48  * Update lighting settings.
49  *
50  * Should be called on every new turn.
51  */
52  void new_turn();
53 
54  virtual const std::set<std::string>& observers() const override { return chat_man_->observers(); }
55  /**
56  * Scrolls to the leader of a certain side.
57  *
58  * This will normally be the playing team.
59  */
60  void scroll_to_leader(int side, SCROLL_TYPE scroll_type = ONSCREEN,bool force = true);
61 
62  /**
63  * Function to display a location as selected.
64  *
65  * If a unit is in the location, and there is no unit in the currently
66  * highlighted hex, the unit will be displayed in the sidebar.
67  */
68  virtual void select_hex(map_location hex) override;
69 
70  /**
71  * Function to highlight a location.
72  *
73  * If a unit is in the location, it will be displayed in the sidebar.
74  * Selection is used when a unit has been clicked on, while highlighting is
75  * used when a location has been moused over.
76  */
77  virtual void highlight_hex(map_location hex) override;
78 
79  /**
80  * Change the unit to be displayed in the sidebar.
81  *
82  * This is used when selecting or highlighting is not wanted.
83  */
85 
86  /**
87  * Sets the paths that are currently displayed as available
88  * for the unit to move along.
89  * All other paths will be grayed out.
90  */
91  void highlight_reach(const pathfind::paths &paths_list);
92 
93  /**
94  * Add more paths to highlight. Print numbers where they overlap.
95  * Used by Show Enemy Moves. If @a goal is not @c null_location, highlight
96  * enemy units that can reach @a goal.
97  */
98  void highlight_another_reach(const pathfind::paths &paths_list,
100  /**
101  * Return the locations of units that can reach @a goal (@see highlight_another_reach()).
102  */
103  const std::set<map_location>& units_that_can_reach_goal() const { return units_that_can_reach_goal_; }
104 
105  /** Reset highlighting of paths. */
106  bool unhighlight_reach();
107 
108  /**
109  * Sets the route along which footsteps are drawn to show movement of a
110  * unit. If nullptr, no route is displayed. @a route does not have to remain
111  * valid after being set.
112  */
113  void set_route(const pathfind::marked_route *route);
114  /**
115  * Gets the route along which footsteps are drawn to show movement of a
116  * unit. If no route is currently being shown, the array get_route().steps
117  * will be empty.
118  */
120 
121  /** Function to float a label above a tile */
122  void float_label(const map_location& loc, const std::string& text, const color_t& color);
123 
124  /** Draws the movement info (turns available) for a given location. */
125  void draw_movement_info(const map_location& loc);
126 
127  /** Function to invalidate that unit status displayed on the sidebar. */
129 
130  /** Same as invalidate_unit() if moving the displayed unit. */
131  void invalidate_unit_after_move(const map_location& src, const map_location& dst);
132 
133  virtual const time_of_day& get_time_of_day(const map_location& loc) const override;
134 
135  virtual bool has_time_area() const override;
136 
137 protected:
138  /**
139  * game_display pre_draw does specific things related e.g. to unit rendering
140  * and calls the whiteboard pre-draw method.
141  */
142  virtual void pre_draw() override;
143  /**
144  * Calls the whiteboard's post-draw method.
145  */
146  virtual void post_draw() override;
147 
148  virtual void draw_invalidated() override;
149 
150  virtual void post_commit() override;
151 
152  virtual void draw_hex(const map_location& loc) override;
153 
154  /** Inherited from display. */
155  virtual overlay_map& get_overlays() override;
156 
157  std::set<map_location> units_that_can_reach_goal_;
158 
159 public:
160  /** Set the attack direction indicator. */
161  void set_attack_indicator(const map_location& src, const map_location& dst);
162  void clear_attack_indicator();
163  // TODO: compare reports::context::mhb()->current_unit_attacks_from()
165 
166  /** Function to get attack direction suffix. */
167  std::string attack_indicator_direction() const {
170  }
171 
172  // Functions used in the editor:
173 
174  //void draw_terrain_palette(int x, int y, terrain_type::TERRAIN selected);
175  t_translation::terrain_code get_terrain_on(int palx, int paly, int x, int y);
176 
177  virtual const map_location &displayed_unit_hex() const override { return displayedUnitHex_; }
178 
179  /**
180  * annotate hex with number, useful for debugging or UI prototype
181  */
182  static int& debug_highlight(const map_location& loc);
183  static void clear_debug_highlights() { debugHighlights_.clear(); }
184 
185  /** The playing team is the team whose turn it is. */
186  virtual int playing_side() const override { return activeTeam_ + 1; }
187 
188  std::string current_team_name() const;
189 
191 
192  void begin_game();
193 
194  virtual bool in_game() const override { return in_game_; }
195 
196  /**
197  * Sets the linger mode for the display.
198  * There have been some discussions on what to do with fog and shroud
199  * the extra variables make it easier to modify the behavior. There
200  * might even be a split between victory and defeat.
201  *
202  * @todo if the current implementation is wanted we can change
203  * the stuff back to a boolean
204  */
205  enum game_mode {
206  RUNNING, /**< no linger overlay, show fog and shroud. */
207  LINGER }; /**< linger overlay, show fog and shroud. */
208 
209  void set_game_mode(const game_mode mode);
210 
211  /** Sets whether the screen (map visuals) needs to be rebuilt. This is typically after the map has been changed by wml. */
212  void needs_rebuild(bool b);
213 
214  /** Rebuilds the screen if needs_rebuild(true) was previously called, and resets the flag. */
215  bool maybe_rebuild();
216 
217 private:
218  game_display(const game_display&);
219  void operator=(const game_display&);
220 
221  virtual void draw_sidebar() override;
222 
224 
225  // Locations of the attack direction indicator's parts
228 
230 
231  void invalidate_route();
232 
234 
235  bool in_game_;
236 
237  const std::unique_ptr<display_chat_manager> chat_man_;
238 
240 
241  // For debug mode
242  static std::map<map_location, int> debugHighlights_;
243 
245 
246 };
void invalidate_unit_after_move(const map_location &src, const map_location &dst)
Same as invalidate_unit() if moving the displayed unit.
Game board class.
Definition: game_board.hpp:50
void set_game_mode(const game_mode mode)
static int & debug_highlight(const map_location &loc)
annotate hex with number, useful for debugging or UI prototype
game_mode
Sets the linger mode for the display.
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:226
virtual overlay_map & get_overlays() override
Inherited from display.
void invalidate_route()
virtual const map_location & displayed_unit_hex() const override
Virtual functions shadowed in game_display.
const std::set< map_location > & units_that_can_reach_goal() const
Return the locations of units that can reach goal (.
const map_location & get_attack_indicator_src()
map_location attack_indicator_src_
void invalidate_unit()
Function to invalidate that unit status displayed on the sidebar.
std::set< map_location > units_that_can_reach_goal_
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
game_mode mode_
map_location displayedUnitHex_
virtual void post_commit() override
Hook for actions to take right after draw() calls drawing_buffer_commit No action here by default...
static display * singleton_
Definition: display.hpp:1078
virtual int playing_side() const override
The playing team is the team whose turn it is.
void new_turn()
Update lighting settings.
static std::map< map_location, int > debugHighlights_
game_display(game_board &board, std::weak_ptr< wb::manager > wb, reports &reports_object, const config &theme_cfg, const config &level)
Unit and team statistics.
#define b
void highlight_another_reach(const pathfind::paths &paths_list, const map_location &goal=map_location::null_location())
Add more paths to highlight.
no linger overlay, show fog and shroud.
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:55
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
void scroll_to_leader(int side, SCROLL_TYPE scroll_type=ONSCREEN, bool force=true)
Scrolls to the leader of a certain side.
virtual void draw_invalidated() override
Only called when there&#39;s actual redrawing to do.
std::map< map_location, std::vector< overlay > > overlay_map
Definition: display.hpp:1041
const pathfind::marked_route & get_route()
Gets the route along which footsteps are drawn to show movement of a unit.
virtual const time_of_day & get_time_of_day(const map_location &loc) const override
virtual const std::set< std::string > & observers() const override
bool invalidateGameStatus_
Definition: display.hpp:767
void highlight_reach(const pathfind::paths &paths_list)
Sets the paths that are currently displayed as available for the unit to move along.
map_display and display: classes which take care of displaying the map and game-data on the screen...
Structure which holds a single route and marks for special events.
Definition: pathfind.hpp:140
void needs_rebuild(bool b)
Sets whether the screen (map visuals) needs to be rebuilt.
virtual void draw_sidebar() override
Called near the end of a draw operation, derived classes can use this to render a specific sidebar...
pathfind::marked_route route_
virtual void draw_hex(const map_location &loc) override
Redraws a single gamemap location.
std::string attack_indicator_direction() const
Function to get attack direction suffix.
Encapsulates the map of the game.
Definition: location.hpp:37
void float_label(const map_location &loc, const std::string &text, const color_t &color)
Function to float a label above a tile.
virtual bool in_game() const override
std::string current_team_name() const
virtual void highlight_hex(map_location hex) override
Function to highlight a location.
std::size_t activeTeam_
Definition: display.hpp:893
static void clear_debug_highlights()
virtual void select_hex(map_location hex) override
Function to display a location as selected.
virtual bool has_time_area() const override
display_chat_manager & get_chat_manager()
overlay_map overlay_map_
bool unhighlight_reach()
Reset highlighting of paths.
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:71
virtual void pre_draw() override
game_display pre_draw does specific things related e.g.
static const map_location & null_location()
Definition: location.hpp:80
virtual void post_draw() override
Calls the whiteboard&#39;s post-draw method.
map_location attack_indicator_dst_
void draw_movement_info(const map_location &loc)
Draws the movement info (turns available) for a given location.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
void set_route(const pathfind::marked_route *route)
Sets the route along which footsteps are drawn to show movement of a unit.
void clear_attack_indicator()
This module contains various pathfinding functions and utilities.
t_translation::terrain_code get_terrain_on(int palx, int paly, int x, int y)
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:140
void display_unit_hex(map_location hex)
Change the unit to be displayed in the sidebar.
void set_attack_indicator(const map_location &src, const map_location &dst)
Set the attack direction indicator.
Definition: display.hpp:48
static game_display * get_singleton()
const std::unique_ptr< display_chat_manager > chat_man_
bool maybe_rebuild()
Rebuilds the screen if needs_rebuild(true) was previously called, and resets the flag.
void operator=(const game_display &)