The Battle for Wesnoth  1.19.0-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 "lexical_cast.hpp"
24 #include "overlay.hpp"
25 #include "team.hpp"
26 #include "terrain/builder.hpp"
27 #include "video.hpp"
28 
29 namespace wb {
30  class manager;
31 }
32 
33 namespace editor {
34 
36  : display(nullptr, std::shared_ptr<wb::manager>(), reports_object, "editor", config())
37  , brush_locations_()
38  , controller_(controller)
39  , mouseover_hex_overlay_()
40 {
41 }
42 
44 {
45  brush_locations_.insert(hex);
46  invalidate(hex);
47 }
48 
49 void editor_display::set_brush_locs(const std::set<map_location>& hexes)
50 {
52  brush_locations_ = hexes;
54 }
55 
57 {
59  brush_locations_.clear();
60 }
61 
63 {
64  brush_locations_.erase(hex);
65  invalidate(hex);
66 }
67 
69  builder_->rebuild_terrain(loc);
70 }
71 
73 {
74  display::draw_hex(loc);
75 
76  if(!map().on_board_with_border(loc) || map_screenshot_) {
77  return;
78  }
79 
80  if(map().in_selection(loc)) {
82  [tex = image::get_texture(image::locator{"editor/selection-overlay.png"}, image::TOD_COLORED)](const rect& d) {
83  draw::blit(tex, d);
84  });
85  }
86 
87  if(brush_locations_.find(loc) != brush_locations_.end()) {
90  draw::blit(tex, d);
91  });
92  }
93 
94  // Paint mouseover overlays
96  drawing_buffer_add(LAYER_MOUSEOVER_OVERLAY, loc, [this](const rect& dest) {
99  mouseover_hex_overlay_.set_alpha_mod(SDL_ALPHA_OPAQUE);
100  });
101  }
102 }
103 
105 {
106  return map_outside_area();
107 }
108 
110 {
111  display::layout();
112 
113  config element;
114  config::attribute_value &text = element.add_child("element")["text"];
115  // Fill in the terrain report
116  if (get_map().on_board_with_border(mouseoverHex_)) {
118  refresh_report("terrain", &element);
119  refresh_report("terrain_info");
120  text = lexical_cast<std::string>(mouseoverHex_);
121  refresh_report("position", &element);
122  }
123 
124  if (dc_->teams().empty()) {
125  text = int(get_map().villages().size());
126  refresh_report("villages", &element);
127  } else {
128  refresh_report("villages");
129  refresh_report("num_units");
130  }
131 
132  // If we're showing hexes near the north of the map, put the help string at the bottom of the screen.
133  // Otherwise, put it at the top.
134  if(help_handle_ != 0) {
135  const bool place_at_top = get_visible_hexes().top[0] > 2;
136 
137  if(place_at_top != help_string_at_top_) {
139  double delta = map_outside_area().h - r.h;
140  if(place_at_top) {
142  } else {
144  }
145  help_string_at_top_ = place_at_top;
146  }
147  }
148 }
149 
151 {
153 }
154 
156 {
158 }
159 
161 {
163  help_handle_ = 0;
164 }
165 
166 void editor_display::set_help_string(const std::string& str)
167 {
169 
170  const color_t color{0, 0, 0, 0xbb};
171 
172  int size = font::SIZE_LARGE;
173  point canvas_size = video::game_canvas_size();
174 
175  while(size > 0) {
176  if(font::pango_line_width(str, size) * 2 > canvas_size.x) {
177  size--;
178  } else {
179  break;
180  }
181  }
182 
183  const int border = 5;
184 
185  font::floating_label flabel(str);
186  flabel.set_font_size(size);
187  flabel.set_position(canvas_size.x / 2, canvas_size.y);
188  flabel.set_bg_color(color);
189  flabel.set_border_size(border);
190 
192 
193  // Put the label near the bottom of the screen. In layout() it'll be moved to the top if the
194  // user is editing hexes at the south edge of the map.
195  help_string_at_top_ = false;
197  font::move_floating_label(help_handle_, 0.0, -double(r.h));
198 }
199 
200 } //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:159
config & add_child(config_key_type key)
Definition: config.cpp:441
virtual const std::vector< team > & teams() const =0
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:81
bool map_screenshot_
Used to indicate to drawing functions that we are doing a map screenshot.
Definition: display.hpp:926
std::map< map_location, std::vector< overlay > > overlay_map
Definition: display.hpp:946
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3137
@ LAYER_MOUSEOVER_OVERLAY
Mouseover overlay used by editor.
Definition: display.hpp:808
@ LAYER_FOG_SHROUD
Fog and shroud.
Definition: display.hpp:831
@ LAYER_SELECTED_HEX
Image on the selected unit.
Definition: display.hpp:834
virtual void layout() override
Finalize screen layout.
Definition: display.cpp:2442
map_location mouseoverHex_
Definition: display.hpp:775
const gamemap & get_map() const
Definition: display.hpp:99
const rect_of_hexes get_visible_hexes() const
Returns the rectangular area of visible hexes.
Definition: display.hpp:355
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:1287
rect map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.cpp:540
const std::unique_ptr< terrain_builder > builder_
Definition: display.hpp:739
virtual void draw_hex(const map_location &loc)
Redraws a single gamemap location.
Definition: display.cpp:2671
const display_context * dc_
Definition: display.hpp:676
void refresh_report(const std::string &report_name, const config *new_cfg=nullptr)
Update the given report.
Definition: display.cpp:2886
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
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().
const editor_map & map() const
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.
void remove_brush_loc(const map_location &hex)
editor_controller & controller_
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)
void clear_help_string()
Removes the help string.
overlay_map & get_overlays()
const tod_manager * get_time_manager() const
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:62
Generic locator abstracting the location of an image.
Definition: picture.hpp:63
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.
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
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:228
@ TOD_COLORED
Same as HEXED, but with Time of Day color tint applied.
Definition: picture.hpp:230
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:959
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:434
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:38
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