The Battle for Wesnoth  1.19.0+dev
theme.hpp
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 /**
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.
70 
71  private:
73  std::string id_;
76  mutable rect last_screen_;
77 
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:
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;
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 
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 
310 
312 
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 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A class grating read only view to a vector of config objects, viewed as one config with all children ...
std::string image_
Definition: theme.hpp:188
std::string title_
Definition: theme.hpp:188
std::string type_
Definition: theme.hpp:188
bool is_context() const
Definition: theme.hpp:171
bool tooltip_name_prepend_
Definition: theme.hpp:187
bool auto_tooltip_
Definition: theme.hpp:187
void set_title(const std::string &new_title)
Definition: theme.hpp:185
const std::string tooltip(std::size_t index) const
Definition: theme.cpp:558
bool context_
Definition: theme.hpp:187
const std::string & overlay() const
Definition: theme.hpp:181
std::vector< std::string > items_
Definition: theme.hpp:189
const std::string & image() const
Definition: theme.hpp:179
std::string tooltip_
Definition: theme.hpp:188
const std::string & type() const
Definition: theme.hpp:177
const std::string & title() const
Definition: theme.hpp:173
std::string overlay_
Definition: theme.hpp:188
const std::vector< std::string > & items() const
Definition: theme.hpp:183
bool font_rgb_set_
Definition: theme.hpp:120
const std::string & text() const
Definition: theme.hpp:108
const std::string & icon() const
Definition: theme.hpp:110
std::size_t font_
Definition: theme.hpp:119
std::size_t font_size() const
Definition: theme.hpp:114
void set_text(const std::string &text)
Definition: theme.hpp:109
bool empty() const
Definition: theme.hpp:112
bool font_rgb_set() const
Definition: theme.hpp:116
color_t font_rgb_
Definition: theme.hpp:121
color_t font_rgb() const
Definition: theme.hpp:115
std::string text_
Definition: theme.hpp:118
std::string icon_
Definition: theme.hpp:118
const std::string & title() const
Definition: theme.hpp:228
bool context_
Definition: theme.hpp:241
const std::string & overlay() const
Definition: theme.hpp:234
const std::string & tooltip() const
Definition: theme.hpp:230
bool is_button() const
Definition: theme.hpp:224
std::string image_
Definition: theme.hpp:242
std::vector< config > items_
Definition: theme.hpp:243
bool is_context() const
Definition: theme.hpp:226
const std::string & image() const
Definition: theme.hpp:232
std::string title_
Definition: theme.hpp:242
void set_title(const std::string &new_title)
Definition: theme.hpp:238
const std::vector< config > & items() const
Definition: theme.hpp:236
bool button_
Definition: theme.hpp:240
std::string overlay_
Definition: theme.hpp:242
std::string tooltip_
Definition: theme.hpp:242
rect last_screen_
Definition: theme.hpp:76
std::size_t spec_height_
Definition: theme.hpp:79
ANCHORING xanchor_
Definition: theme.hpp:78
virtual ~object()
Definition: theme.hpp:50
rect loc_
Definition: theme.hpp:74
virtual rect & location(const SDL_Rect &screen) const
Definition: theme.cpp:317
void modify_location(const _rect &rect)
Definition: theme.cpp:395
bool location_modified_
Definition: theme.hpp:72
const rect & get_location() const
Definition: theme.hpp:53
static ANCHORING read_anchor(const std::string &str)
Definition: theme.cpp:381
const std::string & get_id() const
Definition: theme.hpp:54
std::string id_
Definition: theme.hpp:73
@ BOTTOM_ANCHORED
Definition: theme.hpp:69
@ PROPORTIONAL
Definition: theme.hpp:69
@ TOP_ANCHORED
Definition: theme.hpp:69
rect relative_loc_
Definition: theme.hpp:75
std::size_t spec_width_
Definition: theme.hpp:79
ANCHORING yanchor_
Definition: theme.hpp:78
panel(std::size_t sw, std::size_t sh, const config &cfg)
Definition: theme.cpp:471
std::string image_
Definition: theme.hpp:160
const std::string & image() const
Definition: theme.hpp:157
std::string overlay_
Definition: theme.hpp:212
bool black_line_
Definition: theme.hpp:213
bool black_line() const
Definition: theme.hpp:208
void set_title(const std::string &new_title)
Definition: theme.hpp:210
std::string image_
Definition: theme.hpp:212
const std::string & title() const
Definition: theme.hpp:200
const std::string & tooltip() const
Definition: theme.hpp:202
const std::string & overlay() const
Definition: theme.hpp:206
std::string tooltip_
Definition: theme.hpp:212
std::string title_
Definition: theme.hpp:212
const std::string & image() const
Definition: theme.hpp:204
color_t font_rgb() const
Definition: theme.hpp:139
std::size_t font_size() const
Definition: theme.hpp:138
const std::string & postfix() const
Definition: theme.hpp:133
bool font_rgb_set() const
Definition: theme.hpp:140
color_t font_rgb_
Definition: theme.hpp:147
std::size_t font_
Definition: theme.hpp:145
const std::string & prefix() const
Definition: theme.hpp:132
std::string postfix_
Definition: theme.hpp:143
const label * get_label() const
Definition: theme.hpp:136
status_item(std::size_t sw, std::size_t sh, const config &cfg)
Definition: theme.cpp:449
std::string prefix_
Definition: theme.hpp:143
Definition: theme.hpp:43
object main_map_
Definition: theme.hpp:309
theme::object & find_element(const std::string &id)
Definition: theme.cpp:863
std::vector< panel > panels_
Definition: theme.hpp:298
void add_object(std::size_t sw, std::size_t sh, const config &cfg)
Definition: theme.cpp:677
std::vector< action > actions_
Definition: theme.hpp:301
std::map< std::string, std::unique_ptr< status_item > > status_
Definition: theme.hpp:307
const border_t & border() const
Definition: theme.hpp:282
static const config & get_theme_config(const std::string &id)
Returns the saved config for the theme with the given ID.
Definition: theme.cpp:1003
object mini_map_
Definition: theme.hpp:309
theme(const theme &)=delete
const rect & unit_image_location(const SDL_Rect &screen) const
Definition: theme.hpp:277
object unit_image_
Definition: theme.hpp:309
const action * get_action_item(const std::string &key) const
Definition: theme.cpp:926
object * refresh_title(const std::string &id, const std::string &new_title)
Definition: theme.cpp:935
events::generic_event & theme_reset_event()
Definition: theme.hpp:284
const menu * get_menu_item(const std::string &key) const
Definition: theme.cpp:917
menu context_
Definition: theme.hpp:304
void remove_object(const std::string &id)
Definition: theme.cpp:763
object * refresh_title2(const std::string &id, const std::string &title_tag)
Definition: theme.cpp:956
void modify(const config &cfg)
Definition: theme.cpp:819
border_t border_
Definition: theme.hpp:311
void set_object_location(theme::object &element, std::string rect_str, std::string ref_id)
Definition: theme.cpp:805
theme & operator=(const theme &)=delete
static std::map< std::string, config > known_themes
Definition: theme.hpp:316
const std::vector< action > & actions() const
Definition: theme.hpp:258
std::vector< menu > menus_
Definition: theme.hpp:300
std::size_t cur_spec_height_
Definition: theme.hpp:314
std::vector< slider > sliders_
Definition: theme.hpp:302
std::size_t cur_spec_width_
Definition: theme.hpp:314
const std::vector< menu > & menus() const
Definition: theme.hpp:256
static std::vector< theme_info > get_basic_theme_info(bool include_hidden=false)
Returns minimal info about saved themes, optionally including hidden ones.
Definition: theme.cpp:987
const rect & mini_map_location(const SDL_Rect &screen) const
Definition: theme.hpp:275
action action_context_
Definition: theme.hpp:305
const std::vector< panel > & panels() const
Definition: theme.hpp:254
const rect & main_map_location(const SDL_Rect &screen) const
Definition: theme.hpp:273
const std::vector< slider > & sliders() const
Definition: theme.hpp:257
void modify_label(const std::string &id, const std::string &text)
Definition: theme.cpp:967
std::vector< label > labels_
Definition: theme.hpp:299
config cfg_
Definition: theme.hpp:297
theme & operator=(theme &&)
SDL_Rect screen_dimensions_
Definition: theme.hpp:313
bool set_resolution(const SDL_Rect &screen)
Definition: theme.cpp:604
static void set_known_themes(const game_config_view *cfg)
Copies the theme configs from the main game config.
Definition: theme.cpp:977
object palette_
Definition: theme.hpp:309
const rect & palette_location(const SDL_Rect &screen) const
Definition: theme.hpp:279
events::generic_event theme_reset_event_
Definition: theme.hpp:294
const status_item * get_status_item(const std::string &item) const
Definition: theme.cpp:908
theme(const config &cfg, const SDL_Rect &screen)
Definition: theme.cpp:579
const menu * context_menu() const
Definition: theme.hpp:260
std::string cur_theme
Definition: theme.hpp:296
const std::vector< label > & labels() const
Definition: theme.hpp:255
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
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
Contains the SDL_Rect helper code.
Definition: theme.hpp:33
std::size_t y2
Definition: theme.hpp:33
std::size_t y1
Definition: theme.hpp:33
std::size_t x2
Definition: theme.hpp:33
std::size_t x1
Definition: theme.hpp:33
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
double size
Definition: theme.hpp:90
std::string tile_image
Definition: theme.hpp:93
std::string background_image
Definition: theme.hpp:92
bool show_border
Definition: theme.hpp:95
t_string name
Definition: theme.hpp:38
t_string description
Definition: theme.hpp:39
std::string id
Definition: theme.hpp:37
static map_location::DIRECTION sw