/
arrow.cpp
300 lines (261 loc) · 6.18 KB
/
arrow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright (C) 2010 - 2018 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Method bodies for the arrow class.
*/
#include "arrow.hpp"
#include "game_display.hpp"
#include "log.hpp"
static lg::log_domain log_arrows("arrows");
#define ERR_ARR LOG_STREAM(err, log_arrows)
#define WRN_ARR LOG_STREAM(warn, log_arrows)
#define LOG_ARR LOG_STREAM(info, log_arrows)
#define DBG_ARR LOG_STREAM(debug, log_arrows)
#define SCREEN (static_cast<display*>(game_display::get_singleton()))
arrow::arrow(bool hidden)
: layer_(display::LAYER_ARROWS)
, color_("red")
, style_(STYLE_STANDARD)
, path_()
, previous_path_()
, symbols_map_()
, hidden_(true)
{
if(!hidden)
show();
}
arrow::~arrow()
{
hide();
}
void arrow::hide()
{
if(hidden_)
return;
hidden_ = true;
if (SCREEN)
{
invalidate_arrow_path(path_);
SCREEN->remove_arrow(*this);
}
}
void arrow::show()
{
if(!hidden_)
return;
hidden_ = false;
if(SCREEN)
SCREEN->add_arrow(*this);
}
void arrow::set_path(const arrow_path_t& path)
{
if (valid_path(path))
{
previous_path_ = path_;
path_ = path;
update_symbols();
if(!hidden_)
{
invalidate_arrow_path(previous_path_);
notify_arrow_changed();
}
}
}
void arrow::reset()
{
invalidate_arrow_path(previous_path_);
invalidate_arrow_path(path_);
previous_path_.clear();
path_.clear();
symbols_map_.clear();
notify_arrow_changed();
}
void arrow::set_color(const std::string& color)
{
color_ = color;
if (valid_path(path_))
{
update_symbols();
}
}
const std::string arrow::STYLE_STANDARD = "standard";
const std::string arrow::STYLE_HIGHLIGHTED = "highlighted";
const std::string arrow::STYLE_FOCUS = "focus";
const std::string arrow::STYLE_FOCUS_INVALID = "focus_invalid";
void arrow::set_style(const std::string& style)
{
style_ = style;
if (valid_path(path_))
{
update_symbols();
}
}
const arrow_path_t& arrow::get_path() const
{
return path_;
}
const arrow_path_t& arrow::get_previous_path() const
{
return previous_path_;
}
bool arrow::path_contains(const map_location& hex) const
{
bool contains = symbols_map_.find(hex) != symbols_map_.end();
return contains;
}
void arrow::draw_hex(const map_location& hex)
{
if(path_contains(hex))
{
SCREEN->render_image(SCREEN->get_location_x(hex), SCREEN->get_location_y(hex), layer_,
hex, image::get_image(symbols_map_[hex], image::SCALED_TO_ZOOM));
}
}
bool arrow::valid_path(const arrow_path_t& path)
{
if (path.size() >= 2)
return true;
else
return false;
}
void arrow::update_symbols()
{
if (!valid_path(path_))
{
WRN_ARR << "arrow::update_symbols called with invalid path" << std::endl;
return;
}
symbols_map_.clear();
invalidate_arrow_path(path_);
const std::string mods = "~RC(FF00FF>"+ color_ + ")"; //magenta to current color
const std::string dirname = "arrows/";
std::string prefix = "";
std::string suffix = "";
std::string image_filename = "";
arrow_path_t::const_iterator const arrow_start_hex = path_.begin();
arrow_path_t::const_iterator const arrow_pre_end_hex = path_.end() - 2;
arrow_path_t::const_iterator const arrow_end_hex = path_.end() - 1;
bool teleport_out = false;
arrow_path_t::iterator hex;
for (hex = path_.begin(); hex != path_.end(); ++hex)
{
prefix = "";
suffix = "";
image_filename = "";
bool start = false;
bool pre_end = false;
bool end = false;
// teleport in if we teleported out last hex
bool teleport_in = teleport_out;
teleport_out = false;
// Determine some special cases
if (hex == arrow_start_hex)
start = true;
if (hex == arrow_pre_end_hex)
pre_end = true;
else if (hex == arrow_end_hex)
end = true;
if (hex != arrow_end_hex && !tiles_adjacent(*hex, *(hex + 1)))
teleport_out = true;
// calculate enter and exit directions, if available
map_location::DIRECTION enter_dir = map_location::NDIRECTIONS;
if (!start && !teleport_in)
{
enter_dir = hex->get_relative_dir(*(hex-1));
}
map_location::DIRECTION exit_dir = map_location::NDIRECTIONS;
if (!end && !teleport_out)
{
exit_dir = hex->get_relative_dir(*(hex+1));
}
// Now figure out the actual images
if (teleport_out)
{
prefix = "teleport-out";
if (enter_dir != map_location::NDIRECTIONS)
{
suffix = map_location::write_direction(enter_dir);
}
}
else if (teleport_in)
{
prefix = "teleport-in";
if (exit_dir != map_location::NDIRECTIONS)
{
suffix = map_location::write_direction(exit_dir);
}
}
else if (start)
{
prefix = "start";
suffix = map_location::write_direction(exit_dir);
if (pre_end)
{
suffix = suffix + "_ending";
}
}
else if (end)
{
prefix = "end";
suffix = map_location::write_direction(enter_dir);
}
else
{
std::string enter, exit;
enter = map_location::write_direction(enter_dir);
exit = map_location::write_direction(exit_dir);
if (pre_end)
{
exit = exit + "_ending";
}
assert(std::abs(enter_dir - exit_dir) > 1); //impossible turn?
if (enter_dir < exit_dir)
{
prefix = enter;
suffix = exit;
}
else //(enter_dir > exit_dir)
{
prefix = exit;
suffix = enter;
}
}
image_filename = dirname + style_ + "/" + prefix;
if (!suffix.empty())
{
image_filename += ("-" + suffix);
}
image_filename += ".png";
assert(!image_filename.empty());
image::locator image = image::locator(image_filename, mods);
if (!image.file_exists())
{
ERR_ARR << "Image " << image_filename << " not found." << std::endl;
image = image::locator(game_config::images::missing);
}
symbols_map_[*hex] = image;
}
}
void arrow::invalidate_arrow_path(const arrow_path_t& path)
{
if(!SCREEN) return;
for (const map_location& loc : path)
{
SCREEN->invalidate(loc);
}
}
void arrow::notify_arrow_changed()
{
if(!SCREEN) return;
SCREEN->update_arrow(*this);
}