17
17
* Authored by: Marco Trevisan (Treviño) <mail@3v1n0.net>
20
#include "FadeController.h"
25
FadeController::FadeController()
28
_status = FADING_NONE;
30
_timer_functor = new nux::TimerFunctor();
31
_timeout_connection = _timer_functor->OnTimerExpired.connect(
32
sigc::mem_fun(this, &FadeController::TimerTimeOut));
35
FadeController::FadeController(FadableObject *object)
38
_status = FADING_NONE;
40
_timer_functor = new nux::TimerFunctor();
41
_timeout_connection = _timer_functor->OnTimerExpired.connect(
42
sigc::mem_fun(this, &FadeController::TimerTimeOut));
45
FadeController::~FadeController()
47
if (_fade_timehandler.IsValid())
48
nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
50
_timeout_connection.disconnect();
52
delete _timer_functor;
56
FadeController::SetObject(FadableObject* object)
58
if (_fade_timehandler.IsValid())
59
nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
65
FadeController::FadeIn(unsigned int ms)
70
if (_fade_timehandler.IsValid())
72
if (_status == FADING_IN && _fade_timehandler.GetElapsedTimed() > 1)
75
nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
78
if (_object->GetOpacity() >= 1.0)
80
_status = FADING_NONE;
85
_start_opacity = _object->GetOpacity();
86
_fade_timehandler = nux::GetTimer().AddPeriodicTimerHandler(1, ms, _timer_functor, NULL);
90
FadeController::FadeOut(unsigned int ms)
95
if (_fade_timehandler.IsValid())
97
if (_status == FADING_OUT && _fade_timehandler.GetElapsedTimed() > 1)
100
nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
103
if (_object->GetOpacity() <= 0.0)
105
_status = FADING_NONE;
109
_status = FADING_OUT;
110
_start_opacity = _object->GetOpacity();
111
_fade_timehandler = nux::GetTimer().AddPeriodicTimerHandler(1, ms, _timer_functor, NULL);
115
FadeController::TimerTimeOut(void *data)
117
double progress = _fade_timehandler.GetProgress();
118
double opacity = _start_opacity;
120
if (_status == FADING_IN)
122
else if (_status == FADING_OUT)
125
_object->SetOpacity(opacity);
126
opacity_changed.emit(_status, opacity);
128
if (progress >= 1.0f ||
129
(_object->GetOpacity() <= 0.0f && _status == FADING_OUT) ||
130
(_object->GetOpacity() >= 1.0f && _status == FADING_IN) ||
131
_status == FADING_NONE)
133
nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
135
if (_status == FADING_IN)
137
else if (_status == FADING_OUT)
140
_status = FADING_NONE;
145
FadeController::SetOpacity(double opacity)
149
_object->SetOpacity(opacity);
150
opacity_changed.emit(FADING_NONE, opacity);
25
Animator::Animator(unsigned int rate, unsigned int duration)
30
_start_progress = 0.0f;
34
SetDuration(duration);
41
g_source_remove (_timeout_id);
45
Animator::SetRate(unsigned int rate)
53
Animator::SetDuration(unsigned int duration)
56
_duration = duration * 1000;
66
Animator::GetDuration()
74
return (_timeout_id != 0);
155
FadeController::GetOpacity()
158
return _object->GetOpacity();
78
Animator::GetProgress()
84
Animator::Start(double start_progress)
86
if (_timeout_id == 0 && start_progress < 1.0f)
88
if (start_progress < 0.0f)
89
start_progress = 0.0f;
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);
101
if (_timeout_id != 0)
103
g_source_remove(_timeout_id);
104
animation_updated.emit(_progress);
105
animation_ended.emit();
106
animation_stopped.emit(_progress);
112
Animator::TimerTimeOut(Animator *self)
114
const gint64 current_time = g_get_monotonic_time();
115
const gint64 end_time = self->_start_time + self->_duration;
117
if (current_time < end_time && self->_progress < 1.0f)
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);
125
self->_progress = 1.0f;
126
self->animation_updated.emit(1.0f);
127
self->animation_ended.emit();
128
self->_timeout_id = 0;