- Formatted SelectTool class

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9605 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 13 years ago
parent 7beda908c1
commit 68e17702e2
  1. 345
      sdk/jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/SelectTool.java

@ -44,26 +44,23 @@ import org.openide.util.Lookup;
public class SelectTool extends SceneEditTool { public class SelectTool extends SceneEditTool {
protected Spatial selected; protected Spatial selected;
private enum State {translate, rotate, scale}; private enum State {
translate, rotate, scale
};
private State currentState = null; private State currentState = null;
private Vector3f currentAxis = Vector3f.UNIT_XYZ; private Vector3f currentAxis = Vector3f.UNIT_XYZ;
private StringBuilder numberBuilder = new StringBuilder(); // gets appended with numbers private StringBuilder numberBuilder = new StringBuilder(); // gets appended with numbers
private Quaternion startRot; private Quaternion startRot;
private Vector3f startTrans; private Vector3f startTrans;
private Vector3f startScale; private Vector3f startScale;
private boolean wasDraggingL = false; private boolean wasDraggingL = false;
private boolean wasDraggingR = false; private boolean wasDraggingR = false;
private boolean wasDownR = false; private boolean wasDownR = false;
private boolean ctrlDown = false; private boolean ctrlDown = false;
private boolean shiftDown = false; private boolean shiftDown = false;
private boolean altDown = false; private boolean altDown = false;
private MoveManager.MoveUndo moving; private MoveManager.MoveUndo moving;
private ScaleUndo scaling; private ScaleUndo scaling;
private RotateUndo rotating; private RotateUndo rotating;
@ -90,12 +87,12 @@ public class SelectTool extends SceneEditTool {
*/ */
@Override @Override
public void keyPressed(KeyInputEvent kie) { public void keyPressed(KeyInputEvent kie) {
checkModificatorKeys(kie); // alt,shift,ctrl checkModificatorKeys(kie); // alt,shift,ctrl
if (selected == null) if (selected == null) {
return; // only do anything if a spatial is selected return; // only do anything if a spatial is selected
}
// key released // key released
if (kie.isReleased()) { if (kie.isReleased()) {
boolean commandUsed = checkCommandKey(kie); boolean commandUsed = checkCommandKey(kie);
@ -104,59 +101,62 @@ public class SelectTool extends SceneEditTool {
boolean numberChange = checkNumberKey(kie); boolean numberChange = checkNumberKey(kie);
boolean enterHit = checkEnterHit(kie); boolean enterHit = checkEnterHit(kie);
boolean escHit = checkEscHit(kie); boolean escHit = checkEscHit(kie);
if (commandUsed) if (commandUsed) {
return; // commands take priority return; // commands take priority
}
if (stateChange) { if (stateChange) {
currentAxis = Vector3f.UNIT_XYZ; currentAxis = Vector3f.UNIT_XYZ;
numberBuilder = new StringBuilder(); numberBuilder = new StringBuilder();
recordInitialState(selected); recordInitialState(selected);
} } else if (axisChange) {
else if (axisChange) {} } else if (numberChange) {
else if (numberChange) {} } else if (enterHit) {
else if (enterHit) {
if (currentState != null && numberBuilder.length() > 0) { if (currentState != null && numberBuilder.length() > 0) {
applyKeyedChangeState(selected); applyKeyedChangeState(selected);
clearState(false); clearState(false);
} }
} }
// ----------------------- // -----------------------
// reset conditions below: // reset conditions below:
if (escHit) { if (escHit) {
if (moving != null) if (moving != null) {
moving.sceneUndo(); moving.sceneUndo();
}
moving = null; moving = null;
clearState(); clearState();
} }
if (!stateChange && !axisChange && !numberChange && !enterHit && !escHit) { if (!stateChange && !axisChange && !numberChange && !enterHit && !escHit) {
// nothing valid was hit, reset the state // nothing valid was hit, reset the state
//clearState(); // this will be //clearState(); // this will be
} }
} }
} }
/** /**
* Abort any manipulations * Abort any manipulations
*/ */
private void clearState() { private void clearState() {
clearState(true); clearState(true);
} }
private void clearState(boolean resetSelected) { private void clearState(boolean resetSelected) {
if (resetSelected && selected != null) { if (resetSelected && selected != null) {
// reset the transforms // reset the transforms
if (startRot != null) if (startRot != null) {
selected.setLocalRotation(startRot); selected.setLocalRotation(startRot);
if (startTrans != null) }
if (startTrans != null) {
selected.setLocalTranslation(startTrans); selected.setLocalTranslation(startTrans);
if (startScale != null) }
if (startScale != null) {
selected.setLocalScale(startScale); selected.setLocalScale(startScale);
}
} }
currentState = null; currentState = null;
currentAxis = Vector3f.UNIT_XYZ; currentAxis = Vector3f.UNIT_XYZ;
@ -167,14 +167,13 @@ public class SelectTool extends SceneEditTool {
startMouseCoord = null; startMouseCoord = null;
startSelectedCoord = null; startSelectedCoord = null;
} }
private void recordInitialState(Spatial selected) { private void recordInitialState(Spatial selected) {
startRot = selected.getLocalRotation().clone(); startRot = selected.getLocalRotation().clone();
startTrans = selected.getLocalTranslation().clone(); startTrans = selected.getLocalTranslation().clone();
startScale = selected.getLocalScale().clone(); startScale = selected.getLocalScale().clone();
} }
/** /**
* Applies the changes entered by a number, not by mouse. * Applies the changes entered by a number, not by mouse.
* Translate: adds the value to the current local translation * Translate: adds the value to the current local translation
@ -185,10 +184,10 @@ public class SelectTool extends SceneEditTool {
Float value = null; Float value = null;
try { try {
value = new Float(numberBuilder.toString()); value = new Float(numberBuilder.toString());
} catch (NumberFormatException e){ } catch (NumberFormatException e) {
return; return;
} }
if (currentState == State.translate) { if (currentState == State.translate) {
MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class); MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class);
moveManager.moveAcross(currentAxis, value, toolController.isSnapToGrid()); moveManager.moveAcross(currentAxis, value, toolController.isSnapToGrid());
@ -196,14 +195,14 @@ public class SelectTool extends SceneEditTool {
actionPerformed(moving); actionPerformed(moving);
moving = null; moving = null;
} else if (currentState == State.scale) { } else if (currentState == State.scale) {
float x = 1, y= 1, z = 1; float x = 1, y = 1, z = 1;
if (currentAxis == Vector3f.UNIT_X) if (currentAxis == Vector3f.UNIT_X) {
x = value; x = value;
else if (currentAxis == Vector3f.UNIT_Y) } else if (currentAxis == Vector3f.UNIT_Y) {
y = value; y = value;
else if (currentAxis == Vector3f.UNIT_Z) } else if (currentAxis == Vector3f.UNIT_Z) {
z = value; z = value;
else if (currentAxis == Vector3f.UNIT_XYZ) { } else if (currentAxis == Vector3f.UNIT_XYZ) {
x = value; x = value;
y = value; y = value;
z = value; z = value;
@ -213,17 +212,18 @@ public class SelectTool extends SceneEditTool {
selected.setLocalScale(after); selected.setLocalScale(after);
actionPerformed(new ScaleUndo(selected, before, after)); actionPerformed(new ScaleUndo(selected, before, after));
} else if (currentState == State.rotate) { } else if (currentState == State.rotate) {
float x = 0, y= 0, z = 0; float x = 0, y = 0, z = 0;
if (currentAxis == Vector3f.UNIT_X) if (currentAxis == Vector3f.UNIT_X) {
x = 1; x = 1;
else if (currentAxis == Vector3f.UNIT_Y) } else if (currentAxis == Vector3f.UNIT_Y) {
y = 1; y = 1;
else if (currentAxis == Vector3f.UNIT_Z) } else if (currentAxis == Vector3f.UNIT_Z) {
z = 1; z = 1;
Vector3f axis = new Vector3f(x,y,z); }
Vector3f axis = new Vector3f(x, y, z);
Quaternion initialRot = selected.getLocalRotation().clone(); Quaternion initialRot = selected.getLocalRotation().clone();
Quaternion rot = new Quaternion(); Quaternion rot = new Quaternion();
rot = rot.fromAngleAxis(value*FastMath.DEG_TO_RAD, axis); rot = rot.fromAngleAxis(value * FastMath.DEG_TO_RAD, axis);
selected.setLocalRotation(selected.getLocalRotation().mult(rot)); selected.setLocalRotation(selected.getLocalRotation().mult(rot));
RotateUndo undo = new RotateUndo(selected, initialRot, rot); RotateUndo undo = new RotateUndo(selected, initialRot, rot);
actionPerformed(undo); actionPerformed(undo);
@ -232,18 +232,21 @@ public class SelectTool extends SceneEditTool {
} }
clearState(false); clearState(false);
} }
private void checkModificatorKeys(KeyInputEvent kie) { private void checkModificatorKeys(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_LCONTROL || kie.getKeyCode() == KeyInput.KEY_RCONTROL) if (kie.getKeyCode() == KeyInput.KEY_LCONTROL || kie.getKeyCode() == KeyInput.KEY_RCONTROL) {
ctrlDown = kie.isPressed(); ctrlDown = kie.isPressed();
}
if (kie.getKeyCode() == KeyInput.KEY_LSHIFT || kie.getKeyCode() == KeyInput.KEY_RSHIFT)
if (kie.getKeyCode() == KeyInput.KEY_LSHIFT || kie.getKeyCode() == KeyInput.KEY_RSHIFT) {
shiftDown = kie.isPressed(); shiftDown = kie.isPressed();
}
if (kie.getKeyCode() == KeyInput.KEY_LMENU || kie.getKeyCode() == KeyInput.KEY_RMENU)
if (kie.getKeyCode() == KeyInput.KEY_LMENU || kie.getKeyCode() == KeyInput.KEY_RMENU) {
altDown = kie.isPressed(); altDown = kie.isPressed();
}
} }
private boolean checkCommandKey(KeyInputEvent kie) { private boolean checkCommandKey(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_D) { if (kie.getKeyCode() == KeyInput.KEY_D) {
if (shiftDown) { if (shiftDown) {
@ -260,13 +263,13 @@ public class SelectTool extends SceneEditTool {
} }
return false; return false;
} }
private boolean checkStateKey(KeyInputEvent kie) { private boolean checkStateKey(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_G) { if (kie.getKeyCode() == KeyInput.KEY_G) {
currentState = State.translate; currentState = State.translate;
MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class); MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class);
moveManager.reset(); moveManager.reset();
Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI,Vector3f.UNIT_Y)); Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y));
moveManager.initiateMove(selected, rot, false); moveManager.initiateMove(selected, rot, false);
moving = moveManager.makeUndo(); moving = moveManager.makeUndo();
return true; return true;
@ -279,48 +282,48 @@ public class SelectTool extends SceneEditTool {
} }
return false; return false;
} }
private boolean checkAxisKey(KeyInputEvent kie) { private boolean checkAxisKey(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_X) { if (kie.getKeyCode() == KeyInput.KEY_X) {
currentAxis = Vector3f.UNIT_X; currentAxis = Vector3f.UNIT_X;
MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class); MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class);
Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI,Vector3f.UNIT_Y)); Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y));
Quaternion planRot = null; Quaternion planRot = null;
if(rot.dot(MoveManager.XY)<rot.dot(MoveManager.XZ)){ if (rot.dot(MoveManager.XY) < rot.dot(MoveManager.XZ)) {
planRot = MoveManager.XY; planRot = MoveManager.XY;
}else{ } else {
planRot = MoveManager.XZ; planRot = MoveManager.XZ;
} }
moveManager.updatePlaneRotation(planRot); moveManager.updatePlaneRotation(planRot);
return true; return true;
} else if (kie.getKeyCode() == KeyInput.KEY_Y) { } else if (kie.getKeyCode() == KeyInput.KEY_Y) {
currentAxis = Vector3f.UNIT_Y; currentAxis = Vector3f.UNIT_Y;
MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class); MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class);
Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI,Vector3f.UNIT_Y)); Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y));
Quaternion planRot = null; Quaternion planRot = null;
if(rot.dot(MoveManager.XY)<rot.dot(MoveManager.YZ)){ if (rot.dot(MoveManager.XY) < rot.dot(MoveManager.YZ)) {
planRot = MoveManager.XY; planRot = MoveManager.XY;
}else{ } else {
planRot = MoveManager.YZ; planRot = MoveManager.YZ;
} }
moveManager.updatePlaneRotation(planRot); moveManager.updatePlaneRotation(planRot);
return true; return true;
} else if (kie.getKeyCode() == KeyInput.KEY_Z) { } else if (kie.getKeyCode() == KeyInput.KEY_Z) {
currentAxis = Vector3f.UNIT_Z; currentAxis = Vector3f.UNIT_Z;
MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class); MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class);
Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI,Vector3f.UNIT_Y)); Quaternion rot = camera.getRotation().mult(new Quaternion().fromAngleAxis(FastMath.PI, Vector3f.UNIT_Y));
Quaternion planRot = null; Quaternion planRot = null;
if(rot.dot(MoveManager.XZ)<rot.dot(MoveManager.YZ)){ if (rot.dot(MoveManager.XZ) < rot.dot(MoveManager.YZ)) {
planRot = MoveManager.XZ; planRot = MoveManager.XZ;
}else{ } else {
planRot = MoveManager.YZ; planRot = MoveManager.YZ;
} }
moveManager.updatePlaneRotation(planRot); moveManager.updatePlaneRotation(planRot);
return true; return true;
} }
return false; return false;
} }
private boolean checkNumberKey(KeyInputEvent kie) { private boolean checkNumberKey(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_MINUS) { if (kie.getKeyCode() == KeyInput.KEY_MINUS) {
if (numberBuilder.length() > 0) { if (numberBuilder.length() > 0) {
@ -329,8 +332,9 @@ public class SelectTool extends SceneEditTool {
} else { } else {
numberBuilder.insert(0, '-'); numberBuilder.insert(0, '-');
} }
} else } else {
numberBuilder.append('-'); numberBuilder.append('-');
}
return true; return true;
} else if (kie.getKeyCode() == KeyInput.KEY_0 || kie.getKeyCode() == KeyInput.KEY_NUMPAD0) { } else if (kie.getKeyCode() == KeyInput.KEY_0 || kie.getKeyCode() == KeyInput.KEY_NUMPAD0) {
numberBuilder.append('0'); numberBuilder.append('0');
@ -363,33 +367,34 @@ public class SelectTool extends SceneEditTool {
numberBuilder.append('9'); numberBuilder.append('9');
return true; return true;
} else if (kie.getKeyCode() == KeyInput.KEY_PERIOD) { } else if (kie.getKeyCode() == KeyInput.KEY_PERIOD) {
if (numberBuilder.indexOf(".") == -1){ // if it doesn't exist yet if (numberBuilder.indexOf(".") == -1) { // if it doesn't exist yet
if (numberBuilder.length() == 0 || if (numberBuilder.length() == 0
(numberBuilder.length() == 1 && numberBuilder.charAt(0) == '-')) || (numberBuilder.length() == 1 && numberBuilder.charAt(0) == '-')) {
numberBuilder.append("0."); numberBuilder.append("0.");
else } else {
numberBuilder.append("."); numberBuilder.append(".");
}
} }
return true; return true;
} }
return false; return false;
} }
private boolean checkEnterHit(KeyInputEvent kie) { private boolean checkEnterHit(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_RETURN) { if (kie.getKeyCode() == KeyInput.KEY_RETURN) {
return true; return true;
} }
return false; return false;
} }
private boolean checkEscHit(KeyInputEvent kie) { private boolean checkEscHit(KeyInputEvent kie) {
if (kie.getKeyCode() == KeyInput.KEY_ESCAPE) { if (kie.getKeyCode() == KeyInput.KEY_ESCAPE) {
return true; return true;
} }
return false; return false;
} }
@Override @Override
public void actionPrimary(Vector2f screenCoord, boolean pressed, final JmeNode rootNode, DataObject dataObject) { public void actionPrimary(Vector2f screenCoord, boolean pressed, final JmeNode rootNode, DataObject dataObject) {
if (!pressed) { if (!pressed) {
@ -419,8 +424,9 @@ public class SelectTool extends SceneEditTool {
// mouse released and wasn't dragging, place cursor // mouse released and wasn't dragging, place cursor
final Vector3f result = pickWorldLocation(getCamera(), screenCoord, rootNode); final Vector3f result = pickWorldLocation(getCamera(), screenCoord, rootNode);
if (result != null) { if (result != null) {
if (toolController.isSnapToGrid()) if (toolController.isSnapToGrid()) {
result.set(Math.round(result.x), result.y, Math.round(result.z)); result.set(Math.round(result.x), result.y, Math.round(result.z));
}
toolController.doSetCursorLocation(result); toolController.doSetCursorLocation(result);
} }
} }
@ -433,7 +439,7 @@ public class SelectTool extends SceneEditTool {
public void actionSecondary(final Vector2f screenCoord, boolean pressed, final JmeNode rootNode, DataObject dataObject) { public void actionSecondary(final Vector2f screenCoord, boolean pressed, final JmeNode rootNode, DataObject dataObject) {
if (pressed) { if (pressed) {
// mouse down // mouse down
if (moving != null) { if (moving != null) {
moving.sceneUndo(); moving.sceneUndo();
moving = null; moving = null;
@ -446,25 +452,26 @@ public class SelectTool extends SceneEditTool {
rotating.sceneUndo(); rotating.sceneUndo();
rotating = null; rotating = null;
clearState(); clearState();
} } else if (!wasDraggingR && !wasDownR) { // wasn't dragging and was not down already
else if (!wasDraggingR && !wasDownR) { // wasn't dragging and was not down already
// pick on the spot // pick on the spot
Spatial s = pickWorldSpatial(camera, screenCoord, rootNode); Spatial s = pickWorldSpatial(camera, screenCoord, rootNode);
if (!toolController.selectTerrain() && isTerrain(s) ) { if (!toolController.selectTerrain() && isTerrain(s)) {
// only select non-terrain // only select non-terrain
selected = null; selected = null;
return; return;
} else { } else {
// climb up and find the Model Node (parent) and select that, don't select the geom // climb up and find the Model Node (parent) and select that, don't select the geom
Spatial linkNodeParent = findModelNodeParent(s); Spatial linkNodeParent = findModelNodeParent(s);
if (linkNodeParent != null) if (linkNodeParent != null) {
s = linkNodeParent; s = linkNodeParent;
else } else {
return; return;
}
final Spatial selec = s; final Spatial selec = s;
selected = selec; selected = selec;
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
if (selec != null) { if (selec != null) {
@ -477,7 +484,7 @@ public class SelectTool extends SceneEditTool {
} }
}); });
} }
toolController.updateSelection(selected); toolController.updateSelection(selected);
} }
wasDownR = true; wasDownR = true;
@ -487,28 +494,31 @@ public class SelectTool extends SceneEditTool {
wasDraggingR = false; wasDraggingR = false;
} }
} }
/** /**
* Climb up the spatial until we find the first node parent. * Climb up the spatial until we find the first node parent.
* TODO: use userData to determine the actual model's parent. * TODO: use userData to determine the actual model's parent.
*/ */
private Spatial findModelNodeParent(Spatial child) { private Spatial findModelNodeParent(Spatial child) {
if (child == null) if (child == null) {
return null; return null;
}
if (child instanceof Node)
if (child instanceof Node) {
return child; return child;
}
if (child.getParent() != null)
if (child.getParent() != null) {
return findModelNodeParent(child.getParent()); return findModelNodeParent(child.getParent());
}
return null; return null;
} }
@Override @Override
public void mouseMoved(Vector2f screenCoord, JmeNode rootNode, DataObject currentDataObject, JmeSpatial selectedSpatial) { public void mouseMoved(Vector2f screenCoord, JmeNode rootNode, DataObject currentDataObject, JmeSpatial selectedSpatial) {
if (currentState != null) { if (currentState != null) {
handleMouseManipulate(screenCoord, currentState, currentAxis, rootNode, currentDataObject, selectedSpatial); handleMouseManipulate(screenCoord, currentState, currentAxis, rootNode, currentDataObject, selectedSpatial);
} }
} }
@ -521,88 +531,92 @@ public class SelectTool extends SceneEditTool {
public void draggedSecondary(Vector2f screenCoord, boolean pressed, JmeNode rootNode, DataObject currentDataObject) { public void draggedSecondary(Vector2f screenCoord, boolean pressed, JmeNode rootNode, DataObject currentDataObject) {
wasDraggingR = pressed; wasDraggingR = pressed;
} }
/** /**
* Manipulate the spatial * Manipulate the spatial
*/ */
private void handleMouseManipulate( Vector2f screenCoord, private void handleMouseManipulate(Vector2f screenCoord,
State state, State state,
Vector3f axis, Vector3f axis,
JmeNode rootNode, JmeNode rootNode,
DataObject currentDataObject, DataObject currentDataObject,
JmeSpatial selectedSpatial) JmeSpatial selectedSpatial) {
{
if (state == State.translate) { if (state == State.translate) {
doMouseTranslate(axis, screenCoord, rootNode, selectedSpatial); doMouseTranslate(axis, screenCoord, rootNode, selectedSpatial);
} } else if (state == State.scale) {
else if (state == State.scale) {
doMouseScale(axis, screenCoord, rootNode, selectedSpatial); doMouseScale(axis, screenCoord, rootNode, selectedSpatial);
} } else if (state == State.rotate) {
else if (state == State.rotate) {
doMouseRotate(axis, screenCoord, rootNode, selectedSpatial); doMouseRotate(axis, screenCoord, rootNode, selectedSpatial);
} }
} }
private void doMouseTranslate(Vector3f axis, Vector2f screenCoord, JmeNode rootNode, JmeSpatial selectedSpatial) { private void doMouseTranslate(Vector3f axis, Vector2f screenCoord, JmeNode rootNode, JmeSpatial selectedSpatial) {
MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class); MoveManager moveManager = Lookup.getDefault().lookup(MoveManager.class);
if(toolController.isSnapToScene()){ if (toolController.isSnapToScene()) {
moveManager.setAlternativePickTarget(rootNode.getLookup().lookup(Node.class)); moveManager.setAlternativePickTarget(rootNode.getLookup().lookup(Node.class));
} }
// free form translation // free form translation
moveManager.move(camera, screenCoord, axis,toolController.isSnapToGrid()); moveManager.move(camera, screenCoord, axis, toolController.isSnapToGrid());
} }
private void doMouseScale(Vector3f axis, Vector2f screenCoord, JmeNode rootNode, JmeSpatial selectedSpatial) { private void doMouseScale(Vector3f axis, Vector2f screenCoord, JmeNode rootNode, JmeSpatial selectedSpatial) {
// scale based on the original mouse position and original model-to-screen position // scale based on the original mouse position and original model-to-screen position
// and compare that to the distance from the new mouse position and the original distance // and compare that to the distance from the new mouse position and the original distance
if (startMouseCoord == null) if (startMouseCoord == null) {
startMouseCoord = screenCoord.clone(); startMouseCoord = screenCoord.clone();
}
if (startSelectedCoord == null) { if (startSelectedCoord == null) {
Vector3f screen = getCamera().getScreenCoordinates(selected.getWorldTranslation()); Vector3f screen = getCamera().getScreenCoordinates(selected.getWorldTranslation());
startSelectedCoord = new Vector2f(screen.x, screen.y); startSelectedCoord = new Vector2f(screen.x, screen.y);
} }
if (scaling == null) if (scaling == null) {
scaling = new ScaleUndo(selected, selected.getLocalScale().clone(), null); scaling = new ScaleUndo(selected, selected.getLocalScale().clone(), null);
}
float origDist = startMouseCoord.distanceSquared(startSelectedCoord); float origDist = startMouseCoord.distanceSquared(startSelectedCoord);
float newDist = screenCoord.distanceSquared(startSelectedCoord); float newDist = screenCoord.distanceSquared(startSelectedCoord);
if (origDist == 0) if (origDist == 0) {
origDist = 1; origDist = 1;
float ratio = newDist/origDist; }
float ratio = newDist / origDist;
Vector3f prev = selected.getLocalScale(); Vector3f prev = selected.getLocalScale();
if (axis == Vector3f.UNIT_X) if (axis == Vector3f.UNIT_X) {
selected.setLocalScale(ratio, prev.y, prev.z); selected.setLocalScale(ratio, prev.y, prev.z);
else if (axis == Vector3f.UNIT_Y) } else if (axis == Vector3f.UNIT_Y) {
selected.setLocalScale(prev.x, ratio, prev.z); selected.setLocalScale(prev.x, ratio, prev.z);
else if (axis == Vector3f.UNIT_Z) } else if (axis == Vector3f.UNIT_Z) {
selected.setLocalScale(prev.x, prev.y, ratio); selected.setLocalScale(prev.x, prev.y, ratio);
else } else {
selected.setLocalScale(ratio, ratio, ratio); selected.setLocalScale(ratio, ratio, ratio);
}
} }
private void doMouseRotate(Vector3f axis, Vector2f screenCoord, JmeNode rootNode, JmeSpatial selectedSpatial) { private void doMouseRotate(Vector3f axis, Vector2f screenCoord, JmeNode rootNode, JmeSpatial selectedSpatial) {
if (startMouseCoord == null) if (startMouseCoord == null) {
startMouseCoord = screenCoord.clone(); startMouseCoord = screenCoord.clone();
}
if (startSelectedCoord == null) { if (startSelectedCoord == null) {
Vector3f screen = getCamera().getScreenCoordinates(selected.getWorldTranslation()); Vector3f screen = getCamera().getScreenCoordinates(selected.getWorldTranslation());
startSelectedCoord = new Vector2f(screen.x, screen.y); startSelectedCoord = new Vector2f(screen.x, screen.y);
} }
if (rotating == null) if (rotating == null) {
rotating = new RotateUndo(selected, selected.getLocalRotation().clone(), null); rotating = new RotateUndo(selected, selected.getLocalRotation().clone(), null);
}
Vector2f origRot = startMouseCoord.subtract(startSelectedCoord); Vector2f origRot = startMouseCoord.subtract(startSelectedCoord);
Vector2f newRot = screenCoord.subtract(startSelectedCoord); Vector2f newRot = screenCoord.subtract(startSelectedCoord);
float newRotAngle = origRot.angleBetween(newRot); float newRotAngle = origRot.angleBetween(newRot);
float temp = newRotAngle; float temp = newRotAngle;
if (lastRotAngle != 0) if (lastRotAngle != 0) {
newRotAngle -= lastRotAngle; newRotAngle -= lastRotAngle;
}
lastRotAngle = temp; lastRotAngle = temp;
Quaternion rotate = new Quaternion(); Quaternion rotate = new Quaternion();
if (axis != Vector3f.UNIT_XYZ) { if (axis != Vector3f.UNIT_XYZ) {
rotate = rotate.fromAngleAxis(newRotAngle, axis); rotate = rotate.fromAngleAxis(newRotAngle, axis);
@ -611,24 +625,26 @@ public class SelectTool extends SceneEditTool {
rotate = rotate.fromAngleAxis(newRotAngle, getCamera().getDirection()); rotate = rotate.fromAngleAxis(newRotAngle, getCamera().getDirection());
} }
selected.setLocalRotation(selected.getLocalRotation().mult(rotate)); selected.setLocalRotation(selected.getLocalRotation().mult(rotate));
} }
private void duplicateSelected() { private void duplicateSelected() {
if (selected == null) if (selected == null) {
return; return;
}
Spatial clone = selected.clone(); Spatial clone = selected.clone();
clone.move(1, 0, 1); clone.move(1, 0, 1);
selected.getParent().attachChild(clone); selected.getParent().attachChild(clone);
actionPerformed(new DuplicateUndo(clone, selected.getParent())); actionPerformed(new DuplicateUndo(clone, selected.getParent()));
selected = clone; selected = clone;
final Spatial cloned = clone; final Spatial cloned = clone;
final JmeNode rootNode = toolController.getRootNode(); final JmeNode rootNode = toolController.getRootNode();
refreshSelected(rootNode, selected.getParent()); refreshSelected(rootNode, selected.getParent());
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
if (cloned != null) { if (cloned != null) {
@ -640,47 +656,49 @@ public class SelectTool extends SceneEditTool {
} }
} }
}); });
// set to automatically 'grab'/'translate' the new cloned model // set to automatically 'grab'/'translate' the new cloned model
toolController.updateSelection(selected); toolController.updateSelection(selected);
currentState = State.translate; currentState = State.translate;
currentAxis = Vector3f.UNIT_XYZ; currentAxis = Vector3f.UNIT_XYZ;
} }
private void deleteSelected() { private void deleteSelected() {
if (selected == null) if (selected == null) {
return; return;
}
Node parent = selected.getParent(); Node parent = selected.getParent();
selected.removeFromParent(); selected.removeFromParent();
actionPerformed(new DeleteUndo(selected, parent)); actionPerformed(new DeleteUndo(selected, parent));
selected = null; selected = null;
toolController.updateSelection(selected); toolController.updateSelection(selected);
final JmeNode rootNode = toolController.getRootNode(); final JmeNode rootNode = toolController.getRootNode();
refreshSelected(rootNode, parent); refreshSelected(rootNode, parent);
} }
private void refreshSelected(final JmeNode jmeRootNode, final Node parent) { private void refreshSelected(final JmeNode jmeRootNode, final Node parent) {
java.awt.EventQueue.invokeLater(new Runnable() { java.awt.EventQueue.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
jmeRootNode.getChild(parent).refresh(false); jmeRootNode.getChild(parent).refresh(false);
} }
}); });
} }
private class ScaleUndo extends AbstractUndoableSceneEdit { private class ScaleUndo extends AbstractUndoableSceneEdit {
private Spatial spatial; private Spatial spatial;
private Vector3f before,after; private Vector3f before, after;
ScaleUndo(Spatial spatial, Vector3f before, Vector3f after) { ScaleUndo(Spatial spatial, Vector3f before, Vector3f after) {
this.spatial = spatial; this.spatial = spatial;
this.before = before; this.before = before;
this.after = after; this.after = after;
} }
@Override @Override
public void sceneUndo() { public void sceneUndo() {
spatial.setLocalScale(before); spatial.setLocalScale(before);
@ -691,18 +709,18 @@ public class SelectTool extends SceneEditTool {
spatial.setLocalScale(after); spatial.setLocalScale(after);
} }
} }
private class RotateUndo extends AbstractUndoableSceneEdit { private class RotateUndo extends AbstractUndoableSceneEdit {
private Spatial spatial; private Spatial spatial;
private Quaternion before,after; private Quaternion before, after;
RotateUndo(Spatial spatial, Quaternion before, Quaternion after) { RotateUndo(Spatial spatial, Quaternion before, Quaternion after) {
this.spatial = spatial; this.spatial = spatial;
this.before = before; this.before = before;
this.after = after; this.after = after;
} }
@Override @Override
public void sceneUndo() { public void sceneUndo() {
spatial.setLocalRotation(before); spatial.setLocalRotation(before);
@ -713,17 +731,17 @@ public class SelectTool extends SceneEditTool {
spatial.setLocalRotation(after); spatial.setLocalRotation(after);
} }
} }
private class DeleteUndo extends AbstractUndoableSceneEdit { private class DeleteUndo extends AbstractUndoableSceneEdit {
private Spatial spatial; private Spatial spatial;
private Node parent; private Node parent;
DeleteUndo(Spatial spatial, Node parent) { DeleteUndo(Spatial spatial, Node parent) {
this.spatial = spatial; this.spatial = spatial;
this.parent = parent; this.parent = parent;
} }
@Override @Override
public void sceneUndo() { public void sceneUndo() {
parent.attachChild(spatial); parent.attachChild(spatial);
@ -734,17 +752,17 @@ public class SelectTool extends SceneEditTool {
spatial.removeFromParent(); spatial.removeFromParent();
} }
} }
private class DuplicateUndo extends AbstractUndoableSceneEdit { private class DuplicateUndo extends AbstractUndoableSceneEdit {
private Spatial spatial; private Spatial spatial;
private Node parent; private Node parent;
DuplicateUndo(Spatial spatial, Node parent) { DuplicateUndo(Spatial spatial, Node parent) {
this.spatial = spatial; this.spatial = spatial;
this.parent = parent; this.parent = parent;
} }
@Override @Override
public void sceneUndo() { public void sceneUndo() {
spatial.removeFromParent(); spatial.removeFromParent();
@ -755,7 +773,7 @@ public class SelectTool extends SceneEditTool {
parent.attachChild(spatial); parent.attachChild(spatial);
} }
} }
/** /**
* Check if the selected item is a Terrain * Check if the selected item is a Terrain
* It will climb up the parent tree to see if * It will climb up the parent tree to see if
@ -763,15 +781,16 @@ public class SelectTool extends SceneEditTool {
* Recursive call. * Recursive call.
*/ */
protected boolean isTerrain(Spatial s) { protected boolean isTerrain(Spatial s) {
if (s == null) if (s == null) {
return false; return false;
if (s instanceof Terrain) }
if (s instanceof Terrain) {
return true; return true;
}
if (s.getParent() != null) { if (s.getParent() != null) {
return isTerrain(s.getParent()); return isTerrain(s.getParent());
} }
return false; return false;
} }
} }

Loading…
Cancel
Save