Skip to content

Commit 7ce0454

Browse files
committed
added bullet physics to Model Editor.
1 parent 74bfd7e commit 7ce0454

13 files changed

Lines changed: 285 additions & 53 deletions

File tree

embedded-plugins/ss-editor-font-generator/ss-editor-font-generator-1.0.1.jar renamed to embedded-plugins/ss-editor-font-generator/ss-editor-font-generator-1.0.2.jar

13.1 KB
Binary file not shown.
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.ss.editor.state.editor.impl.model;
2+
3+
import com.jme3.app.Application;
4+
import com.jme3.app.state.AppStateManager;
5+
import com.jme3.bullet.PhysicsSpace;
6+
import com.jme3.bullet.control.PhysicsControl;
7+
import com.jme3.scene.Spatial;
8+
import com.jme3.scene.control.Control;
9+
import com.ss.editor.extension.scene.app.state.impl.bullet.EditableBulletSceneAppState;
10+
import com.ss.editor.state.editor.Editor3DState;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
/**
15+
* The implementation of editor 3D state to work with Bullet physics.
16+
*
17+
* @author JavaSaBr
18+
*/
19+
public class ModelEditorBulletState extends EditableBulletSceneAppState implements Editor3DState {
20+
21+
@NotNull
22+
private final ModelEditor3DState editor3DState;
23+
24+
public ModelEditorBulletState(@NotNull final ModelEditor3DState editor3DState) {
25+
this.editor3DState = editor3DState;
26+
}
27+
28+
@Override
29+
public void initialize(@NotNull final AppStateManager stateManager, @NotNull final Application app) {
30+
super.initialize(stateManager, app);
31+
32+
final Spatial currentModel = editor3DState.getCurrentModel();
33+
if (currentModel != null) {
34+
updateNode(currentModel, physicsSpace);
35+
}
36+
}
37+
38+
@Override
39+
protected void rebuildState() {
40+
super.rebuildState();
41+
if (!isInitialized()) return;
42+
43+
final Spatial currentModel = editor3DState.getCurrentModel();
44+
if (currentModel != null) {
45+
updateNode(currentModel, physicsSpace);
46+
}
47+
}
48+
49+
/**
50+
* Update a spatial.
51+
*
52+
* @param spatial the spatial.
53+
* @param physicsSpace the new physical space or null.
54+
*/
55+
private void updateNode(@NotNull final Spatial spatial, @Nullable final PhysicsSpace physicsSpace) {
56+
spatial.depthFirstTraversal(spatial1 -> {
57+
58+
final int numControls = spatial1.getNumControls();
59+
60+
for (int i = 0; i < numControls; i++) {
61+
final Control control = spatial1.getControl(i);
62+
if (control instanceof PhysicsControl) {
63+
((PhysicsControl) control).setPhysicsSpace(physicsSpace);
64+
}
65+
}
66+
});
67+
}
68+
}

