diff --git a/CHANGES.md b/CHANGES.md index f484be93c2d1..697db81375fb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ Change Log ### 1.32 - 2017-04-03 +* Added the event `Viewer.trackedEntityChanged`, which is raised when the value of `viewer.trackedEntity` changes. [#5060](https://github.com/AnalyticalGraphicsInc/cesium/pull/5060) * Fix crunch compressed textures in IE11. [#5057](https://github.com/AnalyticalGraphicsInc/cesium/pull/5057) ### 1.31 - 2017-03-01 diff --git a/Source/Widgets/Viewer/Viewer.js b/Source/Widgets/Viewer/Viewer.js index ec559ffe581b..3fd5049fe410 100644 --- a/Source/Widgets/Viewer/Viewer.js +++ b/Source/Widgets/Viewer/Viewer.js @@ -664,6 +664,7 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to this._zoomPromise = undefined; this._zoomOptions = undefined; this._selectedEntityChanged = new Event(); + this._trackedEntityChanged = new Event(); knockout.track(this, ['_trackedEntity', '_selectedEntity', '_clockTrackedDataSource']); @@ -1171,12 +1172,13 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to this._entityView = undefined; this.camera.lookAtTransform(Matrix4.IDENTITY); - return; + } else { + //We can't start tracking immediately, so we set a flag and start tracking + //when the bounding sphere is ready (most likely next frame). + this._needTrackedEntityUpdate = true; } - //We can't start tracking immediately, so we set a flag and start tracking - //when the bounding sphere is ready (most likely next frame). - this._needTrackedEntityUpdate = true; + this._trackedEntityChanged.raiseEvent(value); } } }, @@ -1218,6 +1220,17 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to return this._selectedEntityChanged; } }, + /** + * Gets the event that is raised when the tracked entity chages + * @memberof Viewer.prototype + * @type {Event} + * @readonly + */ + trackedEntityChanged : { + get : function() { + return this._trackedEntityChanged; + } + }, /** * Gets or sets the data source to track with the viewer's clock. * @memberof Viewer.prototype diff --git a/Specs/Widgets/Viewer/ViewerSpec.js b/Specs/Widgets/Viewer/ViewerSpec.js index b07e33146e95..9b91922c4b87 100644 --- a/Specs/Widgets/Viewer/ViewerSpec.js +++ b/Specs/Widgets/Viewer/ViewerSpec.js @@ -33,7 +33,7 @@ defineSuite([ 'Widgets/SceneModePicker/SceneModePicker', 'Widgets/SelectionIndicator/SelectionIndicator', 'Widgets/Timeline/Timeline' - ], function( + ], 'Widgets/Viewer/Viewer', function( Cartesian3, ClockRange, ClockStep, @@ -850,6 +850,30 @@ defineSuite([ viewer.destroy(); }); + it('raises an event when the tracked entity changes', function() { + var viewer = createViewer(container); + + var dataSource = new MockDataSource(); + viewer.dataSources.add(dataSource); + + var entity = new Entity(); + entity.position = new ConstantPositionProperty(new Cartesian3(123456, 123456, 123456)); + + dataSource.entities.add(entity); + + var myEntity; + viewer.trackedEntityChanged.addEventListener(function(newValue) { + myEntity = newValue; + }); + viewer.trackedEntity = entity; + expect(myEntity).toBe(entity); + + viewer.trackedEntity = undefined; + expect(myEntity).toBeUndefined(); + + viewer.destroy(); + }); + it('selectedEntity sets InfoBox properties', function() { var viewer = createViewer(container);