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