The Battle for Wesnoth  1.13.10+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 - 2017 by Fabian Mueller <fabianmueller5@gmx.de>
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 
22 
23 #include "gettext.hpp"
24 
25 #include "team.hpp"
26 #include "units/types.hpp"
27 
28 namespace editor {
29 
31  return selected_fg_item().type_name();
32 }
33 
34 void unit_palette::setup(const config& /*cfg*/)
35 {
36  for(const unit_type_data::unit_type_map::value_type &i : unit_types.types()) {
37  if(i.second.do_not_list())
38  continue;
39  item_map_.emplace(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: Implement the concept of "core" units
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  if(group_map_[i.second.id()].empty())
55  continue;
56  config cfg;
57  cfg["id"] = i.second.id();
58  cfg["name"] = i.second.plural_name();
59  if(i.second.editor_icon().empty()) {
60  cfg["icon"] = "icons/unit-groups/race_" + i.second.id();
61  } else {
62  cfg["icon"] = i.second.editor_icon();
63  }
64  cfg["core"] = true;
65  groups_.emplace_back(cfg);
66  }
67 
68  //TODO
69  //move "invalid" items to the end
70  //std::stable_partition(items.begin(), items.end(), is_valid_terrain);
71 
72  select_fg_item(item_map_.begin()->second.id());
73  select_bg_item(item_map_.begin()->second.id());
74 
75  // Set the default group
76  set_group(groups_[0].id);
77 
78  if(active_group().empty()) {
79  ERR_ED << "No items found." << std::endl;
80  }
81 }
82 
83 void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstream& tooltip_text) {
84 
85  std::stringstream filename;
86  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
88 
89  image = image::get_image(filename.str());
90  if(image == nullptr) {
91  tooltip_text << "IMAGE NOT FOUND\n";
92  ERR_ED << "image for unit type: '" << filename.str() << "' not found" << std::endl;
94  if(image == nullptr) {
95  ERR_ED << "Placeholder image not found" << std::endl;
96  return;
97  }
98  }
99 
100  if(image->w != item_size_ || image->h != item_size_) {
101  image.assign(scale_surface(image, item_size_, item_size_));
102  }
103 
104  tooltip_text << u.type_name();
105 }
106 
108  editor_toolkit &toolkit)
109 //TODO avoid magic numbers
110  : editor_palette<unit_type>(gui, cfg, 36, 4, toolkit),
111  selected_bg_items_()
112 {
113 }
114 
116 {
117  return u.id();
118 }
119 
121 {
122  return selected_bg_items_.count(id) != 0;
123 }
124 
126 
127  if(selected_bg_items_.count(item_id) != 0) {
128  selected_bg_items_.erase(item_id);
129  } else {
130  selected_bg_items_.insert(item_id);
131  }
132 
133  set_dirty();
134 }
135 
136 }
137 
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:926
const unit_type & selected_fg_item() const
Return the currently selected foreground/background item.
std::vector< char_t > string
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:137
virtual void select_fg_item(const std::string &item_id) override
Select a foreground item.
std::set< std::string > selected_bg_items_
unit_palette(editor_display &gui, const config &cfg, editor_toolkit &toolkit)
General purpose widgets.
virtual bool is_selected_bg_item(const std::string &id)
unit_type_data unit_types
Definition: types.cpp:1455
const std::string & image() const
Definition: types.hpp:163
const std::vector< std::string > & active_group()
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:262
virtual std::string get_help_string()
-file sdl_utils.hpp
const std::string & flag_rgb() const
Definition: types.cpp:742
A single unit type that the player may recruit.
Definition: types.hpp:43
void set_group(size_t index) override
void set_dirty(bool dirty=true)
Definition: widget.cpp:215
const unit_type_map & types() const
Definition: types.hpp:342
std::map< std::string, std::vector< std::string > > group_map_
Manage the empty-palette in the editor.
Definition: action.cpp:29
int viewing_side() const
Definition: display.hpp:103
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:343
#define i
std::set< std::string > non_core_items_
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:109
static std::string get_side_color_index(int side)
Definition: team.cpp:888
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
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:93
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:140
virtual void draw_item(const unit_type &terrain, surface &image, std::stringstream &tooltip_text)