The Battle for Wesnoth  1.15.0+dev
outro.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 
17 #include "gui/dialogs/outro.hpp"
18 
19 #include "formula/variant.hpp"
20 #include "gettext.hpp"
22 #include "gui/core/timer.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/window.hpp"
25 
26 namespace gui2
27 {
28 namespace dialogs
29 {
30 
31 REGISTER_DIALOG(outro)
32 
33 outro::outro(const std::string& text, unsigned int duration)
34  : text_(text)
35  , duration_(duration)
36  , fade_step_(0)
37  , fading_in_(true)
38  , timer_id_(0)
39  , next_draw_(0)
40 {
41  if(text_.empty()) {
42  text_ = _("The End");
43  }
44 
45  if(!duration_) {
46  duration_ = 3500;
47  }
48 }
49 
51 {
52  window.set_enter_disabled(true);
53  window.get_canvas(0).set_variable("outro_text", wfl::variant(text_));
54 
55  connect_signal_on_draw(window, std::bind(&outro::draw_callback, this, std::ref(window)));
56 
57  set_next_draw();
58 }
59 
61 {
62  /* The UI is rendered at approximately 50 FPS - 1 frame every 20 ms - meaning fading progresses every 3 frames.
63  * TODO: not sure if 60 is a better value in that case?
64  */
65  next_draw_ = SDL_GetTicks() + 60;
66 }
67 
69 {
70  if(SDL_GetTicks() < next_draw_) {
71  return;
72  }
73 
74  /* If we've faded fully in...
75  *
76  * NOTE: we want fading to take around half a second. Given this function runs about every 3 frames, we
77  * limit ourselves to a reasonable 10 fade steps with an alpha difference (rounded up) of 25.5 each cycle.
78  * The actual calculation for alpha is done in the window definition in WFL.
79  */
80  if(fading_in_ && fade_step_ > 10) {
81  // Schedule the fadeout after the provided delay.
82  if(timer_id_ == 0) {
83  timer_id_ = add_timer(duration_, [this](std::size_t) { fading_in_ = false; });
84  }
85 
86  return;
87  }
88 
89  // If we've faded fully out...
90  if(!fading_in_ && fade_step_ < 0) {
91  window.close();
92  return;
93  }
94 
95  canvas& window_canvas = window.get_canvas(0);
96 
97  window_canvas.set_variable("fade_step", wfl::variant(fade_step_));
98  window_canvas.set_is_dirty(true);
99 
100  window.set_is_dirty(true);
101 
102  if(fading_in_) {
103  fade_step_ ++;
104  } else {
105  fade_step_ --;
106  }
107 
108  set_next_draw();
109 }
110 
111 void outro::post_show(window& /*window*/)
112 {
114  timer_id_ = 0;
115 }
116 
117 } // namespace dialogs
118 } // namespace gui2
std::size_t next_draw_
Definition: outro.hpp:63
void close()
Requests to close the window.
Definition: window.hpp:183
unsigned int duration_
Definition: outro.hpp:57
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:171
This file contains the window object, this object is a top level container which has the event manage...
STL namespace.
std::size_t timer_id_
Definition: outro.hpp:62
Generic file dialog.
Definition: field-fwd.hpp:22
void set_next_draw()
Definition: outro.cpp:60
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:463
std::string text_
Definition: outro.hpp:55
Various uncategorised dialogs.
A simple canvas which can be drawn upon.
Definition: canvas.hpp:41
Dialog to display &#39;The End&#39; at the end of a campaign.
Definition: outro.hpp:25
void set_is_dirty(const bool is_dirty)
Definition: canvas.hpp:178
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
void draw_callback(window &window)
Definition: outro.cpp:68
canvas & get_canvas(const unsigned index)
virtual void post_show(window &window) override
Inherited from modal_dialog.
Definition: outro.cpp:111
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: outro.cpp:50
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