OrderedGroundPrimitiveCollection.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import Check from '../Core/Check.js';
  2. import defaultValue from '../Core/defaultValue.js';
  3. import defined from '../Core/defined.js';
  4. import defineProperties from '../Core/defineProperties.js';
  5. import destroyObject from '../Core/destroyObject.js';
  6. import PrimitiveCollection from './PrimitiveCollection.js';
  7. /**
  8. * A primitive collection for helping maintain the order or ground primitives based on a z-index
  9. *
  10. * @private
  11. */
  12. function OrderedGroundPrimitiveCollection() {
  13. this._length = 0;
  14. this._collections = {};
  15. this._collectionsArray = [];
  16. this.show = true;
  17. }
  18. defineProperties(OrderedGroundPrimitiveCollection.prototype, {
  19. /**
  20. * Gets the number of primitives in the collection.
  21. *
  22. * @memberof OrderedGroundPrimitiveCollection.prototype
  23. *
  24. * @type {Number}
  25. * @readonly
  26. */
  27. length : {
  28. get : function() {
  29. return this._length;
  30. }
  31. }
  32. });
  33. /**
  34. * Adds a primitive to the collection.
  35. *
  36. * @param {GroundPrimitive} primitive The primitive to add.
  37. * @param {Number} [zIndex = 0] The index of the primitive
  38. * @returns {GroundPrimitive} The primitive added to the collection.
  39. */
  40. OrderedGroundPrimitiveCollection.prototype.add = function(primitive, zIndex) {
  41. //>>includeStart('debug', pragmas.debug);
  42. Check.defined('primitive', primitive);
  43. if (defined(zIndex)) {
  44. Check.typeOf.number('zIndex', zIndex);
  45. }
  46. //>>includeEnd('debug');
  47. zIndex = defaultValue(zIndex, 0);
  48. var collection = this._collections[zIndex];
  49. if (!defined(collection)) {
  50. collection = new PrimitiveCollection({ destroyPrimitives: false });
  51. collection._zIndex = zIndex;
  52. this._collections[zIndex] = collection;
  53. var array = this._collectionsArray;
  54. var i = 0;
  55. while (i < array.length && array[i]._zIndex < zIndex) {
  56. i++;
  57. }
  58. array.splice(i, 0, collection);
  59. }
  60. collection.add(primitive);
  61. this._length++;
  62. primitive._zIndex = zIndex;
  63. return primitive;
  64. };
  65. /**
  66. * Adjusts the z-index
  67. * @param {GroundPrimitive} primitive
  68. * @param {Number} zIndex
  69. */
  70. OrderedGroundPrimitiveCollection.prototype.set = function(primitive, zIndex) {
  71. //>>includeStart('debug', pragmas.debug);
  72. Check.defined('primitive', primitive);
  73. Check.typeOf.number('zIndex', zIndex);
  74. //>>includeEnd('debug');
  75. if (zIndex === primitive._zIndex) {
  76. return primitive;
  77. }
  78. this.remove(primitive, true);
  79. this.add(primitive, zIndex);
  80. return primitive;
  81. };
  82. /**
  83. * Removes a primitive from the collection.
  84. *
  85. * @param {Object} primitive The primitive to remove.
  86. * @param {Boolean} [doNotDestroy = false]
  87. * @returns {Boolean} <code>true</code> if the primitive was removed; <code>false</code> if the primitive is <code>undefined</code> or was not found in the collection.
  88. */
  89. OrderedGroundPrimitiveCollection.prototype.remove = function(primitive, doNotDestroy) {
  90. if (this.contains(primitive)) {
  91. var index = primitive._zIndex;
  92. var collection = this._collections[index];
  93. var result;
  94. if (doNotDestroy) {
  95. result = collection.remove(primitive);
  96. } else {
  97. result = collection.removeAndDestroy(primitive);
  98. }
  99. if (result) {
  100. this._length--;
  101. }
  102. if (collection.length === 0) {
  103. this._collectionsArray.splice(this._collectionsArray.indexOf(collection), 1);
  104. this._collections[index] = undefined;
  105. collection.destroy();
  106. }
  107. return result;
  108. }
  109. return false;
  110. };
  111. /**
  112. * Removes all primitives in the collection.
  113. *
  114. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  115. *
  116. * @see OrderedGroundPrimitiveCollection#destroyPrimitives
  117. */
  118. OrderedGroundPrimitiveCollection.prototype.removeAll = function() {
  119. var collections = this._collectionsArray;
  120. for (var i = 0; i < collections.length; i++) {
  121. var collection = collections[i];
  122. collection.destroyPrimitives = true;
  123. collection.destroy();
  124. }
  125. this._collections = {};
  126. this._collectionsArray = [];
  127. this._length = 0;
  128. };
  129. /**
  130. * Determines if this collection contains a primitive.
  131. *
  132. * @param {Object} primitive The primitive to check for.
  133. * @returns {Boolean} <code>true</code> if the primitive is in the collection; <code>false</code> if the primitive is <code>undefined</code> or was not found in the collection.
  134. */
  135. OrderedGroundPrimitiveCollection.prototype.contains = function(primitive) {
  136. if (!defined(primitive)) {
  137. return false;
  138. }
  139. var collection = this._collections[primitive._zIndex];
  140. return defined(collection) && collection.contains(primitive);
  141. };
  142. /**
  143. * @private
  144. */
  145. OrderedGroundPrimitiveCollection.prototype.update = function(frameState) {
  146. if (!this.show) {
  147. return;
  148. }
  149. var collections = this._collectionsArray;
  150. for (var i = 0 ; i < collections.length; i++) {
  151. collections[i].update(frameState);
  152. }
  153. };
  154. /**
  155. * Returns true if this object was destroyed; otherwise, false.
  156. * <br /><br />
  157. * If this object was destroyed, it should not be used; calling any function other than
  158. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  159. *
  160. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  161. *
  162. * @see OrderedGroundPrimitiveCollection#destroy
  163. */
  164. OrderedGroundPrimitiveCollection.prototype.isDestroyed = function() {
  165. return false;
  166. };
  167. /**
  168. * Destroys the WebGL resources held by each primitive in this collection. Explicitly destroying this
  169. * collection allows for deterministic release of WebGL resources, instead of relying on the garbage
  170. * collector to destroy this collection.
  171. * <br /><br />
  172. * Since destroying a collection destroys all the contained primitives, only destroy a collection
  173. * when you are sure no other code is still using any of the contained primitives.
  174. * <br /><br />
  175. * Once this collection is destroyed, it should not be used; calling any function other than
  176. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  177. * assign the return value (<code>undefined</code>) to the object as done in the example.
  178. *
  179. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  180. *
  181. *
  182. * @example
  183. * primitives = primitives && primitives.destroy();
  184. *
  185. * @see OrderedGroundPrimitiveCollection#isDestroyed
  186. */
  187. OrderedGroundPrimitiveCollection.prototype.destroy = function() {
  188. this.removeAll();
  189. return destroyObject(this);
  190. };
  191. export default OrderedGroundPrimitiveCollection;