More draw refactoring

This commit is contained in:
John Firebaugh
2013-01-30 14:07:52 -05:00
parent 0a5cdb393a
commit dbac370c71
6 changed files with 76 additions and 113 deletions

View File

@@ -1,25 +1,16 @@
iD.behavior.AddWay = function(mode) {
var map = mode.map,
history = mode.history,
controller = mode.controller,
event = d3.dispatch('startFromNode', 'startFromWay', 'start'),
draw;
function add(datum) {
if (datum.type === 'node') {
event.startFromNode(datum);
} else if (datum.type === 'way') {
var choice = iD.geo.chooseIndex(datum, d3.mouse(map.surface.node()), map);
event.startFromWay(datum, choice.loc, choice.index);
} else if (datum.midpoint) {
var way = history.graph().entity(datum.way);
event.startFromWay(way, datum.loc, datum.index);
} else {
event.start(map.mouseCoordinates());
}
}
draw = iD.behavior.Draw(map);
var addWay = function(surface) {
draw.on('click', event.start)
.on('clickNode', event.startFromNode)
.on('clickWay', event.startFromWay)
.on('cancel', addWay.cancel)
.on('finish', addWay.cancel);
map.fastEnable(false)
.minzoom(16)
.dblclickEnable(false);
@@ -43,10 +34,5 @@ iD.behavior.AddWay = function(mode) {
controller.exit();
};
draw = iD.behavior.Draw()
.on('add', add)
.on('cancel', addWay.cancel)
.on('finish', addWay.cancel);
return d3.rebind(addWay, event, 'on');
};

View File

@@ -1,5 +1,5 @@
iD.behavior.Draw = function () {
var event = d3.dispatch('move', 'add', 'undo', 'cancel', 'finish'),
iD.behavior.Draw = function(map) {
var event = d3.dispatch('move', 'click', 'clickNode', 'clickWay', 'undo', 'cancel', 'finish'),
keybinding = d3.keybinding('draw'),
down, surface, hover;
@@ -26,7 +26,18 @@ iD.behavior.Draw = function () {
}
function click() {
event.add(datum());
var d = datum();
if (d.type === 'node') {
event.clickNode(d);
} else if (d.type === 'way') {
var choice = iD.geo.chooseIndex(d, d3.mouse(map.surface.node()), map);
event.clickWay(d, choice.loc, choice.index);
} else if (d.midpoint) {
var way = history.graph().entity(d.way);
event.clickWay(way, d.loc, d.index);
} else {
event.click(map.mouseCoordinates());
}
}
function keydown() {

View File

@@ -1,11 +1,11 @@
iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
iD.behavior.DrawWay = function(wayId, index, mode, baseGraph) {
var map = mode.map,
history = mode.history,
controller = mode.controller,
event = d3.dispatch('add', 'addHead', 'addTail', 'addNode', 'addWay'),
way = mode.history.graph().entity(wayId),
finished = false,
draw;
annotation = 'added to a way',
draw = iD.behavior.Draw(map);
var node = iD.Node({loc: map.mouseCoordinates()}),
nodeId = node.id;
@@ -26,29 +26,19 @@ iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
history.replace(iD.actions.MoveNode(nodeId, loc));
}
function add(datum) {
if (datum.id === headId) {
event.addHead(datum);
} else if (datum.id === tailId) {
event.addTail(datum);
} else if (datum.type === 'node' && datum.id !== nodeId) {
event.addNode(datum);
} else if (datum.type === 'way') {
var choice = iD.geo.chooseIndex(datum, d3.mouse(map.surface.node()), map);
event.addWay(datum, choice.loc, choice.index);
} else if (datum.midpoint) {
var way = history.graph().entity(datum.way);
event.addWay(way, datum.loc, datum.index);
} else {
event.add(map.mouseCoordinates());
}
}
function undone() {
controller.enter(iD.modes.Browse());
}
var drawWay = function(surface) {
draw.on('move', move)
.on('click', drawWay.add)
.on('clickNode', drawWay.addNode)
.on('clickWay', drawWay.addWay)
.on('undo', history.undo)
.on('cancel', drawWay.cancel)
.on('finish', drawWay.finish);
map.fastEnable(false)
.minzoom(16)
.dblclickEnable(false);
@@ -80,6 +70,12 @@ iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
history.on('undone.draw', null);
};
drawWay.annotation = function(_) {
if (!arguments.length) return annotation;
annotation = _;
return drawWay;
};
function ReplaceTemporaryNode(newNode) {
return function(graph) {
return graph
@@ -89,7 +85,7 @@ iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
}
// Connect the way to an existing node and continue drawing.
drawWay.addNode = function(node, annotation) {
drawWay.addNode = function(node) {
history.perform(
ReplaceTemporaryNode(node),
annotation);
@@ -99,7 +95,7 @@ iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
};
// Connect the way to an existing way.
drawWay.addWay = function(way, loc, wayIndex, annotation) {
drawWay.addWay = function(way, loc, wayIndex) {
var newNode = iD.Node({loc: loc});
history.perform(
@@ -113,7 +109,7 @@ iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
};
// Accept the current position of the temporary node and continue drawing.
drawWay.add = function(loc, annotation) {
drawWay.add = function(loc) {
var newNode = iD.Node({loc: loc});
history.replace(
@@ -149,12 +145,5 @@ iD.behavior.DrawWay = function(wayId, headId, tailId, index, mode, baseGraph) {
controller.enter(iD.modes.Browse());
};
draw = iD.behavior.Draw()
.on('move', move)
.on('add', add)
.on('undo', history.undo)
.on('cancel', drawWay.cancel)
.on('finish', drawWay.finish);
return d3.rebind(drawWay, event, 'on');
};

View File

@@ -16,8 +16,8 @@ iD.modes.AddPoint = function() {
map.tail('Click on the map to add a point.', true);
function add() {
var node = iD.Node({loc: map.mouseCoordinates()});
function add(loc) {
var node = iD.Node({loc: loc});
history.perform(
iD.actions.AddNode(node),
@@ -26,12 +26,22 @@ iD.modes.AddPoint = function() {
controller.enter(iD.modes.Select(node, true));
}
function addWay(way, loc, index) {
add(loc);
}
function addNode(node) {
add(node.loc);
}
function cancel() {
controller.exit();
}
behavior = iD.behavior.Draw()
.on('add', add)
behavior = iD.behavior.Draw(map)
.on('click', add)
.on('clickWay', addWay)
.on('clickNode', addNode)
.on('cancel', cancel)
.on('finish', cancel)
(surface);

View File

@@ -8,33 +8,21 @@ iD.modes.DrawArea = function(wayId, baseGraph) {
mode.enter = function() {
var way = mode.history.graph().entity(wayId),
index = -1,
headId = way.nodes[way.nodes.length - 2],
tailId = way.first(),
annotation = way.isDegenerate() ? 'started an area' : 'continued an area';
tailId = way.first();
function addHeadTail() {
behavior.finish();
}
behavior = iD.behavior.DrawWay(wayId, -1, mode, baseGraph)
.annotation(way.isDegenerate() ? 'started an area' : 'continued an area');
function addNode(node) {
behavior.addNode(node, annotation);
}
var addNode = behavior.addNode;
function addWay(way, loc, index) {
behavior.addWay(way, loc, index, annotation);
}
function add(loc) {
behavior.add(loc, annotation);
}
behavior = iD.behavior.DrawWay(wayId, headId, tailId, index, mode, baseGraph)
.on('addHead', addHeadTail)
.on('addTail', addHeadTail)
.on('addNode', addNode)
.on('addWay', addWay)
.on('add', add);
behavior.addNode = function(node) {
if (node.id === headId || node.id === tailId) {
behavior.finish();
} else {
addNode(node);
}
};
mode.map.surface.call(behavior);
mode.map.tail('Click to add points to your area. Click the first point to finish the area.', true);

View File

@@ -9,41 +9,20 @@ iD.modes.DrawLine = function(wayId, direction, baseGraph) {
mode.enter = function() {
var way = mode.history.graph().entity(wayId),
index = (direction === 'forward') ? undefined : 0,
headId = (direction === 'forward') ? way.last() : way.first(),
tailId = (direction === 'forward') ? way.first() : way.last(),
annotation = way.isDegenerate() ? 'started a line' : 'continued a line';
headId = (direction === 'forward') ? way.last() : way.first();
function addHead() {
behavior.finish();
}
behavior = iD.behavior.DrawWay(wayId, index, mode, baseGraph)
.annotation(way.isDegenerate() ? 'started a line' : 'continued a line');
function addTail(node) {
// connect the way in a loop
if (way.nodes.length > 2) {
behavior.addNode(node, annotation);
var addNode = behavior.addNode;
behavior.addNode = function(node) {
if (node.id === headId) {
behavior.finish();
} else {
behavior.cancel();
addNode(node);
}
}
function addNode(node) {
behavior.addNode(node, annotation);
}
function addWay(way, loc, index) {
behavior.addWay(way, loc, index, annotation);
}
function add(loc) {
behavior.add(loc, annotation);
}
behavior = iD.behavior.DrawWay(wayId, headId, tailId, index, mode, baseGraph)
.on('addHead', addHead)
.on('addTail', addTail)
.on('addNode', addNode)
.on('addWay', addWay)
.on('add', add);
};
mode.map.surface.call(behavior);
mode.map.tail('Click to add more points to the line. ' +