The Battle for Wesnoth  1.19.0-dev
minimap.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 "gui/core/log.hpp"
23 #include "map/map.hpp"
24 #include "map/exception.hpp"
25 #include "sdl/rect.hpp"
26 #include "wml_exception.hpp"
27 #include "../../minimap.hpp" // We want the file in src/
28 
29 #include <functional>
30 
31 #include <algorithm>
32 
33 static lg::log_domain log_config("config");
34 #define ERR_CF LOG_STREAM_INDENT(err, log_config)
35 
36 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
37 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
38 
39 // Define this to enable debug output for the minimap cache.
40 //#define DEBUG_MINIMAP_CACHE
41 
42 namespace gui2
43 {
44 
45 // ------------ WIDGET -----------{
46 
47 REGISTER_WIDGET(minimap)
48 
49 minimap::minimap(const implementation::builder_minimap& builder)
50  : styled_widget(builder, type())
51 {
52 }
53 
54 void minimap::set_active(const bool /*active*/)
55 {
56  /* DO NOTHING */
57 }
58 
59 bool minimap::get_active() const
60 {
61  return true;
62 }
63 
64 unsigned minimap::get_state() const
65 {
66  return 0;
67 }
68 
70 {
71  return false;
72 }
73 
74 void minimap::set_map_data(const std::string& map_data)
75 {
76  queue_redraw();
77 
78  try {
79  map_ = std::make_unique<gamemap>(map_data);
80  } catch(const incorrect_map_format_error& e) {
81  map_.reset(nullptr);
82  ERR_CF << "Error while loading the map: " << e.message;
83  }
84 }
85 
87 {
88  if(map_) {
89  if(const auto drawer = image::prep_minimap_for_rendering(*map_, nullptr, nullptr, nullptr, true)) {
90  const auto [w, h] = get_size();
91  drawer({ 0, 0, w, h });
92  }
93  }
94  return true;
95 }
96 
97 // }---------- DEFINITION ---------{
98 
101 {
102  DBG_GUI_P << "Parsing minimap " << id;
103 
104  load_resolutions<resolution>(cfg);
105 }
106 
108  : resolution_definition(cfg)
109 {
110  // Note the order should be the same as the enum state_t in minimap.hpp.
111  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("minimap_definition][resolution", "state_enabled")));
112 }
113 
114 // }---------- BUILDER -----------{
115 
116 namespace implementation
117 {
118 
119 builder_minimap::builder_minimap(const config& cfg) : builder_styled_widget(cfg)
120 {
121 }
122 
123 std::unique_ptr<widget> builder_minimap::build() const
124 {
125  auto widget = std::make_unique<minimap>(*this);
126 
127  DBG_GUI_G << "Window builder: placed minimap '" << id
128  << "' with definition '" << definition << "'.";
129 
130  return widget;
131 }
132 
133 } // namespace implementation
134 
135 // }------------ END --------------
136 
137 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
The basic minimap class.
Definition: minimap.hpp:45
virtual bool impl_draw_background() override
See widget::impl_draw_background.
Definition: minimap.cpp:86
void set_map_data(const std::string &map_data)
Definition: minimap.cpp:74
std::unique_ptr< gamemap > map_
Game map generated from the provided data.
Definition: minimap.hpp:69
virtual bool get_active() const override
See styled_widget::get_active.
Definition: minimap.cpp:59
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: minimap.cpp:64
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: minimap.cpp:69
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: minimap.cpp:54
Base class for all visible items.
Base class for all widgets.
Definition: widget.hpp:53
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:455
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:307
int w
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:34
static lg::log_domain log_config("config")
Generic file dialog.
std::function< rect(rect)> prep_minimap_for_rendering(const gamemap &map, const team *vw, const unit_map *units, const std::map< map_location, unsigned int > *reach_map, bool ignore_terrain_disabled)
Prepares the minimap texture and returns a function which will render it to the current rendering tar...
Definition: minimap.cpp:40
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.
virtual std::unique_ptr< widget > build() const override
Definition: minimap.cpp:123
std::string definition
Parameters for the styled_widget.
minimap_definition(const config &cfg)
Definition: minimap.cpp:99
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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
#define h