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