The Battle for Wesnoth  1.17.0-dev
theme.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 /**
17  * @file
18  * Definitions related to theme-support.
19  */
20 
21 #pragma once
22 
23 #include "color.hpp"
24 #include "config.hpp"
25 #include "generic_event.hpp"
26 
27 #include <memory>
28 #include <SDL2/SDL_rect.h>
29 
30 class game_config_view;
31 
32 struct _rect { std::size_t x1,y1,x2,y2; };
33 
34 struct theme_info
35 {
36  std::string id;
39 };
40 
41 class theme
42 {
43 
44  class object
45  {
46  public:
47  object();
48  object(std::size_t sw, std::size_t sh, const config& cfg);
49  virtual ~object() { }
50 
51  virtual SDL_Rect& location(const SDL_Rect& screen) const;
52  const SDL_Rect& get_location() const { return loc_; }
53  const std::string& get_id() const { return id_; }
54 
55  // This supports relocating of theme elements ingame.
56  // It is needed for [change] tags in theme WML.
57  void modify_location(const _rect& rect);
58  void modify_location(std::string rect_str, SDL_Rect rect_ref);
59 
60  // All on-screen objects have 'anchoring' in the x and y dimensions.
61  // 'fixed' means that they have fixed co-ordinates and don't move.
62  // 'top anchored' means they are anchored to the top (or left) side
63  // of the screen - the top (or left) edge stays a constant distance
64  // from the top of the screen.
65  // 'bottom anchored' is the inverse of top anchored.
66  // 'proportional' means the location and dimensions change
67  // proportionally to the screen size.
68  enum ANCHORING { FIXED, TOP_ANCHORED, PROPORTIONAL, BOTTOM_ANCHORED };
69 
70  private:
72  std::string id_;
73  SDL_Rect loc_;
74  mutable SDL_Rect relative_loc_;
75  mutable SDL_Rect last_screen_;
76 
77  ANCHORING xanchor_, yanchor_;
78  std::size_t spec_width_, spec_height_;
79 
80  static ANCHORING read_anchor(const std::string& str);
81  };
82 
83  struct border_t
84  {
85 
86  border_t();
87  border_t(const config& cfg);
88 
89  double size;
90 
91  std::string background_image;
92  std::string tile_image;
93 
95  };
96 
97 public:
98 
99  class label : public object
100  {
101  public:
102  label();
103  explicit label(std::size_t sw, std::size_t sh, const config& cfg);
104 
105  using object::location;
106 
107  const std::string& text() const { return text_; }
108  void set_text(const std::string& text) { text_ = text; }
109  const std::string& icon() const { return icon_; }
110 
111  bool empty() const { return text_.empty() && icon_.empty(); }
112 
113  std::size_t font_size() const { return font_; }
114  color_t font_rgb() const { return font_rgb_; }
115  bool font_rgb_set() const { return font_rgb_set_; }
116  private:
117  std::string text_, icon_;
118  std::size_t font_;
121  };
122 
123  class status_item : public object
124  {
125  public:
126 
127  explicit status_item(std::size_t sw, std::size_t sh, const config& cfg);
128 
129  using object::location;
130 
131  const std::string& prefix() const { return prefix_; }
132  const std::string& postfix() const { return postfix_; }
133 
134  // If the item has a label associated with it, Show where the label is
135  const label* get_label() const { return label_.empty() ? nullptr : &label_; }
136 
137  std::size_t font_size() const { return font_; }
138  color_t font_rgb() const { return font_rgb_; }
139  bool font_rgb_set() const { return font_rgb_set_; }
140 
141  private:
142  std::string prefix_, postfix_;
144  std::size_t font_;
147  };
148 
149  class panel : public object
150  {
151  public:
152  explicit panel(std::size_t sw, std::size_t sh, const config& cfg);
153 
154  using object::location;
155 
156  const std::string& image() const { return image_; }
157 
158  private:
159  std::string image_;
160  };
161 
162  class action : public object
163  {
164  public:
165  action();
166  explicit action(std::size_t sw, std::size_t sh, const config& cfg);
167 
168  using object::location;
169 
170  bool is_context() const { return context_; }
171 
172  const std::string& title() const { return title_; }
173 
174  const std::string tooltip(std::size_t index) const;
175 
176  const std::string& type() const { return type_; }
177 
178  const std::string& image() const { return image_; }
179 
180  const std::string& overlay() const { return overlay_; }
181 
182  const std::vector<std::string>& items() const { return items_; }
183 
184  void set_title(const std::string& new_title) { title_ = new_title; }
185  private:
186  bool context_, auto_tooltip_, tooltip_name_prepend_;
187  std::string title_, tooltip_, image_, overlay_, type_;
188  std::vector<std::string> items_;
189  };
190 
191  class slider : public object
192  {
193  public:
194  slider();
195  explicit slider(std::size_t sw, std::size_t sh, const config& cfg);
196 
197  using object::location;
198 
199  const std::string& title() const { return title_; }
200 
201  const std::string& tooltip() const { return tooltip_; }
202 
203  const std::string& image() const { return image_; }
204 
205  const std::string& overlay() const { return overlay_; }
206 
207  bool black_line() const { return black_line_; }
208 
209  void set_title(const std::string& new_title) { title_ = new_title; }
210  private:
211  std::string title_, tooltip_, image_, overlay_;
213  };
214 
215  class menu : public object
216  {
217  public:
218  menu();
219  explicit menu(std::size_t sw, std::size_t sh, const config& cfg);
220 
221  using object::location;
222 
223  bool is_button() const { return button_; }
224 
225  bool is_context() const { return context_; }
226 
227  const std::string& title() const { return title_; }
228 
229  const std::string& tooltip() const { return tooltip_; }
230 
231  const std::string& image() const { return image_; }
232 
233  const std::string& overlay() const { return overlay_; }
234 
235  const std::vector<config>& items() const { return items_; }
236 
237  void set_title(const std::string& new_title) { title_ = new_title; }
238  private:
239  bool button_;
240  bool context_;
241  std::string title_, tooltip_, image_, overlay_;
242  std::vector<config> items_;
243  };
244 
245  explicit theme(const config& cfg, const SDL_Rect& screen);
246  theme(const theme&) = delete;
247  theme& operator=(const theme&) = delete;
248  theme& operator=(theme&&);
249 
250  bool set_resolution(const SDL_Rect& screen);
251  void modify(const config &cfg);
252 
253  const std::vector<panel>& panels() const { return panels_; }
254  const std::vector<label>& labels() const { return labels_; }
255  const std::vector<menu>& menus() const { return menus_; }
256  const std::vector<slider>& sliders() const { return sliders_; }
257  const std::vector<action>& actions() const { return actions_; }
258 
259  const menu* context_menu() const
260  { return context_.is_context() ? &context_ : nullptr; }
261 
262  //refresh_title2 changes the title of a menu entry, identified by id.
263  //If no menu entry is found, an empty menu object is returned.
264  object* refresh_title(const std::string& id, const std::string& new_title);
265  object* refresh_title2(const std::string& id, const std::string& title_tag);
266  void modify_label(const std::string& id, const std::string& text);
267 
268  const status_item* get_status_item(const std::string& item) const;
269  const menu *get_menu_item(const std::string &key) const;
270  const action* get_action_item(const std::string &key) const;
271 
272  const SDL_Rect& main_map_location(const SDL_Rect& screen) const
273  { return main_map_.location(screen); }
274  const SDL_Rect& mini_map_location(const SDL_Rect& screen) const
275  { return mini_map_.location(screen); }
276  const SDL_Rect& unit_image_location(const SDL_Rect& screen) const
277  { return unit_image_.location(screen); }
278  const SDL_Rect& palette_location(const SDL_Rect& screen) const
279  { return palette_.location(screen); }
280 
281  static void set_known_themes(const game_config_view* cfg);
282  static std::vector<theme_info> get_known_themes();
283 
284  const border_t& border() const { return border_; }
285 
286  events::generic_event& theme_reset_event() { return theme_reset_event_; }
287 
288 private:
289  theme::object& find_element(const std::string& id);
290  void add_object(std::size_t sw, std::size_t sh, const config& cfg);
291  void remove_object(const std::string& id);
292  void set_object_location(theme::object& element, std::string rect_str, std::string ref_id);
293 
294  //notify observers that the theme has been rebuilt completely
295  //atm this is used for replay_controller to add replay controls to the standard theme
297 
298  static std::map<std::string, config> known_themes;
299  std::string cur_theme;
301  std::vector<panel> panels_;
302  std::vector<label> labels_;
303  std::vector<menu> menus_;
304  std::vector<action> actions_;
305  std::vector<slider> sliders_;
306 
309 
310  std::map<std::string, std::unique_ptr<status_item>> status_;
311 
312  object main_map_, mini_map_, unit_image_, palette_;
313 
315 
317  std::size_t cur_spec_width_, cur_spec_height_;
318 };
std::vector< label > labels_
Definition: theme.hpp:302
std::size_t font_size() const
Definition: theme.hpp:113
std::map< std::string, std::unique_ptr< status_item > > status_
Definition: theme.hpp:310
t_string description
Definition: theme.hpp:38
bool context_
Definition: theme.hpp:240
events::generic_event theme_reset_event_
Definition: theme.hpp:296
const std::string & icon() const
Definition: theme.hpp:109
config cfg_
Definition: theme.hpp:300
std::string cur_theme
Definition: theme.hpp:299
bool empty() const
Definition: theme.hpp:111
void set_title(const std::string &new_title)
Definition: theme.hpp:184
std::size_t x2
Definition: theme.hpp:32
std::size_t x1
Definition: theme.hpp:32
color_t font_rgb() const
Definition: theme.hpp:114
const std::string & overlay() const
Definition: theme.hpp:205
const std::string & image() const
Definition: theme.hpp:231
Definition: theme.hpp:32
bool is_context() const
Definition: theme.hpp:170
const SDL_Rect & get_location() const
Definition: theme.hpp:52
std::vector< action > actions_
Definition: theme.hpp:304
std::string prefix_
Definition: theme.hpp:142
void set_title(const std::string &new_title)
Definition: theme.hpp:209
bool black_line_
Definition: theme.hpp:212
std::string id
Definition: theme.hpp:36
const std::string & title() const
Definition: theme.hpp:227
t_string name
Definition: theme.hpp:37
std::vector< config > items_
Definition: theme.hpp:242
const SDL_Rect & unit_image_location(const SDL_Rect &screen) const
Definition: theme.hpp:276
color_t font_rgb_
Definition: theme.hpp:146
std::vector< panel > panels_
Definition: theme.hpp:301
SDL_Rect last_screen_
Definition: theme.hpp:75
const label * get_label() const
Definition: theme.hpp:135
const std::string & tooltip() const
Definition: theme.hpp:201
std::vector< slider > sliders_
Definition: theme.hpp:305
const SDL_Rect & main_map_location(const SDL_Rect &screen) const
Definition: theme.hpp:272
std::string text_
Definition: theme.hpp:117
const std::vector< label > & labels() const
Definition: theme.hpp:254
Definitions for the interface to Wesnoth Markup Language (WML).
std::string type_
Definition: theme.hpp:187
std::size_t font_
Definition: theme.hpp:118
static std::map< std::string, config > known_themes
Definition: theme.hpp:298
map_location loc_
const std::string & text() const
Definition: theme.hpp:107
std::size_t y1
Definition: theme.hpp:32
const std::string & prefix() const
Definition: theme.hpp:131
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
std::vector< menu > menus_
Definition: theme.hpp:303
std::string theme()
Definition: game.cpp:815
std::size_t spec_width_
Definition: theme.hpp:78
bool font_rgb_set() const
Definition: theme.hpp:115
menu context_
Definition: theme.hpp:307
const std::string & overlay() const
Definition: theme.hpp:233
const std::string & type() const
Definition: theme.hpp:176
const std::string & image() const
Definition: theme.hpp:203
bool is_button() const
Definition: theme.hpp:223
bool button_
Definition: theme.hpp:239
std::string background_image
Definition: theme.hpp:91
events::generic_event & theme_reset_event()
Definition: theme.hpp:286
double size
Definition: theme.hpp:89
bool font_rgb_set() const
Definition: theme.hpp:139
const std::vector< std::string > & items() const
Definition: theme.hpp:182
const std::string & get_id() const
Definition: theme.hpp:53
virtual ~object()
Definition: theme.hpp:49
std::string tooltip_
Definition: theme.hpp:211
std::string image_
Definition: theme.hpp:159
const std::string & title() const
Definition: theme.hpp:199
bool black_line() const
Definition: theme.hpp:207
const border_t & border() const
Definition: theme.hpp:284
bool show_border
Definition: theme.hpp:94
const std::vector< config > & items() const
Definition: theme.hpp:235
const std::string & postfix() const
Definition: theme.hpp:132
std::string tooltip_
Definition: theme.hpp:241
SDL_Rect screen_dimensions_
Definition: theme.hpp:316
Definition: theme.hpp:41
color_t font_rgb() const
Definition: theme.hpp:138
const std::string & image() const
Definition: theme.hpp:156
bool is_context() const
Definition: theme.hpp:225
const std::vector< action > & actions() const
Definition: theme.hpp:257
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:72
static map_location::DIRECTION sw
const std::string & tooltip() const
Definition: theme.hpp:229
const std::vector< slider > & sliders() const
Definition: theme.hpp:256
std::size_t cur_spec_width_
Definition: theme.hpp:317
std::size_t font_
Definition: theme.hpp:144
const std::string & image() const
Definition: theme.hpp:178
bool location_modified_
Definition: theme.hpp:71
bool tooltip_name_prepend_
Definition: theme.hpp:186
const SDL_Rect & mini_map_location(const SDL_Rect &screen) const
Definition: theme.hpp:274
void set_text(const std::string &text)
Definition: theme.hpp:108
const std::string & overlay() const
Definition: theme.hpp:180
const SDL_Rect & palette_location(const SDL_Rect &screen) const
Definition: theme.hpp:278
border_t border_
Definition: theme.hpp:314
std::string tile_image
Definition: theme.hpp:92
const std::string & title() const
Definition: theme.hpp:172
std::size_t y2
Definition: theme.hpp:32
std::vector< std::string > items_
Definition: theme.hpp:188
std::string id_
Definition: theme.hpp:72
const menu * context_menu() const
Definition: theme.hpp:259
std::size_t font_size() const
Definition: theme.hpp:137
object unit_image_
Definition: theme.hpp:312
const std::vector< menu > & menus() const
Definition: theme.hpp:255
color_t font_rgb_
Definition: theme.hpp:120
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
bool font_rgb_set_
Definition: theme.hpp:119
ANCHORING yanchor_
Definition: theme.hpp:77
SDL_Rect relative_loc_
Definition: theme.hpp:74
const std::vector< panel > & panels() const
Definition: theme.hpp:253
void set_title(const std::string &new_title)
Definition: theme.hpp:237
std::string tooltip
Shown when hovering over an entry in the filter&#39;s drop-down list.
Definition: manager.cpp:219
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
action action_context_
Definition: theme.hpp:308
SDL_Rect loc_
Definition: theme.hpp:73