src/main/java/com/ss/editor/state/editor/impl/model/ModelEditorUtils.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/com/ss/editor/ui/Icons.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public interface Icons {
7070
* The constant PARTICLES_16.
7171
*/
7272
Image PARTICLES_16 = ICON_MANAGER.getImage("/ui/icons/svg/molecule_2.svg", 16);
73+
/**
74+
* The DEBUG_16 PARTICLES_16.
75+
*/
76+
Image DEBUG_16 = ICON_MANAGER.getImage("/ui/icons/svg/debug.svg", 16);
7377
/**
7478
* The constant GEOMETRY_16.
7579
*/

src/main/java/com/ss/editor/ui/component/editor/impl/AbstractFileEditor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ public abstract class AbstractFileEditor<R extends Pane> implements FileEditor {
152152
*/
153153
private boolean saving;
154154

155-
156155
/**
157156
* Instantiates a new Abstract file editor.
158157
*/

src/main/java/com/ss/editor/ui/component/editor/impl/model/ModelFileEditor.java

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ss.editor.annotation.FXThread;
1818
import com.ss.editor.manager.ResourceManager;
1919
import com.ss.editor.state.editor.impl.model.ModelEditor3DState;
20+
import com.ss.editor.state.editor.impl.model.ModelEditorBulletState;
2021
import com.ss.editor.ui.Icons;
2122
import com.ss.editor.ui.component.editor.EditorDescription;
2223
import com.ss.editor.ui.component.editor.impl.AbstractFileEditor;
@@ -77,6 +78,9 @@ public class ModelFileEditor extends AbstractSceneFileEditor<Spatial, ModelEdito
7778
FAST_SKY_LIST.add("graphics/textures/sky/inside.hdr");
7879
}
7980

81+
@NotNull
82+
private final ModelEditorBulletState bulletState;
83+
8084
/**
8185
* The list of fast skies.
8286
*/
@@ -89,8 +93,25 @@ public class ModelFileEditor extends AbstractSceneFileEditor<Spatial, ModelEdito
8993
@Nullable
9094
private ToggleButton lightButton;
9195

96+
/**
97+
* The physics toggle.
98+
*/
99+
@Nullable
100+
private ToggleButton physicsButton;
101+
102+
/**
103+
* The debug physics toggle.
104+
*/
105+
@Nullable
106+
private ToggleButton debugPhysicsButton;
107+
92108
private ModelFileEditor() {
93109
super();
110+
this.bulletState = new ModelEditorBulletState(getEditor3DState());
111+
this.bulletState.setEnabled(false);
112+
this.bulletState.setDebugEnabled(false);
113+
this.bulletState.setSpeed(1F);
114+
addEditorState(bulletState);
94115
}
95116

96117
@Override
@@ -113,6 +134,20 @@ private ModelFileEditor() {
113134
return notNull(lightButton);
114135
}
115136

137+
/**
138+
* @return the physics toggle.
139+
*/
140+
private @NotNull ToggleButton getPhysicsButton() {
141+
return notNull(physicsButton);
142+
}
143+
144+
/**
145+
* @return the debug physics button.
146+
*/
147+
private @NotNull ToggleButton getDebugPhysicsButton() {
148+
return notNull(debugPhysicsButton);
149+
}
150+
116151
@Override
117152
@FXThread
118153
protected void doOpenFile(@NotNull final Path file) {
@@ -156,6 +191,12 @@ protected void loadState() {
156191

157192
final ToggleButton lightButton = getLightButton();
158193
lightButton.setSelected(editorState.isEnableLight());
194+
195+
final ToggleButton physicsButton = getPhysicsButton();
196+
physicsButton.setSelected(editorState.isEnablePhysics());
197+
198+
final ToggleButton debugPhysicsButton = getDebugPhysicsButton();
199+
debugPhysicsButton.setSelected(editorState.isEnableDebugPhysics());
159200
}
160201

161202
@Override
@@ -181,6 +222,25 @@ protected void handleAddedObject(@NotNull final Spatial model) {
181222
}
182223
}
183224

225+
@FXThread
226+
@Override
227+
protected void handleRemovedObject(@NotNull final Spatial model) {
228+
super.handleRemovedObject(model);
229+
230+
final ModelEditor3DState editor3DState = getEditor3DState();
231+
final Array<Geometry> geometries = ArrayFactory.newArray(Geometry.class);
232+
233+
NodeUtils.addGeometry(model, geometries);
234+
235+
if (!geometries.isEmpty()) {
236+
geometries.forEach(geometry -> {
237+
if (geometry.getQueueBucket() == RenderQueue.Bucket.Sky) {
238+
editor3DState.removeCustomSky(geometry);
239+
}
240+
});
241+
}
242+
}
243+
184244
@Override
185245
public @NotNull EditorDescription getDescription() {
186246
return DESCRIPTION;
@@ -219,10 +279,24 @@ protected void createActions(@NotNull final HBox container) {
219279
lightButton.selectedProperty()
220280
.addListener((observable, oldValue, newValue) -> changeLight(newValue));
221281

222-
DynamicIconSupport.addSupport(lightButton);
282+
physicsButton = new ToggleButton();
283+
physicsButton.setTooltip(new Tooltip(Messages.SCENE_FILE_EDITOR_ACTION_CAMERA_LIGHT));
284+
physicsButton.setGraphic(new ImageView(Icons.PHYSICS_16));
285+
physicsButton.setSelected(false);
286+
physicsButton.selectedProperty()
287+
.addListener((observable, oldValue, newValue) -> changePhysics(newValue));
288+
289+
debugPhysicsButton = new ToggleButton();
290+
debugPhysicsButton.setTooltip(new Tooltip(Messages.SCENE_FILE_EDITOR_ACTION_CAMERA_LIGHT));
291+
debugPhysicsButton.setGraphic(new ImageView(Icons.DEBUG_16));
292+
debugPhysicsButton.setSelected(false);
293+
debugPhysicsButton.selectedProperty()
294+
.addListener((observable, oldValue, newValue) -> changeDebugPhysics(newValue));
223295

224-
FXUtils.addClassTo(lightButton, CSSClasses.FILE_EDITOR_TOOLBAR_BUTTON);
225-
FXUtils.addToPane(lightButton, container);
296+
DynamicIconSupport.addSupport(lightButton, physicsButton, debugPhysicsButton);
297+
298+
FXUtils.addClassTo(lightButton, physicsButton, debugPhysicsButton, CSSClasses.FILE_EDITOR_TOOLBAR_BUTTON);
299+
FXUtils.addToPane(lightButton, physicsButton, debugPhysicsButton, container);
226300
}
227301

228302
/**
@@ -258,6 +332,35 @@ private void changeFastSky(@NotNull final String newSky) {
258332
if (editorState != null) editorState.setSkyType(selectedIndex);
259333
}
260334

335+
/**
336+
* @return the bullet state.
337+
*/
338+
private @NotNull ModelEditorBulletState getBulletState() {
339+
return bulletState;
340+
}
341+
342+
/**
343+
* Handle to change enabling of physics.
344+
*/
345+
private void changePhysics(@NotNull final Boolean newValue) {
346+
if (isIgnoreListeners()) return;
347+
348+
EXECUTOR_MANAGER.addJMETask(() -> getBulletState().setEnabled(newValue));
349+
350+
if (editorState != null) editorState.setEnablePhysics(newValue);
351+
}
352+
353+
/**
354+
* Handle to change enabling of physics.
355+
*/
356+
private void changeDebugPhysics(@NotNull final Boolean newValue) {
357+
if (isIgnoreListeners()) return;
358+
359+
EXECUTOR_MANAGER.addJMETask(() -> getBulletState().setDebugEnabled(newValue));
360+
361+
if (editorState != null) editorState.setEnableDebugPhysics(newValue);
362+
}
363+
261364
/**
262365
* Handle changing camera light visibility.
263366
*/
@@ -287,6 +390,8 @@ public void notifyFXAddedChild(@NotNull final Object parent, @NotNull final Obje
287390
editor3DState.updateLightProbe();
288391
}
289392
}
393+
394+
EXECUTOR_MANAGER.addFXTask(() -> getBulletState().notifyAdded(added));
290395
}
291396

