-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlledAnimationTimer.java
More file actions
178 lines (155 loc) · 4.48 KB
/
ControlledAnimationTimer.java
File metadata and controls
178 lines (155 loc) · 4.48 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
178
package controllers;
import java.util.LinkedList;
import javafx.animation.AnimationTimer;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
/**
* Extension of JavaFX's AnimationTimer. It allows the user to determine the
* number of steps to run as well as Runnable tasks to execute when the timer
* starts and stops.
*
* @author Graf
*
*/
public class ControlledAnimationTimer extends AnimationTimer{
// Instance variables
private IntegerProperty currStepProperty;
private IntegerProperty maxStepsProperty;
private boolean running;
private LinkedList<Runnable> onStepTasks;
private LinkedList<Runnable> onFinishTasks;
private LinkedList<Runnable> onStartTasks;
/**
* Constructor.
* @param max The max number of steps to run the timer for.
*/
public ControlledAnimationTimer(int max) {
currStepProperty = new SimpleIntegerProperty();
maxStepsProperty = new SimpleIntegerProperty(max);
onFinishTasks = new LinkedList<>();
onStartTasks = new LinkedList<>();
onStepTasks = new LinkedList<>();
}
/**
* Add a task to run before the timer starts.
* @param r Runnable task to execute.
*/
public void addOnStartTask(Runnable r) {
if (r != null) {
onStartTasks.add(r);
}
}
/**
* Add a task to run after the timer stops.
* @param r Runnable task to execute.
*/
public void addOnFinishTask(Runnable r) {
if (r != null) {
onFinishTasks.add(r);
}
}
/**
* Add a task to run each step of the timer.
* @param r Runnable task to execute.
*/
public void addOnStepTask(Runnable r) {
if (r != null) {
onStepTasks.add(r);
}
}
/**
* Removes a task from the list of tasks that run after the timer finishes.
* @param r The task to remove from the list.
* @return True if a task was removed.
*/
public boolean removeOnFinishTask(Runnable r) {
return onFinishTasks.remove(r);
}
/**
* Removes a task from the list of tasks that run before the timer starts.
* @param r The task to remove from the list.
* @return True if a task was removed.
*/
public boolean removeOnStartTask(Runnable r) {
return onStartTasks.remove(r);
}
/**
* Removes a task from the list of tasks that run as the timer steps.
* @param r The task to remove from the list.
* @return True if a task was removed.
*/
public boolean removeOnStepTask(Runnable r) {
return onStepTasks.remove(r);
}
/**
* Change the number of steps the timer will run for.
* @param max The desired number of steps.
*/
public void setMaxSteps(int max) {
maxStepsProperty.setValue(max);
reset();
}
/**
* Determines if Timer is currently running.
* @return True if Timer is running.
*/
public boolean isRunning() {
return running;
}
/**
* Getter for maximum steps.
* @return Maximum number of steps.
*/
public int getMaxSteps() {
return maxStepsProperty.intValue();
}
/**
* Getter for current step.
* @return Reference to current step IntegerProperty.
*/
public IntegerProperty CurrStepProperty() {
return currStepProperty;
}
/**
* Getter for maximum number of steps.
* @return Reference to maximum steps IntegerProperty.
*/
public IntegerProperty MaxStepsProperty() {
return maxStepsProperty;
}
@Override
public void handle(long now) {
if (currStepProperty.intValue()
< maxStepsProperty.intValue()) {
for (Runnable r : onStepTasks) {
r.run();
}
currStepProperty.set(currStepProperty.intValue() + 1);;
} else {
stop();
}
}
@Override
public void start() {
reset();
running = true;
// Run any on start tasks.
for (Runnable task : onStartTasks) {
task.run();
}
super.start();
}
@Override
public void stop() {
super.stop();
running = false;
// Run any on finish tasks.
for (Runnable task : onFinishTasks) {
task.run();
}
}
// Reset the counter property.
private void reset() {
currStepProperty.set(0);
}
}