From 117ad7d6b65acf4f3211f26b62a1653c269a45fa Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 22 Dec 2017 16:57:27 -0500 Subject: [PATCH] Transitionable actionMoveNode --- modules/actions/move_node.js | 21 ++++++++++++---- test/spec/actions/move_node.js | 46 +++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/modules/actions/move_node.js b/modules/actions/move_node.js index 6e2593b45..8de29a16a 100644 --- a/modules/actions/move_node.js +++ b/modules/actions/move_node.js @@ -1,7 +1,18 @@ -// https://github.com/openstreetmap/josm/blob/mirror/src/org/openstreetmap/josm/command/MoveCommand.java -// https://github.com/openstreetmap/potlatch2/blob/master/net/systemeD/halcyon/connection/actions/MoveNodeAction.as -export function actionMoveNode(nodeId, loc) { - return function(graph) { - return graph.replace(graph.entity(nodeId).move(loc)); +import { geoInterp } from '../geo'; + +export function actionMoveNode(nodeID, toLoc) { + + var action = function(graph, t) { + if (t === null || !isFinite(t)) t = 1; + t = Math.min(Math.max(+t, 0), 1); + + var node = graph.entity(nodeID); + return graph.replace( + node.move(geoInterp(node.loc, toLoc, t)) + ); }; + + action.transitionable = true; + + return action; } diff --git a/test/spec/actions/move_node.js b/test/spec/actions/move_node.js index bc28f748d..771f66b4a 100644 --- a/test/spec/actions/move_node.js +++ b/test/spec/actions/move_node.js @@ -1,8 +1,46 @@ describe('iD.actionMoveNode', function () { it('changes a node\'s location', function () { - var node = iD.Node(), - loc = [2, 3], - graph = iD.actionMoveNode(node.id, loc)(iD.Graph([node])); - expect(graph.entity(node.id).loc).to.eql(loc); + var node = iD.osmNode({id: 'a', loc: [0, 0]}); + var toLoc = [2, 3]; + var graph = iD.coreGraph([node]); + + graph = iD.actionMoveNode('a', toLoc)(graph); + expect(graph.entity('a').loc).to.eql(toLoc); + }); + + describe('transitions', function () { + it('is transitionable', function() { + expect(iD.actionMoveNode().transitionable).to.be.true; + }); + + it('move node at t = 0', function() { + var node = iD.osmNode({id: 'a', loc: [0, 0]}); + var toLoc = [2, 3]; + var graph = iD.coreGraph([node]); + + graph = iD.actionMoveNode('a', toLoc)(graph, 0); + expect(graph.entity('a').loc[0]).to.be.closeTo(0, 1e-6); + expect(graph.entity('a').loc[1]).to.be.closeTo(0, 1e-6); + }); + + it('move node at t = 0.5', function() { + var node = iD.osmNode({id: 'a', loc: [0, 0]}); + var toLoc = [2, 3]; + var graph = iD.coreGraph([node]); + + graph = iD.actionMoveNode('a', toLoc)(graph, 0.5); + expect(graph.entity('a').loc[0]).to.be.closeTo(1, 1e-6); + expect(graph.entity('a').loc[1]).to.be.closeTo(1.5, 1e-6); + }); + + it('move node at t = 1', function() { + var node = iD.osmNode({id: 'a', loc: [0, 0]}); + var toLoc = [2, 3]; + var graph = iD.coreGraph([node]); + + graph = iD.actionMoveNode('a', toLoc)(graph, 1); + expect(graph.entity('a').loc[0]).to.be.closeTo(2, 1e-6); + expect(graph.entity('a').loc[1]).to.be.closeTo(3, 1e-6); + }); }); });