The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
item_palette.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2017 by Fabian Mueller <fabianmueller5@gmx.de>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * Manage the item-palette in the editor.
17  */
18 
19 #define GETTEXT_DOMAIN "wesnoth-editor"
20 
22 #include "gettext.hpp"
23 
24 #include <string>
25 
26 namespace editor {
27 
29 {
30  return selected_fg_item().name;
31 }
32 
33 void item_palette::setup(const config& cfg)
34 {
35  for(const config& group : cfg.child_range("item_group")) {
36  groups_.emplace_back(group);
37 
38  for(const config& item : group.child_range("item")) {
39  item_map_.emplace(item["id"], overlay(item));
40  group_map_[group["id"]].push_back(item["id"]);
41  if(!group["core"].to_bool(false))
42  non_core_items_.insert(item["id"]);
43  }
44  nmax_items_ = std::max<int>(nmax_items_, group_map_[group["id"]].size());
45  }
46 
47  select_fg_item("anvil");
48  select_bg_item("altar");
49 
50  // Set the default group
51  set_group("items");
52 
53  if(active_group().empty()) {
54  ERR_ED << "No items found." << std::endl;
55  }
56 }
57 
58 void item_palette::draw_item(const overlay& item, surface& image, std::stringstream& tooltip_text)
59 {
60  std::stringstream filename;
61  filename << item.image;
62  if(item.image.empty()) {
63  filename << item.halo;
64  }
65 
66  image = image::get_image(filename.str());
67  if(image == nullptr) {
68  tooltip_text << "IMAGE NOT FOUND\n";
69  ERR_ED << "image for item type: '" << filename.str() << "' not found" << std::endl;
71  if(image == nullptr) {
72  ERR_ED << "Placeholder image not found" << std::endl;
73  return;
74  }
75  }
76 
77  if(image->w != item_size_ || image->h != item_size_) {
78  image.assign(scale_surface(image, item_size_, item_size_));
79  }
80 
81  tooltip_text << item.name;
82 }
83 
85  editor_toolkit &toolkit)
86 //TODO avoid magic numbers
87  : editor_palette<overlay>(gui, cfg, 36, 4, toolkit)
88 {
89 }
90 
92 {
93  return item.id;
94 }
95 
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)
function to get the surface corresponding to an image.
Definition: image.cpp:926
const overlay & selected_fg_item() const
Return the currently selected foreground/background item.
std::vector< char_t > string
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)
virtual void setup(const config &cfg)
Setup the internal data structure.
child_itors child_range(config_key_type key)
Definition: config.cpp:343
General purpose widgets.
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:274
void set_group(size_t index) override
std::string id
Definition: overlay.hpp:39
std::map< std::string, std::vector< std::string > > group_map_
t_string name
Definition: overlay.hpp:38
Manage the empty-palette in the editor.
Definition: action.cpp:29
std::set< std::string > non_core_items_
std::string halo
Definition: overlay.hpp:36
void assign(SDL_Surface *surf)
Definition: surface.hpp:46
virtual std::string get_help_string()
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:109
item_palette(editor_display &gui, const config &cfg, editor_toolkit &toolkit)
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:93
std::string image
Definition: overlay.hpp:35