Use the mouse-over input slot
Source file: PointerHitToolExample
The following example shows how to write a tool that gets activated in a mouse-over fashion.
For an introduction to the tool system in jReality see this this introductory document on the jReality scene graph. (Needed: better documentation for toolconfig.xml
)
The tool implemented in this example highlights the
geometry which is "hit" by the pointer device, by changing its color.
To operate the example:
- Move the mouse, and see what happens...
- To rotate the object, move the cursor away from the geometry (over the background) before dragging.
Things to notice:
- The tool sets its activation slot to InputSlot.POINTER_HIT (by calling
super(InputSlot.POINTER_HIT)
. This slot gets "pressed" as soon as some geometry is hit by the pointer ray, and it gets "released" again when the geometry is no longer hit by the pointer. - this slot is implicitly defined from the PointerTransformation, so it does not occur in the
toolconfig.xml
file. - since no used slots are defined, the perform method will never be invoked - the tool only gets activated and deactivated when the geometry is no longer hit by the pointer.
The code of the tool is
static class MouseOverTool extends AbstractTool { Color hcl; public MouseOverTool(Color highLightColor) { super(InputSlot.POINTER_HIT); hcl=highLightColor; } @Override public void activate(ToolContext tc) { SceneGraphComponent cmp = tc.getRootToLocal().getLastComponent(); cmp.getAppearance().setAttribute(CommonAttributes.POLYGON_SHADER+"."+CommonAttributes.DIFFUSE_COLOR, hcl); } @Override public void deactivate(ToolContext tc) { SceneGraphComponent cmp = tc.getRootToLocal().getLastComponent(); cmp.getAppearance().setAttribute(CommonAttributes.POLYGON_SHADER+"."+CommonAttributes.DIFFUSE_COLOR, Appearance.INHERITED); } };
The only thing it does is to switch the face colors on activation/deactivation.
In the main method we attach two instances of the tool (with different highlight colors) to two overlapping copies of the same geometry. This demonstrates that the active tool really gets deactivated when the pointer moves from one instance to another.
|