The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
editor_palettes.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://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  , size_t item_size, 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  , help_handle_(-1)
53  {
54  }
55 
56 
57 
58  virtual sdl_handler_vector handler_members() override;
59 
60  void set_start_item(size_t index) override { items_start_ = index; }
61 
62  size_t start_num(void) override { return items_start_; }
63 
64  /** Menu expanding for palette group list */
65  void expand_palette_groups_menu(std::vector<config>& items, int i) override;
66 
67  void set_group(size_t index) override;
68 // int active_group();
69 
70  const std::vector<item_group>& get_groups() const override { return groups_; }
71 
72  virtual void draw() override {
73  widget::draw();
74  }
75  virtual void draw_contents() override;
76 
77  void next_group() override {
78  set_group( (active_group_index() +1) % (groups_.size()) );
79  }
80  void prev_group() override {
81  set_group( (active_group_index() -1) % (groups_.size()) );
82  }
83 
84  /**
85  * Update the size of this widget.
86  *
87  * Use if the size_specs have changed.
88  */
89  void adjust_size(const SDL_Rect& target) override;
90 
91  virtual bool scroll_up() override;
92  virtual bool can_scroll_up() override;
93  virtual bool scroll_down() override;
94  virtual bool can_scroll_down() override;
95 
96  void swap() override;
97 
98  virtual std::string get_help_string() = 0;
99 
100  /** Return the currently selected foreground/background item. */
101  const Item& selected_fg_item() const { return item_map_.find(selected_fg_item_)->second; }
102  const Item& selected_bg_item() const { return item_map_.find(selected_bg_item_)->second; }
103 
104 private:
105 
106  size_t active_group_index();
107 
108  virtual void draw_item(const Item& item, surface& item_image, std::stringstream& tooltip) = 0;
109 
110  virtual const std::string& get_id(const Item& item) = 0;
111 
112  /** Setup the internal data structure. */
113  virtual void setup(const config& cfg) = 0;
114 
115  virtual const std::string& active_group_id() {return active_group_;}
116 
117  virtual bool is_selected_fg_item(const std::string& id);
118  virtual bool is_selected_bg_item(const std::string& id);
119 
120  /** Return the number of items in the palette. */
121  int num_items() override;
122 
123  /** Return the number of items in the palette. */
124  int num_visible_items() { return buttons_.size(); }
125 
126  void hide(bool hidden) override {
127  widget::hide(hidden);
128  if (!hidden)
131  for (gui::widget& w : buttons_) {
132  w.hide(hidden);
133  }
134  }
135 
136 protected:
137  /**
138  * Sets a group active id
139  *
140  * This can result in no visible
141  * selected items.
142  */
143  virtual void set_group(const std::string& id);
144  const std::vector<std::string>& active_group() { return group_map_[active_group_]; }
145 
146  /** Select a foreground item. */
147  virtual void select_fg_item(const std::string& item_id) override;
148  virtual void select_bg_item(const std::string& item_id) override;
149 
150  /**
151  * The editor_groups as defined in editor-groups.cfg.
152  *
153  * Note the user must make sure the id's here are the same as the
154  * editor_group in terrain.cfg.
155  */
156  std::vector<item_group> groups_;
157 
159 
163 
164 private:
165  unsigned int palette_y_;
166  unsigned int palette_x_;
167 
168 protected:
169  std::map<std::string, std::vector<std::string> > group_map_;
170 
171  typedef std::map<std::string, Item> item_map;
172  item_map item_map_;
174  std::set<std::string> non_core_items_;
175 
176 private:
180 
182  std::vector<gui::tristate_button> buttons_;
183 
185 };
186 
187 
188 } //end namespace editor
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
editor_palette(editor_display &gui, const config &, size_t item_size, size_t item_width, editor_toolkit &toolkit)
std::vector< char_t > string
const Item & selected_bg_item() const
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:176
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
int num_visible_items()
Return the number of items in the palette.
CVideo & video() const
Definition: widget.hpp:84
General purpose widgets.
virtual bool scroll_down() override
Scroll the editor-palette down one step if possible.
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()
size_t start_num(void) override
const std::vector< std::string > items
virtual bool can_scroll_up() override
void set_start_item(size_t index) override
void expand_palette_groups_menu(std::vector< config > &items, int i) override
Menu expanding for palette group list.
void set_group(size_t index) override
std::map< std::string, Item > item_map
virtual bool is_selected_fg_item(const std::string &id)
bool hidden() const
Definition: widget.cpp:196
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
void draw(surface screen)
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_
#define i
int w
void next_group() override
int set_help_string(const std::string &str)
Definition: video.cpp:470
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.
virtual const std::string & get_id(const Item &item)=0
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:195
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:93
virtual const std::string & active_group_id()
void clear_help_string(int handle)
Definition: video.cpp:501