The Battle for Wesnoth  1.15.0-dev
minimap.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/widgets/minimap.hpp"
18 
19 #include "gui/core/log.hpp"
23 #include "map/map.hpp"
24 #include "map/exception.hpp"
25 #include "sdl/rect.hpp"
26 #include "terrain/type_data.hpp"
27 #include "../../minimap.hpp" // We want the file in src/
28 #include "video.hpp"
29 
30 #include "utils/functional.hpp"
31 
32 #include <algorithm>
33 
34 static lg::log_domain log_config("config");
35 #define ERR_CF LOG_STREAM_INDENT(err, log_config)
36 
37 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
38 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
39 
40 // Define this to enable debug output for the minimap cache.
41 //#define DEBUG_MINIMAP_CACHE
42 
43 namespace gui2
44 {
45 
46 // ------------ WIDGET -----------{
47 
48 REGISTER_WIDGET(minimap)
49 
50 minimap::minimap(const implementation::builder_minimap& builder)
51  : styled_widget(builder, type())
52  , map_data_()
53  , terrain_(nullptr)
54  , map_(nullptr)
55 {
56 }
57 
58 void minimap::set_active(const bool /*active*/)
59 {
60  /* DO NOTHING */
61 }
62 
63 bool minimap::get_active() const
64 {
65  return true;
66 }
67 
68 unsigned minimap::get_state() const
69 {
70  return 0;
71 }
72 
74 {
75  return false;
76 }
77 
78 void minimap::set_map_data(const std::string& map_data)
79 {
80  if(map_data == map_data_) {
81  return;
82  }
83 
85 
86  try {
87  map_.reset(new gamemap(std::make_shared<terrain_type_data>(*terrain_), map_data_));
88  } catch(const incorrect_map_format_error& e) {
89  map_.reset(nullptr);
90  ERR_CF << "Error while loading the map: " << e.message << '\n';
91  }
92 }
93 
95 {
96  if(map_) {
97  image::render_minimap(get_width(), get_height(), *map_, nullptr, nullptr, nullptr, true);
98  }
99 }
100 
101 // }---------- DEFINITION ---------{
102 
105 {
106  DBG_GUI_P << "Parsing minimap " << id << '\n';
107 
108  load_resolutions<resolution>(cfg);
109 }
110 
111 /*WIKI
112  * @page = GUIWidgetDefinitionWML
113  * @order = 1_minimap
114  *
115  * == Minimap ==
116  *
117  * @macro = minimap_description
118  *
119  * The following states exist:
120  * * state_enabled, the minimap is enabled.
121  * @begin{parent}{name="gui/"}
122  * @begin{tag}{name="minimap_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
123  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
124  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
125  * @end{tag}{name="state_enabled"}
126  * @end{tag}{name="resolution"}
127  * @end{tag}{name="minimap_definition"}
128  * @end{parent}{name="gui/"}
129  */
131  : resolution_definition(cfg)
132 {
133  // Note the order should be the same as the enum state_t in minimap.hpp.
134  state.emplace_back(cfg.child("state_enabled"));
135 }
136 
137 // }---------- BUILDER -----------{
138 
139 /*WIKI_MACRO
140  * @begin{macro}{minimap_description}
141  *
142  * A minimap to show the gamemap, this only shows the map and has no
143  * interaction options. This version is used for map previews, there
144  * will be a another version which allows interaction.
145  * @end{macro}
146  */
147 
148 /*WIKI
149  * @page = GUIWidgetInstanceWML
150  * @order = 2_minimap
151  *
152  * == Minimap ==
153  *
154  * @macro = minimap_description
155  *
156  * A minimap has no extra fields.
157  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
158  * @begin{tag}{name="minimap"}{min=0}{max=-1}{super="generic/widget_instance"}
159  * @end{tag}{name="minimap"}
160  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
161  */
162 
163 namespace implementation
164 {
165 
166 builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
167 {
168 }
169 
171 {
172  auto widget = std::make_shared<minimap>(*this);
173 
174  DBG_GUI_G << "Window builder: placed minimap '" << id
175  << "' with definition '" << definition << "'.\n";
176 
177  return widget;
178 }
179 
180 } // namespace implementation
181 
182 // }------------ END --------------
183 
184 } // namespace gui2
Define the common log macros for the gui toolkit.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
std::unique_ptr< gamemap > map_
Game map generated from the provided data.
Definition: minimap.hpp:90
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
void set_map_data(const std::string &map_data)
Definition: minimap.cpp:78
std::vector< state_definition > state
std::string map_data_
The map data to be used to generate the map.
Definition: minimap.hpp:80
Base class for all widgets.
Definition: widget.hpp:48
unsigned get_height() const
Definition: widget.cpp:322
const std::string & map_data() const
Definition: minimap.hpp:68
void render_minimap(unsigned dst_w, unsigned dst_h, const gamemap &map, const team *vw, const unit_map *units, const std::map< map_location, unsigned int > *reach_map, bool ignore_terrain_disabled)
Renders the minimap to the screen.
Definition: minimap.cpp:40
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: minimap.cpp:94
-file util.hpp
unsigned get_width() const
Definition: widget.cpp:317
Generic file dialog.
Definition: field-fwd.hpp:22
virtual bool get_active() const override
See styled_widget::get_active.
Definition: minimap.cpp:63
static lg::log_domain log_config("config")
#define ERR_CF
Definition: minimap.cpp:35
std::string definition
Parameters for the styled_widget.
std::shared_ptr< widget > widget_ptr
Definition: widget.hpp:732
Encapsulates the map of the game.
Definition: map.hpp:36
const ::config * terrain_
The config object with the terrain data.
Definition: minimap.hpp:87
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: minimap.cpp:73
minimap_definition(const config &cfg)
Definition: minimap.cpp:103
virtual widget_ptr build() const override
Definition: minimap.cpp:170
Base class for all visible items.
The basic minimap class.
Definition: minimap.hpp:40
Contains the SDL_Rect helper code.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: minimap.cpp:58
std::string message
Definition: exceptions.hpp:31
#define e
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: minimap.cpp:68
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.