Skip to content

Commit

Permalink
Merge pull request #2600 from AnalyticalGraphicsInc/entity-show-faster
Browse files Browse the repository at this point in the history
Support for Entity.show
  • Loading branch information
pjcozzi committed Mar 25, 2015
2 parents 57b94f4 + bca588a commit a7ba397
Show file tree
Hide file tree
Showing 36 changed files with 493 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Apps/Sandcastle/gallery/Custom DataSource.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@
set : function(value) {
this._seriesToDisplay = value;

//Iterate over all polylines and set their show property
//Iterate over all entities and set their show property
//to true only if they are part of the current series.
var collection = this._entityCollection;
var entities = collection.values;
collection.suspendEvents();
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
entity.polyline.show.setValue(value === entity.seriesName);
entity.show = value === entity.seriesName;
}
collection.resumeEvents();
}
Expand Down Expand Up @@ -287,7 +287,6 @@

//WebGL Globe only contains lines, so that's the only graphics we create.
var polyline = new Cesium.PolylineGraphics();
polyline.show = new Cesium.ConstantProperty(show);
polyline.material = new Cesium.ColorMaterialProperty(color);
polyline.width = new Cesium.ConstantProperty(2);
polyline.followSurface = new Cesium.ConstantProperty(false);
Expand All @@ -296,6 +295,7 @@
//The polyline instance itself needs to be on an entity.
var entity = new Cesium.Entity({
id : seriesName + ' index ' + i.toString(),
show : show,
polyline : polyline,
seriesName : seriesName //Custom property to indicate series name
});
Expand Down
95 changes: 95 additions & 0 deletions Apps/Sandcastle/gallery/Show or Hide Entities.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <!-- Use Chrome Frame in IE -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="Show or hide groups of entities.">
<meta name="cesium-sandcastle-labels" content="Beginner">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.9/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl : '../../../Source',
waitSeconds : 60
});
</script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
"use strict";
//Sandcastle_Begin
//Set the random seed for reproducible random colors.
Cesium.Math.setRandomNumberSeed(1234);

var viewer = new Cesium.Viewer('cesiumContainer', { infoBox : false });
var entities = viewer.entities;

//Create Entity "folders" to allow us to turn on/off entities as a group.
var spheres = entities.add(new Cesium.Entity());
var boxes = entities.add(new Cesium.Entity());
var ellipsoids = entities.add(new Cesium.Entity());

//Create the entities and assign each entity's parent to the group to which it belongs.
for (var i = 0; i < 5; ++i) {
var height = 100000.0 + (200000.0 * i);
entities.add({
parent : boxes,
position : Cesium.Cartesian3.fromDegrees(-106.0, 45.0, height),
box : {
dimensions : new Cesium.Cartesian3(90000.0, 90000.0, 90000.0),
material : Cesium.Color.fromRandom({alpha : 1.0})
}
});

entities.add({
parent : ellipsoids,
position : Cesium.Cartesian3.fromDegrees(-102.0, 45.0, height),
ellipsoid : {
radii : new Cesium.Cartesian3(45000.0, 45000.0, 90000.0),
material : Cesium.Color.fromRandom({alpha : 1.0})
}
});

entities.add({
parent : spheres,
position : Cesium.Cartesian3.fromDegrees(-98.0, 45.0, height),
ellipsoid : {
radii : new Cesium.Cartesian3(67500.0, 67500.0, 67500.0),
material : Cesium.Color.fromRandom({alpha : 1.0})
}
});
}

viewer.zoomTo(viewer.entities);

Sandcastle.addToolbarButton('Toggle Boxes', function(){
boxes.show = !boxes.show;
});

Sandcastle.addToolbarButton('Toggle Ellipsoids', function(){
ellipsoids.show = !ellipsoids.show;
});

