ProjectionPickerViewModel.js 4.9 KB

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