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