Sandcastle.addToolbarButton('Toggle Spheres', function(){
spheres.show = !spheres.show;
});
//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== "undefined") {
startup(Cesium);
} else if (typeof require === "function") {
require(["Cesium"], startup);
}
</script>
</body>
</html>
Binary file added Apps/Sandcastle/gallery/Show or Hide Entities.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Change Log
* Removed `Camera.transform`, which was deprecated in Cesium 1.6. Use `Camera.lookAtTransform`.
* Removed the `direction` and `up` options to `Camera.flyTo`, which were deprecated in Cesium 1.6. Use the `orientation` option.
* Removed `Camera.flyToRectangle`, which was deprecated in Cesium 1.6. Use `Camera.flyTo`.
* Added `Entity.show` which is a boolean for easily hiding or showing an entity and its children.
* Added `Entity.isShowing` which is a read-only property that indicates if an entity is currently being drawn.
* Added support for the KML `visibility` element.
* Added `PolylineArrowMaterialProperty` to allow entities materials to use polyline arrows.
* Improved `viewer.zoomTo` and `viewer.flyTo` so they are now "best effort" and work even if some entities being zoomed to are not currently in the scene.
* Fixed `PointerEvent` detection so that it works with older implementations of the specification. This also fixes lack of mouse handling when detection failed, such as when using Cesium in the Windows `WebBrowser` control.
* Fixed an issue with transparency. [#2572](https://github.com/AnalyticalGraphicsInc/cesium/issues/2572)
* Fixed improper handling of null values when loading `GeoJSON` data.
Expand Down
7 changes: 5 additions & 2 deletions Source/Core/AssociativeArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ define([
* Clears the collection.
*/
AssociativeArray.prototype.removeAll = function() {
this._hash = {};
this._array.length = 0;
var array = this._array;
if (array.length > 0) {
this._hash = {};
array.length = 0;
}
};

return AssociativeArray;
Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/BillboardVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ define([
var billboardGraphics = entity._billboard;
var textureValue;
var billboard = item.billboard;
var show = entity.isAvailable(time) && Property.getValueOrDefault(billboardGraphics._show, time, true);
var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(billboardGraphics._show, time, true);

if (show) {
position = Property.getValueOrUndefined(entity._position, time, position);
Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/BoxGeometryUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ define([
var geometryUpdater = this._geometryUpdater;
var entity = geometryUpdater._entity;
var box = entity.box;
if (!entity.isAvailable(time) || !Property.getValueOrDefault(box.show, time, true)) {
if (!entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(box.show, time, true)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/CorridorGeometryUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ define([
var geometryUpdater = this._geometryUpdater;
var entity = geometryUpdater._entity;
var corridor = entity.corridor;
if (!entity.isAvailable(time) || !Property.getValueOrDefault(corridor.show, time, true)) {
if (!entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(corridor.show, time, true)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/CylinderGeometryUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ define([
var geometryUpdater = this._geometryUpdater;
var entity = geometryUpdater._entity;
var cylinder = entity.cylinder;
if (!entity.isAvailable(time) || !Property.getValueOrDefault(cylinder.show, time, true)) {
if (!entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(cylinder.show, time, true)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/EllipseGeometryUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ define([
var geometryUpdater = this._geometryUpdater;
var entity = geometryUpdater._entity;
var ellipse = entity.ellipse;
if (!entity.isAvailable(time) || !Property.getValueOrDefault(ellipse.show, time, true)) {
if (!entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(ellipse.show, time, true)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/EllipsoidGeometryUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ define([

var entity = this._entity;
var ellipsoid = entity.ellipsoid;
if (!entity.isAvailable(time) || !Property.getValueOrDefault(ellipsoid.show, time, true)) {
if (!entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(ellipsoid.show, time, true)) {
if (defined(this._primitive)) {
this._primitive.show = false;
}
Expand Down
103 changes: 91 additions & 12 deletions Source/DataSources/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ define([
* @param {Object} [options] Object with the following properties:
* @param {String} [options.id] A unique identifier for this object. If none is provided, a GUID is generated.
* @param {String} [options.name] A human readable name to display to users. It does not have to be unique.
* @param {Boolean} [options.show] A boolean value indicating if the entity and its children are displayed.
* @param {Property} [options.description] A string Property specifying an HTML description for this entity.
* @param {PositionProperty} [options.position] A Property specifying the entity position.
* @param {Property} [options.orientation] A Property specifying the entity orientation.
Expand Down Expand Up @@ -132,8 +133,9 @@ define([
this._availability = undefined;
this._id = id;
this._definitionChanged = new Event();
this._name = undefined;
this._parent = options.parent;
this._name = options.name;
this._show = defaultValue(options.show, true);
this._parent = undefined;
this._propertyNames = ['billboard', 'box', 'corridor', 'cylinder', 'description', 'ellipse', //
'ellipsoid', 'label', 'model', 'orientation', 'path', 'point', 'polygon', //
'polyline', 'polylineVolume', 'position', 'rectangle', 'viewFrom', 'wall'];
Expand Down Expand Up @@ -176,10 +178,27 @@ define([
this._viewFromSubscription = undefined;
this._wall = undefined;
this._wallSubscription = undefined;
this._children = [];

this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
this.parent = options.parent;
this.merge(options);
};

function updateShow(entity, isShowing) {
var children = entity._children;
var length = children.length;
for (var i = 0; i < length; i++) {
var child = children[i];
var childShow = child._show;
var oldValue = !isShowing && childShow;
var newValue = isShowing && childShow;
if (oldValue !== newValue) {
child._definitionChanged.raiseEvent(child, 'isShowing', newValue, oldValue);
}
}
entity._definitionChanged.raiseEvent(entity, 'isShowing', isShowing, !isShowing);
}

defineProperties(Entity.prototype, {
/**
* The availability, if any, associated with this object.
Expand Down Expand Up @@ -219,25 +238,84 @@ define([
* @memberof Entity.prototype
* @type {String}
*/
name : {
configurable : false,
name : createRawPropertyDescriptor('name'),
/**
* Gets or sets whether this entity should be displayed. When set to true,
* the entity is only displayed if the parent entity's show property is also true.
* @memberof Entity.prototype
* @type {Boolean}
*/
show : {
get : function() {
return this._name;
return this._show;
},
set : function(value) {
var oldValue = this._name;
if (oldValue !== value) {
this._name = value;
this._definitionChanged.raiseEvent(this, 'name', value, oldValue);
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError('value is required.');
}
//>>includeEnd('debug');

if (value === this._show) {
return;
}

var wasShowing = this.isShowing;
this._show = value;
var isShowing = this.isShowing;

if (wasShowing !== isShowing) {
updateShow(this, isShowing);
}

this._definitionChanged.raiseEvent(this, 'show', value, !value);
}
},
/**
* Gets whether this entity is being displayed, taking into account
* the visibility of any ancestor entities.
* @memberof Entity.prototype
* @type {Boolean}
*/
isShowing : {
get : function() {
return this._show && (!defined(this._parent) || this._parent._show);
}
},
/**
* Gets or sets the parent object.
* @memberof Entity.prototype
* @type {Entity}
*/
parent : createRawPropertyDescriptor('parent'),
parent : {
get : function() {
return this._parent;
},
set : function(value) {
var oldValue = this._parent;

if (oldValue === value) {
return;
}

var wasShowing = this.isShowing;
if (defined(oldValue)) {
var index = oldValue._children.indexOf(this);
oldValue._children.splice(index, 1);
}

this._parent = value;
value._children.push(this);

var isShowing = this.isShowing;

if (wasShowing !== isShowing) {
updateShow(this, isShowing);
}

this._definitionChanged.raiseEvent(this, 'parent', value, oldValue);
}
},
/**
* Gets the names of all properties registered on this instance.
* @memberof Entity.prototype
Expand Down Expand Up @@ -448,7 +526,8 @@ define([
}
//>>includeEnd('debug');

//Name and availability are not Property objects and are currently handled differently.
//Name, show, and availability are not Property objects and are currently handled differently.
//source.show is intentionally ignored because this.show always has a value.
this.name = defaultValue(this.name, source.name);
this.availability = defaultValue(source.availability, this.availability);

Expand Down
4 changes: 2 additions & 2 deletions Source/DataSources/KmlDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -1299,8 +1299,8 @@ define([
}
entity.availability = availability;

//var visibility = queryBooleanValue(featureNode, 'visibility', namespaces.kml);
//entity.uiShow = defaultValue(visibility, true);
var visibility = queryBooleanValue(featureNode, 'visibility', namespaces.kml);
entity.show = defaultValue(visibility, true);
//var open = queryBooleanValue(featureNode, 'open', namespaces.kml);

var authorNode = queryFirstNode(featureNode, 'author', namespaces.atom);
Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/LabelVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ define([
var labelGraphics = entity._label;
var text;
var label = item.label;
var show = entity.isAvailable(time) && Property.getValueOrDefault(labelGraphics._show, time, true);
var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(labelGraphics._show, time, true);

if (show) {
position = Property.getValueOrUndefined(entity._position, time, position);
Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/ModelVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ define([

var uri;
var modelData = modelHash[entity.id];
var show = entity.isAvailable(time) && Property.getValueOrDefault(modelGraphics._show, time, true);
var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(modelGraphics._show, time, true);

var modelMatrix;
if (show) {
Expand Down
2 changes: 1 addition & 1 deletion Source/DataSources/PathVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ define([
var sampleStop;
var showProperty = pathGraphics._show;
var polyline = item.polyline;
var show = !defined(showProperty) || showProperty.getValue(time);
var show = entity.isShowing && (!defined(showProperty) || showProperty.getValue(time));

//While we want to show the path, there may not actually be anything to show
//depending on lead/trail settings. Compute the interval of the path to
Expand Down
Loading

0 comments on commit a7ba397

Please sign in to comment.