DataSourceDisplay.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defined',
  5. '../Core/destroyObject',
  6. '../Core/DeveloperError',
  7. '../Core/EventHelper',
  8. './BillboardVisualizer',
  9. './BoxGeometryUpdater',
  10. './CorridorGeometryUpdater',
  11. './CylinderGeometryUpdater',
  12. './EllipseGeometryUpdater',
  13. './EllipsoidGeometryUpdater',
  14. './GeometryVisualizer',
  15. './LabelVisualizer',
  16. './ModelVisualizer',
  17. './PathVisualizer',
  18. './PointVisualizer',
  19. './PolygonGeometryUpdater',
  20. './PolylineGeometryUpdater',
  21. './RectangleGeometryUpdater',
  22. './WallGeometryUpdater'
  23. ], function(
  24. defaultValue,
  25. defined,
  26. destroyObject,
  27. DeveloperError,
  28. EventHelper,
  29. BillboardVisualizer,
  30. BoxGeometryUpdater,
  31. CorridorGeometryUpdater,
  32. CylinderGeometryUpdater,
  33. EllipseGeometryUpdater,
  34. EllipsoidGeometryUpdater,
  35. GeometryVisualizer,
  36. LabelVisualizer,
  37. ModelVisualizer,
  38. PathVisualizer,
  39. PointVisualizer,
  40. PolygonGeometryUpdater,
  41. PolylineGeometryUpdater,
  42. RectangleGeometryUpdater,
  43. WallGeometryUpdater) {
  44. "use strict";
  45. /**
  46. * Visualizes a collection of {@link DataSource} instances.
  47. * @alias DataSourceDisplay
  48. * @constructor
  49. *
  50. * @param {Object} options Object with the following properties:
  51. * @param {Scene} options.scene The scene in which to display the data.
  52. * @param {DataSourceCollection} options.dataSourceCollection The data sources to display.
  53. * @param {DataSourceDisplay~VisualizersCallback} [options.visualizersCallback=DataSourceDisplay.defaultVisualizersCallback]
  54. * A function which creates an array of visualizers used for visualization.
  55. * If undefined, all standard visualizers are used.
  56. */
  57. var DataSourceDisplay = function(options) {
  58. var scene = options.scene;
  59. var dataSourceCollection = options.dataSourceCollection;
  60. //>>includeStart('debug', pragmas.debug);
  61. if (!defined(options)) {
  62. throw new DeveloperError('options is required.');
  63. }
  64. if (!defined(options.scene)) {
  65. throw new DeveloperError('scene is required.');
  66. }
  67. if (!defined(options.dataSourceCollection)) {
  68. throw new DeveloperError('dataSourceCollection is required.');
  69. }
  70. //>>includeEnd('debug');
  71. this._eventHelper = new EventHelper();
  72. this._eventHelper.add(dataSourceCollection.dataSourceAdded, this._onDataSourceAdded, this);
  73. this._eventHelper.add(dataSourceCollection.dataSourceRemoved, this._onDataSourceRemoved, this);
  74. this._dataSourceCollection = dataSourceCollection;
  75. this._scene = scene;
  76. this._visualizersCallback = defaultValue(options.visualizersCallback, DataSourceDisplay.defaultVisualizersCallback);
  77. for (var i = 0, len = dataSourceCollection.length; i < len; i++) {
  78. this._onDataSourceAdded(dataSourceCollection, dataSourceCollection.get(i));
  79. }
  80. };
  81. /**
  82. * Gets or sets the default function which creates an array of visualizers used for visualization.
  83. * By default, this function uses all standard visualizers.
  84. *
  85. * @member
  86. * @type {DataSourceDisplay~VisualizersCallback}
  87. */
  88. DataSourceDisplay.defaultVisualizersCallback = function(scene, dataSource) {
  89. var entities = dataSource.entities;
  90. return [new BillboardVisualizer(scene, entities),
  91. new GeometryVisualizer(BoxGeometryUpdater, scene, entities),
  92. new GeometryVisualizer(CylinderGeometryUpdater, scene, entities),
  93. new GeometryVisualizer(CorridorGeometryUpdater, scene, entities),
  94. new GeometryVisualizer(EllipseGeometryUpdater, scene, entities),
  95. new GeometryVisualizer(EllipsoidGeometryUpdater, scene, entities),
  96. new GeometryVisualizer(PolygonGeometryUpdater, scene, entities),
  97. new GeometryVisualizer(PolylineGeometryUpdater, scene, entities),
  98. new GeometryVisualizer(RectangleGeometryUpdater, scene, entities),
  99. new GeometryVisualizer(WallGeometryUpdater, scene, entities),
  100. new LabelVisualizer(scene, entities),
  101. new ModelVisualizer(scene, entities),
  102. new PointVisualizer(scene, entities),
  103. new PathVisualizer(scene, entities)];
  104. };
  105. /**
  106. * Gets the scene being used for display.
  107. *
  108. * @returns {Scene} The scene.
  109. */
  110. DataSourceDisplay.prototype.getScene = function() {
  111. return this._scene;
  112. };
  113. /**
  114. * Gets the collection of data sources to be displayed.
  115. *
  116. * @returns {DataSourceCollection} The collection of data sources.
  117. */
  118. DataSourceDisplay.prototype.getDataSources = function() {
  119. return this._dataSourceCollection;
  120. };
  121. /**
  122. * Returns true if this object was destroyed; otherwise, false.
  123. * <br /><br />
  124. * If this object was destroyed, it should not be used; calling any function other than
  125. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  126. *
  127. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  128. *
  129. * @see DataSourceDisplay#destroy
  130. */
  131. DataSourceDisplay.prototype.isDestroyed = function() {
  132. return false;
  133. };
  134. /**
  135. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  136. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  137. * <br /><br />
  138. * Once an object is destroyed, it should not be used; calling any function other than
  139. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  140. * assign the return value (<code>undefined</code>) to the object as done in the example.
  141. *
  142. * @returns {undefined}
  143. *
  144. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  145. *
  146. * @see DataSourceDisplay#isDestroyed
  147. *
  148. * @example
  149. * dataSourceDisplay = dataSourceDisplay.destroy();
  150. */
  151. DataSourceDisplay.prototype.destroy = function() {
  152. this._eventHelper.removeAll();
  153. var dataSourceCollection = this._dataSourceCollection;
  154. for (var i = 0, length = dataSourceCollection.length; i < length; ++i) {
  155. this._onDataSourceRemoved(this._dataSourceCollection, dataSourceCollection.get(i));
  156. }
  157. return destroyObject(this);
  158. };
  159. /**
  160. * Updates the display to the provided time.
  161. *
  162. * @param {JulianDate} time The simulation time.
  163. * @returns {Boolean} True if all data sources are ready to be displayed, false otherwise.
  164. */
  165. DataSourceDisplay.prototype.update = function(time) {
  166. //>>includeStart('debug', pragmas.debug);
  167. if (!defined(time)) {
  168. throw new DeveloperError('time is required.');
  169. }
  170. //>>includeEnd('debug');
  171. var result = true;
  172. var i;
  173. var x;
  174. var visualizers;
  175. var vLength;
  176. var dataSources = this._dataSourceCollection;
  177. var length = dataSources.length;
  178. for (i = 0; i < length; i++) {
  179. var dataSource = dataSources.get(i);
  180. if (defined(dataSource.update)) {
  181. result = dataSource.update(time) && result;
  182. }
  183. visualizers = dataSource._visualizers;
  184. vLength = visualizers.length;
  185. for (x = 0; x < vLength; x++) {
  186. result = visualizers[x].update(time) && result;
  187. }
  188. }
  189. return result;
  190. };
  191. DataSourceDisplay.prototype._onDataSourceAdded = function(dataSourceCollection, dataSource) {
  192. var visualizers = this._visualizersCallback(this._scene, dataSource);
  193. dataSource._visualizers = visualizers;
  194. };
  195. DataSourceDisplay.prototype._onDataSourceRemoved = function(dataSourceCollection, dataSource) {
  196. var visualizers = dataSource._visualizers;
  197. var length = visualizers.length;
  198. for (var i = 0; i < length; i++) {
  199. visualizers[i].destroy();
  200. dataSource._visualizers = undefined;
  201. }
  202. };
  203. /**
  204. * A function which creates an array of visualizers used for visualization.
  205. * @callback DataSourceDisplay~VisualizersCallback
  206. *
  207. * @param {Scene} scene The scene to create visualizers for.
  208. * @param {DataSource} dataSource The data source to create visualizers for.
  209. * @returns {Visualizer[]} An array of visualizers used for visualization.
  210. *
  211. * @example
  212. * function createVisualizers(scene, dataSource) {
  213. * return [new BillboardVisualizer(scene, dataSource.entities)];
  214. * }
  215. */
  216. return DataSourceDisplay;
  217. });