/
outro.cpp
106 lines (85 loc) · 2.47 KB
/
outro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright (C) 2017 by Charles Dang <exodia339@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/outro.hpp"
#include "formula/variant.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/timer.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(outro)
outro::outro(const std::string& text, unsigned int duration)
: text_(text)
, duration_(duration)
, fade_step_(0)
, fading_in_(true)
, timer_id_(0)
, timer_id_secondary_(0)
{
if(text_.empty()) {
text_ = _("The End");
}
if(!duration_) {
duration_ = 3500;
}
}
void outro::pre_show(window& window)
{
window.set_enter_disabled(true);
window.get_canvas(0).set_variable("outro_text", wfl::variant(text_));
timer_id_ = add_timer(50, std::bind(&outro::timer_callback, this, std::ref(window)), true);
}
void outro::timer_callback(window& window)
{
/* If we've faded fully in...
*
* NOTE: we want the fade in to happen in around half a second. Given this timer runs every
* 50 ms, we limit ourselves to a reasonable 10 fade steps (500 ms / 50) with an alpha increase
* (rounded up) of 25.5 each cycle. The actual calculation for alpha is done in the window
* definition in WFL.
*/
if(fading_in_ && fade_step_ > 10) {
// Schedule the fadeout after the provided delay.
if(timer_id_secondary_ == 0) {
timer_id_secondary_ = add_timer(duration_, [this](size_t) { fading_in_ = false; });
}
return;
}
// If we've faded fully out...
if(!fading_in_ && fade_step_ < 0) {
window.close();
return;
}
canvas& window_canvas = window.get_canvas(0);
window_canvas.set_variable("fade_step", wfl::variant(fade_step_));
window_canvas.set_is_dirty(true);
window.set_is_dirty(true);
if(fading_in_) {
fade_step_ ++;
} else {
fade_step_ --;
}
}
void outro::post_show(window& /*window*/)
{
remove_timer(timer_id_);
remove_timer(timer_id_secondary_);
timer_id_ = 0;
timer_id_secondary_ = 0;
}
} // namespace dialogs
} // namespace gui2