1597.2.2
by Marco Trevisan (Treviño)
FadeController: new class for handling the fading of FadableObjects |
1 |
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
|
2 |
/*
|
|
3 |
* Copyright (C) 2011 Canonical Ltd
|
|
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 version 3 as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
*
|
|
17 |
* Authored by: Marco Trevisan (Treviño) <mail@3v1n0.net>
|
|
18 |
*/
|
|
19 |
||
1597.2.7
by Marco Trevisan (Treviño)
Remove FadeController, add a more generic Animator class |
20 |
#include "Animator.h" |
1597.2.2
by Marco Trevisan (Treviño)
FadeController: new class for handling the fading of FadableObjects |
21 |
|
22 |
namespace unity |
|
23 |
{
|
|
24 |
||
1597.2.7
by Marco Trevisan (Treviño)
Remove FadeController, add a more generic Animator class |
25 |
Animator::Animator(unsigned int rate, unsigned int duration) |
26 |
{
|
|
27 |
_start_time = 0; |
|
28 |
_timeout_id = 0; |
|
29 |
_progress = 0.0f; |
|
30 |
_start_progress = 0.0f; |
|
31 |
_rate = 1; |
|
32 |
_duration = 0; |
|
33 |
||
34 |
SetDuration(duration); |
|
35 |
SetRate(rate); |
|
36 |
}
|
|
37 |
||
38 |
Animator::~Animator() |
|
39 |
{
|
|
40 |
if (_timeout_id != 0) |
|
41 |
g_source_remove (_timeout_id); |
|
42 |
}
|
|
43 |
||
44 |
void
|
|
45 |
Animator::SetRate(unsigned int rate) |
|
46 |
{
|
|
47 |
if (rate != 0) |
|
48 |
_rate = 1000 / rate; |
|
49 |
||
50 |
}
|
|
51 |
||
52 |
void
|
|
53 |
Animator::SetDuration(unsigned int duration) |
|
54 |
{
|
|
55 |
if (duration != 0) |
|
56 |
_duration = duration * 1000; |
|
57 |
}
|
|
58 |
||
59 |
unsigned int |
|
60 |
Animator::GetRate() |
|
61 |
{
|
|
62 |
return _rate; |
|
63 |
}
|
|
64 |
||
65 |
unsigned int |
|
66 |
Animator::GetDuration() |
|
67 |
{
|
|
68 |
return _duration; |
|
69 |
}
|
|
70 |
||
71 |
bool
|
|
72 |
Animator::IsRunning() |
|
73 |
{
|
|
74 |
return (_timeout_id != 0); |
|
1597.2.2
by Marco Trevisan (Treviño)
FadeController: new class for handling the fading of FadableObjects |
75 |
}
|
76 |
||
77 |
double
|
|
1597.2.7
by Marco Trevisan (Treviño)
Remove FadeController, add a more generic Animator class |
78 |
Animator::GetProgress() |
79 |
{
|
|
80 |
return _progress; |
|
81 |
}
|
|
82 |
||
83 |
void
|
|
84 |
Animator::Start(double start_progress) |
|
85 |
{
|
|
86 |
if (_timeout_id == 0 && start_progress < 1.0f) |
|
87 |
{
|
|
88 |
if (start_progress < 0.0f) |
|
89 |
start_progress = 0.0f; |
|
90 |
||
91 |
_start_progress = start_progress; |
|
92 |
_progress = _start_progress; |
|
93 |
_start_time = g_get_monotonic_time(); |
|
94 |
_timeout_id = g_timeout_add(_rate, (GSourceFunc) &Animator::TimerTimeOut, this); |
|
95 |
}
|
|
96 |
}
|
|
97 |
||
98 |
void
|
|
99 |
Animator::Stop() |
|
100 |
{
|
|
101 |
if (_timeout_id != 0) |
|
102 |
{
|
|
103 |
g_source_remove(_timeout_id); |
|
104 |
animation_updated.emit(_progress); |
|
105 |
animation_ended.emit(); |
|
106 |
animation_stopped.emit(_progress); |
|
107 |
_timeout_id = 0; |
|
108 |
}
|
|
109 |
}
|
|
110 |
||
111 |
gboolean
|
|
112 |
Animator::TimerTimeOut(Animator *self) |
|
113 |
{
|
|
114 |
const gint64 current_time = g_get_monotonic_time(); |
|
115 |
const gint64 end_time = self->_start_time + self->_duration; |
|
116 |
||
117 |
if (current_time < end_time && self->_progress < 1.0f) |
|
118 |
{
|
|
119 |
const double diff_time = current_time - self->_start_time; |
|
120 |
self->_progress = self->_start_progress + (diff_time / self->_duration); |
|
121 |
self->animation_updated.emit(self->_progress); |
|
122 |
||
123 |
return TRUE; |
|
124 |
} else { |
|
125 |
self->_progress = 1.0f; |
|
126 |
self->animation_updated.emit(1.0f); |
|
127 |
self->animation_ended.emit(); |
|
128 |
self->_timeout_id = 0; |
|
129 |
||
130 |
return FALSE; |
|
131 |
}
|
|
1597.2.2
by Marco Trevisan (Treviño)
FadeController: new class for handling the fading of FadableObjects |
132 |
}
|
133 |
||
134 |
} //namespace |