The Battle for Wesnoth  1.17.0-dev
unit_palette.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2021
3  by Fabian Mueller <fabianmueller5@gmx.de>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * Manage the unit-palette in the editor.
18  */
19 
20 #define GETTEXT_DOMAIN "wesnoth-editor"
21 
23 
24 #include "gettext.hpp"
25 
26 #include "team.hpp"
27 #include "units/types.hpp"
28 
29 namespace editor {
30 
32  return selected_fg_item().type_name();
33 }
34 
36 {
37  for(const unit_type_data::unit_type_map::value_type &i : unit_types.types()) {
38  if(i.second.do_not_list())
39  continue;
40  item_map_.emplace(i.second.id(), i.second);
41  group_map_[i.second.race_id()].push_back(i.second.id());
42  nmax_items_ = std::max<int>(nmax_items_, group_map_[i.second.race_id()].size());
43  // Add the unit to the default group
44  group_map_["all"].push_back(i.second.id());
45  nmax_items_ = std::max<int>(nmax_items_, group_map_["all"].size());
46  }
47 
48  for(const race_map::value_type &i : unit_types.races()) {
49  if(group_map_[i.second.id()].empty())
50  continue;
51  config cfg;
52  cfg["id"] = i.second.id();
53  cfg["name"] = i.second.plural_name();
54  cfg["icon"] = i.second.get_icon_path_stem();
55  cfg["core"] = true;
56  groups_.emplace_back(cfg);
57  }
58 
59  //TODO
60  //move "invalid" items to the end
61  //std::stable_partition(items.begin(), items.end(), is_valid_terrain);
62 
63  select_fg_item(item_map_.begin()->second.id());
64  select_bg_item(item_map_.begin()->second.id());
65 
66  // Set the default group
67  set_group(groups_[0].id);
68 
69  if(active_group().empty()) {
70  ERR_ED << "No items found." << std::endl;
71  }
72 }
73 
74 void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstream& tooltip_text) {
75 
76  std::stringstream filename;
77  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
79 
80  image = image::get_image(filename.str());
81  if(image == nullptr) {
82  tooltip_text << "IMAGE NOT FOUND\n";
83  ERR_ED << "image for unit type: '" << filename.str() << "' not found" << std::endl;
85  if(image == nullptr) {
86  ERR_ED << "Placeholder image not found" << std::endl;
87  return;
88  }
89  }
90 
91  if(image->w != item_size_ || image->h != item_size_) {
92  image = scale_surface(image, item_size_, item_size_);
93  }
94 
95  tooltip_text << u.type_name();
96 }
97 
99  editor_toolkit &toolkit)
100 //TODO avoid magic numbers
101  : editor_palette<const unit_type&>(gui, cfg, 36, 4, toolkit)
103 {
104 }
105 
106 const std::string& unit_palette::get_id(const unit_type& u)
107 {
108  return u.id();
109 }
110 
111 bool unit_palette::is_selected_bg_item(const std::string& id)
112 {
113  return selected_bg_items_.count(id) != 0;
114 }
115 
116 void unit_palette::select_bg_item(const std::string& item_id) {
117 
118  if(selected_bg_items_.count(item_id) != 0) {
119  selected_bg_items_.erase(item_id);
120  } else {
121  selected_bg_items_.insert(item_id);
122  }
123 
124  set_dirty();
125 }
126 
127 }
surface get_image(const image::locator &i_locator, TYPE type)
Caches and returns an image.
Definition: picture.cpp:816
virtual void select_fg_item(const std::string &item_id) override
Select a foreground item.
unit_palette(editor_display &gui, const game_config_view &cfg, editor_toolkit &toolkit)
std::set< std::string > selected_bg_items_
const std::string & flag_rgb() const
Definition: types.cpp:723
General purpose widgets.
void set_group(std::size_t index) override
virtual bool is_selected_bg_item(const std::string &id)
int viewing_side() const
Definition: display.hpp:107
unit_type_data unit_types
Definition: types.cpp:1481
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:197
const race_map & races() const
Definition: types.hpp:399
virtual std::string get_help_string()
const unit_type_map & types() const
Definition: types.hpp:398
A single unit type that the player may recruit.
Definition: types.hpp:45
void set_dirty(bool dirty=true)
Definition: widget.cpp:207
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:141
std::map< std::string, std::vector< std::string > > group_map_
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:144
Manage the empty-palette in the editor.
Definition: action.cpp:30
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:973
std::size_t i
Definition: function.cpp:967
virtual const std::string & get_id(const unit_type &terrain)
const std::string & image() const
Definition: types.hpp:179
const const unit_type & & selected_fg_item() const
Return the currently selected foreground/background item.
#define ERR_ED
Functions to load and save images from/to disk.
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:61
virtual void setup(const game_config_view &cfg)
Setup the internal data structure.
virtual void draw_item(const unit_type &terrain, surface &image, std::stringstream &tooltip_text)