continuing to expand and rework selection stuff. Not finished yet but getting there
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWayNode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3     import net.systemeD.potlatch2.EditController;
4     import net.systemeD.halcyon.connection.*;
5         import net.systemeD.halcyon.Globals;
6
7     public class DragWayNode extends ControllerState {
8         private var selectedWay:Way;
9         private var draggingNode:Node;
10         private var isDraggingStarted:Boolean = false;
11
12         private var downX:Number;
13         private var downY:Number;
14                 private var dragstate:uint=NOT_MOVED;
15                 private const NOT_DRAGGING:uint=0;
16                 private const NOT_MOVED:uint=1;
17                 private const DRAGGING:uint=2;
18         
19         public function DragWayNode(way:Way, node:Node, event:MouseEvent) {
20             selectedWay = way;
21             draggingNode = node;
22             downX = event.localX;
23             downY = event.localY;
24         }
25  
26        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
27
28             if (event.type==MouseEvent.MOUSE_UP) {
29                                 if (dragstate==DRAGGING) {
30                                         // mouse-up while dragging, so end drag
31                         return new SelectedWayNode(selectedWay,draggingNode);
32 //                      return endDrag();
33                                 } else if (event.shiftKey) {
34                                         // start new way
35                                         var way:Way = controller.connection.createWay({}, [entity, entity]);
36                                         return new DrawWay(way, true);
37                                 } else {
38                                         // select node
39                                         Globals.vars.root.addDebug("- select node from DragWayNode");
40                                         dragstate=NOT_DRAGGING;
41                         return new SelectedWayNode(selectedWay,draggingNode);
42                                 }
43
44                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
45                                 // dragging
46                                 if (dragstate==NOT_DRAGGING) {
47                                         return this;
48                                 } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
49                                         return this;
50                                 }
51                                 dragstate=DRAGGING;
52                 return dragTo(event);
53
54                         } else {
55                                 // event not handled
56                 return this;
57                         }
58         }
59
60         private function endDrag():ControllerState {
61             return previousState;
62         }
63         
64         private function dragTo(event:MouseEvent):ControllerState {
65             draggingNode.lat = controller.map.coord2lat(event.localY);
66             draggingNode.lon = controller.map.coord2lon(event.localX);
67             return this;
68         }
69         
70                 public function forceDragStart():void {
71                         dragstate=NOT_MOVED;
72                 }
73
74         override public function enterState():void {
75             controller.map.setHighlight(selectedWay, {showNodes: true } );
76         }
77         override public function exitState():void {
78             controller.map.setHighlight(selectedWay, {showNodes: false } );
79         }
80         override public function toString():String {
81             return "DragWayNode";
82         }
83     }
84 }