Class SoEventCallback
- java.lang.Object
-
- com.openinventor.inventor.Inventor
-
- com.openinventor.inventor.misc.SoBase
-
- com.openinventor.inventor.fields.SoFieldContainer
-
- com.openinventor.inventor.nodes.SoNode
-
- com.openinventor.inventor.nodes.SoEventCallback
-
- All Implemented Interfaces:
SafeDisposable
public class SoEventCallback extends SoNode
Node which invokes callbacks for events.SoEventCallback
will invoke application supplied callbacks duringSoHandleEventAction
traversal. Methods allow the application to specify which Open Inventor events should trigger callbacks, and which path must be picked, if any, for the callback invocation to occur. The application callback is able to get information about the event and the pick detail, and may grab events, release events, and set whether the event was handled.If you register more than one callback in an
SoEventCallback
node, all the callbacks will be invoked when an event occurs, even if one of the callbacks handles the event. However, if the event is handled (i.e. setHandled is called) by any of the callbacks , no subsequent node in the scene graph will see the event. GenerallySoEventCallback
nodes should be placed near the beginning of the scene graph, so theSoHandleEventAction
does not need to traverse the rest of the scene graph if the event is handled. Note that events may be handled by other nodes in the scene graph, for example draggers and manipulators. If the event is handled before theSoEventCallback
node is traversed, then none of the callbacks will be invoked.Remember that when using the Open Inventor viewer classes,
SoHandleEventAction
traversal is only done when the viewer is in "selection" mode (the arrow cursor is displayed). Also note that some methods may only be called from the callback .When invoked, this
SoEventCallback
node will be passed to the callback . Using this object, you can get the event being handled (getEvent()
) and query what geometry is under the cursor (getPickedPoint()
). These are convenient wrappers around the corresponding methods in theSoHandleEventAction
(which you can get usinggetAction()
).Picking:
WhengetPickedPoint()
is called, the handle event action automatically applies anSoRayPickAction
to the scene graph using the cursor position in the event. The handle event action remembers the result in case another node needs pick information during the traversal. Often this is more convenient than creating and applying a pick action explicitly.But note: The handle event action does not enable computation of normal vectors and texture coordinates in the pick action that it creates internally. If you need the normal vector at the point of intersection of the pick ray, then you must create your own pick action and apply it. In this case you can get the node to apply the pick action to by calling the handle event action's getPickRoot() method.
The application method can conveniently find out what, if any, geometry is under the cursor by querying the
SoHandleEventAction
(getAction()
), then calling thegetPickedPoint()
method. The first time this method is called during a handle event traversal, the handle event action will automatically apply its internalSoRayPickAction
to the scene graph returned by getPickRoot(). The result is stored in case other nodes make the same query during the same traversal. The stored result can be cleared by calling clearApplyResult().Some, but not all, options can be modified on the internal pick action (see for example setPickRadius()). Note that the internal pick action does not compute texture coordinates or normal vector for the picked point. Thus,
getPickedPoint()
.getNormal() returns (0,0,0) andgetPickedPoint()
.getTextureCoords() returns (0,0,0,0).If your application needs to apply the pick action itself, for example to set different options, get the appropriate root node by calling
SoHandleEventAction.getPickRoot()
.To use GPU picking on compatible shapes, it is mandatory to set the pick radius to 0, see
SoHandleEventAction.setPickRadius(float radiusInPixels)
. Compatible shapes are described in theSoRayPickAction
section Picking Algorithms.EXAMPLE An event callback node that handles key and button events
SoEventCallback eventNode = new SoEventCallback(); eventNode.addEventCallback( SoKeyboardEvent.class , new KeyEventHandler() ); eventNode.addEventCallback( SoMouseButtonEvent.class, new BtnEventHandler() ); class KeyEventHandler extends SoEventCallbackCB { @Override public void invoke( SoEventCallback node ) { // Get the event that triggered the callback SoEvent evt = node.getEvent(); if (SoKeyboardEvent.isKeyPressEvent(evt, SoKeyboardEvent.Keys.UP_ARROW)) { // Do something, then tell action to stop traversal node.setHandled(); } else if (SoKeyboardEvent.isKeyPressEvent(evt, SoKeyboardEvent.Keys.DOWN_ARROW)) { // Do something, then tell action to stop traversal node.setHandled(); } } } class BtnEventHandler extends SoEventCallbackCB { @Override public void invoke( SoEventCallback node ) { SoEvent evt = node.getEvent(); // If button 1 was pressed if (SoMouseButtonEvent.isButtonPressEvent( evt, SoMouseButtonEvent.Buttons.BUTTON1 )) { // Check if any geometry is under the cursor SoHandleEventAction action = node.getAction(); SoPickedPoint pickedPt = action.getPickedPoint(); if (pickedPt != null) { SoPath pickedPath = pickedPt.getPath(); SoNode pickedNode = pickedPath.full.getTail(); } } } } File format/default:
EventCallback {
SoInteraction
- See Also:
SoSelection
,SoHandleEventAction
,SoRayPickAction
,SoDragger
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.openinventor.inventor.nodes.SoNode
SoNode.RenderModes
-
Nested classes/interfaces inherited from class com.openinventor.inventor.Inventor
Inventor.ConstructorCommand
-
-
Field Summary
-
Fields inherited from class com.openinventor.inventor.Inventor
VERBOSE_LEVEL, ZeroHandle
-
-
Constructor Summary
Constructors Constructor Description SoEventCallback()
Constructor creates an event callback node with no event interest and a NULL path.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEventCallback(java.lang.Class eventClass, SoEventCallbackCB f)
Same asaddEventCallback
(eventClass,f,null).void
addEventCallback(java.lang.Class eventClass, SoEventCallbackCB f, java.lang.Object userData)
Same asaddEventCallback
(eventClass.getName(),f,userData).void
addEventCallback(java.lang.String eventClassName, SoEventCallbackCB f, java.lang.Object userData)
Specifies the callbacks to be invoked for different event types.SoHandleEventAction
getAction()
Returns theSoHandleEventAction
currently traversing this node, or NULL if traversal is not taking place.SoEvent
getEvent()
Returns the event currently being handled, or NULL if traversal is not taking place.SoPath
getPath()
Gets the path which must be picked in order for the callbacks to be invoked.SoPickedPoint
getPickedPoint()
Returns pick information duringSoHandleEventAction
traversal, or NULL if traversal is not taking place.void
grabEvents()
Tells the event callback node to grab events.boolean
isHandled()
Returns whether the event has been handled.void
releaseEvents()
Tells the event callback node to release the grab of events.void
removeEventCallback(java.lang.Class eventClass, SoEventCallbackCB f, java.lang.Object userData)
void
removeEventCallback(java.lang.String eventClassName, SoEventCallbackCB f, java.lang.Object userData)
Removes the callback function specified byaddEventCallback
.void
setHandled()
Tells the node the event was handled.void
setPath(SoPath path)
Sets the path which must be picked in order for the callbacks to be invoked.-
Methods inherited from class com.openinventor.inventor.nodes.SoNode
affectsState, callback, copy, copy, distribute, doAction, getAlternateRep, getBoundingBox, getByName, getMatrix, getPrimitiveCount, getRenderEngineMode, getRenderUnitID, GLRender, GLRenderBelowPath, GLRenderInPath, GLRenderOffPath, grabEventsCleanup, grabEventsSetup, handleEvent, isBoundingBoxIgnoring, isOverride, pick, rayPick, search, setOverride, touch, write
-
Methods inherited from class com.openinventor.inventor.fields.SoFieldContainer
copyFieldValues, copyFieldValues, enableNotify, fieldsAreEqual, get, getAllFields, getEventIn, getEventOut, getField, getFieldName, hasDefaultValues, isNotifyEnabled, set, setToDefaults
-
Methods inherited from class com.openinventor.inventor.misc.SoBase
dispose, getName, isDisposable, isSynchronizable, setName, setSynchronizable
-
Methods inherited from class com.openinventor.inventor.Inventor
getNativeResourceHandle
-
-
-
-
Method Detail
-
addEventCallback
public void addEventCallback(java.lang.Class eventClass, SoEventCallbackCB f)
Same asaddEventCallback
(eventClass,f,null).- Parameters:
eventClass
- one of the class in the packagecom.openinventor.inventor.events
-
addEventCallback
public void addEventCallback(java.lang.Class eventClass, SoEventCallbackCB f, java.lang.Object userData)
Same asaddEventCallback
(eventClass.getName(),f,userData).- Parameters:
eventClass
- one of the class in the packagecom.openinventor.inventor.events
-
addEventCallback
public void addEventCallback(java.lang.String eventClassName, SoEventCallbackCB f, java.lang.Object userData)
Specifies the callbacks to be invoked for different event types. When invoked, this SoEventCallback node will be passed to the callback. For example, passing "com.openinventor.inventor.events.SoMouseButtonEvent" as eventClassName argument means callbacks will be invoked only when a mouse button is pressed or released. Passing "com.openinventor.inventor.events.SoEvent" for the eventClassName will cause the callback to be invoked for every event which passes through this event callback node.- Parameters:
eventClassName
- the name of one of the class in the packagecom.openinventor.inventor.events
-
removeEventCallback
public void removeEventCallback(java.lang.Class eventClass, SoEventCallbackCB f, java.lang.Object userData)
- Parameters:
eventClass
- one of the class in the packagecom.openinventor.inventor.events
-
removeEventCallback
public void removeEventCallback(java.lang.String eventClassName, SoEventCallbackCB f, java.lang.Object userData)
Removes the callback function specified byaddEventCallback
.- Parameters:
eventClassName
- the name of one of the class in the packagecom.openinventor.inventor.events
-
getEvent
public SoEvent getEvent()
Returns the event currently being handled, or NULL if traversal is not taking place. This should be called only from callback .
-
setHandled
public void setHandled()
Tells the node the event was handled. The callback is responsible for setting whether the event was handled or not. If there is more than one callback registered with anSoEventCallback
node, all of them will be invoked, regardless of whether one has handled the event or not. This should be called only from callback .
-
getPath
public SoPath getPath()
Gets the path which must be picked in order for the callbacks to be invoked.
-
getPickedPoint
public SoPickedPoint getPickedPoint()
Returns pick information duringSoHandleEventAction
traversal, or NULL if traversal is not taking place. This should be called only from callback .When this method is called, an
SoRayPickAction
is automatically applied to the scene graph that theSoHandleEventAction
is traversing, using the current event. However this is only done once for eachSoHandleEventAction
traversal and the result is cached for subsequent queries during the current traversal.Note: The handle event action does not enable computation of normal vectors and texture coordinates in the pick action that it creates internally. If you need the normal vector at the point of intersection of the pick ray, then you must create your own pick action and apply it. In this case you can get the node to apply the pick action to by calling the handle event action's getPickRoot() method.
-
getAction
public SoHandleEventAction getAction()
Returns theSoHandleEventAction
currently traversing this node, or NULL if traversal is not taking place. This should be called only from callback .
-
isHandled
public boolean isHandled()
Returns whether the event has been handled. This should be called only from callback .
-
releaseEvents
public void releaseEvents()
Tells the event callback node to release the grab of events. Event grabbing is requested usinggrabEvents()
.
-
setPath
public void setPath(SoPath path)
Sets the path which must be picked in order for the callbacks to be invoked. If the path is NULL, the callbacks will be invoked for every interesting event, as specified by addEventCallback(), regardless of what is picked. ThesetPath()
method makes its own copy of the passed path.
-
grabEvents
public void grabEvents()
Tells the event callback node to grab events. While grabbing, the node will consume all events; however, each callback will only be invoked for events of interest.
-
-