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