/
minimap.cpp
198 lines (157 loc) · 4.62 KB
/
minimap.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
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/minimap.hpp"
#include "gui/core/log.hpp"
#include "gui/core/widget_definition.hpp"
#include "gui/core/window_builder.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "map/map.hpp"
#include "map/exception.hpp"
#include "sdl/rect.hpp"
#include "terrain/type_data.hpp"
#include "../../minimap.hpp" // We want the file in src/
#include "video.hpp"
#include "utils/functional.hpp"
#include <algorithm>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM_INDENT(err, log_config)
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
// Define this to enable debug output for the minimap cache.
//#define DEBUG_MINIMAP_CACHE
namespace gui2
{
// ------------ WIDGET -----------{
REGISTER_WIDGET(minimap)
minimap::minimap()
: styled_widget()
, map_data_()
, terrain_(nullptr)
, map_(nullptr)
{
}
void minimap::init()
{
get_canvas(0).set_draw_function(std::bind(&minimap::canvas_draw_background, this, _1));
}
void minimap::set_active(const bool /*active*/)
{
/* DO NOTHING */
}
bool minimap::get_active() const
{
return true;
}
unsigned minimap::get_state() const
{
return 0;
}
bool minimap::disable_click_dismiss() const
{
return false;
}
void minimap::set_map_data(const std::string& map_data)
{
if(map_data == map_data_) {
return;
}
map_data_ = map_data;
try {
map_.reset(new gamemap(std::make_shared<terrain_type_data>(*terrain_), map_data_));
} catch(incorrect_map_format_error& e) {
ERR_CF << "Error while loading the map: " << e.message << '\n';
}
// Flag the background canvas as dirty so the minimap is redrawn.
get_canvas(0).set_is_dirty(true);
}
void minimap::canvas_draw_background(texture& tex)
{
image::render_minimap(tex, *map_);
}
const std::string& minimap::get_control_type() const
{
static const std::string type = "minimap";
return type;
}
// }---------- DEFINITION ---------{
minimap_definition::minimap_definition(const config& cfg)
: styled_widget_definition(cfg)
{
DBG_GUI_P << "Parsing minimap " << id << '\n';
load_resolutions<resolution>(cfg);
}
/*WIKI
* @page = GUIWidgetDefinitionWML
* @order = 1_minimap
*
* == Minimap ==
*
* @macro = minimap_description
*
* The following states exist:
* * state_enabled, the minimap is enabled.
* @begin{parent}{name="gui/"}
* @begin{tag}{name="minimap_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
* @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
* @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
* @end{tag}{name="state_enabled"}
* @end{tag}{name="resolution"}
* @end{tag}{name="minimap_definition"}
* @end{parent}{name="gui/"}
*/
minimap_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg)
{
// Note the order should be the same as the enum state_t in minimap.hpp.
state.emplace_back(cfg.child("state_enabled"));
}
// }---------- BUILDER -----------{
/*WIKI_MACRO
* @begin{macro}{minimap_description}
*
* A minimap to show the gamemap, this only shows the map and has no
* interaction options. This version is used for map previews, there
* will be a another version which allows interaction.
* @end{macro}
*/
/*WIKI
* @page = GUIWidgetInstanceWML
* @order = 2_minimap
*
* == Minimap ==
*
* @macro = minimap_description
*
* A minimap has no extra fields.
* @begin{parent}{name="gui/window/resolution/grid/row/column/"}
* @begin{tag}{name="minimap"}{min=0}{max=-1}{super="generic/widget_instance"}
* @end{tag}{name="minimap"}
* @end{parent}{name="gui/window/resolution/grid/row/column/"}
*/
namespace implementation
{
builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
{
}
widget* builder_minimap::build() const
{
minimap* widget = new minimap();
init_control(widget);
DBG_GUI_G << "Window builder: placed minimap '" << id
<< "' with definition '" << definition << "'.\n";
return widget;
}
} // namespace implementation
// }------------ END --------------
} // namespace gui2