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