Skip to content

Commit

Permalink
Merge pull request #6316 from AnalyticalGraphicsInc/data-sources-order
Browse files Browse the repository at this point in the history
Add ordering at the DataSource level
  • Loading branch information
mramato authored Mar 20, 2018
2 parents 4b7f432 + 5f5c5a5 commit c268786
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 33 deletions.
127 changes: 127 additions & 0 deletions Apps/Sandcastle/gallery/DataSource Ordering.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="Change the order in which DataSources draw ground primitives">
<meta name="cesium-sandcastle-labels" content="DataSources">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<script type="text/javascript">
if(typeof require === "function") {
require.config({
baseUrl : '../../../Source',
waitSeconds : 120
});
}
</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
var czml1 = [{
"id" : "document",
"name" : "CZML Geometries: Rectangle",
"version" : "1.0"
}, {
"rectangle" : {
"coordinates" : {
"wsenDegrees" : [-120, 40, -110, 50]
},
"fill" : true,
"material" : {
"solidColor" : {
"color": {
"rgba" : [255, 0, 0, 255]
}
}
}
}
}, {
"rectangle" : {
"coordinates" : {
"wsenDegrees" : [-110, 40, -100, 50]
},
"fill" : true,
"material" : {
"solidColor" : {
"color": {
"rgba" : [0, 0, 255, 255]
}
}
}
}
}];

var czml2 = [{
"id" : "document",
"name" : "CZML Geometries: Rectangle",
"version" : "1.0"
}, {
"rectangle" : {
"coordinates" : {
"wsenDegrees" : [-120, 45, -110, 55]
},
"fill" : true,
"material" : {
"solidColor" : {
"color": {
"rgba" : [255, 255, 0, 255]
}
}
}
}
}, {
"rectangle" : {
"coordinates" : {
"wsenDegrees" : [-110, 45, -100, 55]
},
"fill" : true,
"material" : {
"solidColor" : {
"color": {
"rgba" : [0, 255, 255, 255]
}
}
}
}
}];

var viewer = new Cesium.Viewer('cesiumContainer');
var promise1 = Cesium.CzmlDataSource.load(czml1);
viewer.dataSources.add(promise1);
var promise2 = Cesium.CzmlDataSource.load(czml2);
viewer.dataSources.add(promise2);

Sandcastle.addToolbarButton('Swap', function() {
Cesium.when.all([promise1, promise2])
.then(function(results) {
var ds1 = results[0];
var ds2 = results[1];
if (viewer.dataSources.indexOf(ds1) === 0) {
viewer.dataSources.raise(ds1);
} else {
viewer.dataSources.lower(ds1);
}
});
});//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/DataSource Ordering.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Change Log

### 1.44 - 2018-04-02

