Custom styles

July 09, 2011

You may add custom styles to the GmlBrushManager by creating your own classes extending the GmlStrokeDrawerBasic or GmlStrokeDrawer classes.

GmlStrokeDrawerBasic
This is an abstract class extending GmlStrokeDrawer.
It has a single abstract draw method that you have to implement as follow with your own way to draw points, given the fact that you’ll have 2 GmlPoint available (current and previous ones).
For this purpose, you’ll use the standard Processing methods prefixed by “g.”

In this example, we draw a simple line between each point, assuming that we are in 3D mode.

class MyCustomDrawer extends GmlStrokeDrawerBasic {

	public draw(PGraphics g, GmlPoint prev, GmlPoint cur) {
		g.line(prev.x, prev.y, prev.z, cur.x, cur.y, cur.z);
	}
}

Then, in your Processing main program, you’ll create an object of this class, and make it available through the GmlBrushManager.

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, 400, P3D);
	// Load a gml file
	
	// Create a custom drawer and register it as default drawer
	MyCustomDrawer customDrawer = new MyCustomeDrawer();
	brushManager.setDefault(customDrawer);
}

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

This example won’t work and you’ll get an error if you use a 2D renderer.
If you apply the following modifications to your draw method, you will be 2D and 3D compliant.

class MyCustomDrawer extends GmlStrokeDrawerBasic {

	public draw(PGraphics g, GmlPoint prev, GmlPoint cur) {
		if (g.is3D()) {
			g.line(prev.x, prev.y, prev.z, cur.x, cur.y, cur.z);
		}
		else if (g.is2D()) {
			g.line(prev.x, prev.y, cur.x, cur.y);
		}
	}
}

Drawing GmlStrokes individually

You may draw strokes by looping through the Gml’s strokes.
The main advantage of it is that you can change parameters in between (like the color).

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, 400, P3D);
	// Load a gml file
	
	// Create a custom drawer and register it as default drawer
	MyCustomDrawer customDrawer = new MyCustomeDrawer();
	brushManager.setDefault(customDrawer);
}

void draw() {
	for (GmlStroke stroke : gml.getStrokes()) {
		// Change colors according to what's stored in the GmlBrush
		GmlBrush brush = stroke.getBrush();
		color(brush.get("color");
		brushManager.draw(g, stroke, 600); 
	}
}

| Tags: , | More: Tutorials, Updates