Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hpinkos committed Jul 9, 2013
1 parent 978ae17 commit 22e8775
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 179 deletions.
7 changes: 3 additions & 4 deletions Source/Scene/CentralBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,17 +782,16 @@ define([
};

function updateLogos(centralBody, context, frameState, commandList) {
var visibleCredits = [];
visibleCredits.push(centralBody._surface._terrainProvider.getCredit());
var creditDisplay = frameState.creditDisplay;
creditDisplay.addCredit(centralBody._surface._terrainProvider.getCredit());

var imageryLayerCollection = centralBody._imageryLayerCollection;
for ( var i = 0, len = imageryLayerCollection.getLength(); i < len; ++i) {
var layer = imageryLayerCollection.get(i);
if (layer.show) {
visibleCredits.push(layer.getImageryProvider().getCredit());
creditDisplay.addCredit(layer.getImageryProvider().getCredit());
}
}
frameState.creditDisplay.showCredits(visibleCredits);
}

return CentralBody;
Expand Down
7 changes: 3 additions & 4 deletions Source/Scene/Credit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ define([
* @constructor
*
* @example
* //Create a credit with an image and link
* var credit = new Credit('cesium-credit', undefined, '/images/cesium_logo.png', 'http://cesium.agi.com/');
* //Create a credit with a tooltip, image and link
* var credit = new Credit('Cesium', '/images/cesium_logo.png', 'http://cesium.agi.com/');
*/

var Credit = function(text, imageUrl, link) {
Expand All @@ -30,7 +30,6 @@ define([
this._imageUrl = imageUrl;

this._link = link;

};

/**
Expand Down Expand Up @@ -87,7 +86,7 @@ define([
*
* @memberof Credit
*
* @param Array {Credit} credits The credits to display
* @param {Credit} credits The credit to compare to.
*
* @return {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
*/
Expand Down
227 changes: 128 additions & 99 deletions Source/Scene/CreditDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@ define([
* var CreditDisplay = new CreditDisplay(creditContainer);
*/

var CreditDisplay = function(container, delimiter, canvas) {
var CreditDisplay = function(container, delimiter) {
if (typeof container === 'undefined') {
if (typeof canvas === 'undefined') {
throw new DeveloperError('credit container is required');
}
container = document.createElement('div');
container.style.position = "absolute";
container.style.bottom = "0";
container.style["text-shadow"] = "0px 0px 2px #000000";
container.style.color = "#ffffff";
container.style["font-size"] = "10pt";
container.style["padding-right"] = "5px";
canvas.parentNode.appendChild(container);
throw new DeveloperError('credit container is required');
}
var imageContainer = document.createElement('span');
var textContainer = document.createElement('span');
Expand All @@ -46,10 +36,33 @@ define([
this.textContainer = textContainer;
this.imageContainer = imageContainer;
this.defaultImageCredits = [];
this.visibleImageCredits = [];
this.defaultTextCredits = [];
this.visibleTextCredits = [];
this.previousCredits = [];

this.displayedCredits = {
imageCredits: [],
textCredits: []
};
this.currentFrameCredits = {
imageCredits: [],
textCredits: []
};
};

/**
* Adds a credit to the list of current credits to be displayed in the in the credit container
*
* @memberof CreditDisplay
*
* @param {Credit} credit The credit to display
*/
CreditDisplay.prototype.addCredit = function(credit) {
if (typeof credit !== 'undefined') {
if (typeof credit.getImageUrl() !== 'undefined') {
this.currentFrameCredits.imageCredits.push(credit);
} else {
this.currentFrameCredits.textCredits.push(credit);
}
}
};

/**
Expand All @@ -63,10 +76,10 @@ define([
if (typeof credit !== 'undefined') {
if (typeof credit.getImageUrl() === 'undefined') {
this.defaultTextCredits.push(credit);
addTextCredits(this);
this.currentFrameCredits.imageCredits.push(credit);
} else {
this.defaultImageCredits.push(credit);
addImageCredits(this);
this.currentFrameCredits.textCredits.push(credit);
}
}
};
Expand All @@ -85,133 +98,116 @@ define([
index = this.defaultTextCredits.indexOf(credit);
if (index !== -1) {
this.defaultTextCredits.splice(index, 1);
addTextCredits(this);
}
} else {
index = this.defaultImageCredits.indexOf(credit);
if (index !== -1) {
this.defaultImageCredits.splice(index, 1);
addImageCredits(this);
}
}
}
};

/**
* Displays all credits in a list to the credit container
* Resets the credit display to a beginning of frame state, clearing out current credits.
*
* @memberof CreditDisplay
*
* @param Array {Credit} credits The credits to display
* @param {Credit} credit The credit to display
*/
CreditDisplay.prototype.showCredits = function(credits) {
if (typeof credits !== 'undefined') {
var previousCredits = this.previousCredits;
if (previousCredits.length !== credits.length) {
processCredits(this, credits);
this.previousCredits = credits;
} else {
for (var i = 0; i < credits.length; i++) {
if (!Credit.equals(credits[i], this.previousCredits[i])) {
processCredits(this, credits);
this.previousCredits = credits;
break;
}
}
}
}
CreditDisplay.prototype.beginFrame = function() {
this.currentFrameCredits.imageCredits = this.defaultImageCredits.slice(0);
this.currentFrameCredits.textCredits = this.defaultTextCredits.slice(0);
};

function removeDuplicates(credits) {
var cleanedCredits = [];
var len = credits.length;
for (var i = 0; i < len; i++) {
var credit = credits[i];
cleanedCredits.push(credit);
for (var j = i + 1; j < len; j++) {
if (Credit.equals(credit, credits[j])) {
credits.splice(j, 1);
len--;
j--;
}
/**
* Sets the credit display to the end of frame state, displaying current credits in the credit container
*
* @memberof CreditDisplay
*
* @param {Credit} credit The credit to display
*/
CreditDisplay.prototype.endFrame = function() {
var credit;
var displayedTextCredits = this.displayedCredits.textCredits;
var displayedImageCredits = this.displayedCredits.imageCredits;
var textCredits = removeDuplicates(this.currentFrameCredits.textCredits);
var imageCredits = removeDuplicates(this.currentFrameCredits.imageCredits);
var i;
var index;
for(i = 0; i < textCredits.length; i++) {
credit = textCredits[i];
index = displayedTextCredits.indexOf(credit);
if (index === -1) {
displayTextCredit(credit, this.textContainer, this.delimiter);
} else {
displayedTextCredits.splice(index, 1);
}
}
return cleanedCredits;
}

function processCredits(creditDisplay, credits) {
var newImages = [];
var newText = [];
var i;
for (i = 0; i < displayedTextCredits.length; i++) {
credit = displayedTextCredits[i];
hideCredit(credit, true);
}

credits = removeDuplicates(credits);
for (i = 0; i < credits.length; i++) {
var credit = credits[i];
if (typeof credit !== 'undefined') {
if (typeof credit.getImageUrl() === 'undefined'){
newText.push(credit);
} else {
newImages.push(credit);
}
for(i = 0; i < imageCredits.length; i++) {
credit = imageCredits[i];
index = displayedImageCredits.indexOf(credit);
if (index === -1) {
displayImageCredit(credit, this.imageContainer);
} else {
displayedImageCredits.splice(index, 1);
}
}
for (i = 0; i < displayedImageCredits.length; i++) {
credit = displayedImageCredits[i];
hideCredit(credit, false);
}

this.displayedCredits.textCredits = textCredits;
this.displayedCredits.imageCredits = imageCredits;
};

creditDisplay.visibleImageCredits = newImages;
addImageCredits(creditDisplay);

creditDisplay.visibleTextCredits = newText;
addTextCredits(creditDisplay);
}

function addTextCredits(creditDisplay) {
var container = creditDisplay.textContainer;
var replacementContainer = document.createElement('span');
var textCredits = creditDisplay.defaultTextCredits.concat(creditDisplay.visibleTextCredits);
for (var i = 0; i < textCredits.length; i++) {
var credit = textCredits[i];
function displayTextCredit(credit, container, delimiter) {
if (typeof credit.element === 'undefined') {
var text = credit.getText();
var link = credit.getLink();
var str;
if (i !== 0) {
str = creditDisplay.delimiter;
} else {
str = '';
}
var txt;
if (typeof text !== 'undefined') {
str += text;
txt = document.createTextNode(text);
} else {
str += link;
txt = document.createTextNode(link);
}
var txt = document.createTextNode(str);
var span = document.createElement('span');
if (typeof link !== 'undefined') {
var a = document.createElement('a');
a.appendChild(txt);
a.href = link;
a.target = "_blank";

span.appendChild(a);
} else {
span.appendChild(txt);
}
replacementContainer.appendChild(span);
span.className = "cesium-credit-text";
credit.element = span;
}
if (container.innerHTML !== replacementContainer.innerHTML) {
container.innerHTML = replacementContainer.innerHTML;
if (container.hasChildNodes()) {
var del = document.createTextNode(delimiter);
var delSpan = document.createElement('span');
delSpan.appendChild(del);
delSpan.className = "cesium-credit-delimiter";
container.appendChild(delSpan);
}
container.appendChild(credit.element);
}

function addImageCredits(creditDisplay) {
var container = creditDisplay.imageContainer;
var replacementContainer = document.createElement('span');
var imageCredits = creditDisplay.defaultImageCredits.concat(creditDisplay.visibleImageCredits);
for (var i = 0; i < imageCredits.length; i++) {
var credit = imageCredits[i];
function displayImageCredit(credit, container) {
if (typeof credit.element === 'undefined') {
var text = credit.getText();
var link = credit.getLink();
var span = document.createElement('span');
var content = document.createElement('img');
content.className = "credit-image";
content.src = credit.getImageUrl();
content.style["vertical-align"] = "bottom";
if (typeof text !== 'undefined') {
Expand All @@ -228,10 +224,43 @@ define([
} else {
span.appendChild(content);
}
replacementContainer.appendChild(span);
span.className = "cesium-credit-image";
credit.element = span;
}
container.appendChild(credit.element);
}

function removeDuplicates(credits) {
var cleanedCredits = [];
var len = credits.length;
for (var i = 0; i < len; i++) {
var credit = credits[i];
cleanedCredits.push(credit);
for (var j = i + 1; j < len; j++) {
if (Credit.equals(credit, credits[j])) {
credits.splice(j, 1);
len--;
j--;
}
}
}
if (container.innerHTML !== replacementContainer.innerHTML) {
container.innerHTML = replacementContainer.innerHTML;
return cleanedCredits;
}

function hideCredit(credit, isText) {
var element = credit.element;
if (typeof element !== 'undefined') {
var container = element.parentNode;
if (isText) {
var delimiter = element.previousSibling;
if (delimiter === null) {
delimiter = element.nextSibling;
}
if (delimiter !== null) {
container.removeChild(delimiter);
}
}
container.removeChild(element);
}
}

Expand Down
12 changes: 11 additions & 1 deletion Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ define([
if (typeof creditContainer !== 'undefined') {
creditDisplay = new CreditDisplay(creditContainer);
} else {
creditDisplay= new CreditDisplay(undefined, undefined, canvas);
var creditDiv = document.createElement('div');
creditDiv.style.position = "absolute";
creditDiv.style.bottom = "0";
creditDiv.style["text-shadow"] = "0px 0px 2px #000000";
creditDiv.style.color = "#ffffff";
creditDiv.style["font-size"] = "10pt";
creditDiv.style["padding-right"] = "5px";
canvas.parentNode.appendChild(creditDiv);
creditDisplay = new CreditDisplay(creditDiv);
}
this._frameState = new FrameState(creditDisplay);
this._passState = new PassState(context);
Expand Down Expand Up @@ -559,6 +567,7 @@ define([
updateFrameState(this, frameNumber, time);
frameState.passes.color = true;
frameState.passes.overlay = true;
frameState.creditDisplay.beginFrame();

us.update(frameState);

Expand All @@ -572,6 +581,7 @@ define([
var passState = this._passState;
executeCommands(this, passState);
executeOverlayCommands(this, passState);
frameState.creditDisplay.endFrame();
};

var orthoPickingFrustum = new OrthographicFrustum();
Expand Down
Loading

0 comments on commit 22e8775

Please sign in to comment.