The Battle for Wesnoth  1.19.5+dev
part.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
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  * Storyscreen parts and floating images representation.
19  */
20 
21 #pragma once
22 
23 #include "storyscreen/parser.hpp"
24 
25 #include <chrono>
26 #include <string>
27 #include <vector>
28 
29 class config;
30 class vconfig;
31 
32 namespace storyscreen
33 {
34 /**
35  * Represents and contains information about image labels used
36  * in story screen parts.
37  */
39 {
40 public:
41  /**
42  * WML-based constructor.
43  * @param cfg Object corresponding to a [image] block's contents from
44  * a [part] node.
45  */
46  floating_image(const config& cfg);
47 
48  floating_image(const floating_image& fi) = default;
49  floating_image(floating_image&& fi) = default;
50 
52  {
53  assign(fi);
54  return *this;
55  }
56 
57  std::string file() const
58  {
59  return file_;
60  }
61 
62  /**
63  * Returns the referential X coordinate of the image.
64  * The actual (corrected) value is determined at render time.
65  */
66  int ref_x() const
67  {
68  return x_;
69  }
70 
71  /**
72  * Returns the referential Y coordinate of the image.
73  * The actual (corrected) value is determined at render time.
74  */
75  int ref_y() const
76  {
77  return y_;
78  }
79 
80  /**
81  * If true, the size of the image is changed in the same way that the ref_x
82  * and ref_y are mapped to use the base layer's pixels as the coordinate
83  * system.
84  */
86  {
88  }
89 
90  /**
91  * Whether the image coordinates specify the location of its
92  * center (true) or top-left corner (false).
93  */
94  bool centered() const
95  {
96  return centered_;
97  }
98 
99  /**
100  * Delay after displaying this image and before displaying the next image,
101  * in milliseconds.
102  */
103  const std::chrono::milliseconds& display_delay() const
104  {
105  return delay_;
106  }
107 
108 private:
109  std::string file_;
110  int x_, y_; // referential (non corrected) x,y
111  std::chrono::milliseconds delay_;
113  bool centered_;
114 
115  /** Copy constructor and operator=() implementation details. */
116  void assign(const floating_image& fi);
117 };
118 
120 {
121 public:
123 
124  /**
125  * Constructor. Initializes a background_layer object from a
126  * [background_layer] WML node.
127  */
128  background_layer(const config& cfg);
129 
130  /** Whether the layer should be scaled horizontally. */
131  bool scale_horizontally() const
132  {
133  return scale_horizontally_;
134  }
135 
136  /** Sets whether the layer should be scaled horizontally. */
138  {
140  }
141 
142  /** Whether the layer should be scaled vertically. */
143  bool scale_vertically() const
144  {
145  return scale_vertically_;
146  }
147 
148  /** Sets whether the layer should be scaled vertically. */
150  {
152  }
153 
154  /** Whether the layer should be tiled horizontally. */
155  bool tile_horizontally() const
156  {
157  return tile_horizontally_;
158  }
159 
160  /** Sets whether the layer should be tiled horizontally. */
162  {
164  }
165 
166  /** Whether the layer should be tiled vertically. */
167  bool tile_vertically() const
168  {
169  return tile_vertically_;
170  }
171 
172  /** Sets whether the layer should be tiled vertically. */
174  {
176  }
177 
178  /** Whether the aspect ratio should be preserved while scaling. */
179  bool keep_aspect_ratio() const
180  {
181  return keep_aspect_ratio_;
182  }
183 
184  /** Sets whether the aspect ratio should be preserved. */
186  {
188  }
189 
190  /** Whether is this layer the base layer. */
191  bool is_base_layer() const
192  {
193  return is_base_layer_;
194  }
195 
196  /** Sets whether is this layer a base layer. */
197  void set_base_layer(bool b)
198  {
199  is_base_layer_ = b;
200  }
201 
202  /** The path to the file to load the image from. */
203  const std::string& file() const
204  {
205  return image_file_;
206  }
207 
208  /** Sets the path to the image file. */
209  void set_file(const std::string& str)
210  {
211  image_file_ = str;
212  }
213 
214 private:
221  std::string image_file_;
222 };
223 
224 /**
225  * Represents and contains information about a single storyscreen part.
226  */
227 class part : private story_parser
228 {
229 public:
230  /**
231  * Currently used to indicate where the text block should be placed.
232  * Note that it will always take as much space as it is
233  * possible horizontally.
234  */
236  BLOCK_TOP, /**< Top of the screen. */
237  BLOCK_MIDDLE, /**< Center of the screen. */
238  BLOCK_BOTTOM /**< Bottom of the screen. This is the default. */
239  };
240 
241  /**
242  * Used to signal user actions.
243  */
244  enum RESULT {
245  NEXT, /**< Jump to next story part. */
246  SKIP, /**< Skip all story parts for this set. */
247  QUIT /**< Quit game and go back to main menu. */
248  };
249 
250  /**
251  * Constructs a storyscreen part from a managed WML node.
252  * @param part_cfg Node object which should correspond to a [part] block's contents.
253  */
254  part(const vconfig& part_cfg);
255 
256  /** Whether the story screen title should be displayed or not. */
257  bool show_title() const
258  {
259  return show_title_;
260  }
261 
262  /** Retrieves the story text itself. */
263  const std::string& text() const
264  {
265  return text_;
266  }
267 
268  /** Changes the story text. */
269  void set_text(const std::string& text)
270  {
271  text_ = text;
272  }
273 
274  /** Retrieves the story screen title. */
275  const std::string& title() const
276  {
277  return text_title_;
278  }
279 
280  /** Changes the story screen title. */
281  void set_title(const std::string& title)
282  {
283  text_title_ = title;
284  }
285 
286  /** Retrieves the background music. */
287  const std::string& music() const
288  {
289  return music_;
290  }
291 
292  /** Retrieves a one-time-only sound effect. */
293  const std::string& sound() const
294  {
295  return sound_;
296  }
297 
298  /** Retrieves a voice track. */
299  const std::string& voice() const
300  {
301  return voice_;
302  }
303 
304  /** Retrieves the area of the screen on which the story text is displayed. */
306  {
307  return text_block_loc_;
308  }
309 
310  /** Retrieves the alignment of the story text within the text area. */
311  const std::string& story_text_alignment() const
312  {
313  return text_alignment_;
314  }
315 
316  /** Retrieves the alignment of the title text against the screen. */
317  const std::string& title_text_alignment() const
318  {
319  return title_alignment_;
320  }
321 
322  /** Retrieve any associated floating images for this story screen. */
323  const std::vector<floating_image>& get_floating_images() const
324  {
325  return floating_images_;
326  }
327 
328  /** Retrieve background layers for this story screen. */
329  const std::vector<background_layer>& get_background_layers() const
330  {
331  return background_layers_;
332  }
333 
334 private:
335  /** Inherited from story_parser. */
336  virtual void resolve_wml(const vconfig& cfg) override;
337 
338  /** Inherited from story_parser. */
339  virtual bool resolve_wml_helper(const std::string& key, const vconfig& node) override;
340 
341  static BLOCK_LOCATION string_tblock_loc(const std::string& s);
342 
344  std::string text_;
345  std::string text_title_;
347  std::string text_alignment_;
348  std::string title_alignment_;
349 
350  std::string music_;
351  std::string sound_;
352  std::string voice_;
353 
354  std::vector<background_layer> background_layers_;
355  std::vector<floating_image> floating_images_;
356 };
357 
358 } // end namespace storyscreen
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
void set_scale_horizontally(bool b)
Sets whether the layer should be scaled horizontally.
Definition: part.hpp:137
bool tile_horizontally() const
Whether the layer should be tiled horizontally.
Definition: part.hpp:155
const std::string & file() const
The path to the file to load the image from.
Definition: part.hpp:203
void set_keep_aspect_ratio(bool b)
Sets whether the aspect ratio should be preserved.
Definition: part.hpp:185
void set_tile_vertically(bool b)
Sets whether the layer should be tiled vertically.
Definition: part.hpp:173
void set_file(const std::string &str)
Sets the path to the image file.
Definition: part.hpp:209
bool scale_vertically() const
Whether the layer should be scaled vertically.
Definition: part.hpp:143
bool keep_aspect_ratio() const
Whether the aspect ratio should be preserved while scaling.
Definition: part.hpp:179
bool tile_vertically() const
Whether the layer should be tiled vertically.
Definition: part.hpp:167
bool is_base_layer() const
Whether is this layer the base layer.
Definition: part.hpp:191
bool scale_horizontally() const
Whether the layer should be scaled horizontally.
Definition: part.hpp:131
void set_base_layer(bool b)
Sets whether is this layer a base layer.
Definition: part.hpp:197
void set_tile_horizontally(bool b)
Sets whether the layer should be tiled horizontally.
Definition: part.hpp:161
void set_scale_vertically(bool b)
Sets whether the layer should be scaled vertically.
Definition: part.hpp:149
Represents and contains information about image labels used in story screen parts.
Definition: part.hpp:39
bool centered() const
Whether the image coordinates specify the location of its center (true) or top-left corner (false).
Definition: part.hpp:94
void assign(const floating_image &fi)
Copy constructor and operator=() implementation details.
const std::chrono::milliseconds & display_delay() const
Delay after displaying this image and before displaying the next image, in milliseconds.
Definition: part.hpp:103
int ref_x() const
Returns the referential X coordinate of the image.
Definition: part.hpp:66
int ref_y() const
Returns the referential Y coordinate of the image.
Definition: part.hpp:75
bool resize_with_background() const
If true, the size of the image is changed in the same way that the ref_x and ref_y are mapped to use ...
Definition: part.hpp:85
floating_image(const floating_image &fi)=default
std::chrono::milliseconds delay_
Definition: part.hpp:111
floating_image(const config &cfg)
WML-based constructor.
Definition: part.cpp:28
std::string file() const
Definition: part.hpp:57
floating_image & operator=(const floating_image &fi)
Definition: part.hpp:51
floating_image(floating_image &&fi)=default
Represents and contains information about a single storyscreen part.
Definition: part.hpp:228
std::string voice_
Definition: part.hpp:352
const std::string & sound() const
Retrieves a one-time-only sound effect.
Definition: part.hpp:293
static BLOCK_LOCATION string_tblock_loc(const std::string &s)
Definition: part.cpp:112
BLOCK_LOCATION text_block_loc_
Definition: part.hpp:346
std::string text_title_
Definition: part.hpp:345
const std::vector< floating_image > & get_floating_images() const
Retrieve any associated floating images for this story screen.
Definition: part.hpp:323
std::string text_
Definition: part.hpp:344
std::vector< floating_image > floating_images_
Definition: part.hpp:355
part(const vconfig &part_cfg)
Constructs a storyscreen part from a managed WML node.
Definition: part.cpp:97
const std::string & title() const
Retrieves the story screen title.
Definition: part.hpp:275
std::string title_alignment_
Definition: part.hpp:348
std::vector< background_layer > background_layers_
Definition: part.hpp:354
const std::string & voice() const
Retrieves a voice track.
Definition: part.hpp:299
std::string music_
Definition: part.hpp:350
std::string text_alignment_
Definition: part.hpp:347
std::string sound_
Definition: part.hpp:351
virtual void resolve_wml(const vconfig &cfg) override
Inherited from story_parser.
Definition: part.cpp:125
void set_title(const std::string &title)
Changes the story screen title.
Definition: part.hpp:281
const std::string & story_text_alignment() const
Retrieves the alignment of the story text within the text area.
Definition: part.hpp:311
RESULT
Used to signal user actions.
Definition: part.hpp:244
@ NEXT
Jump to next story part.
Definition: part.hpp:245
@ QUIT
Quit game and go back to main menu.
Definition: part.hpp:247
@ SKIP
Skip all story parts for this set.
Definition: part.hpp:246
bool show_title() const
Whether the story screen title should be displayed or not.
Definition: part.hpp:257
const std::string & title_text_alignment() const
Retrieves the alignment of the title text against the screen.
Definition: part.hpp:317
const std::string & text() const
Retrieves the story text itself.
Definition: part.hpp:263
BLOCK_LOCATION story_text_location() const
Retrieves the area of the screen on which the story text is displayed.
Definition: part.hpp:305
const std::string & music() const
Retrieves the background music.
Definition: part.hpp:287
void set_text(const std::string &text)
Changes the story text.
Definition: part.hpp:269
virtual bool resolve_wml_helper(const std::string &key, const vconfig &node) override
Inherited from story_parser.
Definition: part.cpp:213
const std::vector< background_layer > & get_background_layers() const
Retrieve background layers for this story screen.
Definition: part.hpp:329
BLOCK_LOCATION
Currently used to indicate where the text block should be placed.
Definition: part.hpp:235
@ BLOCK_BOTTOM
Bottom of the screen.
Definition: part.hpp:238
@ BLOCK_MIDDLE
Center of the screen.
Definition: part.hpp:237
@ BLOCK_TOP
Top of the screen.
Definition: part.hpp:236
bool show_title_
Definition: part.hpp:343
Small helper class to encapsulate the common logic for parsing storyscreen WML.
Definition: parser.hpp:29
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
static map_location::direction s
#define b