All files / engine/Source/DataSources KmlTour.js

85.71% Statements 42/49
64.28% Branches 9/14
88.88% Functions 8/9
85.71% Lines 42/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                              7x         7x         7x         7x           7x                 7x             7x                 7x   7x               1x 12x                   1x 3x   3x 3x 3x   3x 2x   3x             1x 1x         3x 3x       1x             7x 7x 5x 5x 5x 5x 5x                       2x 2x           5x 5x   5x 1x   4x 4x 4x   4x 4x        
import defined from "../Core/defined.js";
import Event from "../Core/Event.js";
/**
 * Describes a KmlTour, which uses KmlTourFlyTo, and KmlTourWait to
 * guide the camera to a specified destinations on given time intervals.
 *
 * @alias KmlTour
 * @constructor
 *
 * @param {string} name name parsed from KML
 * @param {string} id id parsed from KML
 * @param {Array} playlist array with KmlTourFlyTos and KmlTourWaits
 *
 * @see KmlTourFlyTo
 * @see KmlTourWait
 *
 * @demo {@link https://sandcastle.cesium.com/?src=KML%20Tours.html|KML Tours}
 */
function KmlTour(name, id) {
  /**
   * Id of kml gx:Tour entry
   * @type {string}
   */
  this.id = id;
  /**
   * Tour name
   * @type {string}
   */
  this.name = name;
  /**
   * Index of current entry from playlist
   * @type {number}
   */
  this.playlistIndex = 0;
  /**
   * Array of playlist entries
   * @type {Array}
   */
  this.playlist = [];
  /**
   * Event will be called when tour starts to play,
   * before any playlist entry starts to play.
   * @type Event
   */
  this.tourStart = new Event();
  /**
   * Event will be called when all playlist entries are
   * played, or tour playback being canceled.
   *
   * If tour playback was terminated, event callback will
   * be called with terminated=true parameter.
   * @type Event
   */
  this.tourEnd = new Event();
  /**
   * Event will be called when entry from playlist starts to play.
   *
   * Event callback will be called with curent entry as first parameter.
   * @type Event
   */
  this.entryStart = new Event();
  /**
   * Event will be called when entry from playlist ends to play.
   *
   * Event callback will be called with following parameters:
   * 1. entry - entry
   * 2. terminated - true if playback was terminated by calling {@link KmlTour#stop}
   * @type Event
   */
  this.entryEnd = new Event();
 
  this._activeEntries = [];
}
 
/**
 * Add entry to this tour playlist.
 *
 * @param {KmlTourFlyTo|KmlTourWait} entry an entry to add to the playlist.
 */
KmlTour.prototype.addPlaylistEntry = function (entry) {
  this.playlist.push(entry);
};
 
/**
 * Play this tour.
 *
 * @param {CesiumWidget} widget The widget.
 * @param {object} [cameraOptions] these options will be merged with {@link Camera#flyTo}
 * options for FlyTo playlist entries.
 */
KmlTour.prototype.play = function (widget, cameraOptions) {
  this.tourStart.raiseEvent();
 
  const tour = this;
  playEntry.call(this, widget, cameraOptions, function (terminated) {
    tour.playlistIndex = 0;
    // Stop nonblocking entries
    if (!terminated) {
      cancelAllEntries(tour._activeEntries);
    }
    tour.tourEnd.raiseEvent(terminated);
  });
};
 
/**
 * Stop curently playing tour.
 */
KmlTour.prototype.stop = function () {
  cancelAllEntries(this._activeEntries);
};
 
// Stop all activeEntries.
function cancelAllEntries(activeEntries) {
  for (
    let entry = activeEntries.pop();
    entry !== undefined;
    entry = activeEntries.pop()
  ) {
    entry.stop();
  }
}
 
// Play playlist entry.
// This function is called recursevly with playNext and iterates over all entries from playlist.
function playEntry(widget, cameraOptions, allDone) {
  const entry = this.playlist[this.playlistIndex];
  if (entry) {
    const _playNext = playNext.bind(this, widget, cameraOptions, allDone);
    this._activeEntries.push(entry);
    this.entryStart.raiseEvent(entry);
    if (entry.blocking) {
      entry.play(_playNext, widget.scene.camera, cameraOptions);
    } else E{
      const tour = this;
      entry.play(function () {
        tour.entryEnd.raiseEvent(entry);
        const indx = tour._activeEntries.indexOf(entry);
        if (indx >= 0) {
          tour._activeEntries.splice(indx, 1);
        }
      });
      _playNext(widget, cameraOptions, allDone);
    }
  } else Eif (defined(allDone)) {
    allDone(false);
  }
}
 
// Increment playlistIndex and call playEntry if terminated isn't true.
function playNext(widget, cameraOptions, allDone, terminated) {
  const entry = this.playlist[this.playlistIndex];
  this.entryEnd.raiseEvent(entry, terminated);
 
  if (terminated) {
    allDone(terminated);
  } else {
    const indx = this._activeEntries.indexOf(entry);
    Eif (indx >= 0) {
      this._activeEntries.splice(indx, 1);
    }
    this.playlistIndex++;
    playEntry.call(this, widget, cameraOptions, allDone);
  }
}
export default KmlTour;