Skip to content

Commit 98f197d

Browse files
committed
Implemented delete hotkey in a scene editor.
1 parent 7070f28 commit 98f197d

File tree

9 files changed

+139
-30
lines changed

9 files changed

+139
-30
lines changed

src/com/ss/editor/state/editor/impl/AdvancedAbstractEditorAppState.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.jme3.math.Vector3f;
1919
import com.jme3.renderer.Camera;
2020
import com.jme3.scene.Node;
21+
import com.ss.editor.annotation.EditorThread;
2122
import com.ss.editor.model.EditorCamera;
2223
import com.ss.editor.ui.component.editor.FileEditor;
2324

@@ -440,6 +441,7 @@ protected EditorCamera getEditorCamera() {
440441
}
441442

442443
@Override
444+
@EditorThread
443445
public void initialize(@NotNull final AppStateManager stateManager, @NotNull final Application application) {
444446
super.initialize(stateManager, application);
445447
this.stateManager = stateManager;
@@ -498,6 +500,7 @@ protected void registerActionListener(@NotNull final InputManager inputManager)
498500
}
499501

500502
@Override
503+
@EditorThread
501504
public void cleanup() {
502505
super.cleanup();
503506

@@ -577,27 +580,45 @@ protected DirectionalLight getLightForCamera() {
577580
return lightForCamera;
578581
}
579582

580-
public float getPrevHRotation() {
583+
/**
584+
* @return the previous horizontal camera rotation.
585+
*/
586+
protected float getPrevHRotation() {
581587
return prevHRotation;
582588
}
583589

584-
public void setPrevHRotation(final float prevHRotation) {
590+
/**
591+
* @param prevHRotation the previous horizontal camera rotation.
592+
*/
593+
protected void setPrevHRotation(final float prevHRotation) {
585594
this.prevHRotation = prevHRotation;
586595
}
587596

588-
public float getPrevTargetDistance() {
597+
/**
598+
* @return the previous camera zoom.
599+
*/
600+
protected float getPrevTargetDistance() {
589601
return prevTargetDistance;
590602
}
591603

592-
public void setPrevTargetDistance(final float prevTargetDistance) {
604+
/**
605+
* @param prevTargetDistance the previous camera zoom.
606+
*/
607+
protected void setPrevTargetDistance(final float prevTargetDistance) {
593608
this.prevTargetDistance = prevTargetDistance;
594609
}
595610

596-
public float getPrevVRotation() {
611+
/**
612+
* @return the previous vertical camera rotation.
613+
*/
614+
protected float getPrevVRotation() {
597615
return prevVRotation;
598616
}
599617

600-
public void setPrevVRotation(final float prevVRotation) {
618+
/**
619+
* @param prevVRotation the previous vertical camera rotation.
620+
*/
621+
protected void setPrevVRotation(final float prevVRotation) {
601622
this.prevVRotation = prevVRotation;
602623
}
603624

@@ -677,6 +698,7 @@ protected void notifyChangedCamera(@NotNull final Vector3f cameraLocation, final
677698
/**
678699
* Update the editor camera.
679700
*/
701+
@EditorThread
680702
public void updateCamera(@NotNull final Vector3f cameraLocation, final float hRotation,
681703
final float vRotation, final float targetDistance) {
682704

src/com/ss/editor/state/editor/impl/model/ModelEditorAppState.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.jme3.environment.generation.JobProgressAdapter;
66
import com.jme3.light.DirectionalLight;
77
import com.jme3.light.LightProbe;
8-
import com.jme3.math.Vector3f;
98
import com.jme3.scene.Node;
109
import com.jme3.scene.Spatial;
1110
import com.ss.editor.state.editor.impl.scene.AbstractSceneEditorAppState;
@@ -275,12 +274,6 @@ public void updateLightProbe() {
275274
});
276275
}
277276

278-
@Override
279-
protected void notifyChangedCamera(@NotNull final Vector3f cameraLocation, final float hRotation,
280-
final float vRotation, final float targetDistance) {
281-
EXECUTOR_MANAGER.addFXTask(() -> getFileEditor().notifyChangedCamera(cameraLocation, hRotation, vRotation, targetDistance));
282-
}
283-
284277
@Override
285278
public String toString() {
286279
return "ModelEditorAppState{" +

src/com/ss/editor/state/editor/impl/scene/AbstractSceneEditorAppState.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public abstract class AbstractSceneEditorAppState<T extends AbstractSceneFileEdi
7474
protected static final String KEY_S = "SSEditor.editorState.S";
7575
protected static final String KEY_G = "SSEditor.editorState.G";
7676
protected static final String KEY_R = "SSEditor.editorState.R";
77+
protected static final String KEY_DEL = "SSEditor.editorState.Del";
7778

7879
private static final float H_ROTATION = AngleUtils.degreeToRadians(45);
7980
private static final float V_ROTATION = AngleUtils.degreeToRadians(15);
@@ -82,6 +83,7 @@ public abstract class AbstractSceneEditorAppState<T extends AbstractSceneFileEdi
8283
TRIGGERS.put(KEY_S, new KeyTrigger(KeyInput.KEY_S));
8384
TRIGGERS.put(KEY_G, new KeyTrigger(KeyInput.KEY_G));
8485
TRIGGERS.put(KEY_R, new KeyTrigger(KeyInput.KEY_R));
86+
TRIGGERS.put(KEY_DEL, new KeyTrigger(KeyInput.KEY_DELETE));
8587
}
8688

8789
/**
@@ -283,15 +285,16 @@ protected void registerActionHandlers(@NotNull final ObjectDictionary<String, Bo
283285

284286
final T fileEditor = getFileEditor();
285287

286-
actionHandlers.put(KEY_S, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.S, isControlDown()));
287-
actionHandlers.put(KEY_G, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.G, isControlDown()));
288-
actionHandlers.put(KEY_R, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.R, isControlDown()));
288+
actionHandlers.put(KEY_S, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.S, isPressed, isControlDown()));
289+
actionHandlers.put(KEY_G, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.G, isPressed, isControlDown()));
290+
actionHandlers.put(KEY_R, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.R, isPressed, isControlDown()));
291+
actionHandlers.put(KEY_DEL, (isPressed, tpf) -> fileEditor.handleKeyAction(KeyCode.DELETE, isPressed, isControlDown()));
289292
}
290293

291294
@Override
292295
protected void registerActionListener(@NotNull final InputManager inputManager) {
293296
super.registerActionListener(inputManager);
294-
inputManager.addListener(actionListener, KEY_S, KEY_G, KEY_R);
297+
inputManager.addListener(actionListener, KEY_S, KEY_G, KEY_R, KEY_DEL);
295298
}
296299

297300
@Override
@@ -1482,4 +1485,10 @@ public EditorAudioNode getAudioNode(@NotNull final AudioNode audioNode) {
14821485
public EditorAudioNode getAudioNode(@NotNull final Spatial model) {
14831486
return getAudioNodes().search(model, (node, toCheck) -> node.getModel() == toCheck);
14841487
}
1488+
1489+
@Override
1490+
protected void notifyChangedCamera(@NotNull final Vector3f cameraLocation, final float hRotation,
1491+
final float vRotation, final float targetDistance) {
1492+
EXECUTOR_MANAGER.addFXTask(() -> getFileEditor().notifyChangedCamera(cameraLocation, hRotation, vRotation, targetDistance));
1493+
}
14851494
}

src/com/ss/editor/ui/component/editor/impl/scene/AbstractSceneFileEditor.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.jme3.scene.Geometry;
2020
import com.jme3.scene.Node;
2121
import com.jme3.scene.Spatial;
22+
import com.jme3.scene.control.Control;
2223
import com.ss.editor.FileExtensions;
2324
import com.ss.editor.Messages;
2425
import com.ss.editor.annotation.EditorThread;
@@ -44,6 +45,9 @@
4445
import com.ss.editor.ui.control.model.property.operation.ModelPropertyOperation;
4546
import com.ss.editor.ui.control.model.tree.ModelNodeTree;
4647
import com.ss.editor.ui.control.model.tree.action.operation.AddChildOperation;
48+
import com.ss.editor.ui.control.model.tree.action.operation.RemoveChildOperation;
49+
import com.ss.editor.ui.control.model.tree.action.operation.RemoveControlOperation;
50+
import com.ss.editor.ui.control.model.tree.action.operation.RemoveLightOperation;
4751
import com.ss.editor.ui.control.tree.node.ModelNode;
4852
import com.ss.editor.ui.css.CSSClasses;
4953
import com.ss.editor.ui.css.CSSIds;
@@ -373,31 +377,34 @@ protected void processKeyReleased(@NotNull final KeyEvent event) {
373377

374378
final KeyCode code = event.getCode();
375379

376-
if (handleKeyActionImpl(code, event.isControlDown())) {
380+
if (handleKeyActionImpl(code, false, event.isControlDown())) {
377381
event.consume();
378382
}
379383
}
380384

381385
/**
382386
* Handle a key code.
383387
*
388+
* @param isPressed true if key is pressed.
384389
* @param isControlDown true if control is down.
385390
* @param keyCode the key code.
386391
*/
387392
@FromAnyThread
388-
public void handleKeyAction(@NotNull final KeyCode keyCode, final boolean isControlDown) {
389-
EXECUTOR_MANAGER.addFXTask(() -> handleKeyActionImpl(keyCode, isControlDown));
393+
public void handleKeyAction(@NotNull final KeyCode keyCode, final boolean isPressed, final boolean isControlDown) {
394+
EXECUTOR_MANAGER.addFXTask(() -> handleKeyActionImpl(keyCode, isPressed, isControlDown));
390395
}
391396

392397
/**
393398
* Handle a key code.
394399
*
395400
* @param keyCode the key code.
401+
* @param isPressed true if key is pressed.
396402
* @param isControlDown true if control is down.
397403
* @return true if can consume an event.
398404
*/
399405
@EditorThread
400-
protected boolean handleKeyActionImpl(@NotNull final KeyCode keyCode, final boolean isControlDown) {
406+
protected boolean handleKeyActionImpl(@NotNull final KeyCode keyCode, final boolean isPressed, final boolean isControlDown) {
407+
if (isPressed) return false;
401408

402409
if (isControlDown && keyCode == KeyCode.Z) {
403410
undo();
@@ -416,6 +423,28 @@ protected boolean handleKeyActionImpl(@NotNull final KeyCode keyCode, final bool
416423
} else if (keyCode == KeyCode.S) {
417424
final ToggleButton scaleToolButton = getScaleToolButton();
418425
scaleToolButton.setSelected(true);
426+
return true;
427+
} else if (keyCode == KeyCode.DELETE) {
428+
429+
final ModelNodeTree modelNodeTree = getModelNodeTree();
430+
final ModelNode<?> selected = modelNodeTree.getSelected();
431+
if (selected == null || !selected.canRemove()) return false;
432+
433+
final Object element = selected.getElement();
434+
final ModelNode<?> parent = selected.getParent();
435+
final Object parentElement = parent == null ? null : parent.getElement();
436+
437+
if (element instanceof Spatial) {
438+
final Spatial spatial = (Spatial) element;
439+
execute(new RemoveChildOperation(spatial, spatial.getParent()));
440+
} else if (element instanceof Light && parentElement instanceof Node) {
441+
final Light light = (Light) element;
442+
execute(new RemoveLightOperation(light, (Node) parentElement));
443+
} else if (element instanceof Control && parentElement instanceof Spatial) {
444+
final Control control = (Control) element;
445+
execute(new RemoveControlOperation(control, (Spatial) parentElement));
446+
}
447+
419448
return true;
420449
}
421450

src/com/ss/editor/ui/control/model/node/spatial/ParticleNodeModelNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Image getIcon() {
2525
}
2626

2727
@Override
28-
protected boolean canRemove() {
28+
public boolean canRemove() {
2929
return false;
3030
}
3131

src/com/ss/editor/ui/control/model/node/spatial/SpatialModelNode.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
import com.ss.editor.control.transform.SceneEditorControl;
1313
import com.ss.editor.model.undo.editor.ChangeConsumer;
1414
import com.ss.editor.ui.Icons;
15+
import com.ss.editor.ui.control.model.node.control.ControlModelNode;
16+
import com.ss.editor.ui.control.model.node.light.LightModelNode;
1517
import com.ss.editor.ui.control.model.tree.action.AddUserDataAction;
1618
import com.ss.editor.ui.control.model.tree.action.RemoveNodeAction;
1719
import com.ss.editor.ui.control.model.tree.action.RenameNodeAction;
1820
import com.ss.editor.ui.control.model.tree.action.operation.RenameNodeOperation;
1921
import com.ss.editor.ui.control.tree.AbstractNodeTree;
2022
import com.ss.editor.ui.control.tree.node.ModelNode;
21-
import com.ss.editor.ui.control.model.node.control.ControlModelNode;
22-
import com.ss.editor.ui.control.model.node.light.LightModelNode;
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.jetbrains.annotations.Nullable;
@@ -51,10 +51,8 @@ public void fillContextMenu(@NotNull final AbstractNodeTree<?> nodeTree, @NotNul
5151
super.fillContextMenu(nodeTree, items);
5252
}
5353

54-
/**
55-
* @return true if you can remove this node.
56-
*/
57-
protected boolean canRemove() {
54+
@Override
55+
public boolean canRemove() {
5856
final Node parent = getElement().getParent();
5957
return parent != null && parent.getUserData(SceneEditorControl.class.getName()) != Boolean.TRUE;
6058
}

src/com/ss/editor/ui/control/model/node/spatial/emitter/ParticleGeometryModelNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import com.ss.editor.Messages;
44
import com.ss.editor.ui.Icons;
5+
import com.ss.editor.ui.control.model.node.spatial.GeometryModelNode;
56
import com.ss.editor.ui.control.model.tree.action.emitter.mesh.CreateImpostorParticleMeshAction;
67
import com.ss.editor.ui.control.model.tree.action.emitter.mesh.CreatePointParticleMeshAction;
78
import com.ss.editor.ui.control.model.tree.action.emitter.mesh.CreateQuadParticleMeshAction;
89
import com.ss.editor.ui.control.model.tree.action.emitter.mesh.LoadModelParticlesMeshAction;
910
import com.ss.editor.ui.control.tree.AbstractNodeTree;
10-
import com.ss.editor.ui.control.model.node.spatial.GeometryModelNode;
1111

1212
import org.jetbrains.annotations.NotNull;
1313
import org.jetbrains.annotations.Nullable;
@@ -52,7 +52,7 @@ public void fillContextMenu(@NotNull final AbstractNodeTree<?> nodeTree, @NotNul
5252
}
5353

5454
@Override
55-
protected boolean canRemove() {
55+
public boolean canRemove() {
5656
return false;
5757
}
5858

0 commit comments

Comments
 (0)