📄 defaultdominanthandbehavior.java
字号:
package org.j3de.behavior.impl;
import java.io.IOException;
import java.io.NotActiveException;
import java.io.ObjectInputStream;
import java.util.Enumeration;
import javax.swing.event.EventListenerList;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.PickRay;
import javax.media.j3d.PickShape;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Matrix3d;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.j3de.behavior.DominantHandBehavior;
import org.j3de.behavior.FocusListener;
import org.j3de.behavior.FocusEvent;
import org.j3de.exception.ExceptionHandler;
import org.j3de.util.Configurable;
import org.j3de.util.ConfigHelper;
import org.j3de.util.ConfigurationException;
import org.j3de.util.StringParser;
public class DefaultDominantHandBehavior extends DominantHandBehavior implements Configurable {
private transient Transform3D sensorTransform;
private transient TransformGroup transformGroup;
private transient int[] buttonValues;
private transient int[] oldButtonValues;
private transient WakeupOnElapsedFrames wakeup;
private transient Vector3d translation;
private transient EventListenerList focusListenerList;
private Vector3d neutralPosition;
private int focusButton;
public void initialize() {
setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0));
sensorTransform = new Transform3D();
translation = new Vector3d();
buttonValues = new int[getSensor().getSensorButtonCount()];
oldButtonValues = new int[getSensor().getSensorButtonCount()];
wakeup = new WakeupOnElapsedFrames(1);
wakeupOn(wakeup);
}
public void setTransformGroup(TransformGroup group) {
this.transformGroup = group;
}
protected void fireFocusEvent(PickShape shape) {
FocusEvent event = new FocusEvent(this, shape);
// Guaranteed to return a non-null array
Object[] listeners = focusListenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FocusListener.class) {
// Lazily create the event:
((FocusListener)listeners[i+1]).focusChanged(event);
}
}
}
public void buttonDown(int button) {
}
public void buttonUp(int button) {
// The button pressed was the button used to Focus items : Fire Event.
if (button == focusButton) {
Vector3d view_position = new Vector3d();
Vector3d hand_position = new Vector3d();
// determine our position
Transform3D localToVWorld = new Transform3D();
getLocalToVworld(localToVWorld);
localToVWorld.get(view_position);
// determine hand position
localToVWorld.mul(sensorTransform);
localToVWorld.get(hand_position);
// direction = hand_position - view_position
Vector3d direction = new Vector3d(hand_position);
direction.sub(view_position);
fireFocusEvent(new PickRay(new Point3d(hand_position), direction));
}
}
public void processStimulus(Enumeration criteria) {
// read current transformation
getSensor().getRead(sensorTransform);
// get button-readings
System.arraycopy(buttonValues, 0, oldButtonValues, 0, buttonValues.length);
getSensor().lastButtons(buttonValues);
sensorTransform.get(translation);
translation.add(neutralPosition);
sensorTransform.setTranslation(translation);
// set transformation of TransformGroup
transformGroup.setTransform(sensorTransform);
// look at button-values and create Events
for (int i=0; i<buttonValues.length; i++) {
if ((oldButtonValues[i] == 0) && (buttonValues[i] != 0)) {
buttonDown(i);
}
if ((oldButtonValues[i] != 0) && (buttonValues[i] == 0)) {
buttonUp(i);
}
}
wakeupOn(wakeup);
}
public void addFocusListener(FocusListener listener) {
focusListenerList.add(FocusListener.class, listener);
}
public void removeFocusListener(FocusListener listener) {
focusListenerList.remove(FocusListener.class, listener);
}
/** Read Object from InputStream and initialize EventListenerList, because
* EventListeners can be added, before initalize() is called.
* Also Capabilities have to be set, because they'd get lost, when set in
* the constructor.
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
try {
in.defaultReadObject();
} catch (NotActiveException e) {
ExceptionHandler.handleException(e);
}
focusListenerList = new EventListenerList();
this.setCapability(ALLOW_LOCAL_TO_VWORLD_READ);
}
//////////////////////////////////////////////////////////
// Implementation of Configurable interface .. //
//////////////////////////////////////////////////////////
protected transient ConfigHelper helper;
public void configure(Node node, Document nodeFactory) throws ConfigurationException {
if (helper == null)
helper = new ConfigHelper(node, nodeFactory, false);
neutralPosition = StringParser.parseVector3d(helper.getPropertyValue("neutralPosition", "0.0 0.0 0.0", true));
focusButton = helper.getPropertyValue("focusbutton", 0, true);
}
public Node configure(Document nodeFactory, String componentName) throws ConfigurationException {
Element component = ConfigHelper.newComponent(nodeFactory, componentName, this);
helper = new ConfigHelper(component, nodeFactory, true);
configure(component, nodeFactory);
return component;
}
public boolean isConfigurationChanged() {
return helper.isConfigurationChanged();
}
public void configurationSaved() {
helper.configurationSaved();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -