The Battle for Wesnoth  1.19.5+dev
editor_palettes.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
19 
20 #include "gettext.hpp"
21 #include "serialization/markup.hpp"
22 #include "overlay.hpp"
23 #include "filesystem.hpp"
24 
26 
27 namespace editor {
28 
29 template<class Item>
31 {
33  for (gui::widget& b : buttons_) {
34  h.push_back(&b);
35  }
36  return h;
37 }
38 
39 template<class Item>
40 void editor_palette<Item>::expand_palette_groups_menu(std::vector<config>& items, int i)
41 {
42  auto pos = items.erase(items.begin() + i);
43 
44  std::vector<config> groups;
45  const std::vector<item_group>& item_groups = get_groups();
46 
47  for (std::size_t mci = 0; mci < item_groups.size(); ++mci) {
48  std::string groupname = item_groups[mci].name;
49  if (groupname.empty()) {
50  groupname = _("(Unknown Group)");
51  }
52  std::string img = item_groups[mci].icon + "_30";
53  if (mci == active_group_index()) {
54  std::string pressed_img = img + "-pressed.png";
55  if(filesystem::get_binary_file_location("images", pressed_img).has_value()) {
56  img = pressed_img;
57  } else {
58  img += ".png~CS(70,70,0)";
59  }
60  } else {
61  img += ".png";
62  }
63 
64  groups.emplace_back(
65  "label", groupname,
66  "icon", img
67  );
68  }
69 
70  items.insert(pos, groups.begin(), groups.end());
71 }
72 
73 template<class Item>
75 {
76  bool scrolled = false;
77  if(can_scroll_up()) {
78  // This should only be reachable with items_start_ being a multiple of columns_, but guard against underflow anyway.
79  if(items_start_ < columns_) {
80  items_start_ = 0;
81  } else {
82  items_start_ -= columns_;
83  }
84  scrolled = true;
85  set_dirty(true);
86  }
87  return scrolled;
88 }
89 
90 template<class Item>
92 {
93  return (items_start_ != 0);
94 }
95 
96 template<class Item>
98 {
99  return (items_start_ + buttons_.size() < num_items());
100 }
101 
102 template<class Item>
104 {
105  bool scrolled = false;
106  if(can_scroll_down()) {
107  items_start_ += columns_;
108  scrolled = true;
109  set_dirty(true);
110  }
111  return scrolled;
112 }
113 
114 template<class Item>
115 void editor_palette<Item>::set_group(const std::string& id)
116 {
117  assert(!id.empty());
118 
119  bool found = false;
120  for (const item_group& group : groups_) {
121  if (group.id == id) {
122  found = true;
123  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
124  if (palette_menu_button) {
125  palette_menu_button->set_tooltip_string(group.name);
126  palette_menu_button->set_overlay(group.icon);
127  }
128  }
129  }
130  assert(found);
131 
132  active_group_ = id;
133 
134  if(active_group().empty()) {
135  ERR_ED << "No items found in group with the id: '" << id << "'.";
136  }
137 }
138 
139 template<class Item>
141 {
142  assert(groups_.size() > index);
143  set_group(groups_[index].id);
144 }
145 
146 template<class Item>
148 {
149  assert(!active_group_.empty());
150 
151  for (std::size_t i = 0 ; i < groups_.size(); i++) {
152  if (groups_[i].id == active_group_)
153  return i;
154  }
155 
156  return static_cast<std::size_t>(-1);
157 }
158 
159 template<class Item>
160 void editor_palette<Item>::adjust_size(const SDL_Rect& target)
161 {
162  // The number of columns is passed to the constructor, and isn't changed afterwards. It's likely to be
163  // exactly 4, but will always be a small number which makes the next cast reasonable.
164  const int items_fitting = static_cast<int>((target.h / item_space_) * columns_);
165 
166  // This might be called while the palette is not visible onscreen.
167  // If that happens, no items will fit and we'll have a negative number here.
168  // Just skip it in that case.
169  //
170  // New items can be added via the add_item function, so this creates as
171  // many buttons as can fit, even if there aren't yet enough items to need
172  // that many buttons.
173  if(items_fitting > 0) {
174  const auto buttons_needed = static_cast<std::size_t>(items_fitting);
175  if(buttons_.size() != buttons_needed) {
176  buttons_.resize(buttons_needed, gui::tristate_button(this));
177  }
178  }
179 
180  // Update button locations and sizes. Needs to be done even if the number of buttons hasn't changed,
181  // because adjust_size() also handles moving left and right when the window's width is changed.
182  SDL_Rect dstrect;
183  dstrect.w = item_size_ + 2;
184  dstrect.h = item_size_ + 2;
185  for(std::size_t i = 0; i < buttons_.size(); ++i) {
186  dstrect.x = target.x + static_cast<int>(i % columns_) * item_space_;
187  dstrect.y = target.y + static_cast<int>(i / columns_) * item_space_;
188  buttons_[i].set_location(dstrect);
189  }
190 
191  set_location(target);
192  set_dirty(true);
193  gui_.set_help_string(get_help_string());
194 }
195 
196 template<class Item>
197 void editor_palette<Item>::select_fg_item(const std::string& item_id)
198 {
199  if (selected_fg_item_ != item_id) {
200  selected_fg_item_ = item_id;
201  set_dirty();
202  }
203  gui_.set_help_string(get_help_string());
204 }
205 
206 template<class Item>
207 void editor_palette<Item>::select_bg_item(const std::string& item_id)
208 {
209  if (selected_bg_item_ != item_id) {
210  selected_bg_item_ = item_id;
211  set_dirty();
212  }
213  gui_.set_help_string(get_help_string());
214 }
215 
216 template<class Item>
218 {
219  std::swap(selected_fg_item_, selected_bg_item_);
220  select_fg_item(selected_fg_item_);
221  select_bg_item(selected_bg_item_);
222  set_dirty();
223 }
224 
225 template<class Item>
227 {
228  return group_map_[active_group_].size();
229 }
230 
231 
232 template<class Item>
233 void editor_palette<Item>::hide(bool hidden)
234 {
235  widget::hide(hidden);
236 
237  if (!hidden) {
238  gui_.set_help_string(get_help_string());
239  } else {
240  gui_.clear_help_string();
241  }
242 
243  for (gui::widget& w : buttons_) {
244  w.hide(hidden);
245  }
246 }
247 
248 
249 template<class Item>
250 bool editor_palette<Item>::is_selected_fg_item(const std::string& id)
251 {
252  return selected_fg_item_ == id;
253 }
254 
255 template<class Item>
256 bool editor_palette<Item>::is_selected_bg_item(const std::string& id)
257 {
258  return selected_bg_item_ == id;
259 }
260 
261 template<class Item>
263 {
264  if (!dirty()) {
265  return;
266  }
267 
268  toolkit_.set_mouseover_overlay(gui_);
269 
270  std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
271  if(palette_menu_button) {
272  t_string& name = groups_[active_group_index()].name;
273  std::string& icon = groups_[active_group_index()].icon;
274 
275  palette_menu_button->set_tooltip_string(name);
276  palette_menu_button->set_overlay(icon);
277  }
278 
279  // The hotkey system will automatically enable and disable the buttons when it runs, but it doesn't
280  // get triggered when handling mouse-wheel scrolling. Therefore duplicate that functionality here.
281  std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
282  if(upscroll_button)
283  upscroll_button->enable(can_scroll_up());
284  std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
285  if(downscroll_button)
286  downscroll_button->enable(can_scroll_down());
287 
288  for(std::size_t i = 0; i < buttons_.size(); ++i) {
289  const auto item_index = items_start_ + i;
290  gui::tristate_button& tile = buttons_[i];
291 
292  tile.hide(true);
293 
294  // If we've scrolled to the end of the list, or if there aren't many items, leave the button hidden
295  if(item_index >= num_items()) {
296  continue;
297  }
298 
299  const std::string item_id = active_group()[item_index];
300  typename item_map::iterator item = item_map_.find(item_id);
301 
302  texture item_base, item_overlay;
303  std::stringstream tooltip_text;
304  setup_item((*item).second, item_base, item_overlay, tooltip_text);
305  bool is_core = non_core_items_.find(get_id((*item).second)) == non_core_items_.end();
306  if (!is_core) {
307  tooltip_text << " "
308  << _("(non-core)") << "\n"
309  << _("Will not work in game without extra care.");
310  tile.set_tooltip_string(markup::span_color(font::BAD_COLOR, tooltip_text.str()));
311  } else {
312  tile.set_tooltip_string(tooltip_text.str());
313  }
314 
315  tile.set_item_image(item_base, item_overlay);
316  tile.set_item_id(item_id);
317 
318  if (is_selected_bg_item(get_id(item->second))
319  && is_selected_fg_item(get_id(item->second))) {
321  } else if (is_selected_bg_item(get_id(item->second))) {
323  } else if (is_selected_fg_item(get_id(item->second))) {
325  } else {
327  }
328 
329  tile.set_dirty(true);
330  tile.hide(false);
331  }
332 
333  set_dirty(false);
334 }
335 
336 template<class Item>
338 {
339  // This is unnecessary as every GUI1 widget is a TLD.
340  //for(std::size_t i = 0; i < buttons_.size(); ++i) {
341  // gui::tristate_button& tile = buttons_[i];
342  // tile.draw();
343  //}
344 }
345 
346 // Force compilation of the following template instantiations
348 template class editor_palette<const unit_type&>;
349 template class editor_palette<overlay>;
350 
351 } // end namespace editor
void adjust_size(const SDL_Rect &target) override
Update the size of this widget.
virtual bool scroll_up() override
Scroll the editor-palette up one step if possible.
virtual void select_bg_item(const std::string &item_id) override
virtual bool is_selected_bg_item(const std::string &id)
std::size_t num_items() override
Return the number of items in the currently-active group.
virtual bool is_selected_fg_item(const std::string &id)
std::size_t active_group_index()
virtual void draw_contents() override
Called by widget::draw()
void swap() override
For tools which support fg and bg items, exchange the two items.
virtual bool scroll_down() override
Scroll the editor-palette down one step if possible.
virtual bool can_scroll_up() override
virtual void select_fg_item(const std::string &item_id) override
Select a foreground item.
virtual void layout() override
Called by draw_manager to validate layout before drawing.
virtual bool can_scroll_down() override
void hide(bool hidden) override
void expand_palette_groups_menu(std::vector< config > &items, int i) override
Menu expanding for palette group list.
void set_group(std::size_t index) override
virtual sdl_handler_vector handler_members() override
void set_pressed(PRESSED_STATE new_pressed_state)
void set_item_id(const std::string &id)
void set_item_image(const texture &base, const texture &over=texture())
void set_dirty(bool dirty=true)
Definition: widget.cpp:180
virtual void hide(bool value=true)
Definition: widget.cpp:141
void set_tooltip_string(const std::string &str)
Definition: widget.cpp:246
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
Definition: config.cpp:1343
#define ERR_ED
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:182
Declarations for File-IO.
std::size_t i
Definition: function.cpp:1028
int w
static std::string _(const char *str)
Definition: gettext.hpp:93
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:198
Manage the empty-palette in the editor.
Definition: action.cpp:31
utils::optional< std::string > get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type, if it exists.
const color_t BAD_COLOR
std::string img(const std::string &src, const std::string &align, const bool floating)
Definition: markup.cpp:29
std::string span_color(const color_t &color, Args &&... data)
Definition: markup.hpp:68
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:70
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
Stores the info about the groups in a nice format.
#define h
#define b