KmlTourFlyTo.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. } else if (this.view.headingPitchRange) {
  43. var target = new BoundingSphere(this.view.position);
  44. camera.flyToBoundingSphere(target, options);
  45. }
  46. };
  47. /**
  48. * Stop execution of curent entry. Cancel camera flyTo
  49. */
  50. KmlTourFlyTo.prototype.stop = function () {
  51. if (defined(this.activeCamera)) {
  52. this.activeCamera.cancelFlight();
  53. }
  54. if (defined(this.activeCallback)) {
  55. this.activeCallback(true);
  56. }
  57. };
  58. /**
  59. * Returns options for {@link Camera#flyTo} or {@link Camera#flyToBoundingSphere}
  60. * depends on this.view type.
  61. *
  62. * @param {Object} cameraOptions options to merge with generated. See {@link Camera#flyTo}
  63. * @returns {Object} {@link Camera#flyTo} or {@link Camera#flyToBoundingSphere} options
  64. */
  65. KmlTourFlyTo.prototype.getCameraOptions = function (cameraOptions) {
  66. var options = {
  67. duration: this.duration,
  68. };
  69. if (defined(this.activeCallback)) {
  70. options.complete = this.activeCallback;
  71. }
  72. if (this.flyToMode === "smooth") {
  73. options.easingFunction = EasingFunction.LINEAR_NONE;
  74. }
  75. if (this.view.headingPitchRoll) {
  76. options.destination = this.view.position;
  77. options.orientation = this.view.headingPitchRoll;
  78. } else if (this.view.headingPitchRange) {
  79. options.offset = this.view.headingPitchRange;
  80. }
  81. if (defined(cameraOptions)) {
  82. options = combine(options, cameraOptions);
  83. }
  84. return options;
  85. };
  86. /**
  87. * A function that will be executed when the flight completes.
  88. * @callback KmlTourFlyTo.DoneCallback
  89. *
  90. * @param {Boolean} terminated true if {@link KmlTourFlyTo#stop} was
  91. * called before entry done playback.
  92. */
  93. export default KmlTourFlyTo;