Skip to content

Commit a9ebe85

Browse files
committed
Add options.index
1 parent 03de718 commit a9ebe85

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Change Log
22
==========
33
### 1.38 - 2017-10-02
44

5+
* Added ability to add an animation to `ModelAnimationCollection` by its index. [#5815](https://github.com/AnalyticalGraphicsInc/cesium/pull/5815)
56
* 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)
67

78
### 1.37 - 2017-09-01

Source/Scene/ModelAnimationCollection.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ define([
9999
* </p>
100100
*
101101
* @param {Object} options Object with the following properties:
102-
* @param {String} options.name The glTF animation name that identifies the animation.
102+
* @param {String} [options.name] The glTF animation name that identifies the animation. Must be defined if <code>options.id</code> is <code>undefined</code>.
103+
* @param {Number} [options.index] The glTF animation index that identifies the animation. Must be defined if <code>options.name</code> is <code>undefined</code>.
103104
* @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.
104105
* @param {Number} [options.delay=0.0] The delay, in seconds, from <code>startTime</code> to start playing.
105106
* @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.
@@ -111,16 +112,22 @@ define([
111112
*
112113
* @exception {DeveloperError} Animations are not loaded. Wait for the {@link Model#readyPromise} to resolve.
113114
* @exception {DeveloperError} options.name must be a valid animation name.
115+
* @exception {DeveloperError} options.index must be a valid animation index.
114116
* @exception {DeveloperError} options.speedup must be greater than zero.
115117
*
116118
* @example
117-
* // Example 1. Add an animation
119+
* // Example 1. Add an animation by name
118120
* model.activeAnimations.add({
119121
* name : 'animation name'
120122
* });
121123
*
124+
* // Example 2. Add an animation by index
125+
* model.activeAnimations.add({
126+
* index : 0
127+
* });
128+
*
122129
* @example
123-
* // Example 2. Add an animation and provide all properties and events
130+
* // Example 3. Add an animation and provide all properties and events
124131
* var startTime = Cesium.JulianDate.now();
125132
*
126133
* var animation = model.activeAnimations.add({
@@ -154,14 +161,21 @@ define([
154161
if (!defined(animations)) {
155162
throw new DeveloperError('Animations are not loaded. Wait for Model.readyPromise to resolve.');
156163
}
157-
if (!defined(options.name)) {
158-
throw new DeveloperError('options.name must be defined.');
164+
if (!defined(options.name) && !defined(options.index)) {
165+
throw new DeveloperError('Either options.name or options.index must be defined.');
159166
}
160167
if (defined(options.speedup) && (options.speedup <= 0.0)) {
161168
throw new DeveloperError('options.speedup must be greater than zero.');
162169
}
170+
if (defined(options.index) && (options.index >= animations.length || options.index < 0)) {
171+
throw new DeveloperError('options.index must be a valid animation index.');
172+
}
163173
//>>includeEnd('debug');
164174

175+
if (defined(options.index)) {
176+
return add(this, options.index, options);
177+
}
178+
165179
// Find the index of the animation with the given name
166180
var index;
167181
var length = animations.length;

Specs/Scene/ModelSpec.js

+35
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,41 @@ defineSuite([
11661166
animations.animationRemoved.removeEventListener(spyRemove);
11671167
});
11681168

1169+
it('adds an animation by index', function() {
1170+
var animations = animBoxesModel.activeAnimations;
1171+
expect(animations.length).toEqual(0);
1172+
1173+
var spyAdd = jasmine.createSpy('listener');
1174+
animations.animationAdded.addEventListener(spyAdd);
1175+
var a = animations.add({
1176+
index : 1
1177+
});
1178+
expect(a).toBeDefined();
1179+
expect(a.name).toEqual('animation_1');
1180+
animations.remove(a);
1181+
});
1182+
1183+
it('add throws when name and index are not defined', function() {
1184+
var m = new Model();
1185+
expect(function() {
1186+
return m.activeAnimations.add();
1187+
}).toThrowDeveloperError();
1188+
});
1189+
1190+
it('add throws when index is invalid', function() {
1191+
var m = new Model();
1192+
expect(function() {
1193+
return m.activeAnimations.add({
1194+
index : -1
1195+
});
1196+
}).toThrowDeveloperError();
1197+
expect(function() {
1198+
return m.activeAnimations.add({
1199+
index : 2
1200+
});
1201+
}).toThrowDeveloperError();
1202+
});
1203+
11691204
it('add throws when model is not loaded', function() {
11701205
var m = new Model();
11711206
expect(function() {

0 commit comments

Comments
 (0)