Less segregation of ways and nodes in renderer code

This commit is contained in:
Tom MacWright
2012-10-23 15:12:14 -04:00
parent 948a5c600c
commit b70aea97ef
7 changed files with 55 additions and 109 deletions
+2
View File
@@ -89,10 +89,12 @@ require(["dojo/dom-geometry","dojo/dom-class","dojo/on","dojo/dom","dojo/Evented
$('#undo').click(function() {
controller.undoStack.undo();
map.updateUIs(true, true);
});
$('#redo').click(function() {
controller.undoStack.redo();
map.updateUIs(true, true);
});
// ----------------------------------------------------
+16 -26
View File
@@ -12,8 +12,7 @@ iD.Connection = function(apiURL) {
var nextNode = -1, // next negative ids
nextWay = -1, // |
nextRelation = -1, // |
nodes = {},
ways = {},
entities = {},
relations = {},
pois = {},
modified = false,
@@ -24,20 +23,20 @@ iD.Connection = function(apiURL) {
function assign(obj) {
// summary: Save an entity to the data store.
switch (obj.entityType) {
case "node": nodes[obj.id]=obj; break;
case "way": ways[obj.id]=obj; break;
case "relation": relations[obj.id]=obj; break;
case "node": entities[obj.id] = obj; break;
case "way": entities[obj.id] = obj; break;
case "relation": relations[obj.id] = obj; break;
}
}
function getOrCreate(id, type) {
// summary: Return an entity if it exists: if not, create an empty one with the given id, and return that.
if (type === 'node') {
if (!nodes[id]) assign(new iD.Node(connection, id, NaN, NaN, {}, false));
return nodes[id];
if (!entities[id]) assign(new iD.Node(connection, id, NaN, NaN, {}, false));
return entities[id];
} else if (type === 'way') {
if (!ways[id]) assign(new iD.Way(connection, id, [], {}, false));
return ways[id];
if (!entities[id]) assign(new iD.Way(connection, id, [], {}, false));
return entities[id];
} else if (type === 'relation') {
if (!relations[id]) assign(new iD.Relation(connection, id, [], {}, false));
return relations[id];
@@ -66,23 +65,15 @@ iD.Connection = function(apiURL) {
}
function getObjectsByBbox(left,right,top,bottom) {
// summary: Find all drawable entities that are within a given bounding box.
// returns: Object An object with four properties: .poisInside, .poisOutside, .waysInside, .waysOutside.
// summary: Find all drawable entities that are within a given bounding box.
// Each one is an array of entities.
var o = {
poisInside: [],
poisOutside: [],
waysInside: [],
waysOutside: []
inside: [],
outside: []
};
for (var id in ways) {
var way = ways[id];
if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
else { o.waysOutside.push(way); }
}
_.each(pois, function(node) {
if (node.within(left,right,top,bottom)) { o.poisInside.push(node); }
else { o.poisOutside.push(node); }
_.each(this.entities, function(e, id) {
if (e.within(left, right, top, bottom)) { o.inside.push(e); }
else { o.outside.push(e); }
});
return o;
}
@@ -183,7 +174,7 @@ iD.Connection = function(apiURL) {
return _(obj.childNodes).chain()
.filter(filterNodeName('nd'))
.map(function(item) {
return nodes[getAttribute(item,'ref')];
return entities[getAttribute(item,'ref')];
}).value();
}
@@ -202,8 +193,7 @@ iD.Connection = function(apiURL) {
};
}
connection.nodes = nodes;
connection.ways = ways;
connection.entities = entities;
connection.relations = relations;
connection.loadFromAPI = loadFromAPI;
connection.loadFromURL = loadFromURL;
+1 -1
View File
@@ -48,7 +48,7 @@ iD.Way.prototype = {
// ---------------------
// Bounding-box handling
within:function(left,right,top,bottom) {
within: function(left,right,top,bottom) {
// TODO invert and just return
if (!this.extent.west ||
(this.extent.west < left && this.extent.east < left ) ||
+5 -4
View File
@@ -47,6 +47,7 @@ declare("iD.controller.shape.NoSelection", null, {
var entity = entityUI ? entityUI.entity : null;
var entityType = entity ? entity.entityType : null;
var map = this.controller.map;
var connection = map.connection;
if (event.type === 'click') {
if (entityType === 'node') {
@@ -65,18 +66,18 @@ declare("iD.controller.shape.NoSelection", null, {
if (this.intent === 'way') {
// Click to start a new way
var undo = new iD.actions.CompositeUndoableAction();
var startNode = this.getConnection().doCreateNode({},
var startNode = connection.doCreateNode({},
map.coord2lat(map.mouseY(event)),
map.coord2lon(map.mouseX(event)), _.bind(undo.push, undo) );
var way = this.getConnection().doCreateWay({}, [startNode], _.bind(undo.push, undo) );
var way = connection.doCreateWay({}, [startNode], _.bind(undo.push, undo) );
this.controller.undoStack.addAction(undo);
this.controller.map.createUI(way);
return new iD.controller.shape.DrawWay(way);
} else if (this.intent === 'node') {
var action = new iD.actions.CreatePOIAction(this.getConnection(), {},
var action = new iD.actions.CreatePOIAction(connection, {},
map.coord2lat(map.mouseY(event)),
map.coord2lon(map.mouseX(event)));
if (action.doAction()) {
if (action.run()) {
this.controller.undoStack.add(action);
var node = action.getNode();
this.controller.map.createUI(node);
+27 -74
View File
@@ -27,8 +27,7 @@ declare("iD.renderer.Map", null, {
backdrop: null, // coloured backdrop (MapCSS canvas element)
connection: null, // data store
controller: null, // UI controller
nodeuis: {}, // graphic representations of data
wayuis: {}, // |
uis: {},
tilegroup: null, // group within container for adding bitmap tiles
tiles: {}, // index of tile objects
@@ -68,8 +67,7 @@ declare("iD.renderer.Map", null, {
this.mapheight = obj.height ? obj.height : 400;
// Initialise variables
this.nodeuis = {},
this.wayuis = {},
this.uis = {};
this.div=document.getElementById(obj.div);
this.surface=Gfx.createSurface(obj.div, this.mapwidth, this.mapheight);
this.backdrop=this.surface.createRect({
@@ -171,57 +169,40 @@ declare("iD.renderer.Map", null, {
return sub;
}
}
sub=collection.createGroup().moveToFront();
sub = collection.createGroup().moveToFront();
sub.sublayer=sublayer;
return sub; // dojox.gfx.Group
},
createUI: function(entity, stateClasses) {
createUI: function(e, stateClasses) {
// summary: Create a UI (sprite) for an entity, assigning any specified state classes
// (temporary attributes such as ':hover' or ':selected')
var id = entity.id;
if (entity.entityType === 'node') {
if (!this.nodeuis[id]) {
this.nodeuis[id] = new iD.renderer.NodeUI(entity,this,stateClasses);
} else {
this.nodeuis[id].setStateClasses(stateClasses).redraw();
if (!this.uis[e.id]) {
if (e.entityType === 'node') {
this.uis[e.id] = new iD.renderer.NodeUI(e, this, stateClasses);
} else if (e.entityType === 'way') {
this.uis[e.id] = new iD.renderer.WayUI(e, this, stateClasses);
}
return this.nodeuis[id]; // iD.renderer.EntityUI
} else if (entity.entityType === 'way') {
if (!this.wayuis[id]) {
this.wayuis[id] = new iD.renderer.WayUI(entity,this,stateClasses);
} else {
this.wayuis[id].setStateClasses(stateClasses).redraw();
}
return this.wayuis[id]; // iD.renderer.EntityUI
} else {
this.uis[e.id].setStateClasses(stateClasses).redraw();
}
},
getUI: function(entity) {
getUI: function(e) {
// summary: Return the UI for an entity, if it exists.
if (entity.entityType === 'node') {
return this.nodeuis[entity.id]; // iD.renderer.EntityUI
} else if (entity.entityType === 'way') {
return this.wayuis[entity.id]; // iD.renderer.EntityUI
}
return null;
return this.uis[e.id]; // iD.renderer.EntityUI
},
refreshUI: function(entity) {
refreshUI: function(e) {
// summary: Redraw the UI for an entity.
if (entity.entityType === 'node') {
if (this.nodeuis[entity.id]) { this.nodeuis[entity.id].redraw(); }
} else if (entity.entityType === 'way') {
if (this.wayuis[entity.id] ) { this.wayuis[entity.id].redraw(); }
}
if (this.uis[e.id]) { this.uis[e.id].redraw(); }
},
deleteUI: function(entity) {
deleteUI: function(e) {
// summary: Delete the UI for an entity.
var uis = { node: 'nodeuis', way: 'wayuis' }[entity.entityType];
if (uis && this[uis][entity.id]) {
this[uis][entity.id].removeSprites();
delete this[uis][entity.id];
if (this.uis[e.id]) {
this.uis[e.id].removeSprites();
delete this.uis[e.id];
}
},
@@ -231,49 +212,21 @@ declare("iD.renderer.Map", null, {
this.connection.loadFromAPI(this.extent, _.bind(this.updateUIs, this));
},
updateUIs: function(redraw, remove) {
updateUIs: function() {
// summary: Draw/refresh all EntityUIs within the bbox, and remove any others.
// redraw: Boolean Should we redraw any UIs that are already present?
// remove: Boolean Should we delete any UIs that are no longer in the bbox?
$('#progress').hide().removeClass('spinner');
var m = this;
var way, poi;
var o = this.connection.getObjectsByBbox(this.extent);
_(o.waysInside).chain()
_(o.inside).chain()
.filter(function(w) { return w.loaded; })
.each(function(way) {
if (!m.wayuis[way.id]) { m.createUI(way); }
else if (redraw) { m.wayuis[way.id].recalculate(); m.wayuis[way.id].redraw(); }
});
if (remove !== false) {
_.each(o.waysOutside, function(way) {
if (m.wayuis[way.id]) { // && !m.wayuis[way.id].purgable
if (redraw) {
m.wayuis[way.id].recalculate();
m.wayuis[way.id].redraw();
}
} else {
m.deleteUI(way);
.each(_.bind(function(e) {
if (!this.uis[e.id]) {
this.createUI(e);
} else {
this.uis[e.id].redraw();
}
});
}
_.each(o.poisInside, function(poi) {
if (!poi.loaded) return;
if (!m.nodeuis[poi.id]) { m.createUI(poi); }
else if (redraw) { m.nodeuis[poi.id].redraw(); }
});
if (remove !== false) {
_.each(o.poisOutside, function(poi) {
if (m.nodeuis[poi.id]) { // && !m.nodeuis[poi.id].purgable
if (redraw) { m.nodeuis[poi.id].redraw(); }
} else { m.deleteUI(poi); }
});
}
}, this));
},
// -------------
+2 -2
View File
@@ -1,8 +1,8 @@
// iD/renderer/NodeUI.js
// NodeUI classes for iD
define(['dojo/_base/declare','dojo/_base/array','dojox/gfx/_base','iD/renderer/EntityUI'],
function(declare,array,g){
define(['dojo/_base/declare','dojox/gfx/_base','iD/renderer/EntityUI'],
function(declare, g) {
// ----------------------------------------------------------------------
// NodeUI class
+2 -2
View File
@@ -18,7 +18,7 @@ declare("iD.renderer.WayUI", [iD.renderer.EntityUI], {
this.redraw();
},
getEnhancedTags: function() {
var tags=this.inherited(arguments);
var tags = this.inherited(arguments);
if (this.entity.isClosed()) { tags[':area']='yes'; }
return tags;
},
@@ -29,7 +29,7 @@ declare("iD.renderer.WayUI", [iD.renderer.EntityUI], {
redraw: function() {
// summary: Draw the object and add hitzone sprites.
var way = this.entity,
maxwidth=4,
maxwidth = 4,
i;
this.removeSprites();