Skip to content

Commit 803cd48

Browse files
authored
code review
1 parent b9b4d81 commit 803cd48

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

jme3-core/src/main/java/com/jme3/cinematic/Cinematic.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@
9595
public class Cinematic extends AbstractCinematicEvent implements AppState, CinematicHandler {
9696

9797
private static final Logger logger = Logger.getLogger(Cinematic.class.getName());
98+
9899
private static final String CINEMATIC_REF_PREFIX = "Cinematic:Refs:";
99-
private final String CINEMATIC_REF_VALUE = "jme3:Cinematic";
100+
private static final String CINEMATIC_REF_VALUE = "jme3:Cinematic";
100101

101102
private Application app;
102103
private Node scene;
@@ -241,11 +242,13 @@ public void onPause() {
241242
// breaking the scene graph
242243
private void applySerializationHack(CinematicEvent event, Map<CinematicEvent, Object[]> map) {
243244
Object store[] = null;
245+
244246
if (event instanceof AbstractCinematicEvent) {
245247
store = map.computeIfAbsent(event, k -> new Object[2]);
246248
store[0] = ((AbstractCinematicEvent) event).getCinematic();
247249
((AbstractCinematicEvent) event).setCinematic(null);
248250
}
251+
249252
if (event instanceof AnimEvent) {
250253
AnimEvent animEvent = (AnimEvent) event;
251254
store[1] = animEvent.getComposer();
@@ -259,15 +262,18 @@ private void applySerializationHack(CinematicEvent event, Map<CinematicEvent, Ob
259262
}
260263
}
261264

265+
/**
266+
* Restores previously removed references after serialization.
267+
*/
262268
private void undoSerializationHack(CinematicEvent event, Map<CinematicEvent, Object[]> map) {
263269
Object store[] = map.get(event);
264270
if (store == null) return;
271+
265272
if (event instanceof AbstractCinematicEvent) {
266273
((AbstractCinematicEvent) event).setCinematic((CinematicHandler) store[0]);
267274
}
268275
if (event instanceof AnimEvent) {
269-
AnimEvent animEvent = (AnimEvent) event;
270-
animEvent.setComposer((AnimComposer) store[1]);
276+
((AnimEvent) event).setComposer((AnimComposer) store[1]);
271277
}
272278
}
273279

@@ -281,22 +287,21 @@ private void undoSerializationHack(CinematicEvent event, Map<CinematicEvent, Obj
281287
public void write(JmeExporter ex) throws IOException {
282288
super.write(ex);
283289
OutputCapsule oc = ex.getCapsule(this);
284-
290+
285291
Map<CinematicEvent, Object[]> tmp = new HashMap<>();
286-
292+
287293
try {
288-
289-
// hack
294+
// Temporarily detach non‑serializable references
290295
for (CinematicEvent event : cinematicEvents) {
291296
applySerializationHack(event, tmp);
292297
}
293-
294-
oc.write(cinematicEvents.toArray(new CinematicEvent[cinematicEvents.size()]), "cinematicEvents",
295-
null);
296-
oc.writeStringSavableMap(cameras, "cameras", null);
297-
oc.write(timeLine, "timeLine", null);
298+
299+
oc.write(cinematicEvents.toArray(new CinematicEvent[0]), "cinematicEvents", null);
300+
oc.writeStringSavableMap(cameras, "cameras", null);
301+
oc.write(timeLine, "timeLine", null);
302+
298303
} finally {
299-
// unhack
304+
// Restore references after serialization
300305
for (CinematicEvent event : cinematicEvents) {
301306
undoSerializationHack(event, tmp);
302307
}
@@ -338,7 +343,6 @@ public void setSpeed(float speed) {
338343
CinematicEvent ce = cinematicEvents.get(i);
339344
ce.setSpeed(speed);
340345
}
341-
342346
}
343347

344348
/**
@@ -369,9 +373,11 @@ private void relinkAnimComposer(CinematicEvent cinematicEvent) {
369373
if (cinematicEvent instanceof AnimEvent) {
370374
AnimEvent animEvent = (AnimEvent) cinematicEvent;
371375
AnimComposer composer = animEvent.getComposer();
376+
372377
if (composer == null) {
373378
String ref = animEvent.getAnimRef();
374379
Spatial sp = findModelByRef(scene, ref);
380+
375381
if (sp == null) {
376382
throw new IllegalStateException(
377383
"Cannot find model with ref id " + ref + " for AnimEvent");
@@ -901,3 +907,4 @@ public AssetManager getAssetManager() {
901907
return app.getAssetManager();
902908
}
903909
}
910+

0 commit comments

Comments
 (0)