The Battle for Wesnoth
1.15.2+dev
gui
dialogs
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
"
21
#include "
gui/auxiliary/find_widget.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
50
void
outro::pre_show
(
window
&
window
)
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
60
void
outro::set_next_draw
()
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
68
void
outro::draw_callback
(
window
&
window
)
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
{
113
remove_timer
(
timer_id_
);
114
timer_id_
= 0;
115
}
116
117
}
// namespace dialogs
118
}
// namespace gui2
gui2::dialogs::outro::next_draw_
std::size_t next_draw_
Definition:
outro.hpp:63
gui2::window::close
void close()
Requests to close the window.
Definition:
window.hpp:182
REGISTER_DIALOG
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
Definition:
modal_dialog.hpp:89
gettext.hpp
find_widget.hpp
wfl::variant
Definition:
variant.hpp:27
gui2::dialogs::outro::duration_
unsigned int duration_
Definition:
outro.hpp:57
gui2::canvas::set_variable
void set_variable(const std::string &key, const wfl::variant &value)
Definition:
canvas.hpp:171
window.hpp
This file contains the window object, this object is a top level container which has the event manage...
std
STL namespace.
gui2::dialogs::outro::timer_id_
std::size_t timer_id_
Definition:
outro.hpp:62
gui2
Generic file dialog.
Definition:
field-fwd.hpp:22
gui2::dialogs::outro::set_next_draw
void set_next_draw()
Definition:
outro.cpp:60
_
static UNUSEDNOWARN std::string _(const char *str)
Definition:
gettext.hpp:91
settings.hpp
This file contains the settings handling of the widget library.
gui2::widget::set_is_dirty
void set_is_dirty(const bool is_dirty)
Definition:
widget.cpp:463
variant.hpp
gui2::dialogs::outro::fading_in_
bool fading_in_
Definition:
outro.hpp:60
gui2::dialogs::outro::text_
std::string text_
Definition:
outro.hpp:55
dialogs
Various uncategorised dialogs.
gui2::canvas
A simple canvas which can be drawn upon.
Definition:
canvas.hpp:41
gui2::dialogs::outro
Dialog to display 'The End' at the end of a campaign.
Definition:
outro.hpp:25
gui2::canvas::set_is_dirty
void set_is_dirty(const bool is_dirty)
Definition:
canvas.hpp:178
timer.hpp
Contains the gui2 timer routines.
gui2::add_timer
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
gui2::dialogs::outro::draw_callback
void draw_callback(window &window)
Definition:
outro.cpp:68
gui2::styled_widget::get_canvas
canvas & get_canvas(const unsigned index)
Definition:
styled_widget.hpp:255
gui2::dialogs::outro::fade_step_
int fade_step_
Definition:
outro.hpp:58
gui2::dialogs::outro::post_show
virtual void post_show(window &window) override
Inherited from modal_dialog.
Definition:
outro.cpp:111
gui2::window
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition:
window.hpp:62
gui2::dialogs::outro::pre_show
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition:
outro.cpp:50
outro.hpp
gui2::event::connect_signal_on_draw
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
gui2::remove_timer
bool remove_timer(const std::size_t id)
Removes a timer.
Definition:
timer.cpp:167
gui2::window::set_enter_disabled
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition:
window.hpp:286
Generated by
1.8.13