The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unit_palette.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Fabian Mueller <[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  * Manage the unit-palette in the editor.
17  */
18 
19 #define GETTEXT_DOMAIN "wesnoth-editor"
20 
21 #include "unit_palette.hpp"
22 
23 #include "gettext.hpp"
24 
25 #include "units/types.hpp"
26 
27 namespace editor {
28 
30  return selected_fg_item().type_name();
31 }
32 
33 void unit_palette::setup(const config& /*cfg*/)
34 {
35  for (const unit_type_data::unit_type_map::value_type &i : unit_types.types())
36  {
37  if (i.second.do_not_list())
38  continue;
39  item_map_.insert(std::pair<std::string, unit_type>(i.second.id(), i.second));
40  group_map_[i.second.race_id()].push_back(i.second.id());
41  nmax_items_ = std::max<int>(nmax_items_, group_map_[i.second.race_id()].size());
42  //TODO
43  bool core = true;
44  if (core) {
45  // Add the unit to the default group
46  group_map_["all"].push_back(i.second.id());
47  nmax_items_ = std::max<int>(nmax_items_, group_map_["all"].size());
48  } else {
49  non_core_items_.insert(i.second.id());
50  }
51  }
52 
53  for (const race_map::value_type &i : unit_types.races())
54  {
55  if (group_map_[i.second.id()].empty())
56  continue;
57  config cfg;
58  cfg["id"] = i.second.id();
59  cfg["name"] = i.second.plural_name();
60  cfg["icon"] = "icons/unit-groups/race_" + i.second.id();
61  cfg["core"] = "yes";
62  groups_.push_back(item_group(cfg));
63  }
64 
65  //TODO
66  //move "invalid" items to the end
67  //std::stable_partition(items.begin(), items.end(), is_valid_terrain);
68 
69  select_fg_item(item_map_.begin()->second.id());
70  select_bg_item(item_map_.begin()->second.id());
71 
72  // Set the default group
73  set_group(groups_[0].id);
74 
75  if(active_group().empty()) {
76  ERR_ED << "No items found." << std::endl;
77  }
78 }
79 
80 void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstream& tooltip_text) {
81 
82  std::stringstream filename;
83  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
85 
86  image = image::get_image(filename.str());
87  if(image == nullptr) {
88  tooltip_text << "IMAGE NOT FOUND\n";
89  ERR_ED << "image for unit type: '" << filename.str() << "' not found" << std::endl;
91  if (image == nullptr) {
92  ERR_ED << "Placeholder image not found" << std::endl;
93  return;
94  }
95  }
96 
97  if(image->w != item_size_ || image->h != item_size_) {
98  image.assign(scale_surface(image,
100  }
101 
102  tooltip_text << u.type_name();
103 }
104 
106  mouse_action** active_mouse_action)
107 //TODO avoid magic numbers
108  : editor_palette<unit_type>(gui, cfg, 36, 4, active_mouse_action),
109  selected_bg_items_()
110 {
111 }
112 
114 {
115  return u.id();
116 }
117 
119 {
120  return selected_bg_items_.count(id) != 0;
121 }
122 
124 
125  if (selected_bg_items_.count(item_id) != 0)
126  selected_bg_items_.erase(item_id);
127  else selected_bg_items_.insert(item_id);
128 
129  set_dirty();
130 }
131 
132 }
133 
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
const unit_type & selected_fg_item() const
Return the currently selected foreground/background item.
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:112
virtual void select_fg_item(const std::string &item_id)
Select a foreground item.
Stores the info about the groups in a nice format.
std::set< std::string > selected_bg_items_
General purpose widgets.
virtual bool is_selected_bg_item(const std::string &id)
unit_type_data unit_types
Definition: types.cpp:1314
const std::string & image() const
Definition: types.hpp:138
const std::vector< std::string > & active_group()
surface scale_surface(const surface &surf, int w, int h)
Definition: utils.cpp:443
virtual std::string get_help_string()
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
const std::string & flag_rgb() const
Definition: types.hpp:147
const unit_type_map & types() const
Definition: types.hpp:313
std::map< std::string, std::vector< std::string > > group_map_
Manage the empty-palette in the editor.
Definition: action.cpp:28
int viewing_side() const
Definition: display.hpp:103
unit_palette(editor_display &gui, const config &cfg, mouse_action **active_mouse_action)
virtual const std::string & get_id(const unit_type &terrain)
virtual void setup(const config &cfg)
Setup the internal data structure.
const race_map & races() const
Definition: types.hpp:314
size_t i
Definition: function.cpp:1057
std::set< std::string > non_core_items_
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:75
static std::string get_side_color_index(int side)
Definition: team.cpp:840
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...
virtual void select_bg_item(const std::string &item_id)
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
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:115
virtual void draw_item(const unit_type &terrain, surface &image, std::stringstream &tooltip_text)