The Battle for Wesnoth  1.13.11+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 - 2018 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  // Add the unit to the default group
43  group_map_["all"].push_back(i.second.id());
44  nmax_items_ = std::max<int>(nmax_items_, group_map_["all"].size());
45  }
46 
47  for(const race_map::value_type &i : unit_types.races()) {
48  if(group_map_[i.second.id()].empty())
49  continue;
50  config cfg;
51  cfg["id"] = i.second.id();
52  cfg["name"] = i.second.plural_name();
53  cfg["icon"] = i.second.get_icon_path_stem();
54  cfg["core"] = true;
55  groups_.emplace_back(cfg);
56  }
57 
58  //TODO
59  //move "invalid" items to the end
60  //std::stable_partition(items.begin(), items.end(), is_valid_terrain);
61 
62  select_fg_item(item_map_.begin()->second.id());
63  select_bg_item(item_map_.begin()->second.id());
64 
65  // Set the default group
66  set_group(groups_[0].id);
67 
68  if(active_group().empty()) {
69  ERR_ED << "No items found." << std::endl;
70  }
71 }
72 
73 void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstream& tooltip_text) {
74 
75  std::stringstream filename;
76  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
78 
79  image = image::get_image(filename.str());
80  if(image == nullptr) {
81  tooltip_text << "IMAGE NOT FOUND\n";
82  ERR_ED << "image for unit type: '" << filename.str() << "' not found" << std::endl;
84  if(image == nullptr) {
85  ERR_ED << "Placeholder image not found" << std::endl;
86  return;
87  }
88  }
89 
90  if(image->w != item_size_ || image->h != item_size_) {
91  image.assign(scale_surface(image, item_size_, item_size_));
92  }
93 
94  tooltip_text << u.type_name();
95 }
96 
98  editor_toolkit &toolkit)
99 //TODO avoid magic numbers
100  : editor_palette<unit_type>(gui, cfg, 36, 4, toolkit),
101  selected_bg_items_()
102 {
103 }
104 
106 {
107  return u.id();
108 }
109 
111 {
112  return selected_bg_items_.count(id) != 0;
113 }
114 
116 
117  if(selected_bg_items_.count(item_id) != 0) {
118  selected_bg_items_.erase(item_id);
119  } else {
120  selected_bg_items_.insert(item_id);
121  }
122 
123  set_dirty();
124 }
125 
126 }
127 
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:920
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:136
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:1453
const std::string & image() const
Definition: types.hpp:162
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:272
virtual std::string get_help_string()
-file sdl_utils.hpp
const std::string & flag_rgb() const
Definition: types.cpp:745
A single unit type that the player may recruit.
Definition: types.hpp:42
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:359
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:102
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:941
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:360
size_t i
Definition: function.cpp:933
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:103
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:139
virtual void draw_item(const unit_type &terrain, surface &image, std::stringstream &tooltip_text)