The Battle for Wesnoth  1.17.0-dev
arrow.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
3  by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
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 /**
17  * @file
18  * Method bodies for the arrow class.
19  */
20 
21 #include "arrow.hpp"
22 
23 #include "game_display.hpp"
24 #include "log.hpp"
25 
26 static lg::log_domain log_arrows("arrows");
27 #define ERR_ARR LOG_STREAM(err, log_arrows)
28 #define WRN_ARR LOG_STREAM(warn, log_arrows)
29 #define LOG_ARR LOG_STREAM(info, log_arrows)
30 #define DBG_ARR LOG_STREAM(debug, log_arrows)
31 
32 arrow::arrow(bool hidden)
33  : layer_(display::LAYER_ARROWS)
34  , color_("red")
35  , style_(STYLE_STANDARD)
36  , path_()
37  , previous_path_()
38  , symbols_map_()
39  , hidden_(true)
40 {
41  if(!hidden)
42  show();
43 }
44 
46 {
47  hide();
48 }
49 
51 {
52  if(hidden_)
53  return;
54  hidden_ = true;
55 
56  if(display* disp = display::get_singleton()) {
58  disp->remove_arrow(*this);
59  }
60 }
61 
63 {
64  if(!hidden_)
65  return;
66  hidden_ = false;
67 
68  if(display* disp = display::get_singleton()) {
69  disp->add_arrow(*this);
70  }
71 }
72 
74 {
75  if (valid_path(path))
76  {
78  path_ = path;
80  if(!hidden_)
81  {
84  }
85  }
86 }
87 
89 {
92  previous_path_.clear();
93  path_.clear();
94  symbols_map_.clear();
96 }
97 
98 void arrow::set_color(const std::string& color)
99 {
100  color_ = color;
101  if (valid_path(path_))
102  {
103  update_symbols();
104  }
105 }
106 
107 const std::string arrow::STYLE_STANDARD = "standard";
108 const std::string arrow::STYLE_HIGHLIGHTED = "highlighted";
109 const std::string arrow::STYLE_FOCUS = "focus";
110 const std::string arrow::STYLE_FOCUS_INVALID = "focus_invalid";
111 
112 void arrow::set_style(const std::string& style)
113 {
114  style_ = style;
115  if (valid_path(path_))
116  {
117  update_symbols();
118  }
119 }
120 
122 {
123  return path_;
124 }
125 
127 {
128  return previous_path_;
129 }
130 
131 bool arrow::path_contains(const map_location& hex) const
132 {
133  bool contains = symbols_map_.find(hex) != symbols_map_.end();
134  return contains;
135 }
136 
138 {
139  if(path_contains(hex))
140  {
142  disp->render_image(disp->get_location_x(hex), disp->get_location_y(hex), layer_,
144  }
145 }
146 
148 {
149  if (path.size() >= 2)
150  return true;
151  else
152  return false;
153 }
154 
156 {
157  if (!valid_path(path_))
158  {
159  WRN_ARR << "arrow::update_symbols called with invalid path" << std::endl;
160  return;
161  }
162 
163  symbols_map_.clear();
165 
166  const std::string mods = "~RC(FF00FF>"+ color_ + ")"; //magenta to current color
167 
168  const std::string dirname = "arrows/";
169  std::string prefix = "";
170  std::string suffix = "";
171  std::string image_filename = "";
172  arrow_path_t::const_iterator const arrow_start_hex = path_.begin();
173  arrow_path_t::const_iterator const arrow_pre_end_hex = path_.end() - 2;
174  arrow_path_t::const_iterator const arrow_end_hex = path_.end() - 1;
175  bool teleport_out = false;
176 
178  for (hex = path_.begin(); hex != path_.end(); ++hex)
179  {
180  prefix = "";
181  suffix = "";
182  image_filename = "";
183  bool start = false;
184  bool pre_end = false;
185  bool end = false;
186 
187  // teleport in if we teleported out last hex
188  bool teleport_in = teleport_out;
189  teleport_out = false;
190 
191  // Determine some special cases
192  if (hex == arrow_start_hex)
193  start = true;
194  if (hex == arrow_pre_end_hex)
195  pre_end = true;
196  else if (hex == arrow_end_hex)
197  end = true;
198  if (hex != arrow_end_hex && !tiles_adjacent(*hex, *(hex + 1)))
199  teleport_out = true;
200 
201  // calculate enter and exit directions, if available
203  if (!start && !teleport_in)
204  {
205  enter_dir = hex->get_relative_dir(*(hex-1));
206  }
208  if (!end && !teleport_out)
209  {
210  exit_dir = hex->get_relative_dir(*(hex+1));
211  }
212 
213  // Now figure out the actual images
214  if (teleport_out)
215  {
216  prefix = "teleport-out";
217  if (enter_dir != map_location::NDIRECTIONS)
218  {
219  suffix = map_location::write_direction(enter_dir);
220  }
221  }
222  else if (teleport_in)
223  {
224  prefix = "teleport-in";
225  if (exit_dir != map_location::NDIRECTIONS)
226  {
227  suffix = map_location::write_direction(exit_dir);
228  }
229  }
230  else if (start)
231  {
232  prefix = "start";
233  suffix = map_location::write_direction(exit_dir);
234  if (pre_end)
235  {
236  suffix = suffix + "_ending";
237  }
238  }
239  else if (end)
240  {
241  prefix = "end";
242  suffix = map_location::write_direction(enter_dir);
243  }
244  else
245  {
246  std::string enter, exit;
247  enter = map_location::write_direction(enter_dir);
248  exit = map_location::write_direction(exit_dir);
249  if (pre_end)
250  {
251  exit = exit + "_ending";
252  }
253 
254  assert(std::abs(enter_dir - exit_dir) > 1); //impossible turn?
255  if (enter_dir < exit_dir)
256  {
257  prefix = enter;
258  suffix = exit;
259  }
260  else //(enter_dir > exit_dir)
261  {
262  prefix = exit;
263  suffix = enter;
264  }
265  }
266 
267  image_filename = dirname + style_ + "/" + prefix;
268  if (!suffix.empty())
269  {
270  image_filename += ("-" + suffix);
271  }
272  image_filename += ".png";
273  assert(!image_filename.empty());
274 
275  image::locator image = image::locator(image_filename, mods);
276  if (!image.file_exists())
277  {
278  ERR_ARR << "Image " << image_filename << " not found." << std::endl;
280  }
281  symbols_map_[*hex] = image;
282  }
283 }
284 
286 {
287  if(display* disp = display::get_singleton()) {
288  for(const map_location& loc : path) {
289  disp->invalidate(loc);
290  }
291  }
292 }
293 
295 {
296  if(display* disp = display::get_singleton()) {
297  disp->update_arrow(*this);
298  }
299 }
static void invalidate_arrow_path(const arrow_path_t &path)
Invalidates every hex along the given path.
Definition: arrow.cpp:285
surface get_image(const image::locator &i_locator, TYPE type)
Caches and returns an image.
Definition: picture.cpp:816
std::string style_
represents the subdirectory that holds images for this arrow style
Definition: arrow.hpp:98
virtual void notify_arrow_changed()
Definition: arrow.cpp:294
Arrows destined to be drawn on the map.
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:92
void hide()
Sets the arrow&#39;s visibility.
Definition: arrow.cpp:50
const arrow_path_t & get_path() const
Definition: arrow.cpp:121
bool contains(const pane::item &item, const std::string &tag, const text_box &text_box)
A filter testing whether a search string is used in a text.
Definition: filter.hpp:64
bool path_contains(const map_location &hex) const
Definition: arrow.cpp:131
void render_image(int x, int y, const display::drawing_layer drawing_layer, const map_location &loc, surface image, bool hreverse=false, bool greyscale=false, fixed_t alpha=ftofxp(1.0), color_t blendto={0, 0, 0}, double blend_ratio=0, double submerged=0.0, bool vreverse=false)
Draw an image at a certain location.
Definition: display.cpp:1552
virtual void draw_hex(const map_location &hex)
Definition: arrow.cpp:137
arrow_symbols_map_t symbols_map_
Definition: arrow.hpp:104
virtual void set_path(const arrow_path_t &path)
Definition: arrow.cpp:73
std::vector< map_location > arrow_path_t
Definition: arrow.hpp:25
arrow(const arrow &)=delete
static const std::string STYLE_HIGHLIGHTED
Definition: arrow.hpp:68
static const std::string STYLE_FOCUS
Definition: arrow.hpp:69
const arrow_path_t & get_previous_path() const
Definition: arrow.cpp:126
display::drawing_layer layer_
Definition: arrow.hpp:94
virtual void update_symbols()
Calculate the symbols to place along the arrow path.
Definition: arrow.cpp:155
static const std::string STYLE_FOCUS_INVALID
Definition: arrow.hpp:70
std::string path
Definition: game_config.cpp:39
#define ERR_ARR
Definition: arrow.cpp:27
Generic locator abstracting the location of an image.
Definition: picture.hpp:60
Image rescaled according to the zoom settings.
Definition: picture.hpp:234
Encapsulates the map of the game.
Definition: location.hpp:38
bool tiles_adjacent(const map_location &a, const map_location &b)
Function which tells if two locations are adjacent.
Definition: location.cpp:503
std::string color_
Definition: arrow.hpp:96
void show()
Definition: arrow.cpp:62
virtual void reset()
invalidates and clears the present path, forgets the previous path, clears the symbols map ...
Definition: arrow.cpp:88
virtual void set_color(const std::string &color)
The string color parameter is in the same format expected by the image::locator modifiers parameter...
Definition: arrow.cpp:98
int get_location_y(const map_location &loc) const
Definition: display.cpp:755
arrow_path_t path_
Definition: arrow.hpp:100
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:40
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:750
Functions to load and save images from/to disk.
static lg::log_domain log_arrows("arrows")
Standard logging facilities (interface).
EXIT_STATUS start(const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:30
static const std::string STYLE_STANDARD
If you add more styles, you should look at move::update_arrow_style()
Definition: arrow.hpp:67
void set_style(const std::string &style)
Definition: arrow.cpp:112
static bool valid_path(const arrow_path_t &path)
Checks that the path is not of length 0 or 1.
Definition: arrow.cpp:147
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:141
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
arrow_path_t previous_path_
Definition: arrow.hpp:101
virtual ~arrow()
Definition: arrow.cpp:45
#define WRN_ARR
Definition: arrow.cpp:28
bool hidden_
Definition: arrow.hpp:106