diff --git a/CHANGES.md b/CHANGES.md index b09ad7292c39..d4b5dfc10bdb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ Change Log ========== ### 1.38 - 2017-10-02 +* Added ability to add an animation to `ModelAnimationCollection` by its index. [#5815](https://github.com/AnalyticalGraphicsInc/cesium/pull/5815) * Fixed a bug in `ModelAnimationCollection` that caused adding an animation by its name to throw an error. [#5815](https://github.com/AnalyticalGraphicsInc/cesium/pull/5815) ### 1.37 - 2017-09-01 diff --git a/Source/Scene/ModelAnimationCollection.js b/Source/Scene/ModelAnimationCollection.js index c9d54dfa8a51..68d2187198f1 100644 --- a/Source/Scene/ModelAnimationCollection.js +++ b/Source/Scene/ModelAnimationCollection.js @@ -99,7 +99,8 @@ define([ *

* * @param {Object} options Object with the following properties: - * @param {String} options.name The glTF animation name that identifies the animation. + * @param {String} [options.name] The glTF animation name that identifies the animation. Must be defined if options.id is undefined. + * @param {Number} [options.index] The glTF animation index that identifies the animation. Must be defined if options.name is undefined. * @param {JulianDate} [options.startTime] The scene time to start playing the animation. When this is undefined, the animation starts at the next frame. * @param {Number} [options.delay=0.0] The delay, in seconds, from startTime to start playing. * @param {JulianDate} [options.stopTime] The scene time to stop playing the animation. When this is undefined, the animation is played for its full duration. @@ -111,16 +112,23 @@ define([ * * @exception {DeveloperError} Animations are not loaded. Wait for the {@link Model#readyPromise} to resolve. * @exception {DeveloperError} options.name must be a valid animation name. + * @exception {DeveloperError} options.index must be a valid animation index. + * @exception {DeveloperError} Either options.name or options.index must be defined. * @exception {DeveloperError} options.speedup must be greater than zero. * * @example - * // Example 1. Add an animation + * // Example 1. Add an animation by name * model.activeAnimations.add({ * name : 'animation name' * }); * + * // Example 2. Add an animation by index + * model.activeAnimations.add({ + * index : 0 + * }); + * * @example - * // Example 2. Add an animation and provide all properties and events + * // Example 3. Add an animation and provide all properties and events * var startTime = Cesium.JulianDate.now(); * * var animation = model.activeAnimations.add({ @@ -154,14 +162,21 @@ define([ if (!defined(animations)) { throw new DeveloperError('Animations are not loaded. Wait for Model.readyPromise to resolve.'); } - if (!defined(options.name)) { - throw new DeveloperError('options.name must be defined.'); + if (!defined(options.name) && !defined(options.index)) { + throw new DeveloperError('Either options.name or options.index must be defined.'); } if (defined(options.speedup) && (options.speedup <= 0.0)) { throw new DeveloperError('options.speedup must be greater than zero.'); } + if (defined(options.index) && (options.index >= animations.length || options.index < 0)) { + throw new DeveloperError('options.index must be a valid animation index.'); + } //>>includeEnd('debug'); + if (defined(options.index)) { + return add(this, options.index, options); + } + // Find the index of the animation with the given name var index; var length = animations.length; diff --git a/Specs/Scene/ModelSpec.js b/Specs/Scene/ModelSpec.js index 2f591a60eab8..7ae7ba9e3385 100644 --- a/Specs/Scene/ModelSpec.js +++ b/Specs/Scene/ModelSpec.js @@ -1166,6 +1166,41 @@ defineSuite([ animations.animationRemoved.removeEventListener(spyRemove); }); + it('adds an animation by index', function() { + var animations = animBoxesModel.activeAnimations; + expect(animations.length).toEqual(0); + + var spyAdd = jasmine.createSpy('listener'); + animations.animationAdded.addEventListener(spyAdd); + var a = animations.add({ + index : 1 + }); + expect(a).toBeDefined(); + expect(a.name).toEqual('animation_1'); + animations.remove(a); + }); + + it('add throws when name and index are not defined', function() { + var m = new Model(); + expect(function() { + return m.activeAnimations.add(); + }).toThrowDeveloperError(); + }); + + it('add throws when index is invalid', function() { + var m = new Model(); + expect(function() { + return m.activeAnimations.add({ + index : -1 + }); + }).toThrowDeveloperError(); + expect(function() { + return m.activeAnimations.add({ + index : 2 + }); + }).toThrowDeveloperError(); + }); + it('add throws when model is not loaded', function() { var m = new Model(); expect(function() {