HomeButtonViewModel.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*global define*/
  2. define([
  3. '../../Core/Cartesian3',
  4. '../../Core/defaultValue',
  5. '../../Core/defined',
  6. '../../Core/defineProperties',
  7. '../../Core/DeveloperError',
  8. '../../Core/Matrix4',
  9. '../../Core/Rectangle',
  10. '../../Scene/Camera',
  11. '../../Scene/SceneMode',
  12. '../../ThirdParty/knockout',
  13. '../createCommand'
  14. ], function(
  15. Cartesian3,
  16. defaultValue,
  17. defined,
  18. defineProperties,
  19. DeveloperError,
  20. Matrix4,
  21. Rectangle,
  22. Camera,
  23. SceneMode,
  24. knockout,
  25. createCommand) {
  26. "use strict";
  27. function viewHome(scene, duration) {
  28. var mode = scene.mode;
  29. if (defined(scene) && mode === SceneMode.MORPHING) {
  30. scene.completeMorph();
  31. }
  32. var direction;
  33. var right;
  34. var up;
  35. if (mode === SceneMode.SCENE2D) {
  36. scene.camera.flyToRectangle({
  37. destination : Rectangle.MAX_VALUE,
  38. duration : duration,
  39. endTransform : Matrix4.IDENTITY
  40. });
  41. } else if (mode === SceneMode.SCENE3D) {
  42. var destination = scene.camera.getRectangleCameraCoordinates(Camera.DEFAULT_VIEW_RECTANGLE);
  43. var mag = Cartesian3.magnitude(destination);
  44. mag += mag * Camera.DEFAULT_VIEW_FACTOR;
  45. Cartesian3.normalize(destination, destination);
  46. Cartesian3.multiplyByScalar(destination, mag, destination);
  47. direction = Cartesian3.normalize(destination, new Cartesian3());
  48. Cartesian3.negate(direction, direction);
  49. right = Cartesian3.cross(direction, Cartesian3.UNIT_Z, new Cartesian3());
  50. up = Cartesian3.cross(right, direction, new Cartesian3());
  51. scene.camera.flyTo({
  52. destination : destination,
  53. direction: direction,
  54. up : up,
  55. duration : duration,
  56. endTransform : Matrix4.IDENTITY
  57. });
  58. } else if (mode === SceneMode.COLUMBUS_VIEW) {
  59. var maxRadii = scene.globe.ellipsoid.maximumRadius;
  60. var position = new Cartesian3(0.0, -1.0, 1.0);
  61. position = Cartesian3.multiplyByScalar(Cartesian3.normalize(position, position), 5.0 * maxRadii, position);
  62. direction = new Cartesian3();
  63. direction = Cartesian3.normalize(Cartesian3.subtract(Cartesian3.ZERO, position, direction), direction);
  64. right = Cartesian3.cross(direction, Cartesian3.UNIT_Z, new Cartesian3());
  65. up = Cartesian3.cross(right, direction, new Cartesian3());
  66. scene.camera.flyTo({
  67. destination : position,
  68. duration : duration,
  69. up : up,
  70. direction : direction,
  71. endTransform : Matrix4.IDENTITY,
  72. convert : false
  73. });
  74. }
  75. }
  76. /**
  77. * The view model for {@link HomeButton}.
  78. * @alias HomeButtonViewModel
  79. * @constructor
  80. *
  81. * @param {Scene} scene The scene instance to use.
  82. * @param {Number} [duration=1.5] The duration of the camera flight in seconds.
  83. */
  84. var HomeButtonViewModel = function(scene, duration) {
  85. //>>includeStart('debug', pragmas.debug);
  86. if (!defined(scene)) {
  87. throw new DeveloperError('scene is required.');
  88. }
  89. //>>includeEnd('debug');
  90. duration = defaultValue(duration, 1.5);
  91. this._scene = scene;
  92. this._duration = duration;
  93. var that = this;
  94. this._command = createCommand(function() {
  95. viewHome(that._scene, that._duration);
  96. });
  97. /**
  98. * Gets or sets the tooltip. This property is observable.
  99. *
  100. * @type {String}
  101. */
  102. this.tooltip = 'View Home';
  103. knockout.track(this, ['tooltip']);
  104. };
  105. defineProperties(HomeButtonViewModel.prototype, {
  106. /**
  107. * Gets the scene to control.
  108. * @memberof HomeButtonViewModel.prototype
  109. *
  110. * @type {Scene}
  111. */
  112. scene : {
  113. get : function() {
  114. return this._scene;
  115. }
  116. },
  117. /**
  118. * Gets the Command that is executed when the button is clicked.
  119. * @memberof HomeButtonViewModel.prototype
  120. *
  121. * @type {Command}
  122. */
  123. command : {
  124. get : function() {
  125. return this._command;
  126. }
  127. },
  128. /**
  129. * Gets or sets the the duration of the camera flight in seconds.
  130. * A value of zero causes the camera to instantly switch to home view.
  131. * @memberof HomeButtonViewModel.prototype
  132. *
  133. * @type {Number}
  134. */
  135. duration : {
  136. get : function() {
  137. return this._duration;
  138. },
  139. set : function(value) {
  140. //>>includeStart('debug', pragmas.debug);
  141. if (value < 0) {
  142. throw new DeveloperError('value must be positive.');
  143. }
  144. //>>includeEnd('debug');
  145. this._duration = value;
  146. }
  147. }
  148. });
  149. return HomeButtonViewModel;
  150. });