various click-handling improvements/fixes
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWayNode.as
index fc0b0a861ca29ec2c37269bd7ac67c0fd55c0fc5..13c8a43d1975bdd281d4f64fd055321869358811 100644 (file)
@@ -2,49 +2,59 @@ package net.systemeD.potlatch2.controller {
        import flash.events.*;
     import net.systemeD.potlatch2.EditController;
     import net.systemeD.halcyon.connection.*;
-       import net.systemeD.halcyon.Globals;
 
     public class DragWayNode extends ControllerState {
         private var selectedWay:Way;
         private var draggingNode:Node;
         private var isDraggingStarted:Boolean = false;
+
         private var downX:Number;
         private var downY:Number;
+               private var dragstate:uint=NOT_MOVED;
+               private const NOT_DRAGGING:uint=0;
+               private const NOT_MOVED:uint=1;
+               private const DRAGGING:uint=2;
         
-        public function DragWayNode(way:Way, node:Node, mouseDown:MouseEvent) {
+        public function DragWayNode(way:Way, node:Node, event:MouseEvent) {
             selectedWay = way;
             draggingNode = node;
-            downX = mouseDown.localX;
-            downY = mouseDown.localY;
+            downX = event.localX;
+            downY = event.localY;
         }
  
-        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
-            if ( event.type == MouseEvent.MOUSE_UP ) {
-                               Globals.vars.root.addDebug("dragwaynode - mouse-up");
-                return endDrag();
-                       }
-            
-            if ( !isDragging(event) ) {
-                               Globals.vars.root.addDebug("dragwaynode - not dragging");
-                return this;
-                       }
+       override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
+
+            if (event.type==MouseEvent.MOUSE_UP) {
+                               if (dragstate==DRAGGING) {
+                                       // mouse-up while dragging, so end drag
+                       return endDrag();
+                               } else if (event.shiftKey) {
+                                       // start new way
+                                       var way:Way = controller.connection.createWay({}, [entity, entity]);
+                                       return new DrawWay(way, true);
+                               } else {
+                                       // select node
+                                       // *** haven't done node selection code yet!
+                                       dragstate=NOT_DRAGGING;
+                       return new NoSelection();
+                               }
 
-            if ( event.type == MouseEvent.MOUSE_MOVE ) {
+                       } else if ( event.type == MouseEvent.MOUSE_MOVE) {
+                               // dragging
+                               if (dragstate==NOT_DRAGGING) {
+                                       return this;
+                               } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
+                                       return this;
+                               }
+                               dragstate=DRAGGING;
                 return dragTo(event);
+
                        } else {
+                               // event not handled
                 return this;
                        }
         }
 
-        private function isDragging(event:MouseEvent):Boolean {
-            if ( isDraggingStarted )
-                return true;
-            
-            isDraggingStarted = Math.abs(downX - event.localX) > 3 ||
-                                Math.abs(downY - event.localY) > 3;
-            return isDraggingStarted;
-        }
-        
         private function endDrag():ControllerState {
             return previousState;
         }
@@ -55,11 +65,18 @@ package net.systemeD.potlatch2.controller {
             return this;
         }
         
+               public function forceDragStart():void {
+                       dragstate=NOT_MOVED;
+               }
+
         override public function enterState():void {
             controller.map.setHighlight(selectedWay, "showNodes", true);
         }
         override public function exitState():void {
             controller.map.setHighlight(selectedWay, "showNodes", false);
         }
+        override public function toString():String {
+            return "DragWayNode";
+        }
     }
 }