- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlledAnimationTimer.java
More file actions
Latest commit
178 lines (155 loc) · 4.48 KB
/
Copy pathControlledAnimationTimer.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
packagecontrollers;
importjava.util.LinkedList;
importjavafx.animation.AnimationTimer;
importjavafx.beans.property.IntegerProperty;
importjavafx.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
*
*/
publicclassControlledAnimationTimerextendsAnimationTimer{
// Instance variables
privateIntegerPropertycurrStepProperty;
privateIntegerPropertymaxStepsProperty;
privatebooleanrunning;
privateLinkedList<Runnable> onStepTasks;
privateLinkedList<Runnable> onFinishTasks;
privateLinkedList<Runnable> onStartTasks;
/**
* Constructor.
* @param max The max number of steps to run the timer for.
*/
publicControlledAnimationTimer(intmax) {
currStepProperty = newSimpleIntegerProperty();
maxStepsProperty = newSimpleIntegerProperty(max);
onFinishTasks = newLinkedList<>();
onStartTasks = newLinkedList<>();
onStepTasks = newLinkedList<>();
}
/**
* Add a task to run before the timer starts.
* @param r Runnable task to execute.
*/
publicvoidaddOnStartTask(Runnabler) {
if (r != null) {
onStartTasks.add(r);
}
}
/**
* Add a task to run after the timer stops.
* @param r Runnable task to execute.
*/
publicvoidaddOnFinishTask(Runnabler) {
if (r != null) {
onFinishTasks.add(r);
}
}
/**
* Add a task to run each step of the timer.
* @param r Runnable task to execute.
*/
publicvoidaddOnStepTask(Runnabler) {
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.
*/
publicbooleanremoveOnFinishTask(Runnabler) {
returnonFinishTasks.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.
*/
publicbooleanremoveOnStartTask(Runnabler) {
returnonStartTasks.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.
*/
publicbooleanremoveOnStepTask(Runnabler) {
returnonStepTasks.remove(r);
}
/**
* Change the number of steps the timer will run for.
* @param max The desired number of steps.
*/
publicvoidsetMaxSteps(intmax) {
maxStepsProperty.setValue(max);
reset();
}
/**
* Determines if Timer is currently running.
* @return True if Timer is running.
*/
publicbooleanisRunning() {
returnrunning;
}
/**
* Getter for maximum steps.
* @return Maximum number of steps.
*/
publicintgetMaxSteps() {
returnmaxStepsProperty.intValue();
}
/**
* Getter for current step.
* @return Reference to current step IntegerProperty.
*/
publicIntegerPropertyCurrStepProperty() {
returncurrStepProperty;
}
/**
* Getter for maximum number of steps.
* @return Reference to maximum steps IntegerProperty.
*/
publicIntegerPropertyMaxStepsProperty() {
returnmaxStepsProperty;
}
@Override
publicvoidhandle(longnow) {
if (currStepProperty.intValue()
< maxStepsProperty.intValue()) {
for (Runnabler : onStepTasks) {
r.run();
}
currStepProperty.set(currStepProperty.intValue() + 1);;
} else {
stop();
}
}
@Override
publicvoidstart() {
reset();
running = true;
// Run any on start tasks.
for (Runnabletask : onStartTasks) {
task.run();
}
super.start();
}
@Override
publicvoidstop() {
super.stop();
running = false;
// Run any on finish tasks.
for (Runnabletask : onFinishTasks) {
task.run();
}
}
// Reset the counter property.
privatevoidreset() {
currStepProperty.set(0);
}
}