The Battle for Wesnoth  1.17.21+dev
editor_display.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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 "reports.hpp"
26 #include "team.hpp"
27 #include "terrain/builder.hpp"
28 #include "units/map.hpp"
29 #include "video.hpp"
30 
31 namespace wb {
32  class manager;
33 }
34 
35 namespace editor {
36 
38  : display(nullptr, std::shared_ptr<wb::manager>(), reports_object, "editor", config())
39  , brush_locations_()
40  , controller_(controller)
41  , mouseover_hex_overlay_()
42 {
43 }
44 
46 {
47  brush_locations_.insert(hex);
48  invalidate(hex);
49 }
50 
51 void editor_display::set_brush_locs(const std::set<map_location>& hexes)
52 {
54  brush_locations_ = hexes;
56 }
57 
59 {
61  brush_locations_.clear();
62 }
63 
65 {
66  brush_locations_.erase(hex);
67  invalidate(hex);
68 }
69 
71  builder_->rebuild_terrain(loc);
72 }
73 
75 {
76  display::draw_hex(loc);
77 
78  if(!map().on_board_with_border(loc) || map_screenshot_) {
79  return;
80  }
81 
82  if(map().in_selection(loc)) {
84  [tex = image::get_texture(image::locator{"editor/selection-overlay.png"}, image::TOD_COLORED)](const rect& d) {
85  draw::blit(tex, d);
86  });
87  }
88 
89  if(brush_locations_.find(loc) != brush_locations_.end()) {
92  draw::blit(tex, d);
93  });
94  }
95 
96  // Paint mouseover overlays
98  drawing_buffer_add(LAYER_MOUSEOVER_OVERLAY, loc, [this](const rect& dest) {
101  mouseover_hex_overlay_.set_alpha_mod(SDL_ALPHA_OPAQUE);
102  });
103  }
104 }
105 
107 {
108  return map_outside_area();
109 }
110 
112 {
113  display::layout();
114 
115  config element;
116  config::attribute_value &text = element.add_child("element")["text"];
117  // Fill in the terrain report
118  if (get_map().on_board_with_border(mouseoverHex_)) {
120  refresh_report("terrain", &element);
121  refresh_report("terrain_info");
122  text = lexical_cast<std::string>(mouseoverHex_);
123  refresh_report("position", &element);
124  }
125 
126  if (dc_->teams().empty()) {
127  text = int(get_map().villages().size());
128  refresh_report("villages", &element);
129  } else {
130  refresh_report("villages");
131  refresh_report("num_units");
132  }
133 
134  // If we're showing hexes near the north of the map, put the help string at the bottom of the screen.
135  // Otherwise, put it at the top.
136  if(help_handle_ != 0) {
137  const bool place_at_top = get_visible_hexes().top[0] > 2;
138 
139  if(place_at_top != help_string_at_top_) {
141  double delta = map_outside_area().h - r.h;
142  if(place_at_top) {
144  } else {
146  }
147  help_string_at_top_ = place_at_top;
148  }
149  }
150 }
151 
153 {
155 }
156 
158 {
160 }
161 
163 {
165  help_handle_ = 0;
166 }
167 
168 void editor_display::set_help_string(const std::string& str)
169 {
171 
172  const color_t color{0, 0, 0, 0xbb};
173 
174  int size = font::SIZE_LARGE;
175  point canvas_size = video::game_canvas_size();
176 
177  while(size > 0) {
178  if(font::pango_line_width(str, size) * 2 > canvas_size.x) {
179  size--;
180  } else {
181  break;
182  }
183  }
184 
185  const int border = 5;
186 
187  font::floating_label flabel(str);
188  flabel.set_font_size(size);
189  flabel.set_position(canvas_size.x / 2, canvas_size.y);
190  flabel.set_bg_color(color);
191  flabel.set_border_size(border);
192 
194 
195  // Put the label near the bottom of the screen. In layout() it'll be moved to the top if the
196  // user is editing hexes at the south edge of the map.
197  help_string_at_top_ = false;
199  font::move_floating_label(help_handle_, 0.0, -double(r.h));
200 }
201 
202 } //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:161
config & add_child(config_key_type key)
Definition: config.cpp:445
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:87
bool map_screenshot_
Used to indicate to drawing functions that we are doing a map screenshot.
Definition: display.hpp:927
std::map< map_location, std::vector< overlay > > overlay_map
Definition: display.hpp:947
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3094
@ LAYER_MOUSEOVER_OVERLAY
Mouseover overlay used by editor.
Definition: display.hpp:809
@ LAYER_FOG_SHROUD
Fog and shroud.
Definition: display.hpp:832
@ LAYER_SELECTED_HEX
Image on the selected unit.
Definition: display.hpp:835
virtual void layout() override
Finalize screen layout.
Definition: display.cpp:2397
map_location mouseoverHex_
Definition: display.hpp:776
const gamemap & get_map() const
Definition: display.hpp:105
const rect_of_hexes get_visible_hexes() const
Returns the rectangular area of visible hexes.
Definition: display.hpp:361
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:1259
rect map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.cpp:545
const std::unique_ptr< terrain_builder > builder_
Definition: display.hpp:740
virtual void draw_hex(const map_location &loc)
Redraws a single gamemap location.
Definition: display.cpp:2628
const display_context * dc_
Definition: display.hpp:677
void refresh_report(const std::string &report_name, const config *new_cfg=nullptr)
Update the given report.
Definition: display.cpp:2843
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:63
Generic locator abstracting the location of an image.
Definition: picture.hpp:64
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:301
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:235
@ TOD_COLORED
Same as HEXED, but with Time of Day color tint applied.
Definition: picture.hpp:237
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:985
Unit and team statistics.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
Definition: video.cpp:426
Definition: display.hpp:49
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