SceneModePickerViewModel.js 4.9 KB

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