The Battle for Wesnoth  1.19.0-dev
unit_palette.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
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.";
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";
87  if(!base_image) {
88  ERR_ED << "Placeholder image not found";
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)
100  , selected_bg_items_()
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 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
int viewing_side() const
The 1-based equivalent of the 0-based viewing_team() function.
Definition: display.hpp:124
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
const std::vector< std::string > & active_group()
std::map< std::string, std::vector< std::string > > group_map_
virtual void select_fg_item(const std::string &item_id) override
Select a foreground item.
void set_group(std::size_t index) override
const const unit_type & & selected_fg_item() const
Return the currently selected foreground/background item.
virtual std::string get_help_string()
unit_palette(editor_display &gui, const game_config_view &cfg, editor_toolkit &toolkit)
virtual void setup(const game_config_view &cfg)
Setup the internal data structure.
virtual void setup_item(const unit_type &item, texture &item_base_image, texture &item_overlay_image, std::stringstream &tooltip)
virtual void select_bg_item(const std::string &item_id)
virtual bool is_selected_bg_item(const std::string &id)
virtual const std::string & get_id(const unit_type &terrain)
std::set< std::string > selected_bg_items_
A class grating read only view to a vector of config objects, viewed as one config with all children ...
void set_dirty(bool dirty=true)
Definition: widget.cpp:180
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:971
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
const race_map & races() const
Definition: types.hpp:394
const unit_type_map & types() const
Definition: types.hpp:393
A single unit type that the player may recruit.
Definition: types.hpp:43
const std::string & image() const
Definition: types.hpp:176
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:141
const std::string & flag_rgb() const
Definition: types.cpp:719
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:138
#define ERR_ED
std::size_t i
Definition: function.cpp:968
Manage the empty-palette in the editor.
Definition: action.cpp:31
General purpose widgets.
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:959
unit_type_data unit_types
Definition: types.cpp:1485