- added a shortcutManager that provide some usefull methodes for ShortcutTools. Also handle activation of shortcuts. - wip : implementation of the MoveShortcut (based on the same shortcut provided by the selectTool) using ShortcutTool and the ShortcutManager. - modified the SceneComposerToolController to work with the ShortcutManager & ShortcutToolexperimental
parent
82f031cdff
commit
f6b7c3819a
@ -0,0 +1,137 @@ |
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package com.jme3.gde.scenecomposer.tools.shortcuts; |
||||
|
||||
import com.jme3.asset.AssetManager; |
||||
import com.jme3.gde.core.sceneexplorer.nodes.JmeNode; |
||||
import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatial; |
||||
import com.jme3.gde.scenecomposer.SceneComposerToolController; |
||||
import com.jme3.gde.scenecomposer.tools.PickManager; |
||||
import com.jme3.input.KeyInput; |
||||
import com.jme3.input.event.KeyInputEvent; |
||||
import com.jme3.math.Vector2f; |
||||
import com.jme3.math.Vector3f; |
||||
import com.jme3.scene.Node; |
||||
import com.jme3.scene.Spatial; |
||||
import org.openide.loaders.DataObject; |
||||
import org.openide.util.Lookup; |
||||
|
||||
/** |
||||
* |
||||
* @author dokthar |
||||
*/ |
||||
public class MoveShortcut extends ShortcutTool { |
||||
|
||||
private Vector3f currentAxis; |
||||
private StringBuilder numberBuilder; |
||||
private Spatial spatial; |
||||
private Vector3f initalLocation; |
||||
private Vector3f finalLocation; |
||||
private PickManager pickManager; |
||||
|
||||
@Override |
||||
public boolean isActivableBy(KeyInputEvent kie) { |
||||
return kie.getKeyCode() == KeyInput.KEY_G; |
||||
} |
||||
|
||||
@Override |
||||
public void cancel() { |
||||
spatial.setLocalTranslation(initalLocation); |
||||
terminate(); |
||||
} |
||||
|
||||
private void apply() { |
||||
// TODO creat UNDO/REDO
|
||||
terminate(); |
||||
} |
||||
|
||||
@Override |
||||
public void activate(AssetManager manager, Node toolNode, Node onTopToolNode, Spatial selectedSpatial, SceneComposerToolController toolController) { |
||||
super.activate(manager, toolNode, onTopToolNode, selectedSpatial, toolController); //To change body of generated methods, choose Tools | Templates.
|
||||
hideMarker(); |
||||
numberBuilder = new StringBuilder(); |
||||
if (selectedSpatial == null) { |
||||
terminate(); |
||||
} else { |
||||
spatial = selectedSpatial; |
||||
initalLocation = spatial.getLocalTranslation(); |
||||
currentAxis = new Vector3f().set(Vector3f.UNIT_XYZ); |
||||
|
||||
pickManager = Lookup.getDefault().lookup(PickManager.class); |
||||
///pickManager.initiatePick(toolController.getSelectedSpatial(), PickManager.PLANE_YZ, getTransformType(), camera, screenCoord);
|
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void keyPressed(KeyInputEvent kie) { |
||||
if (kie.isPressed()) { |
||||
|
||||
/* |
||||
ShortcutTool otherShortcut = Lookup.getDefault().lookup(ShortcutManager.class).getActivableShortcut(kie); |
||||
if(otherShortcut != null){ |
||||
Lookup.getDefault().lookup(ShortcutManager.class).setShortCut(otherShortcut); |
||||
}*/ |
||||
Lookup.getDefault().lookup(ShortcutManager.class).activateShortcut(kie); |
||||
|
||||
boolean axisChanged = ShortcutManager.checkAxisKey(kie, currentAxis); |
||||
boolean numberChanged = ShortcutManager.checkNumberKey(kie, numberBuilder); |
||||
boolean enterHit = ShortcutManager.checkEnterHit(kie); |
||||
boolean escHit = ShortcutManager.checkEscHit(kie); |
||||
|
||||
if (escHit) { |
||||
cancel(); |
||||
} else if (enterHit) { |
||||
apply(); |
||||
} else if (axisChanged || numberChanged) { |
||||
//update transformation
|
||||
float number = ShortcutManager.getNumberkey(numberBuilder); |
||||
Vector3f translation = currentAxis.mult(number); |
||||
finalLocation = initalLocation.add(translation); |
||||
spatial.setLocalTranslation(finalLocation); |
||||
|
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void actionPrimary(Vector2f screenCoord, boolean pressed, JmeNode rootNode, DataObject dataObject) { |
||||
if (!pressed) { |
||||
apply(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void actionSecondary(Vector2f screenCoord, boolean pressed, JmeNode rootNode, DataObject dataObject) { |
||||
if (pressed) { |
||||
cancel(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void mouseMoved(Vector2f screenCoord, JmeNode rootNode, DataObject dataObject, JmeSpatial selectedSpatial) { |
||||
pickManager.updatePick(camera, screenCoord); |
||||
/* PickManager pickManager = Lookup.getDefault().lookup(PickManager.class); |
||||
if (toolController.isSnapToScene()) { |
||||
moveManager.setAlternativePickTarget(rootNode.getLookup().lookup(Node.class)); |
||||
} |
||||
// free form translation
|
||||
moveManager.move(camera, screenCoord, axis, toolController.isSnapToGrid());*/ |
||||
} |
||||
|
||||
@Override |
||||
public void draggedPrimary(Vector2f screenCoord, boolean pressed, JmeNode rootNode, DataObject currentDataObject) { |
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
} |
||||
|
||||
@Override |
||||
public void draggedSecondary(Vector2f screenCoord, boolean pressed, JmeNode rootNode, DataObject currentDataObject) { |
||||
if (pressed) { |
||||
cancel(); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,197 @@ |
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package com.jme3.gde.scenecomposer.tools.shortcuts; |
||||
|
||||
import com.jme3.gde.scenecomposer.SceneEditTool; |
||||
import com.jme3.input.KeyInput; |
||||
import com.jme3.input.event.KeyInputEvent; |
||||
import com.jme3.math.Vector3f; |
||||
import java.util.ArrayList; |
||||
import org.openide.util.Lookup; |
||||
import org.openide.util.lookup.ServiceProvider; |
||||
|
||||
/** |
||||
* |
||||
* @author dokthar |
||||
*/ |
||||
@ServiceProvider(service = ShortcutManager.class) |
||||
public class ShortcutManager { |
||||
|
||||
private ShortcutTool currentShortcut; |
||||
private ArrayList<ShortcutTool> shortcutList; |
||||
|
||||
public ShortcutManager() { |
||||
shortcutList = new ArrayList<ShortcutTool>(); |
||||
shortcutList.add(new MoveShortcut()); |
||||
} |
||||
|
||||
/* |
||||
Methodes |
||||
*/ |
||||
public void terminate() { |
||||
currentShortcut = null; |
||||
} |
||||
|
||||
public boolean isActive() { |
||||
return currentShortcut != null; |
||||
} |
||||
|
||||
public void setShortCut(ShortcutTool shortcut) { |
||||
if (isActive()) { |
||||
currentShortcut.cancel(); |
||||
} |
||||
currentShortcut = shortcut; |
||||
} |
||||
|
||||
public ShortcutTool getActivableShortcut(KeyInputEvent kie) { |
||||
for (ShortcutTool s : shortcutList) { |
||||
if (s != currentShortcut) { |
||||
if (s.isActivableBy(kie)) { |
||||
return s; |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public ShortcutTool getActiveShortcut() { |
||||
return currentShortcut; |
||||
} |
||||
|
||||
public boolean canActivateShortcut(KeyInputEvent kie) { |
||||
return getActivableShortcut(kie) != null; |
||||
} |
||||
|
||||
public boolean activateShortcut(KeyInputEvent kie) { |
||||
currentShortcut = getActivableShortcut(kie); |
||||
return isActive(); |
||||
} |
||||
|
||||
public void doKeyPressed(KeyInputEvent kie) { |
||||
///todo check commande key
|
||||
if (isActive()) { |
||||
currentShortcut.keyPressed(kie); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
STATIC |
||||
*/ |
||||
public static boolean checkEnterHit(KeyInputEvent kie) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_RETURN) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static boolean checkEscHit(KeyInputEvent kie) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_ESCAPE) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static boolean checkCtrlHit(KeyInputEvent kie) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_LCONTROL || kie.getKeyCode() == KeyInput.KEY_RCONTROL) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static boolean checkShiftHit(KeyInputEvent kie) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_LSHIFT || kie.getKeyCode() == KeyInput.KEY_RSHIFT) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static boolean checkAltHit(KeyInputEvent kie) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_LMENU || kie.getKeyCode() == KeyInput.KEY_RMENU) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static boolean checkNumberKey(KeyInputEvent kie, StringBuilder numberBuilder) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_MINUS) { |
||||
if (numberBuilder.length() > 0) { |
||||
if (numberBuilder.charAt(0) == '-') { |
||||
numberBuilder.replace(0, 1, ""); |
||||
} else { |
||||
numberBuilder.insert(0, '-'); |
||||
} |
||||
} else { |
||||
numberBuilder.append('-'); |
||||
} |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_0 || kie.getKeyCode() == KeyInput.KEY_NUMPAD0) { |
||||
numberBuilder.append('0'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_1 || kie.getKeyCode() == KeyInput.KEY_NUMPAD1) { |
||||
numberBuilder.append('1'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_2 || kie.getKeyCode() == KeyInput.KEY_NUMPAD2) { |
||||
numberBuilder.append('2'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_3 || kie.getKeyCode() == KeyInput.KEY_NUMPAD3) { |
||||
numberBuilder.append('3'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_4 || kie.getKeyCode() == KeyInput.KEY_NUMPAD4) { |
||||
numberBuilder.append('4'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_5 || kie.getKeyCode() == KeyInput.KEY_NUMPAD5) { |
||||
numberBuilder.append('5'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_6 || kie.getKeyCode() == KeyInput.KEY_NUMPAD6) { |
||||
numberBuilder.append('6'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_7 || kie.getKeyCode() == KeyInput.KEY_NUMPAD7) { |
||||
numberBuilder.append('7'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_8 || kie.getKeyCode() == KeyInput.KEY_NUMPAD8) { |
||||
numberBuilder.append('8'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_9 || kie.getKeyCode() == KeyInput.KEY_NUMPAD9) { |
||||
numberBuilder.append('9'); |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_PERIOD) { |
||||
if (numberBuilder.indexOf(".") == -1) { // if it doesn't exist yet
|
||||
if (numberBuilder.length() == 0 |
||||
|| (numberBuilder.length() == 1 && numberBuilder.charAt(0) == '-')) { |
||||
numberBuilder.append("0."); |
||||
} else { |
||||
numberBuilder.append("."); |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
public static float getNumberkey(StringBuilder numberBuilder) { |
||||
if (numberBuilder.length() == 0) { |
||||
return 0; |
||||
} else { |
||||
return new Float(numberBuilder.toString()); |
||||
} |
||||
} |
||||
|
||||
public static boolean checkAxisKey(KeyInputEvent kie, Vector3f axisStore) { |
||||
if (kie.getKeyCode() == KeyInput.KEY_X) { |
||||
axisStore = Vector3f.UNIT_X; |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_Y) { |
||||
axisStore = Vector3f.UNIT_Y; |
||||
return true; |
||||
} else if (kie.getKeyCode() == KeyInput.KEY_Z) { |
||||
axisStore = Vector3f.UNIT_Z; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,29 @@ |
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package com.jme3.gde.scenecomposer.tools.shortcuts; |
||||
|
||||
import com.jme3.gde.scenecomposer.SceneEditTool; |
||||
import com.jme3.input.event.KeyInputEvent; |
||||
import org.openide.util.Lookup; |
||||
|
||||
/** |
||||
* |
||||
* @author dokthar |
||||
*/ |
||||
public abstract class ShortcutTool extends SceneEditTool { |
||||
|
||||
public abstract boolean isActivableBy(KeyInputEvent kie); |
||||
|
||||
public abstract void cancel(); |
||||
|
||||
protected final void terminate() { |
||||
Lookup.getDefault().lookup(ShortcutManager.class).terminate(); |
||||
} |
||||
|
||||
@Override |
||||
public abstract void keyPressed(KeyInputEvent kie); |
||||
|
||||
} |
Loading…
Reference in new issue