|
1 | 1 | package com.ss.editor.ui.component.editor.impl.scene; |
2 | 2 |
|
| 3 | +import static com.ss.editor.control.transform.SceneEditorControl.LOADED_MODEL_KEY; |
3 | 4 | import static com.ss.editor.util.EditorUtil.getAssetFile; |
4 | 5 | import static com.ss.editor.util.EditorUtil.toAssetPath; |
5 | 6 | import static com.ss.editor.util.MaterialUtils.updateMaterialIdNeed; |
|
22 | 23 | import com.ss.editor.Messages; |
23 | 24 | import com.ss.editor.annotation.FXThread; |
24 | 25 | import com.ss.editor.annotation.FromAnyThread; |
25 | | -import com.ss.editor.control.transform.SceneEditorControl; |
26 | 26 | import com.ss.editor.control.transform.SceneEditorControl.TransformType; |
27 | 27 | import com.ss.editor.manager.WorkspaceManager; |
28 | 28 | import com.ss.editor.model.undo.EditorOperation; |
|
40 | 40 | import com.ss.editor.ui.component.split.pane.EditorToolSplitPane; |
41 | 41 | import com.ss.editor.ui.component.tab.EditorToolComponent; |
42 | 42 | import com.ss.editor.ui.control.model.property.ModelPropertyEditor; |
| 43 | +import com.ss.editor.ui.control.model.property.operation.ModelPropertyOperation; |
43 | 44 | import com.ss.editor.ui.control.model.tree.ModelNodeTree; |
44 | 45 | import com.ss.editor.ui.control.model.tree.action.operation.AddChildOperation; |
45 | 46 | import com.ss.editor.ui.control.tree.node.ModelNode; |
@@ -817,35 +818,89 @@ protected void dragDropped(@NotNull final DragEvent dragEvent) { |
817 | 818 | return; |
818 | 819 | } |
819 | 820 |
|
820 | | - final MA editorAppState = getEditorAppState(); |
821 | | - final M currentModel = getCurrentModel(); |
822 | 821 |
|
823 | 822 | for (final File file : files) { |
824 | 823 |
|
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()); |
827 | 828 | } |
| 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(); |
828 | 846 |
|
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(); |
831 | 849 |
|
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; |
833 | 855 |
|
834 | 856 | final AssetManager assetManager = EDITOR.getAssetManager(); |
835 | 857 | assetManager.clearCache(); |
836 | 858 |
|
837 | | - final float sceneX = (float) dragEvent.getSceneX(); |
838 | | - final float sceneY = (float) dragEvent.getSceneY(); |
| 859 | + final Material material = assetManager.loadAsset(materialKey); |
839 | 860 |
|
840 | | - EXECUTOR_MANAGER.addEditorThreadTask(() -> { |
| 861 | + final ModelPropertyOperation<Geometry, Material> operation = |
| 862 | + new ModelPropertyOperation<>(geometry, Messages.MODEL_PROPERTY_MATERIAL, material, geometry.getMaterial()); |
841 | 863 |
|
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); |
845 | 865 |
|
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 | + }); |
849 | 904 | } |
850 | 905 |
|
851 | 906 | /** |
|
0 commit comments