Skip to content

Commit 2bab9e6

Browse files
committed
implemented D&D materials from asset tree to scene editor.
1 parent ff52d29 commit 2bab9e6

File tree

2 files changed

+106
-19
lines changed

2 files changed

+106
-19
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,11 +983,11 @@ private void processClick(final boolean isPressed) {
983983
* @return the position on a scene.
984984
*/
985985
@NotNull
986-
public Vector3f getCurrentCursorPosOnScene(final float worldX, final float worldY) {
986+
public Vector3f getScenePosByScreenPos(final float worldX, final float worldY) {
987987

988988
final Camera camera = EDITOR.getCamera();
989989

990-
final Vector2f cursor = new Vector2f(worldX, camera.getHeight() - worldY);
990+
final Vector2f cursor = new Vector2f(worldX, worldY);
991991
final Vector3f click3d = camera.getWorldCoordinates(cursor, 0f);
992992
final Vector3f dir = camera.getWorldCoordinates(cursor, 1f).subtractLocal(click3d).normalizeLocal();
993993

@@ -1007,6 +1007,38 @@ public Vector3f getCurrentCursorPosOnScene(final float worldX, final float world
10071007
return closestCollision.getContactPoint();
10081008
}
10091009

1010+
/**
1011+
* Get a geometry on a scene for a position on a screen.
1012+
*
1013+
* @param worldX the x position on screen.
1014+
* @param worldY the y position on screen.
1015+
* @return the position on a scene.
1016+
*/
1017+
@Nullable
1018+
public Geometry getGeometryByScreenPos(final float worldX, final float worldY) {
1019+
1020+
final Camera camera = EDITOR.getCamera();
1021+
1022+
final Vector2f cursor = new Vector2f(worldX, worldY);
1023+
final Vector3f click3d = camera.getWorldCoordinates(cursor, 0f);
1024+
final Vector3f dir = camera.getWorldCoordinates(cursor, 1f).subtractLocal(click3d).normalizeLocal();
1025+
1026+
final Ray ray = new Ray();
1027+
ray.setOrigin(click3d);
1028+
ray.setDirection(dir);
1029+
1030+
final CollisionResults results = new CollisionResults();
1031+
1032+
final M currentModel = requireNonNull(getCurrentModel());
1033+
currentModel.updateModelBound();
1034+
currentModel.collideWith(ray, results);
1035+
1036+
final CollisionResult closestCollision = results.getClosestCollision();
1037+
if (closestCollision == null) return null;
1038+
1039+
return closestCollision.getGeometry();
1040+
}
1041+
10101042
protected void notifySelected(@Nullable final Object object) {
10111043
}
10121044

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

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.editor.ui.component.editor.impl.scene;
22

3+
import static com.ss.editor.control.transform.SceneEditorControl.LOADED_MODEL_KEY;
34
import static com.ss.editor.util.EditorUtil.getAssetFile;
45
import static com.ss.editor.util.EditorUtil.toAssetPath;
56
import static com.ss.editor.util.MaterialUtils.updateMaterialIdNeed;
@@ -22,7 +23,6 @@
2223
import com.ss.editor.Messages;
2324
import com.ss.editor.annotation.FXThread;
2425
import com.ss.editor.annotation.FromAnyThread;
25-
import com.ss.editor.control.transform.SceneEditorControl;
2626
import com.ss.editor.control.transform.SceneEditorControl.TransformType;
2727
import com.ss.editor.manager.WorkspaceManager;
2828
import com.ss.editor.model.undo.EditorOperation;
@@ -40,6 +40,7 @@
4040
import com.ss.editor.ui.component.split.pane.EditorToolSplitPane;
4141
import com.ss.editor.ui.component.tab.EditorToolComponent;
4242
import com.ss.editor.ui.control.model.property.ModelPropertyEditor;
43+
import com.ss.editor.ui.control.model.property.operation.ModelPropertyOperation;
4344
import com.ss.editor.ui.control.model.tree.ModelNodeTree;
4445
import com.ss.editor.ui.control.model.tree.action.operation.AddChildOperation;
4546
import com.ss.editor.ui.control.tree.node.ModelNode;
@@ -817,35 +818,89 @@ protected void dragDropped(@NotNull final DragEvent dragEvent) {
817818
return;
818819
}
819820

820-
final MA editorAppState = getEditorAppState();
821-
final M currentModel = getCurrentModel();
822821

823822
for (final File file : files) {
824823

825-
if (!file.getName().endsWith(FileExtensions.JME_OBJECT)) {
826-
continue;
824+
if (file.getName().endsWith(FileExtensions.JME_OBJECT)) {
825+
addNewModel(dragEvent, file.toPath());
826+
} else if (file.getName().endsWith(FileExtensions.JME_MATERIAL)) {
827+
applyMaterial(dragEvent, file.toPath());
827828
}
829+
}
830+
}
831+
832+
/**
833+
* Apply a new material from an asset tree.
834+
*
835+
* @param dragEvent the drag event.
836+
* @param file the file.
837+
*/
838+
protected void applyMaterial(final @NotNull DragEvent dragEvent, @NotNull final Path file) {
839+
840+
final Path assetFile = requireNonNull(getAssetFile(file), "Not found asset file for " + file);
841+
final String assetPath = toAssetPath(assetFile);
842+
843+
final MaterialKey materialKey = new MaterialKey(assetPath);
844+
845+
final Camera camera = EDITOR.getCamera();
828846

829-
final Path assetFile = requireNonNull(getAssetFile(file.toPath()), "Not found asset file for " + file);
830-
final String assetPath = toAssetPath(assetFile);
847+
final float sceneX = (float) dragEvent.getSceneX();
848+
final float sceneY = camera.getHeight() - (float) dragEvent.getSceneY();
831849

832-
final ModelKey modelKey = new ModelKey(assetPath);
850+
EXECUTOR_MANAGER.addEditorThreadTask(() -> {
851+
852+
final MA editorAppState = getEditorAppState();
853+
final Geometry geometry = editorAppState.getGeometryByScreenPos(sceneX, sceneY);
854+
if (geometry == null) return;
833855

834856
final AssetManager assetManager = EDITOR.getAssetManager();
835857
assetManager.clearCache();
836858

837-
final float sceneX = (float) dragEvent.getSceneX();
838-
final float sceneY = (float) dragEvent.getSceneY();
859+
final Material material = assetManager.loadAsset(materialKey);
839860

840-
EXECUTOR_MANAGER.addEditorThreadTask(() -> {
861+
final ModelPropertyOperation<Geometry, Material> operation =
862+
new ModelPropertyOperation<>(geometry, Messages.MODEL_PROPERTY_MATERIAL, material, geometry.getMaterial());
841863

842-
final Spatial loadedModel = assetManager.loadModel(modelKey);
843-
loadedModel.setUserData(SceneEditorControl.LOADED_MODEL_KEY, true);
844-
loadedModel.setLocalTranslation(editorAppState.getCurrentCursorPosOnScene(sceneX, sceneY));
864+
operation.setApplyHandler(Geometry::setMaterial);
845865

846-
execute(new AddChildOperation(loadedModel, (Node) currentModel));
847-
});
848-
}
866+
execute(operation);
867+
});
868+
}
869+
870+
/**
871+
* Add a new model from an asset tree.
872+
*
873+
* @param dragEvent the drag event.
874+
* @param file the file.
875+
*/
876+
protected void addNewModel(final @NotNull DragEvent dragEvent, @NotNull final Path file) {
877+
878+
final M currentModel = getCurrentModel();
879+
if (!(currentModel instanceof Node)) return;
880+
881+
final Path assetFile = requireNonNull(getAssetFile(file), "Not found asset file for " + file);
882+
final String assetPath = toAssetPath(assetFile);
883+
884+
final ModelKey modelKey = new ModelKey(assetPath);
885+
886+
final AssetManager assetManager = EDITOR.getAssetManager();
887+
assetManager.clearCache();
888+
889+
final Camera camera = EDITOR.getCamera();
890+
891+
final float sceneX = (float) dragEvent.getSceneX();
892+
final float sceneY = camera.getHeight() - (float) dragEvent.getSceneY();
893+
894+
EXECUTOR_MANAGER.addEditorThreadTask(() -> {
895+
896+
final MA editorAppState = getEditorAppState();
897+
898+
final Spatial loadedModel = assetManager.loadModel(modelKey);
899+
loadedModel.setUserData(LOADED_MODEL_KEY, true);
900+
loadedModel.setLocalTranslation(editorAppState.getScenePosByScreenPos(sceneX, sceneY));
901+
902+
execute(new AddChildOperation(loadedModel, (Node) currentModel));
903+
});
849904
}
850905

851906
/**

0 commit comments

Comments
 (0)