The Battle for Wesnoth  1.17.10+dev
minimap.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
3  by Mark de Wever <koraq@xs4all.nl>
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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/widgets/minimap.hpp"
19 
20 #include "draw.hpp"
21 #include "gui/core/log.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "map/map.hpp"
27 #include "map/exception.hpp"
28 #include "sdl/rect.hpp"
29 #include "../../minimap.hpp" // We want the file in src/
30 
31 #include <functional>
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, type())
53  , map_data_()
54 {
55 }
56 
57 void minimap::set_active(const bool /*active*/)
58 {
59  /* DO NOTHING */
60 }
61 
62 bool minimap::get_active() const
63 {
64  return true;
65 }
66 
67 unsigned minimap::get_state() const
68 {
69  return 0;
70 }
71 
73 {
74  return false;
75 }
76 
77 void minimap::set_map_data(const std::string& map_data)
78 {
79  if(map_data == map_data_) {
80  return;
81  }
82 
84  queue_redraw();
85 
86  try {
87  map_ = std::make_unique<gamemap>(map_data_);
88  } catch(const incorrect_map_format_error& e) {
89  map_.reset(nullptr);
90  ERR_CF << "Error while loading the map: " << e.message;
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;
107 
108  load_resolutions<resolution>(cfg);
109 }
110 
112  : resolution_definition(cfg)
113 {
114  // Note the order should be the same as the enum state_t in minimap.hpp.
115  state.emplace_back(cfg.child("state_enabled"));
116 }
117 
118 // }---------- BUILDER -----------{
119 
120 namespace implementation
121 {
122 
123 builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
124 {
125 }
126 
127 std::unique_ptr<widget> builder_minimap::build() const
128 {
129  auto widget = std::make_unique<minimap>(*this);
130 
131  DBG_GUI_G << "Window builder: placed minimap '" << id
132  << "' with definition '" << definition << "'.";
133 
134  return widget;
135 }
136 
137 } // namespace implementation
138 
139 // }------------ END --------------
140 
141 } // namespace gui2
Define the common log macros for the gui toolkit.
Drawing functions, for drawing things on the screen.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
std::unique_ptr< gamemap > map_
Game map generated from the provided data.
Definition: minimap.hpp:83
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:402
void set_map_data(const std::string &map_data)
Definition: minimap.cpp:77
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:53
unsigned get_height() const
Definition: widget.cpp:331
const std::string & map_data() const
Definition: minimap.hpp:73
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:293
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: minimap.cpp:94
unsigned get_width() const
Definition: widget.cpp:326
Generic file dialog.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: minimap.cpp:62
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.
virtual std::unique_ptr< widget > build() const override
Definition: minimap.cpp:127
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: minimap.cpp:72
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:442
minimap_definition(const config &cfg)
Definition: minimap.cpp:103
Base class for all visible items.
The basic minimap class.
Definition: minimap.hpp:45
Contains the SDL_Rect helper code.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: minimap.cpp:57
std::string message
Definition: exceptions.hpp:30
#define e
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: minimap.cpp:67
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.