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