The Battle for Wesnoth  1.15.9+dev
story_viewer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by Charles Dang <exodia339@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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "formula/variant.hpp"
21 #include "sdl/point.hpp"
22 #include "gui/core/timer.hpp"
23 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/label.hpp"
26 #include "gui/widgets/settings.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "sound.hpp"
30 #include "variable.hpp"
31 
32 namespace gui2
33 {
34 namespace dialogs
35 {
36 
37 // Helper function to get the canvas shape data for the shading under the title area until
38 // I can figure out how to ensure it always stays on top of the canvas stack.
40 {
41  static config cfg;
42  cfg["x"] = 0;
43  cfg["y"] = 0;
44  cfg["w"] = "(screen_width)";
45  cfg["h"] = "(image_original_height * 2)";
46  cfg["name"] = "dialogs/story_title_decor.png~O(75%)";
47 
48  return cfg;
49 }
50 
51 // Stacked widget layer constants for the text stack.
52 static const unsigned int LAYER_BACKGROUND = 1;
53 static const unsigned int LAYER_TEXT = 2;
54 
56 
57 story_viewer::story_viewer(const std::string& scenario_name, const config& cfg_parsed)
58  : controller_(vconfig(cfg_parsed, true), scenario_name)
59  , part_index_(0)
60  , current_part_(nullptr)
61  , timer_id_(0)
62  , next_draw_(0)
63  , fade_step_(0)
64  , fade_state_(NOT_FADING)
65 {
66  update_current_part_ptr();
67 }
68 
70 {
71  if(timer_id_ != 0) {
73  timer_id_ = 0;
74  }
75 }
76 
78 {
80 }
81 
83 {
84  window.set_enter_disabled(true);
85 
86  // Special callback handle key presses
87  connect_signal_pre_key_press(window, std::bind(&story_viewer::key_press_callback, this, std::placeholders::_5));
88 
89  connect_signal_mouse_left_click(find_widget<button>(&window, "next", false),
91 
92  connect_signal_mouse_left_click(find_widget<button>(&window, "back", false),
94 
96  std::bind(&story_viewer::draw_callback, this));
97 
98  display_part();
99 }
100 
102 {
104 }
105 
107 {
108  static const int VOICE_SOUND_SOURCE_ID = 255;
109  // Update Back button state. Doing this here so it gets called in pre_show too.
110  find_widget<button>(get_window(), "back", false).set_active(part_index_ != 0);
111 
112  //
113  // Music and sound
114  //
115  if(!current_part_->music().empty()) {
116  config music_config;
117  music_config["name"] = current_part_->music();
118  music_config["ms_after"] = 2000;
119  music_config["immediate"] = true;
120 
121  sound::play_music_config(music_config);
122  }
123 
124  if(!current_part_->sound().empty()) {
126  }
127 
128  sound::stop_sound(VOICE_SOUND_SOURCE_ID);
129  if(!current_part_->voice().empty()) {
130  sound::play_sound_positioned(current_part_->voice(), VOICE_SOUND_SOURCE_ID, 0, 0);
131  }
132 
133  config cfg, image;
134 
135  //
136  // Background images
137  //
138  bool has_background = false;
139  config* base_layer = nullptr;
140 
141  for(const auto& layer : current_part_->get_background_layers()) {
142  has_background |= !layer.file().empty();
143 
144  const bool preserve_ratio = layer.keep_aspect_ratio();
145  const bool tile_h = layer.tile_horizontally();
146  const bool tile_v = layer.tile_vertically();
147 
148  // By default, no scaling will be applied.
149  std::string width_formula = "(image_original_width)";
150  std::string height_formula = "(image_original_height)";
151 
152  // Background layers are almost always centered. In case of tiling, we want the full
153  // area in the horizontal or vertical direction, so set the origin to 0 for that axis.
154  // The resize mode will center the original image in the available area first/
155  std::string x_formula;
156  std::string y_formula;
157 
158  if(tile_h) {
159  x_formula = "0";
160  } else {
161  x_formula = "(max(pos, 0) where pos = (width / 2 - image_width / 2))";
162  }
163 
164  if(tile_v) {
165  y_formula = "0";
166  } else {
167  y_formula = "(max(pos, 0) where pos = (height / 2 - image_height / 2))";
168  }
169 
170  if(layer.scale_horizontally() && preserve_ratio) {
171  height_formula = "(min((image_original_height * width / image_original_width), height))";
172  } else if(layer.scale_vertically() || tile_v) {
173  height_formula = "(height)";
174  }
175 
176  if(layer.scale_vertically() && preserve_ratio) {
177  width_formula = "(min((image_original_width * height / image_original_height), width))";
178  } else if(layer.scale_horizontally() || tile_h) {
179  width_formula = "(width)";
180  }
181 
182  image["x"] = x_formula;
183  image["y"] = y_formula;
184  image["w"] = width_formula;
185  image["h"] = height_formula;
186  image["name"] = layer.file();
187  image["resize_mode"] = (tile_h || tile_v) ? "tile_center" : "scale";
188 
189  config& layer_image = cfg.add_child("image", image);
190 
191  if(base_layer == nullptr || layer.is_base_layer()) {
192  base_layer = &layer_image;
193  }
194  }
195 
196  canvas& window_canvas = get_window()->get_canvas(0);
197 
198  /* In order to avoid manually loading the image and calculating the scaling factor, we instead
199  * delegate the task of setting the necessary variables to the canvas once the calculations
200  * have been made internally.
201  *
202  * This sets the necessary values with the data for "this" image when its drawn. If no base
203  * layer was found (which would be the case if no backgrounds were provided at all), simply set
204  * some sane defaults directly.
205  */
206  if(base_layer != nullptr) {
207  (*base_layer)["actions"] = R"((
208  [
209  set_var('base_scale_x', as_decimal(image_width) / as_decimal(image_original_width)),
210  set_var('base_scale_y', as_decimal(image_height) / as_decimal(image_original_height)),
211  set_var('base_origin_x', clip_x),
212  set_var('base_origin_y', clip_y)
213  ]
214  ))";
215  } else {
216  window_canvas.set_variable("base_scale_x", wfl::variant(1));
217  window_canvas.set_variable("base_scale_y", wfl::variant(1));
218  window_canvas.set_variable("base_origin_x", wfl::variant(0));
219  window_canvas.set_variable("base_origin_y", wfl::variant(0));
220  }
221 
222  cfg.add_child("image", get_title_area_decor_config());
223 
224  window_canvas.set_cfg(cfg);
225 
226  // Needed to make the background redraw correctly.
227  window_canvas.set_is_dirty(true);
228  get_window()->set_is_dirty(true);
229 
230  //
231  // Title
232  //
233  label& title_label = find_widget<label>(get_window(), "title", false);
234 
235  std::string title_text = current_part_->title();
236  bool showing_title;
237 
238  if(current_part_->show_title() && !title_text.empty()) {
239  showing_title = true;
240 
241  PangoAlignment title_text_alignment = decode_text_alignment(current_part_->title_text_alignment());
242 
244  title_label.set_text_alignment(title_text_alignment);
245  title_label.set_label(title_text);
246  } else {
247  showing_title = false;
248 
250  }
251 
252  //
253  // Story text
254  //
255  stacked_widget& text_stack = find_widget<stacked_widget>(get_window(), "text_and_control_stack", false);
256 
257  std::string new_panel_mode;
258 
259  switch(current_part_->story_text_location()) {
261  new_panel_mode = "top";
262  break;
264  new_panel_mode = "center";
265  break;
267  new_panel_mode = "bottom";
268  break;
269  }
270 
271  text_stack.set_vertical_alignment(new_panel_mode);
272 
273  /* Set the panel mode control variables.
274  *
275  * We use get_layer_grid here to ensure the widget is always found regardless of
276  * whether the background is visible or not.
277  */
278  canvas& panel_canvas = find_widget<panel>(text_stack.get_layer_grid(LAYER_BACKGROUND), "text_panel", false).get_canvas(0);
279 
280  panel_canvas.set_variable("panel_position", wfl::variant(new_panel_mode));
281  panel_canvas.set_variable("title_present", wfl::variant(static_cast<int>(showing_title))); // cast to 0/1
282 
283  const std::string& part_text = current_part_->text();
284 
285  if(part_text.empty() || !has_background) {
286  // No text or no background for this part, hide the background layer.
287  text_stack.select_layer(LAYER_TEXT);
288  } else if(text_stack.current_layer() != -1) {
289  // If the background layer was previously hidden, re-show it.
290  text_stack.select_layer(-1);
291  }
292 
293  // Convert the story part text alignment types into the Pango equivalents
294  PangoAlignment story_text_alignment = decode_text_alignment(current_part_->story_text_alignment());
295 
296  scroll_label& text_label = find_widget<scroll_label>(get_window(), "part_text", false);
297 
298  text_label.set_text_alignment(story_text_alignment);
299  text_label.set_text_alpha(0);
300  text_label.set_label(part_text);
301 
302  begin_fade_draw(true);
303  // if the previous page was skipped, it is possible that we already have a timer running.
305  //
306  // Floating images (handle this last)
307  //
308  const auto& floating_images = current_part_->get_floating_images();
309 
310  // If we have images to draw, draw the first one now. A new non-repeating timer is added
311  // after every draw to schedule the next one after the specified interval.
312  //
313  // TODO: in the old GUI1 dialog, floating images delayed the appearance of the story panel until
314  // drawing was finished. Might be worth looking into restoring that.
315  if(!floating_images.empty()) {
316  draw_floating_image(floating_images.begin(), part_index_);
317  }
318 }
319 
320 void story_viewer::draw_floating_image(floating_image_list::const_iterator image_iter, int this_part_index)
321 {
322  const auto& images = current_part_->get_floating_images();
323  canvas& window_canvas = get_window()->get_canvas(0);
324 
325  // If the current part has changed or we're out of images to draw, exit the draw loop.
326  while((this_part_index == part_index_) && (image_iter != images.end())) {
327  const auto& floating_image = *image_iter;
328  ++image_iter;
329 
330  std::ostringstream x_ss;
331  std::ostringstream y_ss;
332 
333  // Floating images' locations are scaled by the same factor as the background.
334  x_ss << "(trunc(" << floating_image.ref_x() << " * base_scale_x) + base_origin_x";
335  y_ss << "(trunc(" << floating_image.ref_y() << " * base_scale_y) + base_origin_y";
336 
337  if(floating_image.centered()) {
338  x_ss << " - (image_width / 2)";
339  y_ss << " - (image_height / 2)";
340  }
341 
342  x_ss << ")";
343  y_ss << ")";
344 
345  config image;
346  image["x"] = x_ss.str();
347  image["y"] = y_ss.str();
348 
349  // Width and height don't need to be set unless the image needs to be scaled.
350  if(floating_image.resize_with_background()) {
351  image["w"] = "(image_original_width * base_scale_x)";
352  image["h"] = "(image_original_height * base_scale_y)";
353  }
354 
355  image["name"] = floating_image.file();
356  config cfg{"image", std::move(image)};
357 
358  cfg.add_child("image", std::move(image));
359  window_canvas.append_cfg(std::move(cfg));
360 
361  // Needed to make the background redraw correctly.
362  window_canvas.set_is_dirty(true);
363  get_window()->set_is_dirty(true);
364 
365  // If a delay is specified, schedule the next image draw and break out of the loop.
366  const unsigned int draw_delay = floating_image.display_delay();
367  if(draw_delay != 0) {
368  // This must be a non-repeating timer
369  timer_id_ = add_timer(draw_delay, std::bind(&story_viewer::draw_floating_image, this, image_iter, this_part_index), false);
370  return;
371  }
372  }
373 
374  timer_id_ = 0;
375 }
376 
378 {
379  // If a button is pressed while fading in, abort and set alpha to full opaque.
380  if(fade_state_ == FADING_IN) {
381  halt_fade_draw();
382 
383  // Only set full alpha if Forward was pressed.
384  if(direction == DIR_FORWARD) {
385  find_widget<scroll_label>(get_window(), "part_text", false).set_text_alpha(ALPHA_OPAQUE);
387  return;
388  }
389  }
390 
391  // If a button is pressed while fading out, skip and show next part.
392  if(fade_state_ == FADING_OUT) {
393  display_part();
394  return;
395  }
396 
397  assert(fade_state_ == NOT_FADING);
398 
399  part_index_ = (direction == DIR_FORWARD ? part_index_ + 1 : part_index_ -1);
400 
401  // If we've viewed all the parts, close the dialog.
403  get_window()->close();
404  return;
405  }
406 
407  if(part_index_ < 0) {
408  part_index_ = 0;
409  }
410 
412 
413  begin_fade_draw(false);
414 }
415 
416 void story_viewer::key_press_callback(const SDL_Keycode key)
417 {
418  const bool next_keydown =
419  key == SDLK_SPACE
420  || key == SDLK_RETURN
421  || key == SDLK_KP_ENTER
422  || key == SDLK_RIGHT;
423 
424  const bool back_keydown =
425  key == SDLK_BACKSPACE
426  || key == SDLK_LEFT;
427 
428  if(next_keydown) {
430  } else if(back_keydown) {
432  }
433 }
434 
436 {
437  next_draw_ = SDL_GetTicks() + 20;
438 }
439 
441 {
442  set_next_draw();
443 
444  fade_step_ = fade_in ? 0 : 10;
445  fade_state_ = fade_in ? FADING_IN : FADING_OUT;
446 }
447 
449 {
450  next_draw_ = 0;
451  fade_step_ = -1;
453 }
454 
456 {
457  if(next_draw_ && SDL_GetTicks() < next_draw_) {
458  return;
459  }
460 
461  if(fade_state_ == NOT_FADING) {
462  return;
463  }
464 
465  // If we've faded fully in...
466  if(fade_state_ == FADING_IN && fade_step_ > 10) {
467  halt_fade_draw();
468  return;
469  }
470 
471  // If we've faded fully out...
472  if(fade_state_ == FADING_OUT && fade_step_ < 0) {
473  halt_fade_draw();
474 
475  display_part();
476  return;
477  }
478 
479  unsigned short new_alpha = std::clamp<short>(fade_step_ * 25.5, 0, ALPHA_OPAQUE);
480  find_widget<scroll_label>(get_window(), "part_text", false).set_text_alpha(new_alpha);
481 
482  // The text stack also needs to be marked dirty so the background panel redraws correctly.
484 
485  if(fade_state_ == FADING_IN) {
486  fade_step_ ++;
487  } else if(fade_state_ == FADING_OUT) {
488  fade_step_ --;
489  }
490 
491  set_next_draw();
492 }
493 
495 {
496  find_widget<stacked_widget>(get_window(), "text_and_control_stack", false).set_is_dirty(true);
497 }
498 
499 } // namespace dialogs
500 } // namespace gui2
storyscreen::controller::part_pointer_type current_part_
void close()
Requests to close the window.
Definition: window.hpp:181
Center of the screen.
Definition: part.hpp:236
static const unsigned int LAYER_TEXT
void play_sound_positioned(const std::string &files, int id, int repeats, unsigned int distance)
Definition: sound.cpp:1027
static const unsigned int LAYER_BACKGROUND
grid * get_layer_grid(unsigned int i)
Gets the grid for a specified layer.
void key_press_callback(const SDL_Keycode key)
int draw_delay()
Definition: general.cpp:843
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:169
This file contains the window object, this object is a top level container which has the event manage...
int current_layer() const
Gets the current visible layer number.
virtual void pre_show(window &window) override
Inherited from modal_dialog.
void stop_sound()
Definition: sound.cpp:564
STL namespace.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:63
Bottom of the screen.
Definition: part.hpp:237
void set_cfg(const config &cfg, const bool force=false)
Sets the config.
Definition: canvas.hpp:121
void play_sound(const std::string &files, channel_group group, unsigned int repeats)
Definition: sound.cpp:1020
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void set_label(const t_string &label)
void begin_fade_draw(bool fade_in)
This file contains the settings handling of the widget library.
void append_cfg(const config &cfg)
Appends data to the config.
Definition: canvas.hpp:133
void set_visible(const visibility visible)
Definition: widget.cpp:475
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:171
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
virtual void set_text_alignment(const PangoAlignment text_alignment)
Label showing a text.
virtual void set_text_alignment(const PangoAlignment text_alignment) override
See styled_widget::set_text_alignment.
Various uncategorised dialogs.
A simple canvas which can be drawn upon.
Definition: canvas.hpp:41
static config get_title_area_decor_config()
void set_is_dirty(const bool is_dirty)
Definition: canvas.hpp:176
Top of the screen.
Definition: part.hpp:235
The user set the widget invisible, that means:
Contains the gui2 timer routines.
std::size_t add_timer(const uint32_t interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition: timer.cpp:126
Dialog to view the storyscreen.
void select_layer(const int layer)
Selects and displays a particular layer.
config & add_child(config_key_type key)
Definition: config.cpp:471
part_pointer_type get_part(int index) const
Definition: controller.hpp:40
The user sets the widget visible, that means:
storyscreen::controller controller_
const uint8_t ALPHA_OPAQUE
Definition: color.hpp:48
void connect_signal_pre_key_press(dispatcher &dispatcher, const signal_keyboard_function &signal)
Connects the signal for &#39;snooping&#39; on the keypress.
Definition: dispatcher.cpp:166
void play_music_config(const config &music_node, bool allow_interrupt_current_track, int i)
Definition: sound.cpp:704
A variable-expanding proxy for the config class.
Definition: variable.hpp:44
Functions to load and save images from/to disk.
virtual void set_vertical_alignment(const std::string &alignment)
Sets the horizontal alignment of the widget within its parent grid.
Definition: widget.cpp:281
A stacked widget holds several widgets on top of each other.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:58
canvas & get_canvas(const unsigned index)
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
bool empty() const
Definition: config.cpp:879
void draw_floating_image(floating_image_list::const_iterator image_iter, int this_part_index)
void nav_button_callback(NAV_DIRECTION direction)
void connect_signal_on_draw(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a callback when the widget is drawn.
Definition: dispatcher.cpp:191
bool remove_timer(const std::size_t id)
Removes a timer.
Definition: timer.cpp:167
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:285