The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
terrain_palettes.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://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 /**
16  * Terrain-palette in the editor.
17  */
18 
19 #define GETTEXT_DOMAIN "wesnoth-editor"
20 
22 
23 #include "gettext.hpp"
24 #include "formula/string_utils.hpp"
25 
26 namespace {
27  static t_translation::terrain_code fg_terrain;
28  static t_translation::terrain_code bg_terrain;
29 }
30 
31 namespace editor {
32 
34  return bg_terrain;
35 }
36 
38  return fg_terrain;
39 }
40 
43 
44 
47 }
48 
50  bg_terrain = item_map_[item_id];
52 }
53 
55  fg_terrain = item_map_[item_id];
57 }
58 
60  bg_terrain = terrain;
62 }
63 
65  fg_terrain = terrain;
67 }
68 
69 
71 {
72  // Get the available terrains temporary in items
74 
75  //move "invalid" items to the end
76  std::stable_partition(items.begin(), items.end(), is_valid_terrain);
77 
78  // Get the available groups and add them to the structure
79  std::set<std::string> group_names;
80  for(const config &group : cfg.child_range("terrain_group")) {
81  if(group_names.count(group["id"]) == 0) {
82  config group_cfg;
83  group_cfg["id"] = group["id"];
84  group_cfg["name"] = group["name"];
85 
86  group_cfg["icon"] = group["icon"].str();
87  group_cfg["core"] = group["core"];
88  groups_.emplace_back(group_cfg);
89 
90  group_names.insert(groups_.back().id);
91  }
92  }
93  for(const config &group : cfg.child_range("editor_group")) {
94  if(group_names.count(group["id"]) == 0) {
95  config group_cfg;
96  group_cfg["id"] = group["id"];
97  group_cfg["name"] = group["name"];
98 
99  group_cfg["icon"] = "icons/terrain/terrain_" + group["icon"].str();
100  group_cfg["core"] = group["core"];
101  groups_.emplace_back(group_cfg);
102 
103  group_names.insert(groups_.back().id);
104  }
105  }
106 
107  std::map<std::string, item_group*> id_to_group;
108  for (item_group& group : groups_) {
109  id_to_group.emplace(group.id, &group);
110  }
111 
112  // add the groups for all terrains to the map
113  for (const t_translation::terrain_code& t : items) {
114 
115  const terrain_type& t_info = map().get_terrain_info(t);
116  DBG_ED << "Palette: processing terrain " << t_info.name()
117  << "(editor name: '" << t_info.editor_name() << "') "
118  << "(" << t_info.number() << ")"
119  << ": " << t_info.editor_group() << "\n";
120 
121  // don't display terrains that were automatically created from base+overlay
122  if (t_info.is_combined()) continue;
123  // nor display terrains that have hide_in_editor=true
124  if (t_info.hide_in_editor()) continue;
125 
126  // add the terrain to the requested groups
127  const std::vector<std::string>& keys = utils::split(t_info.editor_group());
128  bool core = false;
129 
130  item_map_[get_id(t)] = t;
131 
132  for (const std::string& k : keys) {
133  group_map_[k].push_back(get_id(t));
134  nmax_items_ = std::max<int>(nmax_items_, group_map_[k].size());
135  std::map<std::string, item_group*>::iterator i = id_to_group.find(k);
136  if (i != id_to_group.end()) {
137  if (i->second->core) {
138  core = true;
139  }
140  }
141  }
142 
143  // A terrain is considered core iff it appears in at least
144  // one core terrain group
145  if (core) {
146  // Add the terrain to the default group
147  group_map_["all"].push_back(get_id(t));
148  nmax_items_ = std::max<int>(nmax_items_, group_map_["all"].size());
149  } else {
150  non_core_items_.insert(get_id(t));
151  }
152 
153  }
154 
155  // Set the default terrain
156  select_fg_item("regular_mountains");
157  select_bg_item("grassland");
158 
159  // Set the default group
160  set_group("all");
161 
162  if(active_group().empty()) {
163  ERR_ED << "No items found." << std::endl;
164  }
165 }
166 
168  surface& image, std::stringstream& tooltip_text) {
169 
170  const t_translation::terrain_code base_terrain = map().get_terrain_info(terrain).default_base();
171 
172  //Draw default base for overlay terrains
173  if(base_terrain != t_translation::NONE_TERRAIN) {
174  const std::string base_filename = map().get_terrain_info(base_terrain).editor_image();
175  surface base_image(image::get_image(base_filename));
176 
177  if(base_image == nullptr) {
178  tooltip_text << "BASE IMAGE NOT FOUND\n";
179  ERR_ED << "image for terrain : '" << base_filename << "' not found" << std::endl;
181  if(base_image == nullptr) {
182  ERR_ED << "Placeholder image not found" << std::endl;
183  return;
184  }
185  }
186 
187  if(base_image->w != item_size_ || base_image->h != item_size_) {
188  base_image.assign(scale_surface(base_image,
190  }
191  }
192 
193  const std::string filename = map().get_terrain_info(terrain).editor_image();
194  image = image::get_image(filename);
195  if(image == nullptr) {
196  tooltip_text << "IMAGE NOT FOUND\n";
197  ERR_ED << "image for terrain: '" << filename << "' not found" << std::endl;
199  if(image == nullptr) {
200  ERR_ED << "Placeholder image not found" << std::endl;
201  return;
202  }
203  }
204 
205  if(image->w != item_size_ || image->h != item_size_) {
206  image.assign(scale_surface(image,
208  }
209 
210  tooltip_text << map().get_terrain_editor_string(terrain);
212  tooltip_text << " " + font::unicode_em_dash + " " << terrain;
213  }
214 }
215 
217 //TODO avoid magic numbers
218  : editor_palette<t_translation::terrain_code>(gui, cfg, 36, 4, toolkit)
219 {
220 }
221 
223 {
224  const terrain_type& t_info = map().get_terrain_info(terrain);
225  return t_info.id();
226 }
227 
229 {
230  std::ostringstream msg;
231  msg << _("FG: ") << map().get_terrain_editor_string(selected_fg_item()) << " | "
232  << _("BG: ") << map().get_terrain_editor_string(selected_bg_item());
233  return msg.str();
234 }
235 
236 
237 }
const t_translation::ter_list & get_terrain_list() const
Gets the list of terrains.
Definition: map.cpp:44
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:926
std::vector< char_t > string
virtual const std::string & get_id(const t_translation::terrain_code &terrain)
const t_translation::terrain_code & get_selected_bg_terrain()
const terrain_code NONE_TERRAIN
Definition: translation.hpp:64
virtual void select_fg_item(const std::string &item_id) override
Select a foreground item.
bool get_draw_terrain_codes() const
Getter for the terrain code debug overlay on tiles.
Definition: display.hpp:354
Stores the info about the groups in a nice format.
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:63
virtual void select_bg_item(const std::string &item_id) override
child_itors child_range(config_key_type key)
Definition: config.cpp:343
const t_translation::terrain_code & selected_fg_item() const
General purpose widgets.
bool hide_in_editor() const
Definition: terrain.hpp:40
const t_string & name() const
Definition: terrain.hpp:33
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:55
const std::string & id() const
Definition: terrain.hpp:37
const std::vector< std::string > & active_group()
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
bool is_combined() const
Definition: terrain.hpp:77
const std::vector< std::string > items
surface scale_surface(const surface &surf, int w, int h)
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gr...
Definition: utils.cpp:274
virtual void setup(const config &cfg)
Setup the internal data structure.
const std::string & editor_group() const
Definition: terrain.hpp:73
const t_translation::terrain_code & get_selected_fg_terrain()
const t_string & editor_name() const
Definition: terrain.hpp:34
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
const gamemap & map() const
const terrain_code VOID_TERRAIN
void select_bg_item(const t_translation::terrain_code &terrain)
t_translation::terrain_code default_base() const
Definition: terrain.hpp:79
virtual std::string get_help_string()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
const terrain_code FOGGED
std::map< std::string, std::vector< std::string > > group_map_
const std::string & editor_image() const
Definition: terrain.hpp:32
t_translation::terrain_code number() const
Definition: terrain.hpp:43
Manage the empty-palette in the editor.
Definition: action.cpp:29
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:98
void select_fg_item(const t_translation::terrain_code &terrain)
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:133
terrain_palette(editor_display &gui, const config &cfg, editor_toolkit &toolkit)
static bool is_valid_terrain(const t_translation::terrain_code &c)
size_t i
Definition: function.cpp:933
void assign(SDL_Surface *surf)
Definition: surface.hpp:46
double t
Definition: astarsearch.cpp:64
const std::string unicode_em_dash
Definition: constants.cpp:39
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:109
std::vector< terrain_code > ter_list
Definition: translation.hpp:83
virtual void draw_item(const t_translation::terrain_code &terrain, surface &item_image, std::stringstream &tooltip_text)
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
#define DBG_ED
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mock_char c
const t_translation::terrain_code & selected_bg_item() const
std::string::const_iterator iterator
Definition: tokenizer.hpp:24