-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatorObject.java
More file actions
177 lines (160 loc) · 4.61 KB
/
AnimatorObject.java
File metadata and controls
177 lines (160 loc) · 4.61 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**************************************************************************************
* fpstiming_tree
* Copyright (c) 2014-2017 National University of Colombia, https://github.com/remixlab
* @author Jean Pierre Charalambos, http://otrolado.info/
*
* All rights reserved. Library that eases the creation of interactive
* scenes, released under the terms of the GNU Public License v3.0
* which is available at http://www.gnu.org/licenses/gpl.html
**************************************************************************************/
package remixlab.fpstiming;
/**
* Class implementing the main {@link remixlab.fpstiming.Animator} behavior.
*/
public class AnimatorObject implements Animator {
protected SeqTimer animationTimer;
protected boolean started;
protected long animationPeriod;
protected TimingHandler handler;
/**
* Constructs an animated object with a default {@link #animationPeriod()} of 40
* milliseconds (25Hz). The handler should explicitly be defined afterwards (
* {@link #setTimingHandler(TimingHandler)}).
*/
public AnimatorObject() {
setAnimationPeriod(40, false); // 25Hz
stopAnimation();
}
/**
* Constructs an animated object with a default {@link #animationPeriod()} of 40
* milliseconds (25Hz).
*/
public AnimatorObject(TimingHandler handler) {
setTimingHandler(handler);
setAnimationPeriod(40, false); // 25Hz
stopAnimation();
}
@Override
public void setTimingHandler(TimingHandler h) {
handler = h;
handler.registerAnimator(this);
animationTimer = new SeqTimer(handler);
}
@Override
public TimingHandler timingHandler() {
return handler;
}
@Override
public SeqTimer timer() {
return animationTimer;
}
/**
* Return {@code true} when the animation loop is started.
* <p>
* The timing handler will check when {@link #animationStarted()} and then called the
* animation callback method every {@link #animationPeriod()} milliseconds.
* <p>
* Use {@link #startAnimation()}, {@link #stopAnimation()} or {@link #toggleAnimation()}
* to change this value.
*
* @see #startAnimation()
* @see #animate()
*/
@Override
public boolean animationStarted() {
return started;
}
/**
* The animation loop period, in milliseconds. When {@link #animationStarted()}, this is
* the delay that takes place between two consecutive iterations of the animation loop.
* <p>
* This delay defines a target frame rate that will only be achieved if your
* {@link #animate()} methods is fast enough.
* <p>
* Default value is 40 milliseconds (25 Hz).
* <p>
* <b>Note:</b> This value is taken into account only the next time you call
* {@link #startAnimation()}. If {@link #animationStarted()}, you should
* {@link #stopAnimation()} first. See {@link #restartAnimation()} and
* {@link #setAnimationPeriod(long, boolean)}.
*
* @see #setAnimationPeriod(long, boolean)
*/
@Override
public long animationPeriod() {
return animationPeriod;
}
/**
* Convenience function that simply calls {@code period(period, true)}.
*
* @see #setAnimationPeriod(long, boolean)
*/
@Override
public void setAnimationPeriod(long period) {
setAnimationPeriod(period, true);
}
/**
* Sets the {@link #animationPeriod()}, in milliseconds. If restart is {@code true} and
* {@link #animationStarted()} then {@link #restartAnimation()} is called.
*
* @see #startAnimation()
*/
@Override
public void setAnimationPeriod(long period, boolean restart) {
if (period > 0) {
animationPeriod = period;
if (animationStarted() && restart)
restartAnimation();
}
}
/**
* Stops animation.
*
* @see #animationStarted()
*/
@Override
public void stopAnimation() {
started = false;
if (timer() != null)
timer().stop();
}
/**
* Starts the animation loop.
*
* @see #animationStarted()
*/
@Override
public void startAnimation() {
started = true;
if (timer() != null)
timer().run(animationPeriod);
}
/**
* Restart the animation.
* <p>
* Simply calls {@link #stopAnimation()} and then {@link #startAnimation()}.
*/
@Override
public void restartAnimation() {
stopAnimation();
startAnimation();
}
/**
* Calls {@link #startAnimation()} or {@link #stopAnimation()}, depending on
* {@link #animationStarted()}.
*/
@Override
public void toggleAnimation() {
if (animationStarted())
stopAnimation();
else
startAnimation();
}
@Override
public void animate() {
}
@Override
public boolean invokeAnimationHandler() {
return false;
}
}