4141import com .jme3 .export .JmeExporter ;
4242import com .jme3 .export .JmeImporter ;
4343import com .jme3 .export .OutputCapsule ;
44- import com .jme3 .scene . Node ;
45- import com .jme3 .scene . Spatial ;
44+ import com .jme3 .util . clone . Cloner ;
45+ import com .jme3 .util . clone . JmeCloneable ;
4646
4747import java .io .IOException ;
4848import java .util .concurrent .atomic .AtomicLong ;
5555 *
5656 * Inspired by Nehon's {@link AnimationEvent}.
5757 */
58- public class AnimEvent extends AbstractCinematicEvent {
58+ public class AnimEvent extends AbstractCinematicEvent implements JmeCloneable {
5959
6060 private static final Logger logger
6161 = Logger .getLogger (AnimEvent .class .getName ());
62-
63- private static final String CINEMATIC_REF = "Cinematic:Refs" ;
6462
65- private static final AtomicLong spatialId = new AtomicLong ();
63+
64+ private static final AtomicLong refCounter = new AtomicLong ();
6665
6766 /**
68- * Reference ID used to find the associated Spatial.
67+ * Unique-ish id that identify this anim event
6968 */
70- protected String spatialRef ;
69+ protected String animRef ;
7170 /*
7271 * Control that will play the animation
7372 */
@@ -102,6 +101,7 @@ public AnimEvent(AnimComposer composer, String actionName) {
102101 */
103102 public AnimEvent (AnimComposer composer , String actionName ,
104103 String layerName ) {
104+ this ();
105105 this .composer = composer ;
106106 this .actionName = actionName ;
107107 this .layerName = layerName ;
@@ -110,50 +110,14 @@ public AnimEvent(AnimComposer composer, String actionName,
110110 */
111111 Action eventAction = composer .action (actionName );
112112 initialDuration = (float ) eventAction .getLength ();
113-
114- spatialRef = generateSpatialRef ();
115- composer .getSpatial ().setUserData (CINEMATIC_REF , spatialRef );
116113 }
117114
118115 /**
119116 * No-argument constructor needed by SavableClassUtil.
120117 */
121118 protected AnimEvent () {
122119 super ();
123- }
124-
125- /**
126- * Generate a unique identifier used to tag a Spatial in the scene graph.
127- *
128- * @return a unique string identifier
129- */
130- private String generateSpatialRef () {
131- return "cine" + System .currentTimeMillis () + "_" + (spatialId .incrementAndGet ());
132- }
133-
134- /**
135- * Recursively search the scene graph for a Spatial whose CINEMATIC_REF
136- * matches the stored spatialRef.
137- *
138- * @param sp the root Spatial to start searching from (not null)
139- * @return the matching Spatial, or null if not found
140- */
141- private Spatial findModelByRef (Spatial sp ) {
142- String refId = sp .getUserData (CINEMATIC_REF );
143- if (spatialRef .equals (refId )) {
144- return sp ;
145- }
146-
147- if (sp instanceof Node ) {
148- for (Spatial child : ((Node ) sp ).getChildren ()) {
149- Spatial model = findModelByRef (child );
150- if (model != null ) {
151- return model ;
152- }
153- }
154- }
155-
156- return null ;
120+ animRef = "animEvent-" + System .currentTimeMillis () + "_" + refCounter .incrementAndGet ();
157121 }
158122
159123 /**
@@ -165,24 +129,9 @@ private Spatial findModelByRef(Spatial sp) {
165129 @ Override
166130 public void initEvent (Application app , Cinematic cinematic ) {
167131 super .initEvent (app , cinematic );
168-
169- if (composer == null ) {
170- Spatial model = findModelByRef (cinematic .getScene ());
171- if (model != null ) {
172- composer = model .getControl (AnimComposer .class );
173- } else {
174- throw new UnsupportedOperationException (
175- "No Spatial found in the scene with Cinematic:Ref=" + spatialRef );
176- }
177- }
178- }
179-
180- @ Override
181- public void dispose () {
182- super .dispose ();
183- composer = null ;
184132 }
185133
134+
186135 /**
187136 * Callback when the event is paused.
188137 */
@@ -320,10 +269,11 @@ public void setTime(float time) {
320269 @ Override
321270 public void read (JmeImporter importer ) throws IOException {
322271 super .read (importer );
323- InputCapsule ic = importer .getCapsule (this );
324- spatialRef = ic .readString ("spatialRef" , null );
325- actionName = ic .readString ("actionName" , null );
326- layerName = ic .readString ("layerName" , AnimComposer .DEFAULT_LAYER );
272+ InputCapsule capsule = importer .getCapsule (this );
273+ actionName = capsule .readString ("actionName" , "" );
274+ composer = (AnimComposer ) capsule .readSavable ("composer" , null );
275+ layerName = capsule .readString ("layerName" , AnimComposer .DEFAULT_LAYER );
276+ animRef = capsule .readString ("animRef" , null );
327277 }
328278
329279 /**
@@ -336,9 +286,37 @@ public void read(JmeImporter importer) throws IOException {
336286 @ Override
337287 public void write (JmeExporter exporter ) throws IOException {
338288 super .write (exporter );
339- OutputCapsule oc = exporter .getCapsule (this );
340- oc .write (spatialRef , "spatialRef" , null );
341- oc .write (actionName , "actionName" , null );
342- oc .write (layerName , "layerName" , AnimComposer .DEFAULT_LAYER );
289+ OutputCapsule capsule = exporter .getCapsule (this );
290+ capsule .write (actionName , "actionName" , "" );
291+ capsule .write (composer , "composer" , null );
292+ capsule .write (layerName , "layerName" , AnimComposer .DEFAULT_LAYER );
293+ capsule .write (animRef , "animRef" , null );
294+ }
295+
296+ public AnimComposer getComposer () {
297+ return composer ;
298+ }
299+
300+ public void setComposer (AnimComposer composer ) {
301+ this .composer = composer ;
302+ }
303+
304+ public String getAnimRef () {
305+ return animRef ;
343306 }
307+
308+ @ Override
309+ public Object jmeClone () {
310+ try {
311+ return super .clone ();
312+ } catch (CloneNotSupportedException e ) {
313+ throw new RuntimeException ("Can't clone AnimEvent" , e );
314+ }
315+ }
316+
317+ @ Override
318+ public void cloneFields (Cloner cloner , Object original ) {
319+
320+ }
321+
344322}
0 commit comments