The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
terrain_layers.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
15#define GETTEXT_DOMAIN "wesnoth-lib"
16
18
19#include "display.hpp"
20#include "formatter.hpp"
21#include "gui/widgets/label.hpp"
24#include "picture.hpp"
25
26namespace gui2::dialogs
27{
28
29REGISTER_DIALOG(terrain_layers)
30
32 : modal_dialog(window_id())
33 , tile_(nullptr)
34 , tile_logs_()
35{
36 terrain_builder& builder = disp.get_builder();
37 tile_ = builder.get_tile(loc);
38
39 assert(tile_);
40
41 const std::string& tod_id = disp.get_time_of_day(loc).id;
42 tile_->rebuild_cache(tod_id, &tile_logs_);
43}
44
46{
47 //
48 // List terrain flags
49 //
50 find_widget<label>("flags").set_label(utils::bullet_list(tile_->flags, 0));
51
52 //
53 // Generate terrain list
54 //
55 listbox& list = find_widget<listbox>("layer_list");
56
57 int order = 1;
59 const terrain_builder::tile::rule_image_rand& ri = *det.first;
60 const terrain_builder::rule_image_variant& variant = *det.second;
61
62 // TODO: also use random image variations (not just take 1st)
63 const image::locator& img = variant.images.front().get_first_frame();
64 const std::string& name = img.get_filename();
65 // TODO: deal with (rarely used) ~modifications
66 //const std::string& modif = img.get_modifications();
67 const map_location& loc_cut = img.get_loc();
68
69 std::ostringstream image_steam;
70
71 const int tz = game_config::tile_size;
72 rect r {0,0,tz,tz};
73
74 const point img_size = image::get_size(img.get_filename());
75
76 // calculate which part of the image the terrain engine uses
77 if(loc_cut.valid()) {
78 // copied from image.cpp : load_image_sub_file()
79 r = {
80 ((tz * 3) / 4) * loc_cut.x
81 , tz * loc_cut.y + (tz / 2) * (loc_cut.x % 2)
82 , tz
83 , tz
84 };
85
86 if(img.get_center_x() >= 0 && img.get_center_y() >= 0) {
87 r.x += img_size.x / 2 - img.get_center_x();
88 r.y += img_size.y / 2 - img.get_center_y();
89 }
90 }
91
92 image_steam << "terrain/foreground.png";
93
94 // Cut and mask the image
95 // ~CROP and ~BLIT have limitations, we do some math to avoid them
96 // TODO: ^ eh? what limitations?
97 rect r2{0, 0, img_size.x, img_size.y};
98 r2.clip(r);
99 if(!r2.empty()) {
100 image_steam
101 << "~BLIT(" << name
102 << "~CROP("
103 << r2.x << "," << r2.y << ","
104 << r2.w << "," << r2.h << ")"
105 << "," << r2.x - r.x << "," << r2.y - r.y
106 << ")"
107 << "~MASK(" << "terrain/alphamask.png" << ")";
108 }
109
110 list.add_row(widget_data{
111 { "index", {
112 { "label", (formatter() << (ri->is_background() ? "B ": "F ") << order).str() }
113 }},
114 { "image_used", {
115 { "label", image_steam.str() }
116 }},
117 { "image_full", {
118 { "label", name + "~SCALE(72,72)" }
119 }},
120 { "name", {
121 { "label", name }
122 }},
123 { "loc", {
124 { "label", (formatter() << img.get_loc()).str() }
125 }},
126 { "layer", {
127 { "label", std::to_string(ri->layer) }
128 }},
129 { "base_x", {
130 { "label", std::to_string(ri->basex) }
131 }},
132 { "base_y", {
133 { "label", std::to_string(ri->basey) }
134 }},
135 { "center", {
136 { "label", (formatter() << ri->center_x << ", " << ri->center_y).str() }
137 }}
138 });
139
140 ++order;
141 }
142}
143
144} // namespace dialogs
map_location loc
Definition move.cpp:172
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition display.hpp:88
std::ostringstream wrapper.
Definition formatter.hpp:40
Abstract base class for all modal dialogs.
virtual void pre_show() override
Actions to be taken before showing the window.
terrain_builder::tile::logs tile_logs_
terrain_builder::tile * tile_
The listbox class.
Definition listbox.hpp:41
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition listbox.cpp:92
point get_size() const
Returns the size of the widget.
Definition widget.cpp:316
Generic locator abstracting the location of an image.
Definition picture.hpp:59
The class terrain_builder is constructed from a config object, and a gamemap object.
Definition builder.hpp:48
tile * get_tile(const map_location &loc)
Definition builder.cpp:1208
map_display and display: classes which take care of displaying the map and game-data on the screen.
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
unsigned int tile_size
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
std::string bullet_list(const T &v, std::size_t indent=4, const std::string &bullet=font::unicode_bullet)
Generates a new string containing a bullet list.
Encapsulates the map of the game.
Definition location.hpp:46
bool valid() const
Definition location.hpp:111
Holds a 2D point.
Definition point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition rect.hpp:49
std::vector< animated< image::locator > > images
An animated image locator built according to the image string.
Definition builder.hpp:207
int basex
The position of the image base (that is, the point where the image reaches the floor) for vertical la...
Definition builder.hpp:238
int layer
The layer of the image for horizontal layering.
Definition builder.hpp:234
int center_x
The position where the center of the image base should be.
Definition builder.hpp:247
Represent a rule_image applied with a random seed.
Definition builder.hpp:306
std::set< std::string > flags
The list of flags present in this tile.
Definition builder.hpp:302
std::pair< const rule_image_rand *, const rule_image_variant * > log_details
Definition builder.hpp:287