The Battle for Wesnoth  1.17.0-dev
editor_palettes.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 item_width, editor_toolkit &toolkit)
33  : tristate_palette(gui.video())
34  , groups_()
35  , gui_(gui)
36  , item_size_(item_size)
37  , item_width_(item_width)
38 //TODO avoid magic number
39  , item_space_(item_size + 3)
40  , palette_y_(0)
41  , palette_x_(0)
42  , group_map_()
43  , item_map_()
44  , nitems_(0)
45  , nmax_items_(0)
46  , items_start_(0)
47  , non_core_items_()
48  , active_group_()
51  , toolkit_(toolkit)
52  , buttons_()
53  , help_handle_(-1)
54  {
55  }
56 
57 
58 
59  virtual sdl_handler_vector handler_members() override;
60 
61  void set_start_item(std::size_t index) override { items_start_ = index; }
62 
63  std::size_t start_num(void) override { return items_start_; }
64 
65  /** Menu expanding for palette group list */
66  void expand_palette_groups_menu(std::vector<config>& items, int i) override;
67 
68  void set_group(std::size_t index) override;
69 // int active_group();
70 
71  const std::vector<item_group>& get_groups() const override { return groups_; }
72 
73  virtual void draw() override {
74  widget::draw();
75  }
76  virtual void draw_contents() override;
77 
78  void next_group() override {
79  set_group( (active_group_index() +1) % (groups_.size()) );
80  }
81  void prev_group() override {
82  set_group( (active_group_index() -1) % (groups_.size()) );
83  }
84 
85  /**
86  * Update the size of this widget.
87  *
88  * Use if the size_specs have changed.
89  */
90  void adjust_size(const SDL_Rect& target) override;
91 
92  virtual bool scroll_up() override;
93  virtual bool can_scroll_up() override;
94  virtual bool scroll_down() override;
95  virtual bool can_scroll_down() override;
96 
97  void swap() override;
98 
99  virtual std::string get_help_string() = 0;
100 
101  /** Return the currently selected foreground/background item. */
102  const Item& selected_fg_item() const { return item_map_.find(selected_fg_item_)->second; }
103  const Item& selected_bg_item() const { return item_map_.find(selected_bg_item_)->second; }
104 
105 private:
106 
107  std::size_t active_group_index();
108 
109  virtual void draw_item(const Item& item, surface& item_image, std::stringstream& tooltip) = 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 palette. */
122  int num_items() override;
123 
124  /** Return the number of items in the palette. */
125  int num_visible_items() { return buttons_.size(); }
126 
127  void hide(bool hidden) override {
128  widget::hide(hidden);
129  if (!hidden)
132  for (gui::widget& w : buttons_) {
133  w.hide(hidden);
134  }
135  }
136 
137 protected:
138  /**
139  * Sets a group active id
140  *
141  * This can result in no visible
142  * selected items.
143  */
144  virtual void set_group(const std::string& id);
145  const std::vector<std::string>& active_group() { return group_map_[active_group_]; }
146 
147  /** Select a foreground item. */
148  virtual void select_fg_item(const std::string& item_id) override;
149  virtual void select_bg_item(const std::string& item_id) override;
150 
151  /**
152  * The editor_groups as defined in editor-groups.cfg.
153  *
154  * Note the user must make sure the id's here are the same as the
155  * editor_group in terrain.cfg.
156  */
157  std::vector<item_group> groups_;
158 
160 
164 
165 private:
166  unsigned int palette_y_;
167  unsigned int palette_x_;
168 
169 protected:
170  std::map<std::string, std::vector<std::string>> group_map_;
171 
172  typedef std::map<std::string, Item> item_map;
173  item_map item_map_;
175  std::set<std::string> non_core_items_;
176 
177 private:
178  std::string active_group_;
179  std::string selected_fg_item_;
180  std::string selected_bg_item_;
181 
183  std::vector<gui::tristate_button> buttons_;
184 
186 };
187 
188 
189 } //end namespace editor
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:190
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:188
int num_visible_items()
Return the number of items in the palette.
std::size_t active_group_index()
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.
virtual bool is_selected_bg_item(const std::string &id)
const std::vector< std::string > & active_group()
const std::vector< std::string > items
virtual bool can_scroll_up() 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
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.
int num_items() override
Return the number of items in the palette.
virtual void draw_item(const Item &item, surface &item_image, std::stringstream &tooltip)=0
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
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
const Item & selected_bg_item() const
int w
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
CVideo & video() const
Definition: widget.hpp:84
int set_help_string(const std::string &str)
Displays a help string with the given text.
Definition: video.cpp:504
std::set< std::string > non_core_items_
virtual void draw_contents() override
virtual std::string get_help_string()=0
void adjust_size(const SDL_Rect &target) override
Update the size of this widget.
editor_palette(editor_display &gui, const game_config_view &, std::size_t item_size, std::size_t item_width, editor_toolkit &toolkit)
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
virtual const std::string & get_id(const Item &item)=0
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:201
virtual void draw() override
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
void clear_help_string(int handle)
Removes the help string with the given handle.
Definition: video.cpp:536
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410