The Battle for Wesnoth  1.19.0-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"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "picture.hpp"
26 
27 namespace gui2::dialogs
28 {
29 
30 REGISTER_DIALOG(terrain_layers)
31 
33  : modal_dialog(window_id())
34  , tile_(nullptr)
35  , tile_logs_()
36 {
37  terrain_builder& builder = disp.get_builder();
38  tile_ = builder.get_tile(loc);
39 
40  assert(tile_);
41 
42  const std::string& tod_id = disp.get_time_of_day(loc).id;
43  tile_->rebuild_cache(tod_id, &tile_logs_);
44 }
45 
47 {
48  //
49  // List terrain flags
50  //
51  std::vector<std::string> flags(tile_->flags.begin(), tile_->flags.end());
52 
53  for(auto& flag : flags) {
54  flag = (formatter() << font::unicode_bullet << " " << flag).str();
55  }
56 
57  find_widget<label>(&window, "flags", false).set_label(utils::join(flags, "\n"));
58 
59  //
60  // Generate terrain list
61  //
62  listbox& list = find_widget<listbox>(&window, "layer_list", false);
63 
64  int order = 1;
66  const terrain_builder::tile::rule_image_rand& ri = *det.first;
67  const terrain_builder::rule_image_variant& variant = *det.second;
68 
69  // TODO: also use random image variations (not just take 1st)
70  const image::locator& img = variant.images.front().get_first_frame();
71  const std::string& name = img.get_filename();
72  // TODO: deal with (rarely used) ~modifications
73  //const std::string& modif = img.get_modifications();
74  const map_location& loc_cut = img.get_loc();
75 
78 
79  item["label"] = (formatter() << (ri->is_background() ? "B ": "F ") << order).str();
80  data.emplace("index", item);
81 
82  std::ostringstream image_steam;
83 
84  const int tz = game_config::tile_size;
85  SDL_Rect r {0,0,tz,tz};
86 
87  const point img_size = image::get_size(img.get_filename());
88 
89  // calculate which part of the image the terrain engine uses
90  if(loc_cut.valid()) {
91  // copied from image.cpp : load_image_sub_file()
92  r = {
93  ((tz * 3) / 4) * loc_cut.x
94  , tz * loc_cut.y + (tz / 2) * (loc_cut.x % 2)
95  , tz
96  , tz
97  };
98 
99  if(img.get_center_x() >= 0 && img.get_center_y() >= 0) {
100  r.x += img_size.x / 2 - img.get_center_x();
101  r.y += img_size.y / 2 - img.get_center_y();
102  }
103  }
104 
105  image_steam << "terrain/foreground.png";
106 
107  // Cut and mask the image
108  // ~CROP and ~BLIT have limitations, we do some math to avoid them
109  // TODO: ^ eh? what limitations?
110  rect r2{0, 0, img_size.x, img_size.y};
111  r2.clip(r);
112  if(!r2.empty()) {
113  image_steam
114  << "~BLIT(" << name
115  << "~CROP("
116  << r2.x << "," << r2.y << ","
117  << r2.w << "," << r2.h << ")"
118  << "," << r2.x - r.x << "," << r2.y - r.y
119  << ")"
120  << "~MASK(" << "terrain/alphamask.png" << ")";
121  }
122 
123  item["label"] = image_steam.str();
124  data.emplace("image_used", item);
125 
126  item["label"] = name + "~SCALE(72,72)";
127  data.emplace("image_full", item);
128 
129  item["label"] = name;
130  data.emplace("name", item);
131 
132  item["label"] = (formatter() << img.get_loc()).str();
133  data.emplace("loc", item);
134 
135  item["label"] = std::to_string(ri->layer);
136  data.emplace("layer", item);
137 
138  item["label"] = std::to_string(ri->basex);
139  data.emplace("base_x", item);
140 
141  item["label"] = std::to_string(ri->basey);
142  data.emplace("base_y", item);
143 
144  item["label"] = (formatter() << ri->center_x << ", " << ri->center_y).str();
145  data.emplace("center", item);
146 
147  ++order;
148 
149  list.add_row(data);
150  }
151 }
152 
153 } // namespace dialogs
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(window &window) 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:43
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:59
point get_size() const
Returns the size of the widget.
Definition: widget.cpp:307
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
Generic locator abstracting the location of an image.
Definition: picture.hpp:63
int get_center_x() const
Definition: picture.hpp:88
const std::string & get_filename() const
Definition: picture.hpp:85
int get_center_y() const
Definition: picture.hpp:89
const map_location & get_loc() const
Definition: picture.hpp:87
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
This file contains the window object, this object is a top level container which has the event manage...
const std::string unicode_bullet
Definition: constants.cpp:47
unsigned int tile_size
Definition: game_config.cpp:51
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::string_view data
Definition: picture.cpp:194
Encapsulates the map of the game.
Definition: location.hpp:38
bool valid() const
Definition: location.hpp:89
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:213
int basex
The position of the image base (that is, the point where the image reaches the floor) for vertical la...
Definition: builder.hpp:252
bool is_background() const
Definition: builder.hpp:242
int layer
The layer of the image for horizontal layering.
Definition: builder.hpp:248
int center_x
The position where the center of the image base should be.
Definition: builder.hpp:264
Represent a rule_image applied with a random seed.
Definition: builder.hpp:345
std::set< std::string > flags
The list of flags present in this tile.
Definition: builder.hpp:341
std::pair< const rule_image_rand *, const rule_image_variant * > log_details
Definition: builder.hpp:325