ProjectionPickerViewModel.js 5.8 KB

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