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 http://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 "gui/widgets/settings.hpp"
24 #include "map/map.hpp"
25 #include "map/exception.hpp"
26 #include "sdl/rect.hpp"
27 #include "terrain/type_data.hpp"
28 #include "../../minimap.hpp" // We want the file in src/
29 #include "video.hpp"
30 
31 #include "utils/functional.hpp"
32 
33 #include <algorithm>
34 
35 static lg::log_domain log_config("config");
36 #define ERR_CF LOG_STREAM_INDENT(err, log_config)
37 
38 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
39 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
40 
41 // Define this to enable debug output for the minimap cache.
42 //#define DEBUG_MINIMAP_CACHE
43 
44 namespace gui2
45 {
46 
47 // ------------ WIDGET -----------{
48 
49 REGISTER_WIDGET(minimap)
50 
51 minimap::minimap(const implementation::builder_minimap& builder)
52  : styled_widget(builder, get_control_type())
53  , map_data_()
54  , terrain_(nullptr)
55  , map_(nullptr)
56 {
57 }
58 
59 void minimap::set_active(const bool /*active*/)
60 {
61  /* DO NOTHING */
62 }
63 
64 bool minimap::get_active() const
65 {
66  return true;
67 }
68 
69 unsigned minimap::get_state() const
70 {
71  return 0;
72 }
73 
75 {
76  return false;
77 }
78 
79 void minimap::set_map_data(const std::string& map_data)
80 {
81  if(map_data == map_data_) {
82  return;
83  }
84 
86 
87  try {
88  map_.reset(new gamemap(std::make_shared<terrain_type_data>(*terrain_), map_data_));
89  } catch(const incorrect_map_format_error& e) {
90  map_.reset(nullptr);
91  ERR_CF << "Error while loading the map: " << e.message << '\n';
92  }
93 }
94 
96 {
97  if(map_) {
98  image::render_minimap(get_width(), get_height(), *map_, nullptr, nullptr, nullptr, true);
99  }
100 }
101 
102 // }---------- DEFINITION ---------{
103 
106 {
107  DBG_GUI_P << "Parsing minimap " << id << '\n';
108 
109  load_resolutions<resolution>(cfg);
110 }
111 
112 /*WIKI
113  * @page = GUIWidgetDefinitionWML
114  * @order = 1_minimap
115  *
116  * == Minimap ==
117  *
118  * @macro = minimap_description
119  *
120  * The following states exist:
121  * * state_enabled, the minimap is enabled.
122  * @begin{parent}{name="gui/"}
123  * @begin{tag}{name="minimap_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
124  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
125  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
126  * @end{tag}{name="state_enabled"}
127  * @end{tag}{name="resolution"}
128  * @end{tag}{name="minimap_definition"}
129  * @end{parent}{name="gui/"}
130  */
132  : resolution_definition(cfg)
133 {
134  // Note the order should be the same as the enum state_t in minimap.hpp.
135  state.emplace_back(cfg.child("state_enabled"));
136 }
137 
138 // }---------- BUILDER -----------{
139 
140 /*WIKI_MACRO
141  * @begin{macro}{minimap_description}
142  *
143  * A minimap to show the gamemap, this only shows the map and has no
144  * interaction options. This version is used for map previews, there
145  * will be a another version which allows interaction.
146  * @end{macro}
147  */
148 
149 /*WIKI
150  * @page = GUIWidgetInstanceWML
151  * @order = 2_minimap
152  *
153  * == Minimap ==
154  *
155  * @macro = minimap_description
156  *
157  * A minimap has no extra fields.
158  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
159  * @begin{tag}{name="minimap"}{min=0}{max=-1}{super="generic/widget_instance"}
160  * @end{tag}{name="minimap"}
161  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
162  */
163 
164 namespace implementation
165 {
166 
167 builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
168 {
169 }
170 
172 {
173  minimap* widget = new minimap(*this);
174 
175  DBG_GUI_G << "Window builder: placed minimap '" << id
176  << "' with definition '" << definition << "'.\n";
177 
178  return widget;
179 }
180 
181 } // namespace implementation
182 
183 // }------------ END --------------
184 
185 } // 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:79
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:47
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:95
-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:64
static lg::log_domain log_config("config")
#define ERR_CF
Definition: minimap.cpp:36
std::string definition
Parameters for the styled_widget.
This file contains the settings handling of the widget library.
Encapsulates the map of the game.
Definition: map.hpp:34
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:74
minimap_definition(const config &cfg)
Definition: minimap.cpp:104
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:59
std::string message
Definition: exceptions.hpp:31
#define e
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: minimap.cpp:69
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.