Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed fix for Issue#2341 - Missing default layer in AnimComposer #2342

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions jme3-core/src/main/java/com/jme3/anim/AnimComposer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* @author Nehon
*/
public class AnimComposer extends AbstractControl {

/**
* The name of the default layer.
*/
Expand Down Expand Up @@ -121,7 +122,7 @@ public void removeAnimClip(AnimClip anim) {
* @return The action corresponding to the given name.
*/
public Action setCurrentAction(String name) {
return setCurrentAction(name, DEFAULT_LAYER);
return setCurrentAction(name, DEFAULT_LAYER, true);
}

/**
Expand All @@ -144,9 +145,9 @@ public Action setCurrentAction(String actionName, String layerName) {
* @return The action corresponding to the given name.
*/
public Action setCurrentAction(String actionName, String layerName, boolean loop) {
AnimLayer l = getLayer(layerName);
AnimLayer layer = getLayer(layerName);
Action currentAction = action(actionName);
l.setCurrentAction(actionName, currentAction, loop);
layer.setCurrentAction(actionName, currentAction, loop);

return currentAction;
}
Expand Down Expand Up @@ -239,7 +240,8 @@ public void setTime(String layerName, double time) {
/**
*
* @param name The name of the action to return.
* @return The action registered with specified name. It will make a new action if there isn't any.
* @return The action registered with specified name. It will make a new
* action if there isn't any.
* @see #makeAction(java.lang.String)
*/
public Action action(String name) {
Expand All @@ -254,7 +256,8 @@ public Action action(String name) {
/**
*
* @param name The name of the action to return.
* @return The action registered with specified name or null if nothing is registered.
* @return The action registered with specified name or null if nothing is
* registered.
*/
public Action getAction(String name) {
return actions.get(name);
Expand Down Expand Up @@ -331,8 +334,8 @@ public AnimLayer removeLayer(String name) {
}

/**
* Creates an action that will interpolate over an entire sequence
* of tweens in order.
* Creates an action that will interpolate over an entire sequence of tweens
* in order.
*
* @param name a name for the new Action
* @param tweens the desired sequence of tweens
Expand Down Expand Up @@ -374,8 +377,9 @@ public void reset() {
}

/**
* Returns an unmodifiable collection of all available animations. When an attempt
* is made to modify the collection, an UnsupportedOperationException is thrown.
* Returns an unmodifiable collection of all available animations. When an
* attempt is made to modify the collection, an
* UnsupportedOperationException is thrown.
*
* @return the unmodifiable collection of animations
*/
Expand Down Expand Up @@ -526,7 +530,9 @@ public void cloneFields(Cloner cloner, Object original) {
for (String key : layers.keySet()) {
newLayers.put(key, cloner.clone(layers.get(key)));
}

if (!newLayers.containsKey(DEFAULT_LAYER)) {
neph1 marked this conversation as resolved.
Show resolved Hide resolved
newLayers.put(DEFAULT_LAYER, new AnimLayer(DEFAULT_LAYER, null));
}
layers = newLayers;

}
Expand All @@ -546,6 +552,9 @@ public void read(JmeImporter im) throws IOException {
animClipMap = (Map<String, AnimClip>) ic.readStringSavableMap("animClipMap", new HashMap<String, AnimClip>());
globalSpeed = ic.readFloat("globalSpeed", 1f);
layers = (Map<String, AnimLayer>) ic.readStringSavableMap("layers", new HashMap<String, AnimLayer>());
if (!layers.containsKey(DEFAULT_LAYER)) {
neph1 marked this conversation as resolved.
Show resolved Hide resolved
layers.put(DEFAULT_LAYER, new AnimLayer(DEFAULT_LAYER, null));
}
}

/**
Expand Down
43 changes: 33 additions & 10 deletions jme3-core/src/test/java/com/jme3/anim/AnimComposerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2019 jMonkeyEngine
* Copyright (c) 2009-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -32,6 +32,7 @@
package com.jme3.anim;

import com.jme3.anim.tween.action.Action;
import com.jme3.util.clone.Cloner;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Assert;
Expand All @@ -57,34 +58,34 @@ public void testGetAnimClipsNames() {
Assert.assertNotNull(composer.getAnimClipsNames());
Assert.assertEquals(0, composer.getAnimClipsNames().size());
}

@Test
public void testMakeLayer() {
AnimComposer composer = new AnimComposer();

final String layerName = "TestLayer";

composer.makeLayer(layerName, null);

final Set<String> layers = new TreeSet<>();
layers.add("Default");
layers.add(layerName);

Assert.assertNotNull(composer.getLayer(layerName));
Assert.assertEquals(layers, composer.getLayerNames());
}

@Test
public void testMakeAction() {
AnimComposer composer = new AnimComposer();

final String animName = "TestClip";

final AnimClip anim = new AnimClip(animName);
composer.addAnimClip(anim);

final Action action = composer.makeAction(animName);

Assert.assertNotNull(action);
}

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

@Test
public void testHasDefaultLayer() {
AnimComposer composer = new AnimComposer();

AnimLayer defaultLayer = composer.getLayer("Default");
Assert.assertNotNull(defaultLayer);
}

@Test
/**
* https://github.com/jMonkeyEngine/jmonkeyengine/issues/2341
*
*/
public void testMissingDefaultLayerIssue2341() {
AnimComposer composer = new AnimComposer();
composer.removeLayer(AnimComposer.DEFAULT_LAYER);

AnimComposer clone = (AnimComposer) composer.jmeClone();
clone.cloneFields(new Cloner(), composer);
Assert.assertNotNull(clone.getLayer(AnimComposer.DEFAULT_LAYER));
}

}
Loading