智慧水务管理系统 - 精河县供水工程综合管理平台

OrderedGroundPrimitiveCollection.js 6.1KB

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