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