Brushes and styles

July 09, 2011

Brushes

You can attach a brush info to give more info about a GmlStroke.
This basically allows you to store information about the tool used to draw and thus reuse this info when you’ll redraw what you captured.


GmlBrush brush = new GmlBrush();

This constructor without arguments will create a new GmlBrush using the default brush from GML4U (curve).
You can add as many parameter to it, just knowing it only accepts 6 kind of object:

Vec3D
String
Integer
Float
Long
Color

When adding a parameter, you have to give it a name (String) that you can use later on when you want to retrieve a specific parameter.


brush.set("alpha", 128);
brush.set("cap", "fat");

When retrieving a parameter, you’ll have to know his type and cast it to get the correct value because the brush’s get method returns generic objects.


String capType = (String) brush.get("cap");
int alphaChannel = (int) brush.get("alpha");

When saving as a GML file, your brush information will be automatically saved to the correct format. Same thing when loading the saved file.

There are already a few predefined brushes that you can use without having to create custom ones.

You can get a list of pre-existing brushes 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();

void setup() {
	for (String styleId : brushManager.getStyles()) {
		println(styleId);
	}
}

At the time being, you should get this as output:

GML4U_STYLE_CURVES0000
GML4U_STYLE_BOXES0000
GML4U_STYLE_MESH0000

Forcing styles

(Maybe take a look at the “Drawing Gml” tutorial first)
You may bypass the GmlStroke brush information by explicitely passing a style information to the GmlBrushManager’s draw method.

By default, the GmlBrushManager contains a certain number of predefined GmlStrokeDrawers.
You can make reference those styles using one of these parameters:
GML4U_STYLE_CURVES0000
GML4U_STYLE_BOXES0000
GML4U_STYLE_MESH0000

So, if you want to use boxes rather than a simple curve stroke, you can call the GmlBrushManager with an extra parameter.

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() {
	// ie: Load a Gml file
}

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

Here is another example which allows you to loop through all the registered styles easily using the + and – keys while your sketch is running.

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;
int currentStyle = 0;

void setup() {
	// ie: Load a Gml file
}

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

void keyPressed() {
	if (key == '-') {
		currentStyle--;
		if (currentStyle < 0) currentStyle = GmlBrushManager.size()-1;
	}
	else if (key == '+') {
		currentStyle++;
		if (currentStyle == GmlBrushManager.size()) currentStyle = 0;
		}
	}
}

| Tags: , | More: Tutorials, Updates