The Battle for Wesnoth  1.15.0-dev
editor_palettes.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
20 
21 namespace editor {
22 
23 class editor_toolkit;
24 
25 template<class Item>
27 
28 public:
29 
31  , std::size_t item_size, std::size_t item_width, editor_toolkit &toolkit)
32  : tristate_palette(gui.video())
33  , groups_()
34  , gui_(gui)
35  , item_size_(item_size)
36  , item_width_(item_width)
37 //TODO avoid magic number
38  , item_space_(item_size + 3)
39  , palette_y_(0)
40  , palette_x_(0)
41  , group_map_()
42  , item_map_()
43  , nitems_(0)
44  , nmax_items_(0)
45  , items_start_(0)
46  , non_core_items_()
47  , active_group_()
50  , toolkit_(toolkit)
51  , buttons_()
52  {
53  }
54 
55 
56 
57  virtual sdl_handler_vector handler_members() override;
58 
59  void set_start_item(std::size_t index) override { items_start_ = index; }
60 
61  std::size_t start_num(void) override { return items_start_; }
62 
63  /** Menu expanding for palette group list */
64  void expand_palette_groups_menu(std::vector<config>& items, int i) override;
65 
66  void set_group(std::size_t index) override;
67 // int active_group();
68 
69  const std::vector<item_group>& get_groups() const override { return groups_; }
70 
71  virtual void draw() override {
72  widget::draw();
73  }
74  virtual void draw_contents() override;
75 
76  void next_group() override {
77  set_group( (active_group_index() +1) % (groups_.size()) );
78  }
79  void prev_group() override {
80  set_group( (active_group_index() -1) % (groups_.size()) );
81  }
82 
83  /**
84  * Update the size of this widget.
85  *
86  * Use if the size_specs have changed.
87  */
88  void adjust_size(const SDL_Rect& target) override;
89 
90  virtual bool scroll_up() override;
91  virtual bool can_scroll_up() override;
92  virtual bool scroll_down() override;
93  virtual bool can_scroll_down() override;
94 
95  void swap() override;
96 
97  virtual std::string get_help_string() = 0;
98 
99  /** Return the currently selected foreground/background item. */
100  const Item& selected_fg_item() const { return item_map_.find(selected_fg_item_)->second; }
101  const Item& selected_bg_item() const { return item_map_.find(selected_bg_item_)->second; }
102 
103 private:
104 
105  std::size_t active_group_index();
106 
107  virtual void draw_item(const Item& item, surface& item_image, std::stringstream& tooltip) = 0;
108 
109  virtual const std::string& get_id(const Item& item) = 0;
110 
111  /** Setup the internal data structure. */
112  virtual void setup(const config& cfg) = 0;
113 
114  virtual const std::string& active_group_id() {return active_group_;}
115 
116  virtual bool is_selected_fg_item(const std::string& id);
117  virtual bool is_selected_bg_item(const std::string& id);
118 
119  /** Return the number of items in the palette. */
120  int num_items() override;
121 
122  /** Return the number of items in the palette. */
123  int num_visible_items() { return buttons_.size(); }
124 
125  void hide(bool hidden) override {
126  widget::hide(hidden);
127  for (gui::widget& w : buttons_) {
128  w.hide(hidden);
129  }
130  }
131 
132 protected:
133  /**
134  * Sets a group active id
135  *
136  * This can result in no visible
137  * selected items.
138  */
139  virtual void set_group(const std::string& id);
140  const std::vector<std::string>& active_group() { return group_map_[active_group_]; }
141 
142  /** Select a foreground item. */
143  virtual void select_fg_item(const std::string& item_id) override;
144  virtual void select_bg_item(const std::string& item_id) override;
145 
146  /**
147  * The editor_groups as defined in editor-groups.cfg.
148  *
149  * Note the user must make sure the id's here are the same as the
150  * editor_group in terrain.cfg.
151  */
152  std::vector<item_group> groups_;
153 
155 
159 
160 private:
161  unsigned int palette_y_;
162  unsigned int palette_x_;
163 
164 protected:
165  std::map<std::string, std::vector<std::string>> group_map_;
166 
167  typedef std::map<std::string, Item> item_map;
168  item_map item_map_;
170  std::set<std::string> non_core_items_;
171 
172 private:
173  std::string active_group_;
174  std::string selected_fg_item_;
175  std::string selected_bg_item_;
176 
178  std::vector<gui::tristate_button> buttons_;
179 };
180 
181 
182 } //end namespace editor
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:167
editor_palette(editor_display &gui, const config &, std::size_t item_size, std::size_t item_width, editor_toolkit &toolkit)
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:191
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 void setup(const config &cfg)=0
Setup the internal data structure.
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
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:29
editor_toolkit & toolkit_
std::vector< gui::tristate_button > buttons_
std::size_t i
Definition: function.cpp:933
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:71
void next_group() override
CVideo & video() const
Definition: widget.hpp:84
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.
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
virtual const std::string & get_id(const Item &item)=0
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.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
virtual const std::string & active_group_id()