Skip to content

Commit b698fca

Browse files
author
Mike Barkmin
committed
add more timer methods and example
1 parent fe55f51 commit b698fca

File tree

7 files changed

+222
-34
lines changed

7 files changed

+222
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 1.4.0
2+
3+
* add more timer methods
4+
* add a timer example

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,29 @@ You can access the timer objects throught `stage.getTimer()` or
199199
| API | Example | Description |
200200
| :-: | :-: | :-: |
201201
| `void getMillis()` | `cat.getMillis()` | Returns the milliseconds since the sprite was created |
202-
| `boolean everyMillis(int)` | `stage.everyMillis(2000)` | Returns true **roughly** every 2000 Milliseconds |
203-
| `void reset()` | `dog.reset()` | Resets a timer back to 0 |
202+
| `boolean everyMillis(int)` | `stage.everyMillis(600)` | Returns true every 600 Milliseconds |
203+
| `boolean forMillis(int)` | `dog.forMillis(600)` | Returns true for the first 600 Milliseconds |
204+
| `boolean afterMillis(int)` | `cat.afterMillis(600)` | Returns true after the first 600 Milliseconds |
205+
| `boolean intervalMillis(int)` | `cat.intervalMillis(600)` | Returns toggles between true and false every 600 Milliseconds starting with false |
206+
| `boolean intervalMillis(int, boolean)` | `cat.intervalMillis(600, true)` | Returns toggles between true and false every 600 Milliseconds starting with true |
207+
| `boolean intervalMillis(int, int)` | `cat.intervalMillis(600, 200)` | Returns toggles between true for 600 Milliseconds and false for 200 Milliseconds starting with false |
208+
| `boolean intervalMillis(int, int, boolean)` | `cat.intervalMillis(600, 200, true)` | Returns toggles between true for 600 Milliseconds and false for 200 Milliseconds starting with true |
209+
210+
For a visual example of these methods see example [Timer](#Timer) or the gif
211+
below. A dot represents the return of true.
212+
213+
![timer](web/assets/timer.gif)
214+
215+
```
216+
everyMillis(600) -> orange (first line)
217+
forMillis(600) -> lime (second line)
218+
afterMillis(600) -> green (third line)
219+
intervalMillis(600) -> skyblue (fourth line)
220+
intervalMillis(600, true) -> blue (fifth line)
221+
intervalMillis(600, 300) -> pink (sixth line)
222+
intervalMillis(600, 300, true) -> red (seventh line)
223+
```
224+
204225

205226
If you want that a sprite should do something every 2000ms and every 1000ms,
206227
you need two timers. To add a timer you simply call
@@ -297,12 +318,21 @@ An example which makes use of timers.
297318

298319
### TimedDot
299320

300-
Source Code: https://github.com/mikebarkmin/processing-library-scratch/tree/master/examples/RandomDot
321+
Source Code: https://github.com/mikebarkmin/processing-library-scratch/tree/master/examples/TimedDot
301322

302323
An example which makes use of timers.
303324

304325
![timed dot](web/assets/timed_dot_60.gif)
305326

327+
### Timer
328+
329+
Source Code: https://github.com/mikebarkmin/processing-library-scratch/tree/master/examples/Timer
330+
331+
An example which makes use of the many methods of the timer.
332+
333+
![timer](web/assets/timer.gif)
334+
335+
306336
## Missing
307337

308338
* blocks which are not listed above are currently missing

examples/Timer/Timer.pde

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import eu.barkmin.processing.scratch.*;
2+
3+
TimerSprite timer;
4+
5+
void setup() {
6+
frameRate(10);
7+
size(1800, 400);
8+
ScratchStage.init(this);
9+
10+
timer = new TimerSprite();
11+
}
12+
13+
void draw() {
14+
if(ScratchStage.getInstance().getTimer().afterMillis(2400)) {
15+
timer.draw();
16+
}
17+
}
18+
19+
class TimerSprite extends ScratchSprite {
20+
int x = 0;
21+
22+
TimerSprite() {
23+
super();
24+
25+
this.getPen().setSize(20);
26+
27+
this.addTimer("every");
28+
this.addTimer("for");
29+
this.addTimer("after");
30+
this.addTimer("interval1");
31+
this.addTimer("interval2");
32+
this.addTimer("interval3");
33+
this.addTimer("interval4");
34+
}
35+
36+
void draw() {
37+
super.draw();
38+
int y = 20;
39+
x += 20;
40+
if (this.getTimer("every").everyMillis(600)) {
41+
this.getPen().down();
42+
this.getPen().setColor(20);
43+
this.setPosition(x, y);
44+
this.getPen().up();
45+
}
46+
y += 20;
47+
if (this.getTimer("for").forMillis(600)) {
48+
this.getPen().down();
49+
this.getPen().setColor(60);
50+
this.setPosition(x, y);
51+
this.getPen().up();
52+
}
53+
y += 20;
54+
if (this.getTimer("after").afterMillis(600)) {
55+
this.getPen().down();
56+
this.getPen().setColor(100);
57+
this.setPosition(x, y);
58+
this.getPen().up();
59+
}
60+
y += 20;
61+
if (this.getTimer("interval1").intervalMillis(600)) {
62+
this.getPen().down();
63+
64+
this.getPen().setColor(140);
65+
this.setPosition(x, y);
66+
this.getPen().up();
67+
}
68+
y += 20;
69+
if (this.getTimer("interval2").intervalMillis(600, true)) {
70+
this.getPen().down();
71+
this.getPen().setColor(180);
72+
this.setPosition(x, y);
73+
this.getPen().up();
74+
}
75+
y += 20;
76+
if (this.getTimer("interval3").intervalMillis(600, 300)) {
77+
this.getPen().down();
78+
this.getPen().setColor(220);
79+
this.setPosition(x, y);
80+
this.getPen().up();
81+
}
82+
y += 20;
83+
if (this.getTimer("interval4").intervalMillis(600, 300, true)) {
84+
this.getPen().down();
85+
this.getPen().setColor(255);
86+
this.setPosition(x, y);
87+
this.getPen().up();
88+
}
89+
}
90+
}

resources/build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ source.repository=https://github.com/mikebarkmin/processing-library-scratch.git
131131
# This is used to compare different versions of the same Library, and check if
132132
# an update is available.
133133

134-
library.version=4
134+
library.version=5
135135

136136

137137
# The version as the user will see it.
138138

139-
library.prettyVersion=1.3.0
139+
library.prettyVersion=1.4.0
140140

141141

142142
# The min and max revision of Processing compatible with your Library.

src/eu/barkmin/processing/scratch/ScratchPen.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.Stack;
7+
import java.util.Iterator;
78

89
public class ScratchPen {
910

@@ -135,28 +136,28 @@ public void draw() {
135136
int pointsBufferSize = this.pointsBuffer.size();
136137
if (pointsBufferSize <= 0) return;
137138

138-
ArrayList<Point> points = this.pointsBuffer.peek();
139-
int pointsSize = points.size();
139+
Iterator<ArrayList<Point>> pointsBufferIter = this.pointsBuffer.iterator();
140140

141141
buffer.beginDraw();
142-
if (pointsSize > 1) {
143-
Point point = points.get(pointsSize - 1);
144-
Point previousPoint = points.get(pointsSize - 2);
145-
buffer.stroke(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
146-
buffer.strokeWeight(point.size);
147-
buffer.line(previousPoint.x, previousPoint.y, point.x, point.y);
148-
// remove rendered points
149-
if (this.down == false) {
150-
this.pointsBuffer.pop();
142+
143+
while (pointsBufferIter.hasNext()){
144+
ArrayList<Point> points = pointsBufferIter.next();
145+
int pointsSize = points.size();
146+
147+
if (pointsSize > 1) {
148+
Point point = points.get(pointsSize - 1);
149+
Point previousPoint = points.get(pointsSize - 2);
150+
buffer.stroke(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
151+
buffer.strokeWeight(point.size);
152+
buffer.line(previousPoint.x, previousPoint.y, point.x, point.y);
153+
} else if (pointsSize == 1) {
154+
Point point = points.get(pointsSize - 1);
155+
buffer.stroke(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
156+
buffer.strokeWeight(point.size);
157+
buffer.point(point.x, point.y);
151158
}
152-
} else if (pointsSize == 1) {
153-
Point point = points.get(pointsSize - 1);
154-
buffer.stroke(point.color.getRed(), point.color.getGreen(), point.color.getBlue(), point.opacity);
155-
buffer.strokeWeight(point.size);
156-
buffer.point(point.x, point.y);
157-
// remove rendered points
158-
if (this.down == false) {
159-
this.pointsBuffer.pop();
159+
if (!this.down || pointsBufferSize > 1) {
160+
pointsBufferIter.remove();
160161
}
161162
}
162163
buffer.endDraw();
Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package eu.barkmin.processing.scratch;
22

33
public class Timer {
4-
private int nextFrame;
4+
private int nextFrameEvery;
5+
private int nextFrameFor;
6+
private int nextFrameAfter;
7+
private int nextFrameInterval;
8+
private int currentInterval;
59

610
public Timer () {
7-
this.nextFrame = -1;
11+
this.nextFrameEvery = -1;
12+
this.nextFrameFor = -1;
13+
this.nextFrameAfter = -1;
14+
this.nextFrameInterval = -1;
15+
this.currentInterval = 0;
816
}
917

1018
public int getMillis() {
@@ -13,21 +21,76 @@ public int getMillis() {
1321

1422
public boolean everyMillis(int millis) {
1523
int frameSkip = this.getFrameFromMillis(millis);
16-
if (nextFrame < 0) {
17-
nextFrame = ScratchStage.parent.frameCount + frameSkip;
24+
if (nextFrameEvery < 0) {
25+
nextFrameEvery = ScratchStage.parent.frameCount + frameSkip;
1826
}
19-
if (ScratchStage.parent.frameCount >= nextFrame) {
20-
nextFrame = ScratchStage.parent.frameCount + frameSkip;
27+
if (ScratchStage.parent.frameCount >= nextFrameEvery) {
28+
nextFrameEvery = ScratchStage.parent.frameCount + frameSkip;
2129
return true;
2230
}
2331
return false;
2432
}
2533

26-
private int getFrameFromMillis(int millis) {
27-
return (int) Math.round(millis * ScratchStage.parent.frameRate / 1000.0);
34+
public boolean forMillis(int millis) {
35+
int frameDuration = this.getFrameFromMillis(millis);
36+
if (nextFrameFor < 0) {
37+
nextFrameFor = ScratchStage.parent.frameCount + frameDuration;
38+
}
39+
if(ScratchStage.parent.frameCount < nextFrameFor) {
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
public boolean afterMillis(int millis) {
46+
int frameDuration = this.getFrameFromMillis(millis);
47+
if (nextFrameAfter < 0) {
48+
nextFrameAfter = ScratchStage.parent.frameCount + frameDuration;
49+
}
50+
if(ScratchStage.parent.frameCount >= nextFrameAfter) {
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
public boolean intervalMillis(int millis) {
57+
return this.intervalMillis(millis, millis, false);
2858
}
2959

30-
public void reset() {
31-
this.nextFrame = -1;
60+
public boolean intervalMillis(int millis, boolean skipFirst) {
61+
return this.intervalMillis(millis, millis, skipFirst);
62+
}
63+
64+
public boolean intervalMillis(int millis1, int millis2) {
65+
return this.intervalMillis(millis1, millis2, false);
66+
}
67+
68+
public boolean intervalMillis(int milli1, int millis2, boolean skipFirst) {
69+
int frameDuration1 = this.getFrameFromMillis(milli1);
70+
int frameDuration2 = this.getFrameFromMillis(millis2);
71+
72+
if (skipFirst && nextFrameInterval < 0) {
73+
nextFrameInterval = ScratchStage.parent.frameCount + frameDuration1;
74+
} else if(!skipFirst && nextFrameInterval < 0) {
75+
currentInterval = 1;
76+
nextFrameInterval = ScratchStage.parent.frameCount + frameDuration2;
77+
}
78+
if(currentInterval == 0 && ScratchStage.parent.frameCount < nextFrameInterval) {
79+
return true;
80+
} else if (currentInterval == 0) {
81+
currentInterval = 1;
82+
nextFrameInterval = ScratchStage.parent.frameCount + frameDuration2;
83+
return false;
84+
} else if (currentInterval == 1 && ScratchStage.parent.frameCount < nextFrameInterval) {
85+
return false;
86+
} else {
87+
currentInterval = 0;
88+
nextFrameInterval = ScratchStage.parent.frameCount + frameDuration1;
89+
return true;
90+
}
91+
}
92+
93+
private int getFrameFromMillis(int millis) {
94+
return (int) Math.round(millis * ScratchStage.parent.frameRate / 1000.0);
3295
}
3396
}

web/assets/timer.gif

55.1 KB
Loading

0 commit comments

Comments
 (0)