~mial/ubuntu/oneiric/unity/bug-791810

« back to all changes in this revision

Viewing changes to plugins/unityshell/src/Animator.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2011-09-22 20:43:08 UTC
  • mto: (1631.1.8 fixes)
  • mto: This revision was merged to the branch mainline in revision 1642.
  • Revision ID: mail@3v1n0.net-20110922204308-9ofy886shvt2168u
Remove FadeController, add a more generic Animator class

It allows basically to manage a timer for animation purposes.
It emits signals that can be used to animat widgets.

Based on the OsAnimation GObject class by Andrea Cimitan.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * Authored by: Marco Trevisan (Treviño) <mail@3v1n0.net>
18
18
 */
19
19
 
20
 
#include "FadeController.h"
 
20
#include "Animator.h"
21
21
 
22
22
namespace unity
23
23
{
24
24
 
25
 
FadeController::FadeController()
26
 
{
27
 
  _object = NULL;
28
 
  _status = FADING_NONE;
29
 
 
30
 
  _timer_functor = new nux::TimerFunctor();
31
 
  _timeout_connection = _timer_functor->OnTimerExpired.connect(
32
 
                        sigc::mem_fun(this, &FadeController::TimerTimeOut));
33
 
}
34
 
 
35
 
FadeController::FadeController(FadableObject *object)
36
 
{
37
 
  _object = object;
38
 
  _status = FADING_NONE;
39
 
 
40
 
  _timer_functor = new nux::TimerFunctor();
41
 
  _timeout_connection = _timer_functor->OnTimerExpired.connect(
42
 
                        sigc::mem_fun(this, &FadeController::TimerTimeOut));
43
 
}
44
 
 
45
 
FadeController::~FadeController()
46
 
{
47
 
  if (_fade_timehandler.IsValid())
48
 
    nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
49
 
 
50
 
  _timeout_connection.disconnect();
51
 
 
52
 
  delete _timer_functor;
53
 
}
54
 
 
55
 
void
56
 
FadeController::SetObject(FadableObject* object)
57
 
{
58
 
  if (_fade_timehandler.IsValid())
59
 
    nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
60
 
 
61
 
  _object = object;
62
 
}
63
 
 
64
 
void
65
 
FadeController::FadeIn(unsigned int ms)
66
 
{
67
 
  if (!_object)
68
 
    return;
69
 
 
70
 
  if (_fade_timehandler.IsValid())
71
 
  {
72
 
    if (_status == FADING_IN && _fade_timehandler.GetElapsedTimed() > 1)
73
 
      return;
74
 
 
75
 
    nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
76
 
  }
77
 
 
78
 
  if (_object->GetOpacity() >= 1.0)
79
 
  {
80
 
    _status = FADING_NONE;
81
 
    return;
82
 
  }
83
 
 
84
 
  _status = FADING_IN;
85
 
  _start_opacity = _object->GetOpacity();
86
 
  _fade_timehandler = nux::GetTimer().AddPeriodicTimerHandler(1, ms, _timer_functor, NULL);
87
 
}
88
 
 
89
 
void
90
 
FadeController::FadeOut(unsigned int ms)
91
 
{
92
 
  if (!_object)
93
 
    return;
94
 
 
95
 
  if (_fade_timehandler.IsValid())
96
 
  {
97
 
    if (_status == FADING_OUT && _fade_timehandler.GetElapsedTimed() > 1)
98
 
      return;
99
 
 
100
 
    nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
101
 
  }
102
 
 
103
 
  if (_object->GetOpacity() <= 0.0)
104
 
  {
105
 
    _status = FADING_NONE;
106
 
    return;
107
 
  }
108
 
 
109
 
  _status = FADING_OUT;
110
 
  _start_opacity = _object->GetOpacity();
111
 
  _fade_timehandler = nux::GetTimer().AddPeriodicTimerHandler(1, ms, _timer_functor, NULL);
112
 
}
113
 
 
114
 
void
115
 
FadeController::TimerTimeOut(void *data)
116
 
{
117
 
  double progress = _fade_timehandler.GetProgress();
118
 
  double opacity = _start_opacity;
119
 
 
120
 
  if (_status == FADING_IN)
121
 
    opacity += progress;
122
 
  else if (_status == FADING_OUT)
123
 
    opacity -= progress;
124
 
 
125
 
  _object->SetOpacity(opacity);
126
 
  opacity_changed.emit(_status, opacity);
127
 
 
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)
132
 
  {
133
 
    nux::GetTimer().RemoveTimerHandler(_fade_timehandler);
134
 
 
135
 
    if (_status == FADING_IN)
136
 
      faded_in.emit();
137
 
    else if (_status == FADING_OUT)
138
 
      faded_out.emit();
139
 
 
140
 
    _status = FADING_NONE;
141
 
  }
142
 
}
143
 
 
144
 
void
145
 
FadeController::SetOpacity(double opacity)
146
 
{
147
 
  if (_object)
148
 
  {
149
 
    _object->SetOpacity(opacity);
150
 
    opacity_changed.emit(FADING_NONE, opacity);
151
 
  }
 
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);
152
75
}
153
76
 
154
77
double
155
 
FadeController::GetOpacity()
156
 
{
157
 
  if (_object)
158
 
    return _object->GetOpacity();
159
 
 
160
 
  return 0.0;
 
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
  }
161
132
}
162
133
 
163
134
} //namespace