The Battle for Wesnoth  1.15.12+dev
part.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Iris Morelle <shadowm2006@gmail.com>
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  * Storyscreen parts and floating images representation.
18  */
19 
20 #include "storyscreen/part.hpp"
21 
22 #include "config.hpp"
23 #include "variable.hpp"
24 
25 namespace storyscreen
26 {
28  : file_(cfg["file"])
29  , x_(cfg["x"])
30  , y_(cfg["y"])
31  , delay_(cfg["delay"])
32  , resize_with_background_(cfg["resize_with_background"].to_bool())
33  , centered_(cfg["centered"].to_bool())
34 {
35 }
36 
38  : scale_horizontally_(true)
39  , scale_vertically_(true)
40  , tile_horizontally_(false)
41  , tile_vertically_(false)
42  , keep_aspect_ratio_(true)
43  , is_base_layer_(false)
44  , image_file_()
45 {
46 }
47 
49  : scale_horizontally_(true)
50  , scale_vertically_(true)
51  , tile_horizontally_(false)
52  , tile_vertically_(false)
53  , keep_aspect_ratio_(true)
54  , is_base_layer_(false)
55  , image_file_()
56 {
57  if(cfg.has_attribute("image")) {
58  image_file_ = cfg["image"].str();
59  }
60 
61  if(cfg.has_attribute("scale")) {
62  scale_vertically_ = cfg["scale"].to_bool(true);
63  scale_horizontally_ = cfg["scale"].to_bool(true);
64  } else {
65  if(cfg.has_attribute("scale_vertically")) {
66  scale_vertically_ = cfg["scale_vertically"].to_bool(true);
67  }
68 
69  if(cfg.has_attribute("scale_horizontally")) {
70  scale_horizontally_ = cfg["scale_horizontally"].to_bool(true);
71  }
72  }
73 
74  if(cfg.has_attribute("tile")) {
75  tile_vertically_ = cfg["tile"].to_bool(false);
76  tile_horizontally_ = cfg["tile"].to_bool(false);
77  } else {
78  if(cfg.has_attribute("tile_vertically")) {
79  tile_vertically_ = cfg["tile_vertically"].to_bool(false);
80  }
81 
82  if(cfg.has_attribute("tile_horizontally")) {
83  tile_horizontally_ = cfg["tile_horizontally"].to_bool(false);
84  }
85  }
86 
87  if(cfg.has_attribute("keep_aspect_ratio")) {
88  keep_aspect_ratio_ = cfg["keep_aspect_ratio"].to_bool(true);
89  }
90 
91  if(cfg.has_attribute("base_layer")) {
92  is_base_layer_ = cfg["base_layer"].to_bool(false);
93  }
94 }
95 
96 part::part(const vconfig& part_cfg)
97  : show_title_()
98  , text_()
99  , text_title_()
100  , text_block_loc_(part::BLOCK_BOTTOM)
101  , text_alignment_("left")
102  , title_alignment_("left")
103  , music_()
104  , sound_()
105  , background_layers_()
106  , floating_images_()
107 {
108  resolve_wml(part_cfg);
109 }
110 
112 {
113  if(s.empty() != true) {
114  if(s == "top") {
115  return part::BLOCK_TOP;
116  } else if(s == "middle") {
117  return part::BLOCK_MIDDLE;
118  }
119  }
120 
121  return part::BLOCK_BOTTOM;
122 }
123 
124 void part::resolve_wml(const vconfig& cfg)
125 {
126  if(cfg.null()) {
127  return;
128  }
129 
130  // Converts shortcut syntax to members of [background_layer]
131  background_layer bl;
132 
133  if(cfg.has_attribute("background")) {
134  bl.set_file(cfg["background"].str());
135  }
136 
137  if(cfg.has_attribute("scale_background")) {
138  bl.set_scale_horizontally(cfg["scale_background"].to_bool(true));
139  bl.set_scale_vertically(cfg["scale_background"].to_bool(true));
140  } else {
141  if(cfg.has_attribute("scale_background_vertically")) {
142  bl.set_scale_vertically(cfg["scale_background_vertically"].to_bool(true));
143  }
144 
145  if(cfg.has_attribute("scale_background_horizontally")) {
146  bl.set_scale_horizontally(cfg["scale_background_horizontally"].to_bool(true));
147  }
148  }
149 
150  if(cfg.has_attribute("tile_background")) {
151  bl.set_tile_horizontally(cfg["tile_background"].to_bool(false));
152  bl.set_tile_vertically(cfg["tile_background"].to_bool(false));
153  } else {
154  if(cfg.has_attribute("tile_background_vertically")) {
155  bl.set_tile_vertically(cfg["tile_background_vertically"].to_bool(false));
156  }
157 
158  if(cfg.has_attribute("tile_background_horizontally")) {
159  bl.set_tile_vertically(cfg["tile_background_horizontally"].to_bool(false));
160  }
161  }
162 
163  if(cfg.has_attribute("keep_aspect_ratio")) {
164  bl.set_keep_aspect_ratio(cfg["keep_aspect_ratio"].to_bool(true));
165  }
166 
167  background_layers_.push_back(bl);
168 
169  if(cfg.has_attribute("show_title")) {
170  show_title_ = cfg["show_title"].to_bool();
171  }
172 
173  if(cfg.has_attribute("story")) {
174  text_ = cfg["story"].str();
175  }
176 
177  if(cfg.has_attribute("title")) {
178  text_title_ = cfg["title"].str();
179  if(!cfg.has_attribute("show_title")) {
180  show_title_ = true;
181  }
182  }
183 
184  if(cfg.has_attribute("text_layout")) {
185  text_block_loc_ = string_tblock_loc(cfg["text_layout"]);
186  }
187 
188  if(cfg.has_attribute("text_alignment")) {
189  text_alignment_ = cfg["text_alignment"].str();
190  }
191 
192  if(cfg.has_attribute("title_alignment")) {
193  title_alignment_ = cfg["title_alignment"].str();
194  }
195 
196  if(cfg.has_attribute("music")) {
197  music_ = cfg["music"].str();
198  }
199 
200  if(cfg.has_attribute("sound")) {
201  sound_ = cfg["sound"].str();
202  }
203 
204  if(cfg.has_attribute("voice")) {
205  voice_ = cfg["voice"].str();
206  }
207 
208  // Inherited
210 }
211 
212 bool part::resolve_wml_helper(const std::string& key, const vconfig& node)
213 {
214  bool found = false;
215 
216  // [background_layer]
217  if(key == "background_layer") {
218  background_layers_.push_back(node.get_parsed_config());
219  found = true;
220  }
221  // [image]
222  else if(key == "image") {
223  floating_images_.push_back(node.get_parsed_config());
224  found = true;
225  }
226 
227  return found;
228 }
229 
230 } // end namespace storyscreen
std::string music_
Definition: part.hpp:349
void set_keep_aspect_ratio(bool b)
Sets whether the aspect ratio should be preserved.
Definition: part.hpp:184
Center of the screen.
Definition: part.hpp:236
Storyscreen parts and floating images representation.
bool has_attribute(config_key_type key) const
Definition: config.cpp:207
bool has_attribute(const std::string &key) const
< Synonym for operator[]
Definition: variable.hpp:99
std::string title_alignment_
Definition: part.hpp:347
std::string text_
Definition: part.hpp:343
std::string voice_
Definition: part.hpp:351
Bottom of the screen.
Definition: part.hpp:237
Definitions for the interface to Wesnoth Markup Language (WML).
void set_tile_vertically(bool b)
Sets whether the layer should be tiled vertically.
Definition: part.hpp:172
virtual void resolve_wml(const vconfig &cfg)
Takes care of initializing and branching properties.
Definition: parser.cpp:29
BLOCK_LOCATION text_block_loc_
Definition: part.hpp:345
void set_file(const std::string &str)
Sets the path to the image file.
Definition: part.hpp:208
void set_tile_horizontally(bool b)
Sets whether the layer should be tiled horizontally.
Definition: part.hpp:160
virtual bool resolve_wml_helper(const std::string &key, const vconfig &node) override
Inherited from story_parser.
Definition: part.cpp:212
config get_parsed_config() const
Definition: variable.cpp:176
std::vector< background_layer > background_layers_
Definition: part.hpp:353
Represents and contains information about a single storyscreen part.
Definition: part.hpp:226
std::vector< floating_image > floating_images_
Definition: part.hpp:354
std::string sound_
Definition: part.hpp:350
static BLOCK_LOCATION string_tblock_loc(const std::string &s)
Definition: part.cpp:111
std::string text_title_
Definition: part.hpp:344
Top of the screen.
Definition: part.hpp:235
static map_location::DIRECTION s
bool show_title_
Definition: part.hpp:342
virtual void resolve_wml(const vconfig &cfg) override
Inherited from story_parser.
Definition: part.cpp:124
std::string text_alignment_
Definition: part.hpp:346
floating_image(const config &cfg)
WML-based constructor.
Definition: part.cpp:27
part(const vconfig &part_cfg)
Constructs a storyscreen part from a managed WML node.
Definition: part.cpp:96
A variable-expanding proxy for the config class.
Definition: variable.hpp:44
void set_scale_horizontally(bool b)
Sets whether the layer should be scaled horizontally.
Definition: part.hpp:136
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
bool null() const
Definition: variable.hpp:72
void set_scale_vertically(bool b)
Sets whether the layer should be scaled vertically.
Definition: part.hpp:148
BLOCK_LOCATION
Currently used to indicate where the text block should be placed.
Definition: part.hpp:234