SceneModePickerViewModel.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import {
  2. defaultValue,
  3. defined,
  4. destroyObject,
  5. DeveloperError,
  6. EventHelper,
  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 SceneModePicker}.
  13. * @alias SceneModePickerViewModel
  14. * @constructor
  15. *
  16. * @param {Scene} scene The Scene to morph
  17. * @param {number} [duration=2.0] The duration of scene morph animations, in seconds
  18. */
  19. function SceneModePickerViewModel(scene, duration) {
  20. //>>includeStart('debug', pragmas.debug);
  21. if (!defined(scene)) {
  22. throw new DeveloperError("scene is required.");
  23. }
  24. //>>includeEnd('debug');
  25. this._scene = scene;
  26. const that = this;
  27. const morphStart = function (transitioner, oldMode, newMode, isMorphing) {
  28. that.sceneMode = newMode;
  29. that.dropDownVisible = false;
  30. };
  31. this._eventHelper = new EventHelper();
  32. this._eventHelper.add(scene.morphStart, morphStart);
  33. this._duration = defaultValue(duration, 2.0);
  34. /**
  35. * Gets or sets the current SceneMode. This property is observable.
  36. * @type {SceneMode}
  37. */
  38. this.sceneMode = scene.mode;
  39. /**
  40. * Gets or sets whether the button drop-down is currently visible. This property is observable.
  41. * @type {boolean}
  42. * @default false
  43. */
  44. this.dropDownVisible = false;
  45. /**
  46. * Gets or sets the 2D tooltip. This property is observable.
  47. * @type {string}
  48. * @default '2D'
  49. */
  50. this.tooltip2D = "2D";
  51. /**
  52. * Gets or sets the 3D tooltip. This property is observable.
  53. * @type {string}
  54. * @default '3D'
  55. */
  56. this.tooltip3D = "3D";
  57. /**
  58. * Gets or sets the Columbus View tooltip. This property is observable.
  59. * @type {string}
  60. * @default 'Columbus View'
  61. */
  62. this.tooltipColumbusView = "Columbus View";
  63. knockout.track(this, [
  64. "sceneMode",
  65. "dropDownVisible",
  66. "tooltip2D",
  67. "tooltip3D",
  68. "tooltipColumbusView",
  69. ]);
  70. /**
  71. * Gets the currently active tooltip. This property is observable.
  72. * @type {string}
  73. */
  74. this.selectedTooltip = undefined;
  75. knockout.defineProperty(this, "selectedTooltip", function () {
  76. const mode = that.sceneMode;
  77. if (mode === SceneMode.SCENE2D) {
  78. return that.tooltip2D;
  79. }
  80. if (mode === SceneMode.SCENE3D) {
  81. return that.tooltip3D;
  82. }
  83. return that.tooltipColumbusView;
  84. });
  85. this._toggleDropDown = createCommand(function () {
  86. that.dropDownVisible = !that.dropDownVisible;
  87. });
  88. this._morphTo2D = createCommand(function () {
  89. scene.morphTo2D(that._duration);
  90. });
  91. this._morphTo3D = createCommand(function () {
  92. scene.morphTo3D(that._duration);
  93. });
  94. this._morphToColumbusView = createCommand(function () {
  95. scene.morphToColumbusView(that._duration);
  96. });
  97. //Used by knockout
  98. this._sceneMode = SceneMode;
  99. }
  100. Object.defineProperties(SceneModePickerViewModel.prototype, {
  101. /**
  102. * Gets the scene
  103. * @memberof SceneModePickerViewModel.prototype
  104. * @type {Scene}
  105. */
  106. scene: {
  107. get: function () {
  108. return this._scene;
  109. },
  110. },
  111. /**
  112. * Gets or sets the the duration of scene mode transition animations in seconds.
  113. * A value of zero causes the scene to instantly change modes.
  114. * @memberof SceneModePickerViewModel.prototype
  115. * @type {number}
  116. */
  117. duration: {
  118. get: function () {
  119. return this._duration;
  120. },
  121. set: function (value) {
  122. //>>includeStart('debug', pragmas.debug);
  123. if (value < 0.0) {
  124. throw new DeveloperError("duration value must be positive.");
  125. }
  126. //>>includeEnd('debug');
  127. this._duration = value;
  128. },
  129. },
  130. /**
  131. * Gets the command to toggle the drop down box.
  132. * @memberof SceneModePickerViewModel.prototype
  133. *
  134. * @type {Command}
  135. */
  136. toggleDropDown: {
  137. get: function () {
  138. return this._toggleDropDown;
  139. },
  140. },
  141. /**
  142. * Gets the command to morph to 2D.
  143. * @memberof SceneModePickerViewModel.prototype
  144. *
  145. * @type {Command}
  146. */
  147. morphTo2D: {
  148. get: function () {
  149. return this._morphTo2D;
  150. },
  151. },
  152. /**
  153. * Gets the command to morph to 3D.
  154. * @memberof SceneModePickerViewModel.prototype
  155. *
  156. * @type {Command}
  157. */
  158. morphTo3D: {
  159. get: function () {
  160. return this._morphTo3D;
  161. },
  162. },
  163. /**
  164. * Gets the command to morph to Columbus View.
  165. * @memberof SceneModePickerViewModel.prototype
  166. *
  167. * @type {Command}
  168. */
  169. morphToColumbusView: {
  170. get: function () {
  171. return this._morphToColumbusView;
  172. },
  173. },
  174. });
  175. /**
  176. * @returns {boolean} true if the object has been destroyed, false otherwise.
  177. */
  178. SceneModePickerViewModel.prototype.isDestroyed = function () {
  179. return false;
  180. };
  181. /**
  182. * Destroys the view model.
  183. */
  184. SceneModePickerViewModel.prototype.destroy = function () {
  185. this._eventHelper.removeAll();
  186. destroyObject(this);
  187. };
  188. export default SceneModePickerViewModel;