Add lots of icons. All from SJJB except two from http://www.clker.com/.
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.geom.Point;
4     import flash.ui.Keyboard;
5     import net.systemeD.potlatch2.EditController;
6     import net.systemeD.halcyon.connection.*;
7     import net.systemeD.halcyon.connection.actions.*;
8         import net.systemeD.halcyon.Globals;
9
10     /** The state of moving a way around with the mouse. */
11     public class DragWay extends ControllerState {
12         private var isDraggingStarted:Boolean = false;
13                 private var enterTime:Number;
14
15         private var downX:Number;
16         private var downY:Number;
17                 private var dragstate:uint=NOT_MOVED;
18                 /** Not used? */
19                 private const NOT_DRAGGING:uint=0;
20                 
21                 /** "Dragging" but hasn't actually moved yet. */
22                 private const NOT_MOVED:uint=1;
23                 
24                 /** While moving. */
25                 private const DRAGGING:uint=2;
26         
27         /** Start the drag by recording the dragged way, where it started, and when. */
28         public function DragWay(way:Way, event:MouseEvent) {
29             selection = [way];
30             downX = event.localX;
31             downY = event.localY;
32                         enterTime = (new Date()).getTime();
33         }
34  
35        /** Handle dragging and end drag events. Filters out very short or quick drags. */
36        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
37             if (event.type==MouseEvent.MOUSE_UP) {
38                 if (dragstate==DRAGGING) { 
39                   MainUndoStack.getGlobalStack().addAction(
40                           new MoveWayAction(firstSelected as Way, downX, downY, event.localX, event.localY, controller.map)); 
41                 }
42                 return new SelectedWay(firstSelected as Way, new Point(event.stageX,event.stageY));
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 && 
49                                                ((Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) ||
50                                                 (new Date().getTime()-enterTime)<300)) {
51                                         return this;
52                                 }
53                                 dragstate=DRAGGING;
54                 return dragTo(event);
55
56                         } else {
57                                 // event not handled
58                 return this;
59                         }
60         }
61
62                 /** Abort dragging if ESC pressed. */
63                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
64                         if (event.keyCode==Keyboard.ESCAPE) {
65                                 firstSelected.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, firstSelected as Way, 0, 0));
66                                 return new SelectedWay(firstSelected as Way);
67                         }
68                         return this;
69                 }
70
71         private function dragTo(event:MouseEvent):ControllerState {
72                         firstSelected.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, firstSelected as Way, event.localX-downX, event.localY-downY));
73             return this;
74         }
75         
76                 public function forceDragStart():void {
77                         dragstate=NOT_MOVED;
78                 }
79
80         /** Highlight the dragged way. */
81         override public function enterState():void {
82             controller.map.setHighlight(firstSelected, { selected: true } );
83                         Globals.vars.root.addDebug("**** -> "+this);
84         }
85         
86         /** Un-highlight the dragged way. */
87         override public function exitState(newState:ControllerState):void {
88             controller.map.setHighlight(firstSelected, { selected: false } );
89                         Globals.vars.root.addDebug("**** <- "+this);
90         }
91         /** "DragWay" */
92         override public function toString():String {
93             return "DragWay";
94         }
95
96
97     }
98 }