The Battle for Wesnoth  1.19.4+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  std::vector<std::string> flags(tile_->flags.begin(), tile_->flags.end());
51 
52  for(auto& flag : flags) {
53  flag = (formatter() << font::unicode_bullet << " " << flag).str();
54  }
55 
56  find_widget<label>("flags").set_label(utils::join(flags, "\n"));
57 
58  //
59  // Generate terrain list
60  //
61  listbox& list = find_widget<listbox>("layer_list");
62 
63  int order = 1;
65  const terrain_builder::tile::rule_image_rand& ri = *det.first;
66  const terrain_builder::rule_image_variant& variant = *det.second;
67 
68  // TODO: also use random image variations (not just take 1st)
69  const image::locator& img = variant.images.front().get_first_frame();
70  const std::string& name = img.get_filename();
71  // TODO: deal with (rarely used) ~modifications
72  //const std::string& modif = img.get_modifications();
73  const map_location& loc_cut = img.get_loc();
74 
77 
78  item["label"] = (formatter() << (ri->is_background() ? "B ": "F ") << order).str();
79  data.emplace("index", item);
80 
81  std::ostringstream image_steam;
82 
83  const int tz = game_config::tile_size;
84  SDL_Rect r {0,0,tz,tz};
85 
86  const point img_size = image::get_size(img.get_filename());
87 
88  // calculate which part of the image the terrain engine uses
89  if(loc_cut.valid()) {
90  // copied from image.cpp : load_image_sub_file()
91  r = {
92  ((tz * 3) / 4) * loc_cut.x
93  , tz * loc_cut.y + (tz / 2) * (loc_cut.x % 2)
94  , tz
95  , tz
96  };
97 
98  if(img.get_center_x() >= 0 && img.get_center_y() >= 0) {
99  r.x += img_size.x / 2 - img.get_center_x();
100  r.y += img_size.y / 2 - img.get_center_y();
101  }
102  }
103 
104  image_steam << "terrain/foreground.png";
105 
106  // Cut and mask the image
107  // ~CROP and ~BLIT have limitations, we do some math to avoid them
108  // TODO: ^ eh? what limitations?
109  rect r2{0, 0, img_size.x, img_size.y};
110  r2.clip(r);
111  if(!r2.empty()) {
112  image_steam
113  << "~BLIT(" << name
114  << "~CROP("
115  << r2.x << "," << r2.y << ","
116  << r2.w << "," << r2.h << ")"
117  << "," << r2.x - r.x << "," << r2.y - r.y
118  << ")"
119  << "~MASK(" << "terrain/alphamask.png" << ")";
120  }
121 
122  item["label"] = image_steam.str();
123  data.emplace("image_used", item);
124 
125  item["label"] = name + "~SCALE(72,72)";
126  data.emplace("image_full", item);
127 
128  item["label"] = name;
129  data.emplace("name", item);
130 
131  item["label"] = (formatter() << img.get_loc()).str();
132  data.emplace("loc", item);
133 
134  item["label"] = std::to_string(ri->layer);
135  data.emplace("layer", item);
136 
137  item["label"] = std::to_string(ri->basex);
138  data.emplace("base_x", item);
139 
140  item["label"] = std::to_string(ri->basey);
141  data.emplace("base_y", item);
142 
143  item["label"] = (formatter() << ri->center_x << ", " << ri->center_y).str();
144  data.emplace("center", item);
145 
146  ++order;
147 
148  list.add_row(data);
149  }
150 }
151 
152 } // namespace dialogs
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:89
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: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:58
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
int get_center_x() const
Definition: picture.hpp:85
const std::string & get_filename() const
Definition: picture.hpp:82
int get_center_y() const
Definition: picture.hpp:86
const map_location & get_loc() const
Definition: picture.hpp:84
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...
const std::string unicode_bullet
Definition: constants.cpp:47
unsigned int tile_size
Definition: game_config.cpp:52
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::pair< std::string, unsigned > item
Definition: help_impl.hpp:387
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:178
Encapsulates the map of the game.
Definition: location.hpp:45
bool valid() const
Definition: location.hpp:97
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:205
int basex
The position of the image base (that is, the point where the image reaches the floor) for vertical la...
Definition: builder.hpp:236
bool is_background() const
Definition: builder.hpp:226
int layer
The layer of the image for horizontal layering.
Definition: builder.hpp:232
int center_x
The position where the center of the image base should be.
Definition: builder.hpp:245
Represent a rule_image applied with a random seed.
Definition: builder.hpp:304
std::set< std::string > flags
The list of flags present in this tile.
Definition: builder.hpp:300
std::pair< const rule_image_rand *, const rule_image_variant * > log_details
Definition: builder.hpp:284