The Battle for Wesnoth  1.17.0-dev
item_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 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  nmax_items_ = std::max<int>(nmax_items_, group_map_[group["id"]].size());
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." << std::endl;
57  }
58 }
59 
60 void item_palette::draw_item(const overlay& item, surface& image, std::stringstream& tooltip_text)
61 {
62  std::stringstream filename;
63  filename << item.image;
64  if(item.image.empty()) {
65  filename << item.halo;
66  }
67 
68  image = image::get_image(filename.str());
69  if(image == nullptr) {
70  tooltip_text << "IMAGE NOT FOUND\n";
71  ERR_ED << "image for item type: '" << filename.str() << "' not found" << std::endl;
73  if(image == nullptr) {
74  ERR_ED << "Placeholder image not found" << std::endl;
75  return;
76  }
77  }
78 
79  if(image->w != item_size_ || image->h != item_size_) {
80  image = scale_surface(image, item_size_, item_size_);
81  }
82 
83  tooltip_text << item.name;
84 }
85 
87  editor_toolkit &toolkit)
88 //TODO avoid magic numbers
89  : editor_palette<overlay>(gui, cfg, 36, 4, toolkit)
90 {
91 }
92 
93 const std::string& item_palette::get_id(const overlay& item)
94 {
95  return item.id;
96 }
97 
98 }
virtual void draw_item(const overlay &item, surface &image, std::stringstream &tooltip_text)
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.
virtual void select_bg_item(const std::string &item_id) override
virtual const std::string & get_id(const overlay &item)
config_array_view child_range(config_key_type key) const
General purpose widgets.
void set_group(std::size_t index) override
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
virtual void setup(const game_config_view &cfg)
Setup the internal data structure.
std::string id
Definition: overlay.hpp:56
item_palette(editor_display &gui, const game_config_view &cfg, editor_toolkit &toolkit)
std::map< std::string, std::vector< std::string > > group_map_
t_string name
Definition: overlay.hpp:55
Manage the empty-palette in the editor.
Definition: action.cpp:30
std::set< std::string > non_core_items_
std::string halo
Definition: overlay.hpp:53
const overlay & selected_fg_item() const
Return the currently selected foreground/background item.
virtual std::string get_help_string()
#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.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
std::string image
Definition: overlay.hpp:52