add undo for creating stuff
[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                 private var isNew:Boolean = false;
12
13         private var downX:Number;
14         private var downY:Number;
15                 private var dragstate:uint=NOT_MOVED;
16                 private const NOT_DRAGGING:uint=0;
17                 private const NOT_MOVED:uint=1;
18                 private const DRAGGING:uint=2;
19         
20         public function DragWayNode(way:Way, node:Node, event:MouseEvent, newNode:Boolean) {
21             selectedWay = way;
22             draggingNode = node;
23             downX = event.localX;
24             downY = event.localY;
25                         isNew = newNode;
26         }
27  
28        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
29
30             if (event.type==MouseEvent.MOUSE_UP) {
31                                 if (dragstate==DRAGGING) {
32                                         // mouse-up while dragging, so end drag
33                         return new SelectedWayNode(selectedWay,draggingNode);
34 //                      return endDrag();
35                                 } else if (event.shiftKey && !isNew) {
36                                         // start new way
37                                         var way:Way = controller.connection.createWay({}, [entity],
38                                             MainUndoStack.getGlobalStack().addAction);
39                                         return new DrawWay(way, true, false);
40                                 } else if (event.shiftKey && isNew) {
41                         return new SelectedWayNode(selectedWay,draggingNode);
42                                 } else {
43                                         // select node
44                                         dragstate=NOT_DRAGGING;
45                         return SelectedWayNode.selectOrEdit(selectedWay, draggingNode);
46                                 }
47
48                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
49                                 // dragging
50                                 if (dragstate==NOT_DRAGGING) {
51                                         return this;
52                                 } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
53                                         return this;
54                                 }
55                                 dragstate=DRAGGING;
56                 return dragTo(event);
57
58                         } else {
59                                 // event not handled
60                 return this;
61                         }
62         }
63
64         private function endDrag():ControllerState {
65             return previousState;
66         }
67         
68         private function dragTo(event:MouseEvent):ControllerState {
69             draggingNode.lat = controller.map.coord2lat(event.localY);
70             draggingNode.lon = controller.map.coord2lon(event.localX);
71             return this;
72         }
73         
74                 public function forceDragStart():void {
75                         dragstate=NOT_MOVED;
76                 }
77
78         override public function enterState():void {
79             controller.map.setHighlight(selectedWay, { showNodes: true } );
80                         Globals.vars.root.addDebug("**** -> "+this);
81         }
82         override public function exitState():void {
83             controller.map.setHighlight(selectedWay, { showNodes: false } );
84                         Globals.vars.root.addDebug("**** <- "+this);
85         }
86         override public function toString():String {
87             return "DragWayNode";
88         }
89     }
90 }