KmlTourFlyTo.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import BoundingSphere from '../Core/BoundingSphere.js';
  2. import combine from '../Core/combine.js';
  3. import defined from '../Core/defined.js';
  4. import EasingFunction from '../Core/EasingFunction.js';
  5. /**
  6. * @alias KmlTourFlyTo
  7. * @constructor
  8. *
  9. * @param {Number} duration entry duration
  10. * @param {String} flyToMode KML fly to mode: bounce, smooth, etc
  11. * @param {KmlCamera|KmlLookAt} view KmlCamera or KmlLookAt
  12. */
  13. function KmlTourFlyTo(duration, flyToMode, view) {
  14. this.type = 'KmlTourFlyTo';
  15. this.blocking = true;
  16. this.activeCamera = null;
  17. this.activeCallback = null;
  18. this.duration = duration;
  19. this.view = view;
  20. this.flyToMode = flyToMode;
  21. }
  22. /**
  23. * Play this playlist entry
  24. *
  25. * @param {KmlTourFlyTo~DoneCallback} done function which will be called when playback ends
  26. * @param {Camera} camera Cesium camera
  27. * @param {Object} [cameraOptions] which will be merged with camera flyTo options. See {@link Camera#flyTo}
  28. */
  29. KmlTourFlyTo.prototype.play = function(done, camera, cameraOptions) {
  30. this.activeCamera = camera;
  31. if (defined(done) && done !== null) {
  32. var self = this;
  33. this.activeCallback = function(terminated) {
  34. delete self.activeCallback;
  35. delete self.activeCamera;
  36. done(defined(terminated) ? false : terminated);
  37. };
  38. }
  39. var options = this.getCameraOptions(cameraOptions);
  40. if (this.view.headingPitchRoll) {
  41. camera.flyTo(options);
  42. }
  43. else if (this.view.headingPitchRange) {
  44. var target = new BoundingSphere(this.view.position);
  45. camera.flyToBoundingSphere(target, options);
  46. }
  47. };
  48. /**
  49. * Stop execution of curent entry. Cancel camera flyTo
  50. */
  51. KmlTourFlyTo.prototype.stop = function() {
  52. if (defined(this.activeCamera)) {
  53. this.activeCamera.cancelFlight();
  54. }
  55. if (defined(this.activeCallback)) {
  56. this.activeCallback(true);
  57. }
  58. };
  59. /**
  60. * Returns options for {@link Camera#flyTo} or {@link Camera#flyToBoundingSphere}
  61. * depends on this.view type.
  62. *
  63. * @param {Object} cameraOptions options to merge with generated. See {@link Camera#flyTo}
  64. * @returns {Object} {@link Camera#flyTo} or {@link Camera#flyToBoundingSphere} options
  65. */
  66. KmlTourFlyTo.prototype.getCameraOptions = function(cameraOptions) {
  67. var options = {
  68. duration: this.duration
  69. };
  70. if (defined(this.activeCallback)) {
  71. options.complete = this.activeCallback;
  72. }
  73. if (this.flyToMode === 'smooth' ) {
  74. options.easingFunction = EasingFunction.LINEAR_NONE;
  75. }
  76. if (this.view.headingPitchRoll) {
  77. options.destination = this.view.position;
  78. options.orientation = this.view.headingPitchRoll;
  79. }
  80. else if (this.view.headingPitchRange) {
  81. options.offset = this.view.headingPitchRange;
  82. }
  83. if (defined(cameraOptions)) {
  84. options = combine(options, cameraOptions);
  85. }
  86. return options;
  87. };
  88. /**
  89. * A function that will be executed when the flight completes.
  90. * @callback KmlTourFlyTo~DoneCallback
  91. *
  92. * @param {Boolean} terminated true if {@link KmlTourFlyTo#stop} was
  93. * called before entry done playback.
  94. */
  95. export default KmlTourFlyTo;