The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 
21 #include "terrain_palettes.hpp"
22 
23 #include "gettext.hpp"
24 #include "formula/string_utils.hpp"
25 
26 namespace {
27  static t_translation::t_terrain fg_terrain;
28  static t_translation::t_terrain bg_terrain;
29 }
30 
31 namespace editor {
32 
34  return bg_terrain;
35 }
36 
38  return fg_terrain;
39 }
40 
41 const t_translation::t_terrain& terrain_palette::selected_fg_item() const { return fg_terrain; }
42 const t_translation::t_terrain& terrain_palette::selected_bg_item() const { return bg_terrain; }
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("editor_group"))
81  {
82  if (group_names.find(group["id"]) == group_names.end()) {
83 
84  config cfg;
85  cfg["id"] = group["id"];
86  cfg["name"] = group["name"];
87 
88  cfg["icon"] = "icons/terrain/terrain_" + group["icon"].str();
89  cfg["core"] = group["core"];
90  groups_.push_back(item_group(cfg));
91 
92  group_names.insert(groups_.back().id);
93  }
94  }
95 
96  std::map<std::string, item_group*> id_to_group;
97  for (item_group& group : groups_) {
98  id_to_group.insert(std::make_pair(group.id, &group));
99  }
100 
101  // add the groups for all terrains to the map
102  for (const t_translation::t_terrain& t : items) {
103 
104  const terrain_type& t_info = map().get_terrain_info(t);
105  DBG_ED << "Palette: processing terrain " << t_info.name()
106  << "(editor name: '" << t_info.editor_name() << "') "
107  << "(" << t_info.number() << ")"
108  << ": " << t_info.editor_group() << "\n";
109 
110  // don't display terrains that were automatically created from base+overlay
111  if (t_info.is_combined()) continue;
112  // nor display terrains that have hide_in_editor=true
113  if (t_info.hide_in_editor()) continue;
114 
115  // add the terrain to the requested groups
116  const std::vector<std::string>& keys = utils::split(t_info.editor_group());
117  bool core = false;
118 
119  item_map_[get_id(t)] = t;
120 
121  for (const std::string& k : keys) {
122  group_map_[k].push_back(get_id(t));
123  nmax_items_ = std::max<int>(nmax_items_, group_map_[k].size());
124  std::map<std::string, item_group*>::iterator i = id_to_group.find(k);
125  if (i != id_to_group.end()) {
126  if (i->second->core) {
127  core = true;
128  }
129  }
130  }
131 
132  // A terrain is considered core iff it appears in at least
133  // one core terrain group
134  if (core) {
135  // Add the terrain to the default group
136  group_map_["all"].push_back(get_id(t));
137  nmax_items_ = std::max<int>(nmax_items_, group_map_["all"].size());
138  } else {
139  non_core_items_.insert(get_id(t));
140  }
141 
142  }
143 
144  // Set the default terrain
145  select_fg_item("regular_mountains");
146  select_bg_item("grassland");
147 
148  // Set the default group
149  set_group("all");
150 
151  if(active_group().empty()) {
152  ERR_ED << "No items found." << std::endl;
153  }
154 }
155 
157  surface& image, std::stringstream& tooltip_text) {
158 
159  const t_translation::t_terrain base_terrain =
160  map().get_terrain_info(terrain).default_base();
161 
162  //Draw default base for overlay terrains
163  if(base_terrain != t_translation::NONE_TERRAIN) {
164  const std::string base_filename = map().get_terrain_info(base_terrain).editor_image();
165  surface base_image(image::get_image(base_filename));
166 
167  if(base_image == nullptr) {
168  tooltip_text << "BASE IMAGE NOT FOUND\n";
169  ERR_ED << "image for terrain : '" << base_filename << "' not found" << std::endl;
171  if (base_image == nullptr) {
172  ERR_ED << "Placeholder image not found" << std::endl;
173  return;
174  }
175  }
176 
177  if(base_image->w != item_size_ || base_image->h != item_size_) {
178  base_image.assign(scale_surface(base_image,
180  }
181  }
182 
183  const std::string filename = map().get_terrain_info(terrain).editor_image();
184  image = image::get_image(filename);
185  if(image == nullptr) {
186  tooltip_text << "IMAGE NOT FOUND\n";
187  ERR_ED << "image for terrain: '" << filename << "' not found" << std::endl;
189  if (image == nullptr) {
190  ERR_ED << "Placeholder image not found" << std::endl;
191  return;
192  }
193  }
194 
195  if(image->w != item_size_ || image->h != item_size_) {
196  image.assign(scale_surface(image,
198  }
199 
200  tooltip_text << map().get_terrain_editor_string(terrain);
202  tooltip_text << " " + utils::unicode_em_dash + " " << terrain;
203  }
204 }
205 
207  mouse_action** active_mouse_action)
208 //TODO avoid magic numbers
209  : editor_palette<t_translation::t_terrain>(gui, cfg, 36, 4, active_mouse_action)
210 {
211 }
212 
214 {
215  const terrain_type& t_info = map().get_terrain_info(terrain);
216  return t_info.id();
217 }
218 
220 {
221  std::ostringstream msg;
222  msg << _("FG: ") << map().get_terrain_editor_string(selected_fg_item()) << " | "
223  << _("BG: ") << map().get_terrain_editor_string(selected_bg_item());
224  return msg.str();
225 }
226 
227 
228 }
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
child_itors child_range(const std::string &key)
Definition: config.cpp:613
virtual void select_bg_item(const std::string &item_id)
const t_translation::t_terrain & selected_bg_item() const
terrain_palette(editor_display &gui, const config &cfg, mouse_action **active_mouse_action)
virtual void select_fg_item(const std::string &item_id)
Select a foreground item.
bool get_draw_terrain_codes() const
Getter for the terrain code debug overlay on tiles.
Definition: display.hpp:369
Stores the info about the groups in a nice format.
std::string get_terrain_editor_string(const map_location &loc) const
Definition: map.cpp:65
const GLfloat * c
Definition: glew.h:12741
const t_translation::t_list & get_terrain_list() const
Gets the list of terrains.
Definition: map.cpp:46
const t_terrain NONE_TERRAIN
Definition: translation.hpp:56
General purpose widgets.
bool hide_in_editor() const
Definition: terrain.hpp:40
const t_string & name() const
Definition: terrain.hpp:33
const std::string & id() const
Definition: terrain.hpp:37
const std::vector< std::string > & active_group()
const t_terrain FOGGED
bool is_combined() const
Definition: terrain.hpp:77
const std::vector< std::string > items
surface scale_surface(const surface &surf, int w, int h)
Definition: utils.cpp:443
virtual void setup(const config &cfg)
Setup the internal data structure.
const std::string & editor_group() const
Definition: terrain.hpp:73
GLdouble GLdouble t
Definition: glew.h:1366
const t_string & editor_name() const
Definition: terrain.hpp:34
const gamemap & map() const
const t_translation::t_terrain & selected_fg_item() const
virtual std::string get_help_string()
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
std::map< std::string, std::vector< std::string > > group_map_
const std::string & editor_image() const
Definition: terrain.hpp:32
t_translation::t_terrain number() const
Definition: terrain.hpp:43
const std::string unicode_em_dash
t_translation::t_terrain default_base() const
Definition: terrain.hpp:79
Manage the empty-palette in the editor.
Definition: action.cpp:28
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
const terrain_type & get_terrain_info(const t_translation::t_terrain &terrain) const
Definition: map.cpp:100
void select_fg_item(const t_translation::t_terrain &terrain)
const t_translation::t_terrain & get_selected_bg_terrain()
void select_bg_item(const t_translation::t_terrain &terrain)
size_t i
Definition: function.cpp:1057
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
virtual void draw_item(const t_translation::t_terrain &terrain, surface &item_image, std::stringstream &tooltip_text)
const t_terrain VOID_TERRAIN
GLboolean GLuint group
Definition: glew.h:2589
static bool is_valid_terrain(const t_translation::t_terrain &c)
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:75
void assign(const surface &o)
Definition: utils.hpp:83
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
A mouse action receives events from the controller, and responds to them by creating an appropriate e...
#define DBG_ED
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual const std::string & get_id(const t_translation::t_terrain &terrain)
std::vector< t_terrain > t_list
Definition: translation.hpp:75
const t_translation::t_terrain & get_selected_fg_terrain()