The Battle for Wesnoth  1.19.7+dev
terrain_layers.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
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"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/window.hpp"
24 #include "picture.hpp"
25 
26 namespace gui2::dialogs
27 {
28 
29 REGISTER_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 
70  widget_item item;
71 
72  item["label"] = (formatter() << (ri->is_background() ? "B ": "F ") << order).str();
73  data.emplace("index", item);
74 
75  std::ostringstream image_steam;
76 
77  const int tz = game_config::tile_size;
78  SDL_Rect r {0,0,tz,tz};
79 
80  const point img_size = image::get_size(img.get_filename());
81 
82  // calculate which part of the image the terrain engine uses
83  if(loc_cut.valid()) {
84  // copied from image.cpp : load_image_sub_file()
85  r = {
86  ((tz * 3) / 4) * loc_cut.x
87  , tz * loc_cut.y + (tz / 2) * (loc_cut.x % 2)
88  , tz
89  , tz
90  };
91 
92  if(img.get_center_x() >= 0 && img.get_center_y() >= 0) {
93  r.x += img_size.x / 2 - img.get_center_x();
94  r.y += img_size.y / 2 - img.get_center_y();
95  }
96  }
97 
98  image_steam << "terrain/foreground.png";
99 
100  // Cut and mask the image
101  // ~CROP and ~BLIT have limitations, we do some math to avoid them
102  // TODO: ^ eh? what limitations?
103  rect r2{0, 0, img_size.x, img_size.y};
104  r2.clip(r);
105  if(!r2.empty()) {
106  image_steam
107  << "~BLIT(" << name
108  << "~CROP("
109  << r2.x << "," << r2.y << ","
110  << r2.w << "," << r2.h << ")"
111  << "," << r2.x - r.x << "," << r2.y - r.y
112  << ")"
113  << "~MASK(" << "terrain/alphamask.png" << ")";
114  }
115 
116  item["label"] = image_steam.str();
117  data.emplace("image_used", item);
118 
119  item["label"] = name + "~SCALE(72,72)";
120  data.emplace("image_full", item);
121 
122  item["label"] = name;
123  data.emplace("name", item);
124 
125  item["label"] = (formatter() << img.get_loc()).str();
126  data.emplace("loc", item);
127 
128  item["label"] = std::to_string(ri->layer);
129  data.emplace("layer", item);
130 
131  item["label"] = std::to_string(ri->basex);
132  data.emplace("base_x", item);
133 
134  item["label"] = std::to_string(ri->basey);
135  data.emplace("base_y", item);
136 
137  item["label"] = (formatter() << ri->center_x << ", " << ri->center_y).str();
138  data.emplace("center", item);
139 
140  ++order;
141 
142  list.add_row(data);
143  }
144 }
145 
146 } // 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:97
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:1211
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...
unsigned int tile_size
Definition: game_config.cpp:55
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
std::string img(const std::string &src, const std::string &align, const bool floating)
Definition: markup.cpp:29
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.
std::string_view data
Definition: picture.cpp:178
Encapsulates the map of the game.
Definition: location.hpp:45
bool valid() const
Definition: location.hpp:110
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
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
bool is_background() const
Definition: builder.hpp:228
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:286