The Battle for Wesnoth  1.19.0-dev
item_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 item-palette in the editor.
18  */
19 
20 #define GETTEXT_DOMAIN "wesnoth-editor"
21 
23 #include "gettext.hpp"
24 #include "game_config_view.hpp"
25 
26 #include <string>
27 
28 namespace editor {
29 
31 {
32  return selected_fg_item().name;
33 }
34 
36 {
37  for(const config& group : cfg.child_range("item_group")) {
38  groups_.emplace_back(group);
39 
40  for(const config& item : group.child_range("item")) {
41  item_map_.emplace(item["id"], overlay(item));
42  group_map_[group["id"]].push_back(item["id"]);
43  if(!group["core"].to_bool(false))
44  non_core_items_.insert(item["id"]);
45  }
46  }
47 
48  select_fg_item("anvil");
49  select_bg_item("altar");
50 
51  // Set the default group
52  set_group("items");
53 
54  if(active_group().empty()) {
55  ERR_ED << "No items found.";
56  }
57 }
58 
60  const overlay& item,
61  texture& base_image,
62  texture& /*overlay_image*/,
63  std::stringstream& tooltip_text)
64 {
65  std::stringstream filename;
66  filename << item.image;
67  if(item.image.empty()) {
68  filename << item.halo;
69  }
70 
71  base_image = image::get_texture(filename.str());
72  if(!base_image) {
73  tooltip_text << "IMAGE NOT FOUND\n";
74  ERR_ED << "image for item type: '" << filename.str() << "' not found";
76  if(!base_image) {
77  ERR_ED << "Placeholder image not found";
78  return;
79  }
80  }
81 
82  tooltip_text << item.name;
83 }
84 
86  editor_toolkit &toolkit)
87 //TODO avoid magic numbers
88  : editor_palette<overlay>(gui, cfg, 36, 4, toolkit)
89 {
90 }
91 
92 const std::string& item_palette::get_id(const overlay& item)
93 {
94  return item.id;
95 }
96 
97 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::set< std::string > non_core_items_
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
virtual void select_bg_item(const std::string &item_id) override
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 overlay & selected_fg_item() const
Return the currently selected foreground/background item.
virtual std::string get_help_string()
virtual const std::string & get_id(const overlay &item)
item_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 overlay &item, texture &item_base_image, texture &item_overlay_image, std::stringstream &tooltip)
Setup item image and tooltip.
A class grating read only view to a vector of config objects, viewed as one config with all children ...
config_array_view child_range(config_key_type key) const
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
#define ERR_ED
Manage the empty-palette in the editor.
Definition: action.cpp:31
General purpose widgets.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
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
t_string name
Definition: overlay.hpp:58