KmlTourFlyTo.js 3.1 KB

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