The Battle for Wesnoth  1.19.7+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 "formula/string_utils.hpp"
24 #include "gettext.hpp"
25 #include "game_config_view.hpp"
26 
27 #include <string>
28 
29 namespace editor {
30 
31 std::string item_palette::get_help_string() const
32 {
33  return VGETTEXT("Left-click: Place item $item | Right-click to remove", {{ "item", selected_fg_item().name }});
34 }
35 
37 {
38  for(const config& group : cfg.child_range("item_group")) {
39  groups_.emplace_back(group);
40 
41  for(const config& item : group.child_range("item")) {
42  item_map_.emplace(item["id"], overlay(item));
43  group_map_[group["id"]].push_back(item["id"]);
44  if(!group["core"].to_bool(false))
45  non_core_items_.insert(item["id"]);
46  }
47  }
48 
49  select_fg_item("anvil");
50  select_bg_item("altar");
51 
52  // Set the default group
53  set_group("items");
54 
55  if(active_group().empty()) {
56  ERR_ED << "No items found.";
57  }
58 }
59 
61  const overlay& item,
62  texture& base_image,
63  texture& /*overlay_image*/,
64  std::stringstream& tooltip_text)
65 {
66  std::stringstream filename;
67  filename << item.image;
68  if(item.image.empty()) {
69  filename << item.halo;
70  }
71 
72  base_image = image::get_texture(filename.str());
73  if(!base_image) {
74  tooltip_text << "IMAGE NOT FOUND\n";
75  ERR_ED << "image for item type: '" << filename.str() << "' not found";
77  if(!base_image) {
78  ERR_ED << "Placeholder image not found";
79  return;
80  }
81  }
82 
83  tooltip_text << item.name;
84 }
85 
87 //TODO avoid magic numbers
88  : editor_palette<overlay>(gui, 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:172
child_itors child_range(config_key_type key)
Definition: config.cpp:272
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() const override
virtual void setup(const game_config_view &cfg) override
Setup the internal data structure.
virtual const std::string & get_id(const overlay &item) override
virtual void setup_item(const overlay &item, texture &item_base_image, texture &item_overlay_image, std::stringstream &tooltip) override
Setup item image and tooltip.
item_palette(editor_display &gui, editor_toolkit &toolkit)
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
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
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:920
std::string filename
Filename.
std::string image
Definition: overlay.hpp:55
t_string name
Definition: overlay.hpp:58
std::string id
Definition: overlay.hpp:59
std::string halo
Definition: overlay.hpp:56