The Battle for Wesnoth  1.19.0-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 
32  , int 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 
65  const std::vector<item_group>& get_groups() const override { return groups_; }
66 
67  /** Called by draw_manager to validate layout before drawing. */
68  virtual void layout() override;
69  /** Called by widget::draw() */
70  virtual void draw_contents() override;
71 
72  void next_group() override {
73  set_group( (active_group_index() +1) % (groups_.size()) );
74  }
75  void prev_group() override {
76  set_group( (active_group_index() -1) % (groups_.size()) );
77  }
78 
79  /**
80  * Update the size of this widget.
81  *
82  * Use if the size_specs have changed.
83  */
84  void adjust_size(const SDL_Rect& target) override;
85 
86  virtual bool scroll_up() override;
87  virtual bool can_scroll_up() override;
88  virtual bool scroll_down() override;
89  virtual bool can_scroll_down() override;
90 
91  void swap() override;
92 
93  virtual std::string get_help_string() = 0;
94 
95  /** Return the currently selected foreground/background item. */
96  const Item& selected_fg_item() const { return item_map_.find(selected_fg_item_)->second; }
97  const Item& selected_bg_item() const { return item_map_.find(selected_bg_item_)->second; }
98 
99 private:
100 
101  std::size_t active_group_index();
102 
103  /** Setup item image and tooltip. */
104  virtual void setup_item(
105  const Item& item,
106  texture& item_base_image,
107  texture& item_overlay_image,
108  std::stringstream& tooltip
109  ) = 0;
110 
111  virtual const std::string& get_id(const Item& item) = 0;
112 
113  /** Setup the internal data structure. */
114  virtual void setup(const game_config_view& cfg) = 0;
115 
116  virtual const std::string& active_group_id() {return active_group_;}
117 
118  virtual bool is_selected_fg_item(const std::string& id);
119  virtual bool is_selected_bg_item(const std::string& id);
120 
121  /** Return the number of items in the currently-active group. */
122  std::size_t num_items() override;
123 
124  void hide(bool hidden) override;
125 
126 protected:
127  /**
128  * Sets a group active id
129  *
130  * This can result in no visible
131  * selected items.
132  */
133  virtual void set_group(const std::string& id);
134  const std::vector<std::string>& active_group() { return group_map_[active_group_]; }
135 
136  /** Select a foreground item. */
137  virtual void select_fg_item(const std::string& item_id) override;
138  virtual void select_bg_item(const std::string& item_id) override;
139 
140  /**
141  * The editor_groups as defined in editor-groups.cfg.
142  *
143  * Note the user must make sure the id's here are the same as the
144  * editor_group in terrain.cfg.
145  */
146  std::vector<item_group> groups_;
147 
149 
150  /**
151  * Both the width and the height of the square buttons.
152  *
153  * This is a size measured in pixels, and should match the type of
154  * SDL_rect.w and SDL_rect.h.
155  */
157  /**
158  * item_size_ plus some padding.
159  */
161 
162  /**
163  * Number of items per row.
164  */
165  std::size_t columns_;
166 
167 protected:
168  std::map<std::string, std::vector<std::string>> group_map_;
169 
170  typedef std::map<std::string, Item> item_map;
172  /**
173  * Index of the item at the top-left of the visible area, used for scrolling up and down.
174  */
175  std::size_t items_start_;
176  std::set<std::string> non_core_items_;
177 
178 private:
179  std::string active_group_;
180  std::string selected_fg_item_;
181  std::string selected_bg_item_;
182 
184  std::vector<gui::tristate_button> buttons_;
185 };
186 
187 
188 } //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()
std::size_t num_items() override
Return the number of items in the currently-active group.
virtual std::string get_help_string()=0
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.
virtual const std::string & get_id(const Item &item)=0
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
editor_palette(editor_display &gui, const game_config_view &, int item_size, std::size_t columns, editor_toolkit &toolkit)
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:190
std::size_t i
Definition: function.cpp:968
std::string tooltip
Shown when hovering over an entry in the filter's drop-down list.
Definition: manager.cpp:211
Manage the empty-palette in the editor.
Definition: action.cpp:31
General purpose widgets.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
const std::vector< std::string > items
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