The Battle for Wesnoth  1.17.21+dev
minimap.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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 "wml_exception.hpp"
30 #include "gettext.hpp"
31 #include "../../minimap.hpp" // We want the file in src/
32 
33 #include <functional>
34 
35 #include <algorithm>
36 
37 static lg::log_domain log_config("config");
38 #define ERR_CF LOG_STREAM_INDENT(err, log_config)
39 
40 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
41 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
42 
43 // Define this to enable debug output for the minimap cache.
44 //#define DEBUG_MINIMAP_CACHE
45 
46 namespace gui2
47 {
48 
49 // ------------ WIDGET -----------{
50 
51 REGISTER_WIDGET(minimap)
52 
53 minimap::minimap(const implementation::builder_minimap& builder)
54  : styled_widget(builder, type())
55  , map_data_()
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  queue_redraw();
87 
88  try {
89  map_ = std::make_unique<gamemap>(map_data_);
90  } catch(const incorrect_map_format_error& e) {
91  map_.reset(nullptr);
92  ERR_CF << "Error while loading the map: " << e.message;
93  }
94 }
95 
97 {
98  if(map_) {
99  image::render_minimap(get_width(), get_height(), *map_, nullptr, nullptr, nullptr, true);
100  }
101 }
102 
103 // }---------- DEFINITION ---------{
104 
107 {
108  DBG_GUI_P << "Parsing minimap " << id;
109 
110  load_resolutions<resolution>(cfg);
111 }
112 
114  : resolution_definition(cfg)
115 {
116  // Note the order should be the same as the enum state_t in minimap.hpp.
117  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for minimap control")));
118 }
119 
120 // }---------- BUILDER -----------{
121 
122 namespace implementation
123 {
124 
125 builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
126 {
127 }
128 
129 std::unique_ptr<widget> builder_minimap::build() const
130 {
131  auto widget = std::make_unique<minimap>(*this);
132 
133  DBG_GUI_G << "Window builder: placed minimap '" << id
134  << "' with definition '" << definition << "'.";
135 
136  return widget;
137 }
138 
139 } // namespace implementation
140 
141 // }------------ END --------------
142 
143 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
The basic minimap class.
Definition: minimap.hpp:46
const std::string & map_data() const
Definition: minimap.hpp:73
void set_map_data(const std::string &map_data)
Definition: minimap.cpp:79
std::string map_data_
The map data to be used to generate the map.
Definition: minimap.hpp:80
std::unique_ptr< gamemap > map_
Game map generated from the provided data.
Definition: minimap.hpp:83
virtual bool get_active() const override
See styled_widget::get_active.
Definition: minimap.cpp:64
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: minimap.cpp:69
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: minimap.cpp:74
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: minimap.cpp:59
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: minimap.cpp:96
Base class for all visible items.
Base class for all widgets.
Definition: widget.hpp:54
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:442
unsigned get_width() const
Definition: widget.cpp:326
unsigned get_height() const
Definition: widget.cpp:331
Drawing functions, for drawing things on the screen.
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define ERR_CF
Definition: minimap.cpp:38
static lg::log_domain log_config("config")
Generic file dialog.
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
Contains the implementation details for lexical_cast and shouldn't be used directly.
Contains the SDL_Rect helper code.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
virtual std::unique_ptr< widget > build() const override
Definition: minimap.cpp:129
std::string definition
Parameters for the styled_widget.
minimap_definition(const config &cfg)
Definition: minimap.cpp:105
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)
#define e