The Battle for Wesnoth  1.17.8+dev
editor_palettes.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
3  by David White <dave@whitevine.net>
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 #pragma once
17 
21 
22 namespace editor {
23 
24 class editor_toolkit;
25 
26 template<class Item>
28 
29 public:
30 
32  , std::size_t item_size, std::size_t columns, editor_toolkit &toolkit)
34  , groups_()
35  , gui_(gui)
36  , item_size_(item_size)
37 //TODO avoid magic number
38  , item_space_(item_size + 3)
39  , columns_(columns)
40  , group_map_()
41  , item_map_()
42  , items_start_(0)
43  , non_core_items_()
44  , active_group_()
47  , toolkit_(toolkit)
48  , buttons_()
49  {
50  }
51 
52 
53 
54  virtual sdl_handler_vector handler_members() override;
55 
56  void set_start_item(std::size_t index) override { items_start_ = index; }
57 
58  std::size_t start_num(void) override { return items_start_; }
59 
60  /** Menu expanding for palette group list */
61  void expand_palette_groups_menu(std::vector<config>& items, int i) override;
62 
63  void set_group(std::size_t index) override;
64 // int active_group();
65 
66  const std::vector<item_group>& get_groups() const override { return groups_; }
67 
68  /** Called by draw_manager to validate layout before drawing. */
69  virtual void layout() override;
70  /** Called by widget::draw() */
71  virtual void draw_contents() override;
72 
73  void next_group() override {
74  set_group( (active_group_index() +1) % (groups_.size()) );
75  }
76  void prev_group() override {
77  set_group( (active_group_index() -1) % (groups_.size()) );
78  }
79 
80  /**
81  * Update the size of this widget.
82  *
83  * Use if the size_specs have changed.
84  */
85  void adjust_size(const SDL_Rect& target) override;
86 
87  virtual bool scroll_up() override;
88  virtual bool can_scroll_up() override;
89  virtual bool scroll_down() override;
90  virtual bool can_scroll_down() override;
91 
92  void swap() override;
93 
94  virtual std::string get_help_string() = 0;
95 
96  /** Return the currently selected foreground/background item. */
97  const Item& selected_fg_item() const { return item_map_.find(selected_fg_item_)->second; }
98  const Item& selected_bg_item() const { return item_map_.find(selected_bg_item_)->second; }
99 
100 private:
101 
102  std::size_t active_group_index();
103 
104  /** Setup item image and tooltip. */
105  virtual void setup_item(
106  const Item& item,
107  texture& item_base_image,
108  texture& item_overlay_image,
109  std::stringstream& tooltip
110  ) = 0;
111 
112  virtual const std::string& get_id(const Item& item) = 0;
113 
114  /** Setup the internal data structure. */
115  virtual void setup(const game_config_view& cfg) = 0;
116 
117  virtual const std::string& active_group_id() {return active_group_;}
118 
119  virtual bool is_selected_fg_item(const std::string& id);
120  virtual bool is_selected_bg_item(const std::string& id);
121 
122  /** Return the number of items in the currently-active group. */
123  std::size_t num_items() override;
124 
125  void hide(bool hidden) override;
126 
127 protected:
128  /**
129  * Sets a group active id
130  *
131  * This can result in no visible
132  * selected items.
133  */
134  virtual void set_group(const std::string& id);
135  const std::vector<std::string>& active_group() { return group_map_[active_group_]; }
136 
137  /** Select a foreground item. */
138  virtual void select_fg_item(const std::string& item_id) override;
139  virtual void select_bg_item(const std::string& item_id) override;
140 
141  /**
142  * The editor_groups as defined in editor-groups.cfg.
143  *
144  * Note the user must make sure the id's here are the same as the
145  * editor_group in terrain.cfg.
146  */
147  std::vector<item_group> groups_;
148 
150 
151  /**
152  * Both the width and the height of the square buttons.
153  */
155  /**
156  * item_space_ plus some padding.
157  */
159 
160  /**
161  * Number of items per row.
162  */
163  std::size_t columns_;
164 
165 protected:
166  std::map<std::string, std::vector<std::string>> group_map_;
167 
168  typedef std::map<std::string, Item> item_map;
169  item_map item_map_;
170  /**
171  * Index of the item at the top-left of the visible area, used for scrolling up and down.
172  */
173  std::size_t items_start_;
174  std::set<std::string> non_core_items_;
175 
176 private:
177  std::string active_group_;
178  std::string selected_fg_item_;
179  std::string selected_bg_item_;
180 
182  std::vector<gui::tristate_button> buttons_;
183 };
184 
185 
186 } //end namespace editor
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:187
virtual sdl_handler_vector handler_members() override
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
bool hidden() const
Definition: widget.cpp:162
std::size_t active_group_index()
std::size_t items_start_
Index of the item at the top-left of the visible area, used for scrolling up and down.
virtual void setup_item(const Item &item, texture &item_base_image, texture &item_overlay_image, std::stringstream &tooltip)=0
Setup item image and tooltip.
editor_palette(editor_display &gui, const game_config_view &, std::size_t item_size, std::size_t columns, editor_toolkit &toolkit)
General purpose widgets.
void set_start_item(std::size_t index) override
virtual bool scroll_down() override
Scroll the editor-palette down one step if possible.
void set_group(std::size_t index) override
virtual bool scroll_up() override
Scroll the editor-palette up one step if possible.
const std::vector< std::string > items
virtual bool is_selected_bg_item(const std::string &id)
const std::vector< std::string > & active_group()
int item_size_
Both the width and the height of the square buttons.
virtual bool can_scroll_up() override
void expand_palette_groups_menu(std::vector< config > &items, int i) override
Menu expanding for palette group list.
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:32
std::map< std::string, Item > item_map
std::size_t columns_
Number of items per row.
virtual bool is_selected_fg_item(const std::string &id)
std::size_t start_num(void) override
virtual void setup(const game_config_view &cfg)=0
Setup the internal data structure.
std::map< std::string, std::vector< std::string > > group_map_
virtual bool can_scroll_down() override
const std::vector< item_group > & get_groups() const override
virtual void layout() override
Called by draw_manager to validate layout before drawing.
Manage the empty-palette in the editor.
Definition: action.cpp:30
editor_toolkit & toolkit_
std::vector< gui::tristate_button > buttons_
std::size_t i
Definition: function.cpp:967
std::size_t num_items() override
Return the number of items in the currently-active group.
const Item & selected_bg_item() const
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:72
void next_group() override
std::set< std::string > non_core_items_
int item_space_
item_space_ plus some padding.
virtual void draw_contents() override
Called by widget::draw()
virtual std::string get_help_string()=0
void adjust_size(const SDL_Rect &target) override
Update the size of this widget.
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
virtual const std::string & get_id(const Item &item)=0
void hide(bool hidden) override
void prev_group() override
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
virtual const std::string & active_group_id()
std::string tooltip
Shown when hovering over an entry in the filter&#39;s drop-down list.
Definition: manager.cpp:219
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414