Add way extending by clicking on first/last node
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
index 3525bea0c7421c931baba796fa047ac1d0f0565a..24dbe484f68bef8c6f8c5c2b93e54fea04a14b11 100644 (file)
@@ -9,28 +9,24 @@ package net.systemeD.potlatch2.controller {
         protected var initNode:Node;
         
         public function SelectedWayNode(way:Way,node:Node) {
-                       Globals.vars.root.addDebug("- SelectedWayNode: constructor");
                        super (way);
             initNode = node;
         }
  
         protected function selectNode(way:Way,node:Node):void {
-                       Globals.vars.root.addDebug("- SelectedWayNode: selectNode");
             if ( way == selectedWay && node == selectedNode )
                 return;
 
             clearSelection();
             controller.setTagViewer(node);
-            controller.map.setHighlight(node, { selected: true });
-            controller.map.setHighlight(way, { showNodes: true });
+            controller.map.setHighlight(way, { showNodes: true, nodeSelected: node.id });
             selectedWay = way;   initWay  = way;
             selectedNode = node; initNode = node;
         }
                 
         override protected function clearSelection():void {
             if ( selectedNode != null ) {
-                controller.map.setHighlight(selectedNode, { selected: false });
-               controller.map.setHighlight(selectedWay, { selected: false, showNodes: false });
+               controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
                 controller.setTagViewer(null);
                 selectedNode = null;
                                selectedWay = null;
@@ -44,30 +40,25 @@ package net.systemeD.potlatch2.controller {
             if ( event.type == MouseEvent.MOUSE_UP ) {
                                if ( entity is Node && event.shiftKey ) {
                                        // start new way
-                                       Globals.vars.root.addDebug("- SelectedWayNode: start new way");
-                    var way:Way = controller.connection.createWay({}, [entity, entity]);
-                    return new DrawWay(way, true);
-                               } else if ( entity is Node ) {
+                    var way:Way = controller.connection.createWay({}, [entity]);
+                    return new DrawWay(way, true, false);
+                               } else if ( entity is Node && focus == selectedWay ) {
                                        // select node within way
-                                       Globals.vars.root.addDebug("- SelectedWayNode: select other node");
-                                       return new SelectedWayNode(selectedWay,Node(entity));
+                                       return selectOrEdit(selectedWay, Node(entity));
                 } else if ( entity is Way ) {
                                        // select way
-                                       Globals.vars.root.addDebug("- SelectedWayNode: select way");
-                                       return new SelectedWay(selectedWay);
+                                       return new SelectedWay(Way(entity));
                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
-                                       Globals.vars.root.addDebug("- SelectedWayNode: deselect");
                     return new NoSelection();
                                }
             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
                                if ( entity is Way && focus==selectedWay && event.shiftKey) {
                                        // insert node within way (shift-click)
-                       var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event);
+                       var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
                                        d.forceDragStart();
                                        return d;
                                } else if ( entity is Node && entity.hasParent(selectedWay) ) {
-                                       Globals.vars.root.addDebug("- SelectedWayNode: dragwaynode");
-                    return new DragWayNode(selectedWay, Node(entity), event);
+                    return new DragWayNode(selectedWay, Node(entity), event, false);
                                }
             }
 
@@ -76,11 +67,26 @@ package net.systemeD.potlatch2.controller {
 
                override public function enterState():void {
             selectNode(initWay,initNode);
+                       Globals.vars.root.addDebug("**** -> "+this);
+        }
+               override public function exitState():void {
+            clearSelection();
+                       Globals.vars.root.addDebug("**** <- "+this);
         }
 
         override public function toString():String {
             return "SelectedWayNode";
         }
 
+        public static function selectOrEdit(selectedWay:Way, entity:Node):ControllerState {
+               var isFirst:Boolean = false;
+                       var isLast:Boolean = false;
+                       isFirst = selectedWay.getNode(0) == entity;
+                       isLast = selectedWay.getNode(selectedWay.length - 1) == entity;
+                       if ( isFirst == isLast )    // both == looped, none == central node 
+                           return new SelectedWayNode(selectedWay, entity);
+                       else
+                           return new DrawWay(selectedWay, isLast, true);
+        }
     }
 }