Skip to content

Commit

Permalink
Add options.index
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Sep 7, 2017
1 parent 03de718 commit 3a06502
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions Source/Scene/ModelAnimationCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ define([
* </p>
*
* @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 <code>options.id</code> is <code>undefined</code>.
* @param {Number} [options.index] The glTF animation index that identifies the animation. Must be defined if <code>options.name</code> is <code>undefined</code>.
* @param {JulianDate} [options.startTime] The scene time to start playing the animation. When this is <code>undefined</code>, the animation starts at the next frame.
* @param {Number} [options.delay=0.0] The delay, in seconds, from <code>startTime</code> to start playing.
* @param {JulianDate} [options.stopTime] The scene time to stop playing the animation. When this is <code>undefined</code>, the animation is played for its full duration.
Expand All @@ -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({
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions Specs/Scene/ModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 3a06502

Please sign in to comment.