The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 #ifndef EDITOR_PALETTES_H_INCLUDED
16 #define EDITOR_PALETTES_H_INCLUDED
17 
19 #include "common_palette.hpp"
20 #include "tristate_button.hpp"
21 
22 namespace editor {
23 template<class Item>
25 
26 public:
27 
29  , size_t item_size, size_t item_width, mouse_action** active_mouse_action)
30  : tristate_palette(gui)
31  , groups_()
32  , gui_(gui)
33  , item_size_(item_size)
34  , item_width_(item_width)
35 //TODO avoid magic number
36  , item_space_(item_size + 3)
37  , palette_y_(0)
38  , palette_x_(0)
39  , group_map_()
40  , item_map_()
41  , nitems_(0)
42  , nmax_items_(0)
43  , items_start_(0)
44  , non_core_items_()
45  , active_group_()
48  , active_mouse_action_(active_mouse_action)
49  , buttons_()
50  , help_handle_(-1)
51  {
52  }
53 
54 
55 
57 
58  void set_start_item(size_t index) { items_start_ = index; }
59 
60  size_t start_num(void) { return items_start_; }
61 
62  /** Menu expanding for palette group list */
63  void expand_palette_groups_menu(std::vector< std::pair<std::string, std::string> >& items);
64  void expand_palette_groups_menu(std::vector<std::string>& items);
65 
66  void set_group(size_t index);
67 // int active_group();
68 
69  const std::vector<item_group>& get_groups() const { return groups_; }
70 
71  virtual void draw() {
72  widget::draw();
73  }
74  virtual void draw_contents();
75 
76  void next_group() {
77  set_group( (active_group_index() +1) % (groups_.size()) );
78  }
79  void prev_group() {
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);
89 
90  virtual bool scroll_up();
91  virtual bool can_scroll_up();
92  virtual bool scroll_down();
93  virtual bool can_scroll_down();
94 
95  void swap();
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  size_t active_group_index();
106 
107  /** Scroll the editor-palette to the top. */
108  void scroll_top();
109 
110  /** Scroll the editor-palette to the bottom. */
111  void scroll_bottom();
112 
113  virtual void draw_item(const Item& item, surface& item_image, std::stringstream& tooltip) = 0;
114 
115  virtual const std::string& get_id(const Item& item) = 0;
116 
117  /** Setup the internal data structure. */
118  virtual void setup(const config& cfg) = 0;
119 
120  virtual const std::string& active_group_id() {return active_group_;}
121 
122  virtual bool is_selected_fg_item(const std::string& id);
123  virtual bool is_selected_bg_item(const std::string& id);
124 
125  /** Return the number of items in the palette. */
126  int num_items();
127 
128  /** Return the number of items in the palette. */
129  int num_visible_items() { return buttons_.size(); }
130 
131  void hide(bool hidden) {
132  widget::hide(hidden);
133  if (!hidden)
136  for (gui::widget& w : buttons_) {
137  w.hide(hidden);
138  }
139  }
140 
141 protected:
142  /**
143  * Sets a group active id
144  *
145  * This can result in no visible
146  * selected items.
147  */
148  virtual void set_group(const std::string& id);
149  const std::vector<std::string>& active_group() { return group_map_[active_group_]; }
150 
151  /** Select a foreground item. */
152  virtual void select_fg_item(const std::string& item_id);
153  virtual void select_bg_item(const std::string& item_id);
154 
155  /**
156  * The editor_groups as defined in editor-groups.cfg.
157  *
158  * Note the user must make sure the id's here are the same as the
159  * editor_group in terrain.cfg.
160  */
161  std::vector<item_group> groups_;
162 
164 
168 
169 private:
170  unsigned int palette_y_;
171  unsigned int palette_x_;
172 
173 protected:
174  std::map<std::string, std::vector<std::string> > group_map_;
175 
176  typedef std::map<std::string, Item> item_map;
177  item_map item_map_;
179  std::set<std::string> non_core_items_;
180 
181 private:
185 
187  std::vector<gui::tristate_button> buttons_;
188 
190 };
191 
192 
193 } //end namespace editor
194 #endif // EDITOR_PALETTES_H_INCLUDED
195 
virtual bool scroll_up()
Scroll the editor-palette up one step if possible.
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
virtual void select_bg_item(const std::string &item_id)
mouse_action ** active_mouse_action_
const Item & selected_bg_item() const
virtual bool can_scroll_down()
void set_start_item(size_t index)
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:163
void hide(bool hidden)
virtual void select_fg_item(const std::string &item_id)
Select a foreground item.
int num_visible_items()
Return the number of items in the palette.
General purpose widgets.
virtual void setup(const config &cfg)=0
Setup the internal data structure.
virtual bool can_scroll_up()
const std::vector< std::string > & active_group()
virtual bool is_selected_bg_item(const std::string &id)
const std::vector< std::string > items
virtual bool scroll_down()
Scroll the editor-palette down one step if possible.
std::map< std::string, Item > item_map
bool hidden() const
Definition: widget.cpp:198
virtual bool is_selected_fg_item(const std::string &id)
virtual void draw_item(const Item &item, surface &item_image, std::stringstream &tooltip)=0
std::map< std::string, std::vector< std::string > > group_map_
editor_palette(editor_display &gui, const config &, size_t item_size, size_t item_width, mouse_action **active_mouse_action)
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
void draw(surface screen)
Manage the empty-palette in the editor.
Definition: action.cpp:28
int num_items()
Return the number of items in the palette.
std::vector< gui::tristate_button > buttons_
void adjust_size(const SDL_Rect &target)
Update the size of this widget.
const std::vector< item_group > & get_groups() const
GLuint index
Definition: glew.h:1782
int set_help_string(const std::string &str)
Definition: video.cpp:625
void scroll_top()
Scroll the editor-palette to the top.
std::set< std::string > non_core_items_
void expand_palette_groups_menu(std::vector< std::pair< std::string, std::string > > &items)
Menu expanding for palette group list.
virtual std::string get_help_string()=0
virtual const std::string & get_id(const Item &item)=0
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:202
std::vector< item_group > groups_
The editor_groups as defined in editor-groups.cfg.
A mouse action receives events from the controller, and responds to them by creating an appropriate e...
virtual void draw_contents()
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual sdl_handler_vector handler_members()
virtual const std::string & active_group_id()
GLsizei const GLcharARB ** string
Definition: glew.h:4503
GLenum target
Definition: glew.h:5190
void clear_help_string(int handle)
Definition: video.cpp:656
void set_group(size_t index)
void scroll_bottom()
Scroll the editor-palette to the bottom.