The Battle for Wesnoth  1.17.5+dev
unit_palette.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2022
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  // Add the unit to the default group
43  group_map_["all"].push_back(i.second.id());
44  }
45 
46  for(const race_map::value_type &i : unit_types.races()) {
47  if(group_map_[i.second.id()].empty())
48  continue;
49  config cfg;
50  cfg["id"] = i.second.id();
51  cfg["name"] = i.second.plural_name();
52  cfg["icon"] = i.second.get_icon_path_stem();
53  cfg["core"] = true;
54  groups_.emplace_back(cfg);
55  }
56 
57  //TODO
58  //move "invalid" items to the end
59  //std::stable_partition(items.begin(), items.end(), is_valid_terrain);
60 
61  select_fg_item(item_map_.begin()->second.id());
62  select_bg_item(item_map_.begin()->second.id());
63 
64  // Set the default group
65  set_group(groups_[0].id);
66 
67  if(active_group().empty()) {
68  ERR_ED << "No items found." << std::endl;
69  }
70 }
71 
73  const unit_type& u,
74  texture& base_image,
75  texture& /*overlay_image*/,
76  std::stringstream& tooltip_text)
77 {
78  std::stringstream filename;
79  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
81 
82  base_image = image::get_texture(filename.str());
83  if(!base_image) {
84  tooltip_text << "IMAGE NOT FOUND\n";
85  ERR_ED << "image for unit type: '" << filename.str() << "' not found" << std::endl;
87  if(!base_image) {
88  ERR_ED << "Placeholder image not found" << std::endl;
89  return;
90  }
91  }
92 
93  tooltip_text << u.type_name();
94 }
95 
97  editor_toolkit &toolkit)
98 //TODO avoid magic numbers
99  : editor_palette<const unit_type&>(gui, cfg, 36, 4, toolkit)
101 {
102 }
103 
104 const std::string& unit_palette::get_id(const unit_type& u)
105 {
106  return u.id();
107 }
108 
109 bool unit_palette::is_selected_bg_item(const std::string& id)
110 {
111  return selected_bg_items_.count(id) != 0;
112 }
113 
114 void unit_palette::select_bg_item(const std::string& item_id) {
115 
116  if(selected_bg_items_.count(item_id) != 0) {
117  selected_bg_items_.erase(item_id);
118  } else {
119  selected_bg_items_.insert(item_id);
120  }
121 
122  set_dirty();
123 }
124 
125 }
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:721
General purpose widgets.
void set_group(std::size_t index) override
virtual void setup_item(const unit_type &item, texture &item_base_image, texture &item_overlay_image, std::stringstream &tooltip)
virtual bool is_selected_bg_item(const std::string &id)
int viewing_side() const
Definition: display.hpp:112
unit_type_data unit_types
Definition: types.cpp:1466
const std::vector< std::string > & active_group()
const race_map & races() const
Definition: types.hpp:397
virtual std::string get_help_string()
const unit_type_map & types() const
Definition: types.hpp:396
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:30
A single unit type that the player may recruit.
Definition: types.hpp:45
void set_dirty(bool dirty=true)
Definition: widget.cpp:208
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:972
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
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:60
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:1105
virtual void setup(const game_config_view &cfg)
Setup the internal data structure.