ProjectionPickerViewModel.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {
  2. defined,
  3. destroyObject,
  4. DeveloperError,
  5. EventHelper,
  6. OrthographicFrustum,
  7. SceneMode,
  8. } from "@cesium/engine";
  9. import knockout from "../ThirdParty/knockout.js";
  10. import createCommand from "../createCommand.js";
  11. /**
  12. * The view model for {@link ProjectionPicker}.
  13. * @alias ProjectionPickerViewModel
  14. * @constructor
  15. *
  16. * @param {Scene} scene The Scene to switch projections.
  17. */
  18. function ProjectionPickerViewModel(scene) {
  19. //>>includeStart('debug', pragmas.debug);
  20. if (!defined(scene)) {
  21. throw new DeveloperError("scene is required.");
  22. }
  23. //>>includeEnd('debug');
  24. this._scene = scene;
  25. this._orthographic = scene.camera.frustum instanceof OrthographicFrustum;
  26. this._flightInProgress = false;
  27. /**
  28. * Gets or sets whether the button drop-down is currently visible. This property is observable.
  29. * @type {boolean}
  30. * @default false
  31. */
  32. this.dropDownVisible = false;
  33. /**
  34. * Gets or sets the perspective projection tooltip. This property is observable.
  35. * @type {string}
  36. * @default 'Perspective Projection'
  37. */
  38. this.tooltipPerspective = "Perspective Projection";
  39. /**
  40. * Gets or sets the orthographic projection tooltip. This property is observable.
  41. * @type {string}
  42. * @default 'Orthographic Projection'
  43. */
  44. this.tooltipOrthographic = "Orthographic Projection";
  45. /**
  46. * Gets the currently active tooltip. This property is observable.
  47. * @type {string}
  48. */
  49. this.selectedTooltip = undefined;
  50. /**
  51. * Gets or sets the current SceneMode. This property is observable.
  52. * @type {SceneMode}
  53. */
  54. this.sceneMode = scene.mode;
  55. knockout.track(this, [
  56. "_orthographic",
  57. "_flightInProgress",
  58. "sceneMode",
  59. "dropDownVisible",
  60. "tooltipPerspective",
  61. "tooltipOrthographic",
  62. ]);
  63. const that = this;
  64. knockout.defineProperty(this, "selectedTooltip", function () {
  65. if (that._orthographic) {
  66. return that.tooltipOrthographic;
  67. }
  68. return that.tooltipPerspective;
  69. });
  70. this._toggleDropDown = createCommand(function () {
  71. if (that.sceneMode === SceneMode.SCENE2D || that._flightInProgress) {
  72. return;
  73. }
  74. that.dropDownVisible = !that.dropDownVisible;
  75. });
  76. this._eventHelper = new EventHelper();
  77. this._eventHelper.add(scene.morphComplete, function (
  78. transitioner,
  79. oldMode,
  80. newMode,
  81. isMorphing
  82. ) {
  83. that.sceneMode = newMode;
  84. that._orthographic =
  85. newMode === SceneMode.SCENE2D ||
  86. that._scene.camera.frustum instanceof OrthographicFrustum;
  87. });
  88. this._eventHelper.add(scene.preRender, function () {
  89. that._flightInProgress = defined(scene.camera._currentFlight);
  90. });
  91. this._switchToPerspective = createCommand(function () {
  92. if (that.sceneMode === SceneMode.SCENE2D) {
  93. return;
  94. }
  95. that._scene.camera.switchToPerspectiveFrustum();
  96. that._orthographic = false;
  97. that.dropDownVisible = false;
  98. });
  99. this._switchToOrthographic = createCommand(function () {
  100. if (that.sceneMode === SceneMode.SCENE2D) {
  101. return;
  102. }
  103. that._scene.camera.switchToOrthographicFrustum();
  104. that._orthographic = true;
  105. that.dropDownVisible = false;
  106. });
  107. //Used by knockout
  108. this._sceneMode = SceneMode;
  109. }
  110. Object.defineProperties(ProjectionPickerViewModel.prototype, {
  111. /**
  112. * Gets the scene
  113. * @memberof ProjectionPickerViewModel.prototype
  114. * @type {Scene}
  115. */
  116. scene: {
  117. get: function () {
  118. return this._scene;
  119. },
  120. },
  121. /**
  122. * Gets the command to toggle the drop down box.
  123. * @memberof ProjectionPickerViewModel.prototype
  124. *
  125. * @type {Command}
  126. */
  127. toggleDropDown: {
  128. get: function () {
  129. return this._toggleDropDown;
  130. },
  131. },
  132. /**
  133. * Gets the command to switch to a perspective projection.
  134. * @memberof ProjectionPickerViewModel.prototype
  135. *
  136. * @type {Command}
  137. */
  138. switchToPerspective: {
  139. get: function () {
  140. return this._switchToPerspective;
  141. },
  142. },
  143. /**
  144. * Gets the command to switch to orthographic projection.
  145. * @memberof ProjectionPickerViewModel.prototype
  146. *
  147. * @type {Command}
  148. */
  149. switchToOrthographic: {
  150. get: function () {
  151. return this._switchToOrthographic;
  152. },
  153. },
  154. /**
  155. * Gets whether the scene is currently using an orthographic projection.
  156. * @memberof ProjectionPickerViewModel.prototype
  157. *
  158. * @type {Command}
  159. */
  160. isOrthographicProjection: {
  161. get: function () {
  162. return this._orthographic;
  163. },
  164. },
  165. });
  166. /**
  167. * @returns {boolean} true if the object has been destroyed, false otherwise.
  168. */
  169. ProjectionPickerViewModel.prototype.isDestroyed = function () {
  170. return false;
  171. };
  172. /**
  173. * Destroys the view model.
  174. */
  175. ProjectionPickerViewModel.prototype.destroy = function () {
  176. this._eventHelper.removeAll();
  177. destroyObject(this);
  178. };
  179. export default ProjectionPickerViewModel;