The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
editor_palettes.cpp
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 #define GETTEXT_DOMAIN "wesnoth-editor"
16 
17 #include "editor_palettes.hpp"
18 
19 #include "gettext.hpp"
20 #include "marked-up_text.hpp"
21 #include "tooltips.hpp"
22 #include "overlay.hpp"
23 #include "filesystem.hpp"
24 #include "units/types.hpp"
25 
27 
28 #include "wml_separators.hpp"
29 
30 namespace editor {
31 
32 template<class Item>
34 {
36  for (gui::widget& b : buttons_) {
37  h.push_back(&b);
38  }
39  return h;
40 }
44 
45 template<class Item>
47 {
48  for (unsigned int i = 0; i < items.size(); ++i) {
49  if (items[i] == "editor-palette-groups") {
50  items.erase(items.begin() + i);
51 
52  std::vector<std::string> groups;
53  const std::vector<item_group>& item_groups = get_groups();
54 
55  for (size_t mci = 0; mci < item_groups.size(); ++mci) {
56  std::string groupname = item_groups[mci].name;
57  if (groupname.empty()) {
58  groupname = _("(Unknown Group)");
59  }
60  std::stringstream str;
61  str << IMAGE_PREFIX << item_groups[mci].icon;
62  if (mci == active_group_index()) {
63 
64  if (filesystem::file_exists(str.str() + "_30-pressed.png" ) ) {
65  str << "_30-pressed.png";
66  } else {
67  str << "_30.png~CS(70,70,0)";
68  }
69 
70  } else {
71  str << "_30.png";
72  }
73  str << COLUMN_SEPARATOR << groupname;
74  groups.push_back(str.str());
75 
76 
77  }
78  items.insert(items.begin() + i, groups.begin(), groups.end());
79  break;
80  }
81  }
82 }
84 template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector<std::string>& items);
85 template void editor_palette<overlay>::expand_palette_groups_menu(std::vector<std::string>& items);
86 
87 template<class Item>
89 {
90  int decrement = item_width_;
91  if (items_start_ + num_visible_items() == num_items() && num_items() % item_width_ != 0) {
92  decrement = num_items() % item_width_;
93  }
94  if(items_start_ >= decrement) {
95  items_start_ -= decrement;
96  draw();
97  return true;
98  }
99  return false;
100 }
103 template bool editor_palette<overlay>::scroll_up();
104 
105 template<class Item>
106 void editor_palette<Item>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items)
107 {
108  const std::vector<item_group>& item_groups = get_groups();
109 
110  for (size_t mci = 0; mci < item_groups.size(); ++mci) {
111  std::string groupname = item_groups[mci].name;
112  if (groupname.empty()) {
113  groupname = _("(Unknown Group)");
114  }
115  const std::string& img = item_groups[mci].icon;
116  items.push_back(std::pair<std::string, std::string>( img, groupname));
117  }
118 }
119 template void editor_palette<t_translation::t_terrain>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items);
120 template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items);
121 template void editor_palette<overlay>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items);
122 
123 template<class Item>
125 {
126  return (items_start_ != 0);
127 }
131 
132 template<class Item>
134 {
135  return (items_start_ + nitems_ + item_width_ <= num_items());
136 }
140 
141 template<class Item>
143 {
144  bool end_reached = (!(items_start_ + nitems_ + item_width_ <= num_items()));
145  bool scrolled = false;
146 
147  // move downwards
148  if(!end_reached) {
149  items_start_ += item_width_;
150  scrolled = true;
151  }
152  else if (items_start_ + nitems_ + (num_items() % item_width_) <= num_items()) {
153  items_start_ += num_items() % item_width_;
154  scrolled = true;
155  }
156  set_dirty(scrolled);
157  draw();
158  return scrolled;
159 }
163 
164 template<class Item>
166 {
167  assert(!id.empty());
168 
169  bool found = false;
170  for (const item_group& group : groups_) {
171  if (group.id == id) {
172  found = true;
173  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
174  if (palette_menu_button) {
175  //palette_menu_button->set_label(group.name);
176  palette_menu_button->set_tooltip_string(group.name);
177  palette_menu_button->set_overlay(group.icon);
178  }
179  }
180  }
181  assert(found);
182 
183  active_group_ = id;
184 
185  if(active_group().empty()) {
186  ERR_ED << "No items found in group with the id: '" << id << "'." << std::endl;
187  }
188 }
190 template void editor_palette<unit_type>::set_group(const std::string& id);
191 template void editor_palette<overlay>::set_group(const std::string& id);
192 
193 template<class Item>
195 {
196  assert(groups_.size() > index);
197  set_group(groups_[index].id);
198 }
200 template void editor_palette<unit_type>::set_group(size_t index);
201 template void editor_palette<overlay>::set_group(size_t index);
202 
203 template<class Item>
205 {
206  assert(!active_group_.empty());
207 
208  for (size_t i = 0 ; i < groups_.size(); i++) {
209  if (groups_[i].id == active_group_)
210  return i;
211  }
212 
213  return static_cast<size_t>(-1);
214 }
218 
219 template<class Item>
221 {
222  palette_x_ = target.x;
223  palette_y_ = target.y;
224  const int space_for_items = target.h;
225  const int items_fitting = (space_for_items / item_space_) * item_width_;
226  nitems_ = std::min(items_fitting, nmax_items_);
227  if (num_visible_items() != nitems_) {
228  buttons_.resize(nitems_, gui::tristate_button(gui_.video(), this));
229  }
230  set_location(target);
231  set_dirty(true);
232  gui_.video().clear_help_string(help_handle_);
233  help_handle_ = gui_.video().set_help_string(get_help_string());
234 }
236 template void editor_palette<unit_type>::adjust_size(const SDL_Rect& target);
237 template void editor_palette<overlay>::adjust_size(const SDL_Rect& target);
238 
239 template<class Item>
241 {
242  if (selected_fg_item_ != item_id) {
243  selected_fg_item_ = item_id;
244  set_dirty();
245  }
246  gui_.video().clear_help_string(help_handle_);
247  help_handle_ = gui_.video().set_help_string(get_help_string());
248 }
250 template void editor_palette<unit_type>::select_fg_item(const std::string& unit_id);
251 template void editor_palette<overlay>::select_fg_item(const std::string& unit_id);
252 
253 template<class Item>
255 {
256  if (selected_bg_item_ != item_id) {
257  selected_bg_item_ = item_id;
258  set_dirty();
259  }
260  gui_.video().clear_help_string(help_handle_);
261  help_handle_ = gui_.video().set_help_string(get_help_string());
262 }
264 template void editor_palette<unit_type>::select_bg_item(const std::string& unit_id);
265 template void editor_palette<overlay>::select_bg_item(const std::string& unit_id);
266 
267 template<class Item>
269 {
270  std::swap(selected_fg_item_, selected_bg_item_);
271  select_fg_item(selected_fg_item_);
272  select_bg_item(selected_bg_item_);
273  set_dirty();
274 }
276 template void editor_palette<unit_type>::swap();
277 template void editor_palette<overlay>::swap();
278 
279 template<class Item>
281 {
282  return group_map_[active_group_].size();
283 }
287 
288 template<class Item>
290 {
291  return selected_fg_item_ == id;
292 }
296 
297 template<class Item>
299 {
300  return selected_bg_item_ == id;
301 }
305 
306 template<class Item>
308 {
309  if (*active_mouse_action_)
310  (*active_mouse_action_)->set_mouse_overlay(gui_);
311 
312  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
313  if (palette_menu_button) {
314 
315  t_string& name = groups_[active_group_index()].name;
316  std::string& icon = groups_[active_group_index()].icon;
317 
318  palette_menu_button->set_tooltip_string(name);
319  palette_menu_button->set_overlay(icon);
320  }
321 
322  unsigned int y = palette_y_;
323  unsigned int x = palette_x_;
324  int starting = items_start_;
325  int ending = std::min<int>(starting + nitems_, num_items());
326 
327  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
328  if (upscroll_button)
329  upscroll_button->enable(starting != 0);
330  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
331  if (downscroll_button)
332  downscroll_button->enable(ending != num_items());
333 
334 
335  int counter = starting;
336  for (int i = 0, size = num_visible_items(); i < size ; ++i) {
337  //TODO check if the conditions still hold for the counter variable
338  //for (unsigned int counter = starting; counter < ending; counter++)
339 
340  gui::tristate_button& tile = buttons_[i];
341 
342  tile.hide(true);
343 
344  if (i >= ending) continue;
345 
346  const std::string item_id = active_group()[counter];
347  //typedef std::map<std::string, Item> item_map_wurscht;
348  typename item_map::iterator item = item_map_.find(item_id);
349 
350  surface item_image(nullptr);
351  std::stringstream tooltip_text;
352  draw_item((*item).second, item_image, tooltip_text);
353 
354  bool is_core = non_core_items_.find(get_id((*item).second)) == non_core_items_.end();
355  if (!is_core) {
356  tooltip_text << " "
358  << _("(non-core)") << "\n"
359  << _("Will not work in game without extra care.")
360  << "</span>";
361  }
362 
363  const int counter_from_zero = counter - starting;
364  SDL_Rect dstrect;
365  dstrect.x = x + (counter_from_zero % item_width_) * item_space_;
366  dstrect.y = y;
367  dstrect.w = item_size_ + 2;
368  dstrect.h = item_size_ + 2;
369 
370  tile.set_location(dstrect);
371  tile.set_tooltip_string(tooltip_text.str());
372  tile.set_item_image(item_image);
373  tile.set_item_id(item_id);
374 
375 // if (get_id((*item).second) == selected_bg_item_
376 // && get_id((*item).second) == selected_fg_item_) {
377 // tile.set_pressed(gui::tristate_button::BOTH);
378 // } else if (get_id((*item).second) == selected_bg_item_) {
379 // tile.set_pressed(gui::tristate_button::RIGHT);
380 // } else if (get_id((*item).second) == selected_fg_item_) {
381 // tile.set_pressed(gui::tristate_button::LEFT);
382 // } else {
383 // tile.set_pressed(gui::tristate_button::NONE);
384 // }
385 
386  if (is_selected_bg_item(get_id(item->second))
387  && is_selected_fg_item(get_id(item->second))) {
389  } else if (is_selected_bg_item(get_id(item->second))) {
391  } else if (is_selected_fg_item(get_id(item->second))) {
393  } else {
395  }
396 
397  tile.set_dirty(true);
398  tile.hide(false);
399  tile.draw();
400 
401  // Adjust location
402  if (counter_from_zero % item_width_ == item_width_ - 1)
403  y += item_space_;
404  counter++;
405  }
407 }
411 
412 
413 } // end namespace editor
virtual bool scroll_up()
Scroll the editor-palette up one step if possible.
virtual void select_bg_item(const std::string &item_id)
GLuint counter
Definition: glew.h:2584
virtual bool can_scroll_down()
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:163
virtual void select_fg_item(const std::string &item_id)
Select a foreground item.
Stores the info about the groups in a nice format.
void set_item_id(const std::string &id)
virtual bool can_scroll_up()
#define h
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
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::string span_color(const SDL_Color &color)
Creates pango markup of a color.
char const IMAGE_PREFIX
void set_pressed(PRESSED_STATE new_pressed_state)
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
virtual bool is_selected_fg_item(const std::string &id)
GLuint id
Definition: glew.h:1647
const SDL_Color BAD_COLOR
Definition: font.cpp:568
virtual void hide(bool value=true)
Definition: widget.cpp:162
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.
void adjust_size(const SDL_Rect &target)
Update the size of this widget.
GLuint index
Definition: glew.h:1782
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
Declarations for File-IO.
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:319
GLuint const GLchar * name
Definition: glew.h:1782
void expand_palette_groups_menu(std::vector< std::pair< std::string, std::string > > &items)
Menu expanding for palette group list.
GLsizeiptr size
Definition: glew.h:1649
GLboolean GLuint group
Definition: glew.h:2589
char const COLUMN_SEPARATOR
void swap(game_board &one, game_board &other)
Definition: game_board.cpp:56
#define ERR_ED
GLint GLvoid * img
Definition: glew.h:1353
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
map_location location
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
virtual void draw_contents()
virtual sdl_handler_vector handler_members()
void update_rect(const SDL_Rect &)
Definition: dummy_video.cpp:27
GLsizei GLuint * groups
Definition: glew.h:2588
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
HOTKEY_COMMAND get_id(const std::string &command)
returns get_hotkey_command(command).id
void set_item_image(const surface &image)
GLenum target
Definition: glew.h:5190
void set_group(size_t index)