The Battle for Wesnoth  1.15.2+dev
terrain_layers.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #define GETTEXT_DOMAIN "wesnoth-lib"
15 
17 
18 #include "display.hpp"
19 #include "formatter.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "picture.hpp"
26 
27 namespace gui2
28 {
29 namespace dialogs
30 {
31 
32 REGISTER_DIALOG(terrain_layers)
33 
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 
77  std::map<std::string, string_map> data;
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 
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 += surf->w / 2 - img.get_center_x();
102  r.y += surf->h / 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  SDL_Rect r2 = sdl::intersect_rects(r, {0,0,surf->w,surf->h});
112  if(r2.w > 0 && r2.h > 0) {
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
154 } // namespace gui2
std::pair< const rule_image_rand *, const rule_image_variant * > log_details
Definition: builder.hpp:323
int layer
The layer of the image for horizontal layering.
Definition: builder.hpp:246
int get_center_y() const
Definition: picture.hpp:88
tile * get_tile(const map_location &loc)
Definition: builder.cpp:1212
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
This file contains the window object, this object is a top level container which has the event manage...
Represent a rule_image applied with a random seed.
Definition: builder.hpp:341
t_string get_image() const
Wrapper for label.
Definition: image.hpp:58
The class terrain_builder is constructed from a config object, and a gamemap object.
Definition: builder.hpp:45
bool is_background() const
Definition: builder.hpp:240
int basex
The position of the image base (that is, the point where the image reaches the floor) for vertical la...
Definition: builder.hpp:250
SDL_Rect intersect_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the intersection of two rectangles.
Definition: rect.cpp:39
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
This file contains the settings handling of the widget library.
std::ostringstream wrapper.
Definition: formatter.hpp:38
bool valid() const
Definition: location.hpp:93
map_display and display: classes which take care of displaying the map and game-data on the screen...
Various uncategorised dialogs.
Encapsulates the map of the game.
Definition: location.hpp:42
std::set< std::string > flags
The list of flags present in this tile.
Definition: builder.hpp:338
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
int get_center_x() const
Definition: picture.hpp:87
const std::string & get_filename() const
Definition: picture.hpp:84
int center_x
The position where the center of the image base should be.
Definition: builder.hpp:262
const std::string unicode_bullet
Definition: constants.cpp:43
terrain_builder::tile * tile_
terrain_builder::tile::logs tile_logs_
const map_location & get_loc() const
Definition: picture.hpp:86
unsigned int tile_size
Definition: game_config.cpp:68
std::vector< animated< image::locator > > images
An animated image locator built according to the image string.
Definition: builder.hpp:211
virtual void pre_show(window &window) override
Inherited from modal_dialog.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371