Write a simple tool

From JReality Wiki
Revision as of 11:51, 18 September 2009 by Andre (Talk | contribs)

(diff) ←Older revision | view current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Source file: ToolIntro

Run as Java webstart


Note: This demo does print out to the console. So as a Java WebStart, it is only useful when the java console is enabled. We recommend to use this tutorial from Eclipse or via command line/ant build.


We will now write a simple Tool, that illustrates what is explained in the Documentation#The jReality Tool system. First, we define the InputSlot we want to use:

private static final InputSlot LEFT_BUTTON = InputSlot.LEFT_BUTTON;
private static final InputSlot FORWARD_BACKWARD = InputSlot.getDevice("ForwardBackwardAxis");
private static final InputSlot POINTER = InputSlot.getDevice("PointerTransformation");


Now extend the abstract base class AbstractTool, use the LEFT_BUTTON-slot as activation, and overwrite the methods activate, perform, deactivate to do some printout:


static class SimpleTool extends AbstractTool {
 
	public SimpleTool() {
		super(LEFT_BUTTON);
	}
 
	@Override
	public void activate(ToolContext tc) {
		System.out.println("now I am active. Activated by "+tc.getSource());
	}
 
	@Override
	public void deactivate(ToolContext tc) {
		System.out.println("No longer active. Deactivated by "+tc.getSource());
	}
 
	@Override
	public void perform(ToolContext tc) {
		System.out.println("Performed by "+tc.getSource());
	}
}


This tool is used in the simple main method of the example. It takes a SceneGraphComponent, sets an Icosahedron as geometry, and attaches our Tool to that component. Then it shows it using the JRViewer.display method:


public static void main(String[] args) {
	SceneGraphComponent cmp = new SceneGraphComponent("tool cmp");
	cmp.setGeometry(Primitives.icosahedron());
	cmp.addTool(new SimpleTool());
	JRViewer.display(cmp);
}


Now when you start this simpler example, you will see the activated/deactivated print outs when you click/release the left mouse botton while pointing on it. The perform method will never be called. The reason is that there is no current slot defined for the tool, that is a slot that the tool reacts on when it is active.


To get the tools perform method called, we add the FORWARD_BACKWARD input slot as a current slot. Change the constructor to

public SimpleTool() {
	super(LEFT_BUTTON);
	addCurrentSlot(FORWARD_BACKWARD, "triggers perform");
}


This input slot is mapped to both the W- and S-Key, or to a Joystick axis. Now again pick the geometry with left mouse button, and type S or W while keeping the mouse pressed. You will see that perform is now getting called.


Now we will add the POINTER slot as a current slot, but only when the FORWARD_BACKWARD axis is pressed. modify the perform method to

public void perform(ToolContext tc) {
	AxisState as = tc.getAxisState(FORWARD_BACKWARD);
	if (tc.getSource() == FORWARD_BACKWARD) {
		if (as.isPressed()) addCurrentSlot(POINTER);
		if (as.isReleased()) removeCurrentSlot(POINTER);
		System.out.println(as);
	}
	if (tc.getSource() == POINTER) System.out.println("Pointer moved");
}


Ok, now when you move click on the geometry with the left mouse button and you move the mouse then, nothing happens. Now hold the S-Key, and again move the mouse (still you have to keep the left mouse button pressed, otherwise the tool gets deactivated). You will a lot of print outs, stating "Pointer moved".