OrderedGroundPrimitiveCollection.js 6.2 KB

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