Skip to content

Commit

Permalink
Replace Bucket::add methods with top level methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Oct 26, 2015
1 parent 912cc77 commit 916b75a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
23 changes: 17 additions & 6 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ function Bucket(options) {
this.add = {};
for (var shaderName in this.shaders) {
var shader = this.shaders[shaderName];

this.add[shaderName] = {
vertex: createVertexAddMethod(shaderName, shader).bind(this),
element: createElementAddMethod(shaderName, shader, false).bind(this),
secondElement: createElementAddMethod(shaderName, shader, true).bind(this)
};
this[this.getAddMethodName(shaderName, 'vertex')] = createVertexAddMethod(shaderName, shader).bind(this);
this[this.getAddMethodName(shaderName, 'element')] = createElementAddMethod(shaderName, shader, false).bind(this);
this[this.getAddMethodName(shaderName, 'secondElement')] = createElementAddMethod(shaderName, shader, true).bind(this);
}
}

Expand Down Expand Up @@ -135,6 +132,16 @@ Bucket.prototype.resetBuffers = function(buffers) {
this.elementGroups = createElementGroups(this.shaders, this.buffers);
};

/**
* Get the name of the method used to add an item to a buffer.
* @param {string} shaderName The name of the shader that will use the buffer
* @param {string} type One of "vertex", "element", or "secondElement"
* @returns {string}
*/
Bucket.prototype.getAddMethodName = function(shaderName, type) {
return 'add' + capitalize(shaderName) + capitalize(type);
};

function createLayoutProperties(layer, zoom) {
var values = new StyleDeclarationSet('layout', layer.type, layer.layout, {}).values();
var fakeZoomHistory = { lastIntegerZoom: Infinity, lastIntegerZoomTime: 0, lastZoom: 0 };
Expand Down Expand Up @@ -226,3 +233,7 @@ function createElementBuffer(components) {
}]
});
}

function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
12 changes: 6 additions & 6 deletions js/data/circle_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ CircleBucket.prototype.addFeature = function(feature) {
// │ 0 1 │
// └─────────┘

var vertex0 = this.add.circle.vertex(x, y, -1, -1);
var vertex1 = this.add.circle.vertex(x, y, 1, -1);
var vertex2 = this.add.circle.vertex(x, y, 1, 1);
var vertex3 = this.add.circle.vertex(x, y, -1, 1);
var vertex0 = this.addCircleVertex(x, y, -1, -1);
var vertex1 = this.addCircleVertex(x, y, 1, -1);
var vertex2 = this.addCircleVertex(x, y, 1, 1);
var vertex3 = this.addCircleVertex(x, y, -1, 1);

this.add.circle.element(vertex0, vertex1, vertex2);
this.add.circle.element(vertex0, vertex3, vertex2);
this.addCircleElement(vertex0, vertex1, vertex2);
this.addCircleElement(vertex0, vertex3, vertex2);
}

};
6 changes: 3 additions & 3 deletions js/data/fill_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ FillBucket.prototype.addFill = function(vertices) {
for (var i = 0; i < vertices.length; i++) {
var currentVertex = vertices[i];

var currentIndex = this.add.fill.vertex(currentVertex.x, currentVertex.y);
var currentIndex = this.addFillVertex(currentVertex.x, currentVertex.y);
if (i === 0) firstIndex = currentIndex;

// Only add triangles that have distinct vertices.
if (i >= 2 && (currentVertex.x !== vertices[0].x || currentVertex.y !== vertices[0].y)) {
this.add.fill.element(firstIndex, prevIndex, currentIndex);
this.addFillElement(firstIndex, prevIndex, currentIndex);
}

if (i >= 1) {
this.add.fill.secondElement(prevIndex, currentIndex);
this.addFillSecondElement(prevIndex, currentIndex);
}

prevIndex = currentIndex;
Expand Down
12 changes: 6 additions & 6 deletions js/data/line_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,18 @@ LineBucket.prototype.addCurrentVertex = function(currentVertex, flip, distance,

extrude = normal.mult(flip);
if (endLeft) extrude._sub(normal.perp()._mult(endLeft));
this.e3 = this.add.line.vertex(currentVertex, extrude, tx, 0, distance);
this.e3 = this.addLineVertex(currentVertex, extrude, tx, 0, distance);
if (this.e1 >= 0 && this.e2 >= 0) {
this.add.line.element(this.e1, this.e2, this.e3);
this.addLineElement(this.e1, this.e2, this.e3);
}
this.e1 = this.e2;
this.e2 = this.e3;

extrude = normal.mult(-flip);
if (endRight) extrude._sub(normal.perp()._mult(endRight));
this.e3 = this.add.line.vertex(currentVertex, extrude, tx, 1, distance);
this.e3 = this.addLineVertex(currentVertex, extrude, tx, 1, distance);
if (this.e1 >= 0 && this.e2 >= 0) {
this.add.line.element(this.e1, this.e2, this.e3);
this.addLineElement(this.e1, this.e2, this.e3);
}
this.e1 = this.e2;
this.e2 = this.e3;
Expand All @@ -349,10 +349,10 @@ LineBucket.prototype.addPieSliceVertex = function(currentVertex, flip, distance,
var ty = lineTurnsLeft ? 1 : 0;
extrude = extrude.mult(flip * (lineTurnsLeft ? -1 : 1));

this.e3 = this.add.line.vertex(currentVertex, extrude, 0, ty, distance);
this.e3 = this.addLineVertex(currentVertex, extrude, 0, ty, distance);

if (this.e1 >= 0 && this.e2 >= 0) {
this.add.line.element(this.e1, this.e2, this.e3);
this.addLineElement(this.e1, this.e2, this.e3);
}

if (lineTurnsLeft) {
Expand Down
20 changes: 10 additions & 10 deletions js/data/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ SymbolBucket.prototype.addSymbols = function(shaderName, quads, scale, keepUprig

this.makeRoomFor(shaderName, 4 * quads.length);

var addElement = this.add[shaderName].element;
var addVertex = this.add[shaderName].vertex;
var addElement = this[this.getAddMethodName(shaderName, 'element')];
var addVertex = this[this.getAddMethodName(shaderName, 'vertex')];

var zoom = this.zoom;
var placementZoom = Math.max(Math.log(scale) / Math.LN2 + zoom, 0);
Expand Down Expand Up @@ -488,14 +488,14 @@ SymbolBucket.prototype.addToDebugBuffers = function(collisionTile) {
var maxZoom = Math.max(0, Math.min(25, this.zoom + Math.log(box.maxScale) / Math.LN2));
var placementZoom = Math.max(0, Math.min(25, this.zoom + Math.log(box.placementScale) / Math.LN2));

this.add.collisionBox.vertex(anchorPoint, tl, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, tr, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, tr, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, br, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, br, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, bl, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, bl, maxZoom, placementZoom);
this.add.collisionBox.vertex(anchorPoint, tl, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, tl, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, tr, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, tr, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, br, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, br, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, bl, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, bl, maxZoom, placementZoom);
this.addCollisionBoxVertex(anchorPoint, tl, maxZoom, placementZoom);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ test('Bucket', function(t) {
Class.prototype.addFeature = function(feature) {
this.makeRoomFor('test', 1);
var point = feature.loadGeometry()[0][0];
this.add.test.vertex(point.x, point.y);
this.add.test.element(1, 2, 3);
this.add.test.secondElement(point.x, point.y);
this.addTestVertex(point.x, point.y);
this.addTestElement(1, 2, 3);
this.addTestSecondElement(point.x, point.y);
};

return Class;
Expand Down

0 comments on commit 916b75a

Please sign in to comment.