The Battle for Wesnoth  1.19.5+dev
editor_display.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Tomasz Sniatowski <kailoran@gmail.com>
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-editor"
17 
18 #include "draw.hpp"
21 #include "floating_label.hpp"
22 #include "font/sdl_ttf_compat.hpp" // for pango_line_width
23 #include "formula/string_utils.hpp"
24 #include "lexical_cast.hpp"
25 #include "overlay.hpp"
26 #include "team.hpp"
27 #include "terrain/builder.hpp"
28 #include "video.hpp"
29 
30 namespace wb {
31  class manager;
32 }
33 
34 namespace editor {
35 
37  : display(nullptr, std::shared_ptr<wb::manager>(), reports_object, "editor", config())
38  , brush_locations_()
39  , controller_(controller)
40  , mouseover_hex_overlay_()
41 {
42 }
43 
45 {
46  brush_locations_.insert(hex);
47  invalidate(hex);
48 }
49 
50 void editor_display::set_brush_locs(const std::set<map_location>& hexes)
51 {
53  brush_locations_ = hexes;
55 }
56 
58 {
60  brush_locations_.clear();
61 }
62 
64 {
65  brush_locations_.erase(hex);
66  invalidate(hex);
67 }
68 
70  builder_->rebuild_terrain(loc);
71 }
72 
74 {
75  display::draw_hex(loc);
76 
77  if(!get_map().on_board_with_border(loc) || map_screenshot_) {
78  return;
79  }
80 
81  if(get_map().in_selection(loc)) {
83  [tex = image::get_texture(image::locator{"editor/selection-overlay.png"}, image::TOD_COLORED)](const rect& d) {
84  draw::blit(tex, d);
85  });
86  }
87 
88  if(brush_locations_.find(loc) != brush_locations_.end()) {
91  draw::blit(tex, d);
92  });
93  }
94 
95  // Paint mouseover overlays
97  drawing_buffer_add(drawing_layer::mouseover_overlay, loc, [this](const rect& dest) {
100  mouseover_hex_overlay_.set_alpha_mod(SDL_ALPHA_OPAQUE);
101  });
102  }
103 }
104 
106 {
107  return map_outside_area();
108 }
109 
111 {
112  display::layout();
113 
114  config element;
115  config::attribute_value& text = element.add_child("element")["text"];
116  // Fill in the terrain report
117  if (get_map().on_board_with_border(mouseoverHex_)) {
119  refresh_report("terrain", &element);
120  refresh_report("terrain_info");
121  text = lexical_cast<std::string>(mouseoverHex_);
122  refresh_report("position", &element);
123  }
124 
125  if (context().teams().empty()) {
126  text = int(get_map().villages().size());
127  refresh_report("villages", &element);
128  } else {
129  refresh_report("villages");
130  refresh_report("num_units");
131  }
132 
133  // If we're showing hexes near the north of the map, put the help string at the bottom of the screen.
134  // Otherwise, put it at the top.
135  if(help_handle_ != 0) {
136  const bool place_at_top = get_visible_hexes().top[0] > 2;
137 
138  if(place_at_top != help_string_at_top_) {
140  double delta = map_outside_area().h - r.h;
141  if(place_at_top) {
143  } else {
145  }
146  help_string_at_top_ = place_at_top;
147  }
148  }
149 }
150 
152 {
154 }
155 
157 {
159 }
160 
161 void editor_display::set_status(const std::string& str, const bool is_success)
162 {
163  const color_t color{0, 0, 0, 0xbb};
164  int size = font::SIZE_SMALL;
165  point canvas_size = video::game_canvas_size();
166  const int border = 3;
167 
168  std::string formatted_str;
169  if (is_success) {
170  formatted_str = VGETTEXT("<span color='#66ff00'><span face='DejaVuSans'>✔</span> $msg</span>", {{"msg", str}});
171  } else {
172  formatted_str = VGETTEXT("<span color='red'><span face='DejaVuSans'>✘</span> $msg</span>", {{"msg", str}});
173  }
174 
175  font::floating_label flabel(formatted_str);
176  flabel.set_font_size(size);
177  flabel.set_position(0, canvas_size.y);
178  flabel.set_bg_color(color);
179  flabel.set_border_size(border);
180  flabel.set_lifetime(1000, 10);
181  flabel.use_markup(true);
182 
183  const int f_handle = font::add_floating_label(flabel);
184  const auto& r = font::get_floating_label_rect(f_handle);
185  font::move_floating_label(f_handle, r.w, -r.h);
186 }
187 
189 {
190  help_string_enabled_ = value;
191 
192  if (!value) {
194  } else if (!help_string_.empty()) {
196  }
197 }
198 
200 {
202  help_handle_ = 0;
203 }
204 
205 void editor_display::set_help_string(const std::string& str)
206 {
207  // Always update the internal string so we can toggle its visibility back
208  // at any time without having to ask the current editor_palette.
209  help_string_ = str;
210 
212 
213  if (!help_string_enabled_ || help_string_.empty()) {
214  return;
215  }
216 
217  const color_t color{0, 0, 0, 0xbb};
218 
219  int size = font::SIZE_LARGE;
220  point canvas_size = video::game_canvas_size();
221 
222  while(size > 0) {
223  if(font::pango_line_width(str, size) * 2 > canvas_size.x) {
224  size--;
225  } else {
226  break;
227  }
228  }
229 
230  const int border = 5;
231 
232  font::floating_label flabel(str);
233  flabel.set_font_size(size);
234  flabel.set_position(canvas_size.x / 2, canvas_size.y);
235  flabel.set_bg_color(color);
236  flabel.set_border_size(border);
237 
239 
240  // Put the label near the bottom of the screen. In layout() it'll be moved to the top if the
241  // user is editing hexes at the south edge of the map.
242  help_string_at_top_ = false;
244  font::move_floating_label(help_handle_, 0.0, -double(r.h));
245 }
246 
247 } //end namespace editor
Definitions for the terrain builder.
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
config & add_child(config_key_type key)
Definition: config.cpp:440
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:97
bool map_screenshot_
Used to indicate to drawing functions that we are doing a map screenshot.
Definition: display.hpp:883
std::map< map_location, std::vector< overlay > > overlay_map
Definition: display.hpp:903
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3091
virtual void layout() override
Finalize screen layout.
Definition: display.cpp:2387
map_location mouseoverHex_
Definition: display.hpp:783
const rect_of_hexes get_visible_hexes() const
Returns the rectangular area of visible hexes.
Definition: display.hpp:365
void drawing_buffer_add(const drawing_layer layer, const map_location &loc, decltype(draw_helper::do_draw) draw_func)
Add an item to the drawing buffer.
Definition: display.cpp:1256
rect map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.cpp:519
const display_context & context() const
Definition: display.hpp:193
const std::unique_ptr< terrain_builder > builder_
Definition: display.hpp:747
virtual void draw_hex(const map_location &loc)
Redraws a single gamemap location.
Definition: display.cpp:2607
void refresh_report(const std::string &report_name, const config *new_cfg=nullptr)
Update the given report.
Definition: display.cpp:2840
The brush class represents a single brush – a set of relative locations around a "hotspot",...
Definition: brush.hpp:27
The editor_controller class contains the mouse and keyboard event handling routines for the editor.
map_context & get_current_map_context() const
std::string help_string_
Current help string, cached for easy visibility toggling.
virtual overlay_map & get_overlays() override
Inherited from display.
virtual void layout() override
TLD layout() override.
virtual const time_of_day & get_time_of_day(const map_location &loc=map_location::null_location()) const override
Inherited from display.
void set_help_string(const std::string &str)
Sets and shows the tooltip-like text at the top or bottom of the map area.
void rebuild_terrain(const map_location &loc)
int help_handle_
ID of the floating label that's controlled by set_help_string() / clear_help_string().
rect get_clip_rect() const override
Get the clipping rectangle for drawing.
std::set< map_location > brush_locations_
bool help_string_at_top_
Ignored when help_handle_ == 0.
bool help_string_enabled_
Whether the help text is currently shown.
void remove_brush_loc(const map_location &hex)
editor_controller & controller_
void set_help_string_enabled(bool value)
Sets whether the help text should be shown.
editor_display(editor_controller &controller, reports &reports_object)
void add_brush_loc(const map_location &hex)
void draw_hex(const map_location &loc) override
Redraws a single gamemap location.
void set_brush_locs(const std::set< map_location > &hexes)
const editor_map & get_map() const
void set_status(const std::string &str, const bool is_success)
Set a status text at the bottom left of the map area.
void clear_help_string()
Removes the help string.
overlay_map & get_overlays()
const tod_manager * get_time_manager() const
void set_lifetime(int lifetime, int fadeout=100)
void set_position(double xpos, double ypos)
void set_border_size(int border)
void set_bg_color(const color_t &bg_color)
void set_font_size(int font_size)
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:63
Generic locator abstracting the location of an image.
Definition: picture.hpp:59
void set_alpha_mod(uint8_t alpha)
Alpha modifier.
Definition: texture.cpp:151
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
Definition: tod_manager.hpp:56
Drawing functions, for drawing things on the screen.
@ border
The border of the map.
@ mouseover_overlay
Mouseover overlay used by editor.
@ fog_shroud
Fog and shroud.
@ selected_hex
Image on the selected unit.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
New lexcical_cast header.
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition: draw.cpp:310
Manage the empty-palette in the editor.
Definition: action.cpp:31
const int SIZE_LARGE
Definition: constants.cpp:30
int pango_line_width(const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style=font::pango_text::STYLE_NORMAL)
Determine the width of a line of text given a certain font size.
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
void remove_floating_label(int handle, int fadeout)
removes the floating label given by 'handle' from the screen
const int SIZE_SMALL
Definition: constants.cpp:24
SDL_Rect get_floating_label_rect(int handle)
void move_floating_label(int handle, double xmove, double ymove)
moves the floating label given by 'handle' by (xmove,ymove)
std::string editor_brush
@ HEXED
Standard hexagonal tile mask applied, removing portions that don't fit.
Definition: picture.hpp:166
@ TOD_COLORED
Same as HEXED, but with Time of Day color tint applied.
Definition: picture.hpp:168
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:920
Unit and team statistics.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
Definition: video.cpp:432
Definition: display.hpp:45
Transitional API for porting SDL_ttf-based code to Pango.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
Encapsulates the map of the game.
Definition: location.hpp:45
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
#define d