-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeqTimer.java
More file actions
187 lines (160 loc) · 4.24 KB
/
SeqTimer.java
File metadata and controls
187 lines (160 loc) · 4.24 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
179
180
181
182
183
184
185
186
187
/**************************************************************************************
* 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;
/**
* Sequential timers are single-threaded timers handled by a TimingHandler.
*/
public class SeqTimer implements Timer {
protected Taskable task;
protected TimingHandler handler;
protected boolean active;
protected boolean runOnlyOnce;
private long counter;
private long prd;
private long startTime;
/**
* Defines a single shot sequential (single-threaded) timer.
*
* @param h timing handler owner
*/
public SeqTimer(TimingHandler h) {
this(h, false, null);
}
/**
* Defines a sequential (single-threaded) timer.
*
* @param h timing handler owner
* @param singleShot
*/
public SeqTimer(TimingHandler h, boolean singleShot) {
this(h, singleShot, null);
}
public SeqTimer(TimingHandler h, Taskable t) {
this(h, false, t);
}
public SeqTimer(TimingHandler h, boolean singleShot, Taskable t) {
handler = h;
runOnlyOnce = singleShot;
task = t;
create();
}
@Override
public Taskable timingTask() {
return task;
}
/**
* Executes the callback method defined by the {@link #timingTask()}.
* <p>
* <b>Note:</b> You should not call this method since it's done by the timing handler
* (see {@link remixlab.fpstiming.TimingHandler#handle()}).
*/
protected boolean execute() {
boolean result = trigggered();
if (result) {
timingTask().execute();
if (runOnlyOnce)
inactivate();
}
return result;
}
@Override
public void cancel() {
stop();
handler.unregisterTask(this);
}
@Override
public void create() {
inactivate();
}
@Override
public void run(long period) {
setPeriod(period);
run();
}
@Override
public void run() {
if (prd <= 0)
return;
inactivate();
counter = 1;
active = true;
startTime = System.currentTimeMillis();
}
@Override
public void stop() {
inactivate();
}
@Override
public boolean isActive() {
return active;
}
// others
/**
* Deactivates the SeqTimer.
*/
public void inactivate() {
active = false;
}
/**
* Returns {@code true} if the timer was triggered at the given frame.
* <p>
* <b>Note:</b> You should not call this method since it's done by the timing handler
* (see {@link remixlab.fpstiming.TimingHandler#handle()}).
*/
public boolean trigggered() {
if (!active)
return false;
long elapsedTime = System.currentTimeMillis() - startTime;
float timePerFrame = (1 / handler.frameRate()) * 1000;
long threshold = counter * prd;
boolean result = false;
if (threshold >= elapsedTime) {
long diff = elapsedTime + (long) timePerFrame - threshold;
if (diff >= 0) {
if ((threshold - elapsedTime) < diff) {
result = true;
}
}
} else {
result = true;
}
if (result) {
counter++;
// if (prd < timePerFrame)
// System.out.println("Your current frame rate (~" + handler.frameRate() +
// " fps) is not high enough " + "to run the timer and reach the specified
// " + prd + " ms period, "
// + timePerFrame
// + " ms period will be used instead. If you want to sustain a lower
// timer " +
// "period, define a higher frame rate (minimum of " + 1000f / prd + "
// fps) " +
// "before running the timer (you may need to simplify your drawing to
// achieve it.)");
}
return result;
}
@Override
public long period() {
return prd;
}
@Override
public void setPeriod(long period) {
prd = period;
}
@Override
public boolean isSingleShot() {
return runOnlyOnce;
}
@Override
public void setSingleShot(boolean singleShot) {
runOnlyOnce = singleShot;
}
}