Skip to content

Commit

Permalink
Fix area drawing for #3676
Browse files Browse the repository at this point in the history
These changes are needed now that `addNode`
* wants to preserve circularity
* automatically remove duplicates
* range checks index argument (can't call it with -1 anymore)
  • Loading branch information
bhousel committed Jan 12, 2017
1 parent f109efd commit 45b7a40
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
74 changes: 45 additions & 29 deletions modules/behavior/draw_way.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ export function behaviorDrawWay(context, wayId, index, mode, baseGraph) {
annotation = t((way.isDegenerate() ?
'operations.start.annotation.' :
'operations.continue.annotation.') + context.geometry(wayId)),
draw = behaviorDraw(context);
draw = behaviorDraw(context),
end = osmNode({ loc: context.map().mouseCoordinates() }),
startIndex, start, segment;

var startIndex = typeof index === 'undefined' ? way.nodes.length - 1 : 0,
start = osmNode({loc: context.graph().entity(way.nodes[startIndex]).loc}),
end = osmNode({loc: context.map().mouseCoordinates()}),
if (!isArea) {
startIndex = typeof index === 'undefined' ? way.nodes.length - 1 : 0;
start = osmNode({ loc: context.entity(way.nodes[startIndex]).loc });
segment = osmWay({
nodes: typeof index === 'undefined' ? [start.id, end.id] : [end.id, start.id],
tags: _.clone(way.tags)
});
}


var fn = context[way.isDegenerate() ? 'replace' : 'perform'];
if (isArea) {
fn(actionAddEntity(end),
actionAddVertex(wayId, end.id, index)
actionAddVertex(wayId, end.id)
);
} else {
fn(actionAddEntity(start),
Expand All @@ -70,7 +74,7 @@ export function behaviorDrawWay(context, wayId, index, mode, baseGraph) {
if (datum.type === 'node' && datum.id !== end.id) {
loc = datum.loc;

} else if (datum.type === 'way' && datum.id !== segment.id) {
} else if (datum.type === 'way') { // && (segment || datum.id !== segment.id)) {
var dims = context.map().dimensions(),
mouse = context.mouse(),
pad = 5,
Expand Down Expand Up @@ -145,9 +149,8 @@ export function behaviorDrawWay(context, wayId, index, mode, baseGraph) {
return function(graph) {
if (isArea) {
return graph
.replace(way.addNode(newNode.id, index))
.replace(way.addNode(newNode.id))
.remove(end);

} else {
return graph
.replace(graph.entity(wayId).addNode(newNode.id, index))
Expand All @@ -165,13 +168,19 @@ export function behaviorDrawWay(context, wayId, index, mode, baseGraph) {
var last = context.hasEntity(way.nodes[way.nodes.length - (isArea ? 2 : 1)]);
if (last && last.loc[0] === loc[0] && last.loc[1] === loc[1]) return;

var newNode = osmNode({loc: loc});

context.replace(
actionAddEntity(newNode),
ReplaceTemporaryNode(newNode),
annotation
);
if (isArea) {
context.replace(
actionMoveNode(end.id, loc),
annotation
);
} else {
var newNode = osmNode({loc: loc});
context.replace(
actionAddEntity(newNode),
ReplaceTemporaryNode(newNode),
annotation
);
}

finished = true;
context.enter(mode);
Expand All @@ -180,21 +189,28 @@ export function behaviorDrawWay(context, wayId, index, mode, baseGraph) {

// Connect the way to an existing way.
drawWay.addWay = function(loc, edge) {
var previousEdge = startIndex ?
[way.nodes[startIndex], way.nodes[startIndex - 1]] :
[way.nodes[0], way.nodes[1]];

// Avoid creating duplicate segments
if (!isArea && geoEdgeEqual(edge, previousEdge))
return;

var newNode = osmNode({ loc: loc });

context.perform(
actionAddMidpoint({ loc: loc, edge: edge}, newNode),
ReplaceTemporaryNode(newNode),
annotation
);
if (isArea) {
context.perform(
actionAddMidpoint({ loc: loc, edge: edge}, end),
annotation
);
} else {
var previousEdge = startIndex ?
[way.nodes[startIndex], way.nodes[startIndex - 1]] :
[way.nodes[0], way.nodes[1]];

// Avoid creating duplicate segments
if (geoEdgeEqual(edge, previousEdge))
return;

var newNode = osmNode({ loc: loc });
context.perform(
actionAddMidpoint({ loc: loc, edge: edge}, newNode),
ReplaceTemporaryNode(newNode),
annotation
);
}

finished = true;
context.enter(mode);
Expand Down
13 changes: 10 additions & 3 deletions modules/modes/add_area.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export function modeAddArea(context) {
defaultTags = { area: 'yes' };


function actionClose(wayId) {
return function (graph) {
return graph.replace(graph.entity(wayId).close());
};
}


function start(loc) {
var graph = context.graph(),
node = osmNode({ loc: loc }),
Expand All @@ -36,7 +43,7 @@ export function modeAddArea(context) {
actionAddEntity(node),
actionAddEntity(way),
actionAddVertex(way.id, node.id),
actionAddVertex(way.id, node.id)
actionClose(way.id)
);

context.enter(modeDrawArea(context, way.id, graph));
Expand All @@ -52,7 +59,7 @@ export function modeAddArea(context) {
actionAddEntity(node),
actionAddEntity(way),
actionAddVertex(way.id, node.id),
actionAddVertex(way.id, node.id),
actionClose(way.id),
actionAddMidpoint({ loc: loc, edge: edge }, node)
);

Expand All @@ -67,7 +74,7 @@ export function modeAddArea(context) {
context.perform(
actionAddEntity(way),
actionAddVertex(way.id, node.id),
actionAddVertex(way.id, node.id)
actionClose(way.id)
);

context.enter(modeDrawArea(context, way.id, graph));
Expand Down
11 changes: 6 additions & 5 deletions modules/modes/draw_area.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ export function modeDrawArea(context, wayId, baseGraph) {


mode.enter = function() {
var way = context.entity(wayId),
headId = way.nodes[way.nodes.length - 2],
tailId = way.first();
var way = context.entity(wayId);

behavior = behaviorDrawWay(context, wayId, -1, mode, baseGraph)
behavior = behaviorDrawWay(context, wayId, undefined, mode, baseGraph)
.tail(t('modes.draw_area.tail'));

var addNode = behavior.addNode;

behavior.addNode = function(node) {
if (node.id === headId || node.id === tailId) {
var length = way.nodes.length,
penultimate = length > 2 ? way.nodes[length - 2] : null;

if (node.id === way.first() || node.id === penultimate) {
behavior.finish();
} else {
addNode(node);
Expand Down

0 comments on commit 45b7a40

Please sign in to comment.