Skip to content

Commit 3e3e2b6

Browse files
authored
Merge pull request #2342 from neph1/Issue2341
Proposed fix for Issue#2341 - Missing default layer in AnimComposer
2 parents 80623a1 + 7d27d14 commit 3e3e2b6

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

jme3-core/src/main/java/com/jme3/anim/AnimComposer.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Nehon
5353
*/
5454
public class AnimComposer extends AbstractControl {
55+
5556
/**
5657
* The name of the default layer.
5758
*/
@@ -121,7 +122,7 @@ public void removeAnimClip(AnimClip anim) {
121122
* @return The action corresponding to the given name.
122123
*/
123124
public Action setCurrentAction(String name) {
124-
return setCurrentAction(name, DEFAULT_LAYER);
125+
return setCurrentAction(name, DEFAULT_LAYER, true);
125126
}
126127

127128
/**
@@ -144,9 +145,9 @@ public Action setCurrentAction(String actionName, String layerName) {
144145
* @return The action corresponding to the given name.
145146
*/
146147
public Action setCurrentAction(String actionName, String layerName, boolean loop) {
147-
AnimLayer l = getLayer(layerName);
148+
AnimLayer layer = getLayer(layerName);
148149
Action currentAction = action(actionName);
149-
l.setCurrentAction(actionName, currentAction, loop);
150+
layer.setCurrentAction(actionName, currentAction, loop);
150151

151152
return currentAction;
152153
}
@@ -239,7 +240,8 @@ public void setTime(String layerName, double time) {
239240
/**
240241
*
241242
* @param name The name of the action to return.
242-
* @return The action registered with specified name. It will make a new action if there isn't any.
243+
* @return The action registered with specified name. It will make a new
244+
* action if there isn't any.
243245
* @see #makeAction(java.lang.String)
244246
*/
245247
public Action action(String name) {
@@ -254,7 +256,8 @@ public Action action(String name) {
254256
/**
255257
*
256258
* @param name The name of the action to return.
257-
* @return The action registered with specified name or null if nothing is registered.
259+
* @return The action registered with specified name or null if nothing is
260+
* registered.
258261
*/
259262
public Action getAction(String name) {
260263
return actions.get(name);
@@ -331,8 +334,8 @@ public AnimLayer removeLayer(String name) {
331334
}
332335

333336
/**
334-
* Creates an action that will interpolate over an entire sequence
335-
* of tweens in order.
337+
* Creates an action that will interpolate over an entire sequence of tweens
338+
* in order.
336339
*
337340
* @param name a name for the new Action
338341
* @param tweens the desired sequence of tweens
@@ -374,8 +377,9 @@ public void reset() {
374377
}
375378

376379
/**
377-
* Returns an unmodifiable collection of all available animations. When an attempt
378-
* is made to modify the collection, an UnsupportedOperationException is thrown.
380+
* Returns an unmodifiable collection of all available animations. When an
381+
* attempt is made to modify the collection, an
382+
* UnsupportedOperationException is thrown.
379383
*
380384
* @return the unmodifiable collection of animations
381385
*/
@@ -526,9 +530,8 @@ public void cloneFields(Cloner cloner, Object original) {
526530
for (String key : layers.keySet()) {
527531
newLayers.put(key, cloner.clone(layers.get(key)));
528532
}
529-
533+
newLayers.putIfAbsent(DEFAULT_LAYER, new AnimLayer(DEFAULT_LAYER, null));
530534
layers = newLayers;
531-
532535
}
533536

534537
/**
@@ -546,6 +549,7 @@ public void read(JmeImporter im) throws IOException {
546549
animClipMap = (Map<String, AnimClip>) ic.readStringSavableMap("animClipMap", new HashMap<String, AnimClip>());
547550
globalSpeed = ic.readFloat("globalSpeed", 1f);
548551
layers = (Map<String, AnimLayer>) ic.readStringSavableMap("layers", new HashMap<String, AnimLayer>());
552+
layers.putIfAbsent(DEFAULT_LAYER, new AnimLayer(DEFAULT_LAYER, null));
549553
}
550554

551555
/**

jme3-core/src/test/java/com/jme3/anim/AnimComposerTest.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2019 jMonkeyEngine
2+
* Copyright (c) 2009-2024 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -32,6 +32,7 @@
3232
package com.jme3.anim;
3333

3434
import com.jme3.anim.tween.action.Action;
35+
import com.jme3.util.clone.Cloner;
3536
import java.util.Set;
3637
import java.util.TreeSet;
3738
import org.junit.Assert;
@@ -57,34 +58,34 @@ public void testGetAnimClipsNames() {
5758
Assert.assertNotNull(composer.getAnimClipsNames());
5859
Assert.assertEquals(0, composer.getAnimClipsNames().size());
5960
}
60-
61+
6162
@Test
6263
public void testMakeLayer() {
6364
AnimComposer composer = new AnimComposer();
64-
65+
6566
final String layerName = "TestLayer";
6667

6768
composer.makeLayer(layerName, null);
68-
69+
6970
final Set<String> layers = new TreeSet<>();
7071
layers.add("Default");
7172
layers.add(layerName);
72-
73+
7374
Assert.assertNotNull(composer.getLayer(layerName));
7475
Assert.assertEquals(layers, composer.getLayerNames());
7576
}
76-
77+
7778
@Test
7879
public void testMakeAction() {
7980
AnimComposer composer = new AnimComposer();
80-
81+
8182
final String animName = "TestClip";
82-
83+
8384
final AnimClip anim = new AnimClip(animName);
8485
composer.addAnimClip(anim);
85-
86+
8687
final Action action = composer.makeAction(animName);
87-
88+
8889
Assert.assertNotNull(action);
8990
}
9091

@@ -102,4 +103,26 @@ public void testGetAnimClipsNamesIsNotModifiable() {
102103
composer.getAnimClipsNames().add("test");
103104
}
104105

106+
@Test
107+
public void testHasDefaultLayer() {
108+
AnimComposer composer = new AnimComposer();
109+
110+
AnimLayer defaultLayer = composer.getLayer("Default");
111+
Assert.assertNotNull(defaultLayer);
112+
}
113+
114+
@Test
115+
/**
116+
* https://github.com/jMonkeyEngine/jmonkeyengine/issues/2341
117+
*
118+
*/
119+
public void testMissingDefaultLayerIssue2341() {
120+
AnimComposer composer = new AnimComposer();
121+
composer.removeLayer(AnimComposer.DEFAULT_LAYER);
122+
123+
AnimComposer clone = (AnimComposer) composer.jmeClone();
124+
clone.cloneFields(new Cloner(), composer);
125+
Assert.assertNotNull(clone.getLayer(AnimComposer.DEFAULT_LAYER));
126+
}
127+
105128
}

0 commit comments

Comments
 (0)