292397
@Override
@@ -305,6 +410,8 @@ public void notifyFXRemovedChild(@NotNull final Object parent, @NotNull final Ob
305410
editor3DState.updateLightProbe();
306411
}
307412
}
413+
414+
EXECUTOR_MANAGER.addFXTask(() -> getBulletState().notifyRemoved(removed));
308415
}
309416

310417
@Override

src/main/java/com/ss/editor/ui/component/editor/state/impl/EditorModelEditorState.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ public class EditorModelEditorState extends BaseEditorSceneEditorState {
2525
private volatile boolean enableLight;
2626

2727
/**
28-
* Instantiates a new Model file editor state.
28+
* Is enabled physics.
2929
*/
30+
private volatile boolean enablePhysics;
31+
32+
/**
33+
* Is enabled debug physics.
34+
*/
35+
private volatile boolean enableDebugPhysics;
36+
3037
public EditorModelEditorState() {
3138
this.skyType = 0;
3239
this.enableLight = EDITOR_CONFIG.isDefaultEditorCameraEnabled();
40+
this.enablePhysics = false;
41+
this.enableDebugPhysics = false;
3342
}
3443

3544
/**
@@ -72,6 +81,38 @@ public boolean isEnableLight() {
7281
return enableLight;
7382
}
7483

84+
/**
85+
* @return true if physics is enabled.
86+
*/
87+
public boolean isEnablePhysics() {
88+
return enablePhysics;
89+
}
90+
91+
/**
92+
* @param enablePhysics true if physics is enabled.
93+
*/
94+
public void setEnablePhysics(final boolean enablePhysics) {
95+
final boolean changed = isEnablePhysics() != enablePhysics;
96+
this.enablePhysics = enablePhysics;
97+
if (changed) notifyChange();
98+
}
99+
100+
/**
101+
* @return true if debug physics is enabled.
102+
*/
103+
public boolean isEnableDebugPhysics() {
104+
return enableDebugPhysics;
105+
}
106+
107+
/**
108+
* @param enableDebugPhysics true if debug physics is enabled.
109+
*/
110+
public void setEnableDebugPhysics(final boolean enableDebugPhysics) {
111+
final boolean changed = isEnableDebugPhysics() != enableDebugPhysics;
112+
this.enableDebugPhysics = enableDebugPhysics;
113+
if (changed) notifyChange();
114+
}
115+
75116
@Override
76117
public String toString() {
77118
return "EditorModelEditorState{" +

0 commit comments

Comments
 (0)