Working with time

July 09, 2011

Timer
To ease the use of time, the library comes with a timer that you can use for both recording and replay.
Here is a timer example that you can adapt to both recording or replay situations.

import gml4u.brushes.*;
import gml4u.recording.*;
import gml4u.test.*;
import gml4u.utils.*;
import gml4u.drawing.*;
import gml4u.events.*;
import gml4u.model.*;

Timer timer = new Timer();

void setup() {
	timer.setStep(10);
	ellipseMode(CENTER);
	fill(255, 0, 0);
}

void draw() {
	// Increments the timer by the interval defined earlier
	timer.tick();
	// Alternatively, you can call the same method and passing the interval that you want
	// timer.tick(10);
	text(""+timer.getTime(), 20, 20);
	if (timer.started()) { // Draws a red circle
		ellipse(10, 10, 10, 10);
	}
	if (timer.paused()) {
		text("||", 5, 5);
	}
}

void keyPressed() {
	if (key == 'a' || key == 'A') {
		// Starts the timer
		timer.start();
	}
	if (key == 'z' || key == 'Z') {
		// Pause/Continue
		timer.pause(!timer.paused());
	}
	if (key == 'e' || key == 'E') {
		// Stops the timer and resets the timer
		timer.stop();
	}
	if (key == 'r' || key == 'R') {
		// Resets the timer (time set to 0)
		timer.reset();
	}
}

Timebox

For some reasons, you may want to draw gml files consecutively but also want them to have the same duration.
To do so, you can timebox the gml by using the static method timebox of the GmlUtils class.
This method takes 3 parameters:
A Gml object you wanna modify
The total duration (in seconds)
A boolean to tell wether you want the time ratio being kept or not.

If true:
If the Gml object has time information, then it will be timeboxed y setting the first point’s time to 0 and mapping every other point between 0 and the target duration according to its original time.
This basically means that the drawing rythme will be kept.
If the Gml object has no time information, then every stroke’s point will be mapped between 0 and the target duration based on an interval calculated using the total number of points in the Gml file.
This means that strokes will be drawn one after the others.

If false:
Will be the same as set to true when the Gml object has no time information.

import gml4u.brushes.*;
import gml4u.recording.*;
import gml4u.test.*;
import gml4u.utils.*;
import gml4u.drawing.*;
import gml4u.events.*;
import gml4u.model.*;

GmlBrushManager brushManager = new GmlBrushManager();
Gml gml;

void setup() {
	size(600, 600);
	// Load a gml file
	
	// Timebox it to 15 seconds
	GmlUtils.timebox(gml, 15, true);
	//GmlUtils.timebox(gml, 15, false);
}

void draw() {
	brushManager.draw(g, gml, 600); 
}

| Tags: , | More: Tutorials, Updates