Skip to content

Commit

Permalink
Add snapping to target node/way in addTargets click handler
Browse files Browse the repository at this point in the history
(still hacky)
  • Loading branch information
bhousel committed Jun 14, 2015
1 parent 4a5e245 commit df4381a
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions js/id/behavior/draw_way.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,45 @@ iD.behavior.DrawWay = function(context, wayId, index, mode, baseGraph) {
if (t.entity.id === way.nodes[0] || t.entity.id === way.nodes[1]) return;
}

var newNode1 = iD.Node({loc: targets[0].loc}),
var entity, newNode1, newNode2, choice, edge;

// targets[0]
entity = targets[0].entity;
if (entity) {
entity = context.entity(entity.id);
if (entity.type === 'node') {
newNode1 = entity;
} else if (entity.type === 'way') {
choice = iD.geo.chooseEdge(context.childNodes(entity), targets[0].point, context.projection);
edge = [entity.nodes[choice.index - 1], entity.nodes[choice.index]];
newNode1 = iD.Node({loc: choice.loc});
context.replace(iD.actions.AddMidpoint({ loc: choice.loc, edge: edge}, newNode1));
}
} else {
newNode1 = iD.Node({loc: targets[0].loc});
context.replace(iD.actions.AddEntity(newNode1));
}


// targets[1]
entity = targets[1].entity;
if (entity) {
entity = context.entity(entity.id);
if (entity.type === 'node') {
newNode2 = entity;
} else if (entity.type === 'way') {
choice = iD.geo.chooseEdge(context.childNodes(entity), targets[1].point, context.projection);
edge = [entity.nodes[choice.index - 1], entity.nodes[choice.index]];
newNode2 = iD.Node({loc: choice.loc});
context.replace(iD.actions.AddMidpoint({ loc: choice.loc, edge: edge}, newNode2));
}
} else {
newNode2 = iD.Node({loc: targets[1].loc});
context.replace(iD.actions.AddEntity(newNode2));
}

context.replace(
iD.actions.AddEntity(newNode1),
iD.actions.AddEntity(newNode2),

context.perform(
ReplaceTemporaryNode(newNode1, newNode2),
iD.actions.ChangeTags(wayId, {building:'yes'}), // just for show, remove later..
annotation);
Expand Down

0 comments on commit df4381a

Please sign in to comment.