##### Breaking Changes :mega:
* `GeometryVisualizer` now requires `primitive` and `groundPrimitive` parameters. [#6316](https://github.com/AnalyticalGraphicsInc/cesium/pull/6316)

##### Deprecated :hourglass_flowing_sand:
* `ClippingPlaneCollection` is now supported in Internet Explorer, so `ClippingPlaneCollection.isSupported` has been deprecated and will be removed in Cesium 1.45.
* `ClippingPlaneCollection` should now be used with `ClippingPlane` objects instead of `Plane`. Use of `Plane` objects has been deprecated and will be removed in Cesium 1.45.
Expand All @@ -20,6 +23,9 @@ Change Log
* sourceType specifies the type of data source if the URL doesn't have a known file extension.
* flyTo=false optionally disables the automatic flyTo after loading the data source.
* Added a multi-part CZML example to Sandcastle. [#6320](https://github.com/AnalyticalGraphicsInc/cesium/pull/6320)
* Added support for ordering in `DataSourceCollection` [#6316](https://github.com/AnalyticalGraphicsInc/cesium/pull/6316)
* All ground geometry from one `DataSource` will render in front of all ground geometry from another `DataSource` in the same collection with a lower index.
* Use `DataSourceCollection.raise`, `DataSourceCollection.lower`, `DataSourceCollection.raiseToTop` and `DataSourceCollection.lowerToBottom` functions to change the ordering of a `DataSource` in the collection.

##### Fixes :wrench:
* Fixed support of glTF-supplied tangent vectors. [#6302](https://github.com/AnalyticalGraphicsInc/cesium/pull/6302)
Expand Down
115 changes: 115 additions & 0 deletions Source/DataSources/DataSourceCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ define([
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/Event',
'../Core/Math',
'../ThirdParty/when'
], function(
defaultValue,
Expand All @@ -13,6 +14,7 @@ define([
destroyObject,
DeveloperError,
Event,
CesiumMath,
when) {
'use strict';

Expand All @@ -25,6 +27,7 @@ define([
this._dataSources = [];
this._dataSourceAdded = new Event();
this._dataSourceRemoved = new Event();
this._dataSourceMoved = new Event();
}

defineProperties(DataSourceCollection.prototype, {
Expand Down Expand Up @@ -64,6 +67,19 @@ define([
get : function() {
return this._dataSourceRemoved;
}
},

/**
* An event that is raised when a data source changes position in the collection. Event handlers are passed the data source
* that was moved, its new index after the move, and its old index prior to the move.
* @memberof DataSourceCollection.prototype
* @type {Event}
* @readonly
*/
dataSourceMoved : {
get : function() {
return this._dataSourceMoved;
}
}
});

Expand Down Expand Up @@ -177,6 +193,105 @@ define([
return this._dataSources[index];
};

function getIndex(dataSources, dataSource) {
//>>includeStart('debug', pragmas.debug);
if (!defined(dataSource)) {
throw new DeveloperError('dataSource is required.');
}
//>>includeEnd('debug');

var index = dataSources.indexOf(dataSource);

//>>includeStart('debug', pragmas.debug);
if (index === -1) {
throw new DeveloperError('dataSource is not in this collection.');
}
//>>includeEnd('debug');

return index;
}

function swapDataSources(collection, i, j) {
var arr = collection._dataSources;
var length = arr.length - 1;
i = CesiumMath.clamp(i, 0, length);
j = CesiumMath.clamp(j, 0, length);

if (i === j) {
return;
}

var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

collection.dataSourceMoved.raiseEvent(temp, j, i);
}

/**
* Raises a data source up one position in the collection.
*
* @param {DataSource} dataSource The data source to move.
*
* @exception {DeveloperError} dataSource is not in this collection.
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*/
DataSourceCollection.prototype.raise = function(dataSource) {
var index = getIndex(this._dataSources, dataSource);
swapDataSources(this, index, index + 1);
};

/**
* Lowers a data source down one position in the collection.
*
* @param {DataSource} dataSource The data source to move.
*
* @exception {DeveloperError} dataSource is not in this collection.
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*/
DataSourceCollection.prototype.lower = function(dataSource) {
var index = getIndex(this._dataSources, dataSource);
swapDataSources(this, index, index - 1);
};

/**
* Raises a data source to the top of the collection.
*
* @param {DataSource} dataSource The data source to move.
*
* @exception {DeveloperError} dataSource is not in this collection.
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*/
DataSourceCollection.prototype.raiseToTop = function(dataSource) {
var index = getIndex(this._dataSources, dataSource);
if (index === this._dataSources.length - 1) {
return;
}
this._dataSources.splice(index, 1);
this._dataSources.push(dataSource);

this.dataSourceMoved.raiseEvent(dataSource, this._dataSources.length - 1, index);
};

/**
* Lowers a data source to the bottom of the collection.
*
* @param {DataSource} dataSource The data source to move.
*
* @exception {DeveloperError} dataSource is not in this collection.
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*/
DataSourceCollection.prototype.lowerToBottom = function(dataSource) {
var index = getIndex(this._dataSources, dataSource);
if (index === 0) {
return;
}
this._dataSources.splice(index, 1);
this._dataSources.splice(0, 0, dataSource);

this.dataSourceMoved.raiseEvent(dataSource, 0, index);
};

/**
* Returns true if this object was destroyed; otherwise, false.
* If this object was destroyed, it should not be used; calling any function other than
Expand Down
Loading

0 comments on commit c268786

Please sign in to comment.