@@ -100,18 +100,29 @@ define([
100
100
* Adds a primitive to the collection.
101
101
*
102
102
* @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.
103
105
* @returns {Object } The primitive added to the collection.
104
106
*
105
107
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
106
108
*
107
109
* @example
108
110
* var billboards = scene.primitives.add(new Cesium.BillboardCollection());
109
111
*/
110
- PrimitiveCollection . prototype . add = function ( primitive ) {
112
+ PrimitiveCollection . prototype . add = function ( primitive , index ) {
113
+ var hasIndex = defined ( index ) ;
114
+
111
115
//>>includeStart('debug', pragmas.debug);
112
116
if ( ! defined ( primitive ) ) {
113
117
throw new DeveloperError ( 'primitive is required.' ) ;
114
118
}
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
+ }
115
126
//>>includeEnd('debug');
116
127
117
128
var external = ( primitive . _external = primitive . _external || { } ) ;
@@ -120,7 +131,11 @@ define([
120
131
collection : this
121
132
} ;
122
133
123
- this . _primitives . push ( primitive ) ;
134
+ if ( ! hasIndex ) {
135
+ this . _primitives . push ( primitive ) ;
136
+ } else {
137
+ this . _primitives . splice ( index , 0 , primitive ) ;
138
+ }
124
139
125
140
return primitive ;
126
141
} ;
@@ -183,7 +198,7 @@ define([
183
198
PrimitiveCollection . prototype . removeAll = function ( ) {
184
199
var primitives = this . _primitives ;
185
200
var length = primitives . length ;
186
- for ( var i = 0 ; i < length ; ++ i ) {
201
+ for ( var i = 0 ; i < length ; ++ i ) {
187
202
delete primitives [ i ] . _external . _composites [ this . _guid ] ;
188
203
if ( this . destroyPrimitives ) {
189
204
primitives [ i ] . destroy ( ) ;
0 commit comments