Skip to content

Commit 7a6d4b0

Browse files
authoredAug 5, 2019
Merge pull request #8041 from AnalyticalGraphicsInc/primitive-index
index option for adding primitive to PrimitiveCollection
2 parents 927b4bf + a3ab91d commit 7a6d4b0

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed
 

‎CHANGES.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Change Log
22
==========
33

4+
### 1.61 - 2019-09-03
5+
6+
##### Additions :tada:
7+
* Added optional `index` parameter to `PrimitiveCollection.add`. [#8041](https://github.com/AnalyticalGraphicsInc/cesium/pull/8041)
8+
49
### 1.60 - 2019-08-01
510

611
##### Additions :tada:

‎Source/Scene/PrimitiveCollection.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,29 @@ define([
100100
* Adds a primitive to the collection.
101101
*
102102
* @param {Object} primitive The primitive to add.
103+
* @param {Number} [index] the index to add the layer at. If omitted, the primitive will
104+
* added at the bottom of all existing primitives.
103105
* @returns {Object} The primitive added to the collection.
104106
*
105107
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
106108
*
107109
* @example
108110
* var billboards = scene.primitives.add(new Cesium.BillboardCollection());
109111
*/
110-
PrimitiveCollection.prototype.add = function(primitive) {
112+
PrimitiveCollection.prototype.add = function(primitive, index) {
113+
var hasIndex = defined(index);
114+
111115
//>>includeStart('debug', pragmas.debug);
112116
if (!defined(primitive)) {
113117
throw new DeveloperError('primitive is required.');
114118
}
119+
if (hasIndex) {
120+
if (index < 0) {
121+
throw new DeveloperError('index must be greater than or equal to zero.');
122+
} else if (index > this._primitives.length) {
123+
throw new DeveloperError('index must be less than or equal to the number of primitives.');
124+
}
125+
}
115126
//>>includeEnd('debug');
116127

117128
var external = (primitive._external = primitive._external || {});
@@ -120,7 +131,11 @@ define([
120131
collection : this
121132
};
122133

123-
this._primitives.push(primitive);
134+
if (!hasIndex) {
135+
this._primitives.push(primitive);
136+
} else {
137+
this._primitives.splice(index, 0, primitive);
138+
}
124139

125140
return primitive;
126141
};
@@ -183,7 +198,7 @@ define([
183198
PrimitiveCollection.prototype.removeAll = function() {
184199
var primitives = this._primitives;
185200
var length = primitives.length;
186-
for ( var i = 0; i < length; ++i) {
201+
for (var i = 0; i < length; ++i) {
187202
delete primitives[i]._external._composites[this._guid];
188203
if (this.destroyPrimitives) {
189204
primitives[i].destroy();

‎Specs/Scene/PrimitiveCollectionSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,33 @@ defineSuite([
130130
expect(primitives.length).toEqual(1);
131131
});
132132

133+
it('add works with an index', function() {
134+
var p0 = createLabels();
135+
var p1 = createLabels();
136+
137+
expect(function() {
138+
primitives.add(p0, 1);
139+
}).toThrowDeveloperError();
140+
141+
expect(function() {
142+
primitives.add(p0, -1);
143+
}).toThrowDeveloperError();
144+
145+
primitives.add(p0, 0);
146+
expect(primitives.get(0)).toBe(p0);
147+
148+
expect(function() {
149+
primitives.add(p1, -1);
150+
}).toThrowDeveloperError();
151+
152+
expect(function() {
153+
primitives.add(p1, 2);
154+
}).toThrowDeveloperError();
155+
156+
primitives.add(p1, 0);
157+
expect(primitives.get(0)).toBe(p1);
158+
});
159+
133160
it('removes the first primitive', function() {
134161
var p0 = createLabels();
135162
var p1 = createLabels();

0 commit comments

Comments
 (0)