forked from jMonkeyEngine/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneEditorController.java
747 lines (654 loc) · 25.6 KB
/
SceneEditorController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.scenecomposer;
import com.jme3.asset.AssetManager;
import com.jme3.asset.ModelKey;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.gde.core.assets.ProjectAssetManager;
import com.jme3.gde.core.assets.SpatialAssetDataObject;
import com.jme3.gde.core.scene.SceneApplication;
import com.jme3.gde.core.sceneexplorer.nodes.AbstractSceneExplorerNode;
import com.jme3.gde.core.sceneexplorer.nodes.JmeSpatial;
import com.jme3.gde.core.undoredo.AbstractUndoableSceneEdit;
import com.jme3.gde.core.undoredo.SceneUndoRedoManager;
import com.jme3.gde.core.util.TerrainUtils;
import com.jme3.light.Light;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.AssetLinkNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.control.Control;
import com.jme3.util.TangentBinormalGenerator;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import org.netbeans.api.progress.ProgressHandle;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.NotifyDescriptor.Confirmation;
import org.openide.NotifyDescriptor.Message;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.nodes.NodeEvent;
import org.openide.nodes.NodeListener;
import org.openide.nodes.NodeMemberEvent;
import org.openide.nodes.NodeReorderEvent;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
/**
* Example of a class that edits the scene, logic happens on the AWT side, do..
* methods do not use global variables.
*
* @author normenhansen
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class SceneEditorController implements NodeListener {
private final JmeSpatial jmeRootNode;
private Spatial selectedSpat;
private AbstractSceneExplorerNode selectedExplorerNode;
private DataObject currentFileObject;
// private boolean needSave = false;
private SceneComposerToolController toolController;
public SceneEditorController(JmeSpatial jmeRootNode, DataObject currentFileObject) {
this.jmeRootNode = jmeRootNode;
jmeRootNode.addNodeListener(this);
this.currentFileObject = currentFileObject;
}
public void setToolController(SceneComposerToolController toolController) {
this.toolController = toolController;
}
public JmeSpatial getJmeRootNode() {
return jmeRootNode;
}
public Spatial getSelectedSpat() {
return selectedSpat;
}
public void setSelectedExplorerNode(AbstractSceneExplorerNode node) {
if (selectedExplorerNode != null) {
selectedExplorerNode.removePropertyChangeListener(this);
selectedExplorerNode.removeNodeListener(this);
}
selectedExplorerNode = node;
if (selectedExplorerNode != null) {
selectedExplorerNode.addPropertyChangeListener(this);
selectedExplorerNode.addNodeListener(this);
}
}
public void setSelectedSpat(Spatial selectedSpat) {
this.selectedSpat = selectedSpat;
}
public FileObject getCurrentFileObject() {
return currentFileObject.getPrimaryFile();
}
public DataObject getCurrentDataObject() {
return currentFileObject;
}
private void addSpatialUndo(final Node undoParent, final Spatial undoSpatial, final Light undoLight, final AbstractSceneExplorerNode parentNode) {
//add undo
if (undoParent != null && undoSpatial != null) {
Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
@Override
public void sceneUndo() throws CannotUndoException {
//undo stuff here
undoSpatial.removeFromParent();
}
@Override
public void sceneRedo() throws CannotRedoException {
//redo stuff here
undoParent.attachChild(undoSpatial);
}
@Override
public void awtRedo() {
if (parentNode != null) {
parentNode.refresh(true);
}
}
@Override
public void awtUndo() {
if (parentNode != null) {
parentNode.refresh(true);
}
}
});
}
}
public void moveSelectedSpatial(final Vector3f point) {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
if (node != null) {
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doMoveSpatial(node, point);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doMoveSpatial(Spatial selected, Vector3f translation) {
Vector3f localTranslation = selected.getLocalTranslation();
Vector3f before = new Vector3f(localTranslation);
Node parent = selected.getParent();
if (parent != null) {
localTranslation.set(translation).subtractLocal(parent.getWorldTranslation());
localTranslation.divideLocal(parent.getWorldScale());
//TODO: reuse quaternion..
new Quaternion().set(parent.getWorldRotation()).inverseLocal().multLocal(localTranslation);
} else {
localTranslation.set(translation);
}
Vector3f after = new Vector3f(localTranslation);
selected.setLocalTranslation(localTranslation);
moveUndo(selected, before, after);
}
private void moveUndo(final Spatial spatial, final Vector3f before, final Vector3f after) {
if (spatial != null && before != null) {
Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
@Override
public void sceneUndo() throws CannotUndoException {
//undo stuff here
spatial.setLocalTranslation(before);
}
@Override
public void sceneRedo() throws CannotRedoException {
//redo stuff here
spatial.setLocalTranslation(after);
}
});
}
}
public void nudgeSelectedSpatial(final Vector3f amount) {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
if (node != null) {
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doNudgeSpatial(node, amount);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doNudgeSpatial(Spatial selected, Vector3f translation) {
Vector3f before = new Vector3f(selected.getLocalTranslation());
selected.setLocalTranslation(before.add(translation));
Vector3f after = new Vector3f(selected.getLocalTranslation());
nudgeUndo(selected, before, after);
}
private void nudgeUndo(final Spatial spatial, final Vector3f before, final Vector3f after) {
if (spatial != null && before != null) {
Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
@Override
public void sceneUndo() throws CannotUndoException {
//undo stuff here
spatial.setLocalTranslation(before);
}
@Override
public void sceneRedo() throws CannotRedoException {
//redo stuff here
spatial.setLocalTranslation(after);
}
});
}
}
public void rotateSelectedSpatial(final Quaternion amount) {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
if (node != null) {
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doRotateSpatial(node, amount);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doRotateSpatial(Spatial selected, Quaternion rotation) {
Quaternion before = new Quaternion(selected.getLocalRotation());
selected.rotate(rotation);
Quaternion after = new Quaternion(selected.getLocalRotation());
rotateUndo(selected, before, after);
}
private void rotateUndo(final Spatial spatial, final Quaternion before, final Quaternion after) {
if (spatial != null && before != null) {
Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
@Override
public void sceneUndo() throws CannotUndoException {
//undo stuff here
spatial.setLocalRotation(before);
}
@Override
public void sceneRedo() throws CannotRedoException {
//redo stuff here
spatial.setLocalRotation(after);
}
});
}
}
public void createTangentsForSelectedSpatial() {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
if (node != null) {
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doCreateTangents(node);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doCreateTangents(Spatial selected) {
if (selected instanceof Geometry) {
Geometry geom = (Geometry) selected;
Mesh mesh = geom.getMesh();
if (mesh != null) {
TangentBinormalGenerator.generate(mesh);
createTrangentsUndo(mesh);
}
}
}
private void createTrangentsUndo(final Mesh mesh) {
if (mesh != null) {
Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
@Override
public void sceneUndo() throws CannotUndoException {
mesh.clearBuffer(Type.Tangent);
}
@Override
public void sceneRedo() throws CannotRedoException {
TangentBinormalGenerator.generate(mesh);
}
});
}
}
public void createPhysicsMeshForSelectedSpatial() {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
setNeedsSave(true);
if (node != null) {
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doCreatePhysicsMesh(node);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doCreatePhysicsMesh(Spatial selected) {
RigidBodyControl control = selected.getControl(RigidBodyControl.class);
if (control != null) {
selected.removeControl(control);
}
Node parent = selected.getParent();
selected.removeFromParent();
control = new RigidBodyControl(0);
selected.addControl(new RigidBodyControl(0));
if (parent != null) {
parent.attachChild(selected);
}
refreshSelected();
addControlUndo(parent, control, jmeRootNode);
}
public void createDynamicPhysicsMeshForSelectedSpatial(final float weight) {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
setNeedsSave(true);
if (node != null) {
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doCreateDynamicPhysicsMesh(node, weight);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doCreateDynamicPhysicsMesh(Spatial selected, float weight) {
RigidBodyControl control = selected.getControl(RigidBodyControl.class);
if (control != null) {
selected.removeControl(control);
}
Node parent = selected.getParent();
selected.removeFromParent();
control = new RigidBodyControl(weight);
selected.addControl(control);
if (parent != null) {
parent.attachChild(selected);
}
refreshSelected();
addControlUndo(parent, control, jmeRootNode);
}
public void createCharacterControlForSelectedSpatial(final boolean auto, final float radius, final float height) {
if (selectedSpat == null) {
return;
}
try {
final Spatial node = selectedSpat;
setNeedsSave(true);
if (node != null) {
SceneApplication.getApplication().enqueue(new Callable() {
public Object call() throws Exception {
doCreateCharacterControl(node, auto, radius, height);
return null;
}
}).get();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
public void doCreateCharacterControl(Spatial selected, boolean auto, float radius, float height) {
CharacterControl control = selected.getControl(CharacterControl.class);
if (control != null) {
selected.removeControl(control);
}
Node parent = selected.getParent();
selected.removeFromParent();
if (!auto) {
control = new CharacterControl(new CapsuleCollisionShape(radius, height), 0.03f);
} else {
//TODO: auto-creation by bounding volume
control = new CharacterControl(new CapsuleCollisionShape(radius, height), 0.03f);
}
selected.addControl(control);
if (parent != null) {
parent.attachChild(selected);
}
refreshSelected();
addControlUndo(parent, control, jmeRootNode);
}
private void addControlUndo(final Node undoParent, final Control undoControl, final AbstractSceneExplorerNode parentNode) {
if (undoParent != null && undoControl != null) {
Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
@Override
public void sceneUndo() throws CannotUndoException {
//undo stuff here
undoParent.removeControl(undoControl);
}
@Override
public void sceneRedo() throws CannotRedoException {
//redo stuff here
undoParent.addControl(undoControl);
}
@Override
public void awtRedo() {
if (parentNode != null) {
parentNode.refresh(true);
}
}
@Override
public void awtUndo() {
if (parentNode != null) {
parentNode.refresh(true);
}
}
});
}
}
public void addModel(final SpatialAssetDataObject file, final Vector3f location) {
if (selectedSpat == null) {
return;
}
if (selectedSpat instanceof Node) {
final Node selected = (Node) selectedSpat;
ProjectAssetManager manager = file.getLookup().lookup(ProjectAssetManager.class);
if (manager != null) {
manager.clearCache();
}
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable<Object>() {
public Object call() throws Exception {
doAddModel(file, selected, location);
return null;
}
});
} else {
displayInfo("Please select a Node to attach to\nin the SceneExplorer.");
}
}
public void doAddModel(SpatialAssetDataObject file, Node selected, Vector3f location) {
ProgressHandle progressHandle = ProgressHandle.createHandle("Adding Model..");
progressHandle.start();
try {
Spatial linkNode = file.loadAsset();
if (linkNode != null) {
selected.attachChild(linkNode);
if (location != null) {
Vector3f localVec = new Vector3f();
selected.worldToLocal(location, localVec);
linkNode.setLocalTranslation(localVec);
}
addSpatialUndo(selected, linkNode, null, jmeRootNode);
}
file.closeAsset();
refreshSelected();
} catch (Exception ex) {
Confirmation msg = new NotifyDescriptor.Confirmation(
"Error importing " + file.getName() + "\n" + ex.toString(),
NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notifyLater(msg);
}
progressHandle.finish();
}
public void linkModel(final AssetManager manager, final String assetName, final Vector3f location) {
if (selectedSpat == null) {
return;
}
if (selectedSpat instanceof Node) {
final Node selected = (Node) ((selectedSpat instanceof AssetLinkNode) ? selectedSpat.getParent() : selectedSpat);
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable<Object>() {
public Object call() throws Exception {
doLinkModel(manager, assetName, selected, location);
return null;
}
});
} else {
displayInfo("Please select a Node to attach to\nin the SceneExplorer.");
}
}
public void doLinkModel(AssetManager manager, String assetName, Node selected, Vector3f location) {
ProgressHandle progressHandle = ProgressHandle.createHandle("Adding Model..");
progressHandle.start();
try {
if (selected instanceof AssetLinkNode) {
AssetLinkNode linkNode = (AssetLinkNode) selected;
linkNode.attachLinkedChild(manager, new ModelKey(assetName));
} else {
manager.clearCache();
ModelKey key = new ModelKey(assetName);
AssetLinkNode linkNode = new AssetLinkNode(key);
linkNode.attachLinkedChildren(manager);
selected.attachChild(linkNode);
if (location != null) {
Vector3f localVec = new Vector3f();
selected.worldToLocal(location, localVec);
linkNode.setLocalTranslation(localVec);
}
addSpatialUndo(selected, linkNode, null, jmeRootNode);
}
refreshSelected();
} catch (Exception ex) {
Confirmation msg = new NotifyDescriptor.Confirmation(
"Error importing " + assetName + "\n" + ex.toString(),
NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notifyLater(msg);
}
progressHandle.finish();
}
public void addModel(final Spatial file) {
addModel(file, null);
}
public void addModel(final Spatial file, final Vector3f location) {
if (selectedSpat == null) {
return;
}
if (selectedSpat instanceof Node) {
final Node selected = (Node) selectedSpat;
setNeedsSave(true);
SceneApplication.getApplication().enqueue(new Callable<Object>() {
public Object call() throws Exception {
doAddModel(file, selected, location);
return null;
}
});
} else {
displayInfo("Please select a Node to attach to\nin the SceneExplorer.");
}
}
public void doAddModel(Spatial file, Node selected, Vector3f location) {
ProgressHandle progressHandle = ProgressHandle.createHandle("Adding Model..");
progressHandle.start();
try {
if (file != null) {
selected.attachChild(file);
if (location != null) {
Vector3f localVec = new Vector3f();
selected.worldToLocal(location, localVec);
file.setLocalTranslation(localVec);
}
}
refreshSelected();
addSpatialUndo(selected, file, null, jmeRootNode);
} catch (Exception ex) {
Confirmation msg = new NotifyDescriptor.Confirmation(
"Error importing " + file.getName() + "\n" + ex.toString(),
NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notifyLater(msg);
}
progressHandle.finish();
}
public void setNeedsSave(boolean state) {
currentFileObject.setModified(state);
}
public boolean isNeedSave() {
return currentFileObject.isModified();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
// if ((evt.getOldValue() == null && !(evt.getNewValue() == null)) || ((evt.getOldValue() != null) && !evt.getOldValue().equals(evt.getNewValue()))) {
// setNeedsSave(true);
// }
}
@Override
public void childrenAdded(NodeMemberEvent ev) {
// setNeedsSave(true);
toolController.refreshNonSpatialMarkers();
}
@Override
public void childrenRemoved(NodeMemberEvent ev) {
// setNeedsSave(true);
toolController.refreshNonSpatialMarkers();
}
@Override
public void childrenReordered(NodeReorderEvent ev) {
// setNeedsSave(true);
toolController.refreshNonSpatialMarkers();
}
@Override
public void nodeDestroyed(NodeEvent ev) {
// setNeedsSave(true);
toolController.refreshNonSpatialMarkers();
}
public void saveScene() {
try {
currentFileObject.getLookup().lookup(SaveCookie.class).save();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
private void refreshSelected() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
if(selectedExplorerNode != null) {
selectedExplorerNode.refresh(true); // rootNode doesn't seem to be recursive, TODO: Fix it.
}
jmeRootNode.refresh(false);
}
});
}
public void cleanup() {
final Node node = jmeRootNode.getLookup().lookup(Node.class);
}
/**
* Terrain has a LOD control that requires the camera to function.
*/
protected void setTerrainLodCamera() {
Camera camera = SceneApplication.getApplication().getCamera();
Node root = jmeRootNode.getLookup().lookup(Node.class);
TerrainUtils.enableLodControl(camera, root);
}
public void displayInfo(String info) {
Message msg = new NotifyDescriptor.Message(info);
DialogDisplayer.getDefault().notifyLater(msg);
}
}