Don't drop a point if the user pans in a drawing mode
[potlatch2.git] / net / systemeD / potlatch2 / EditController.as
1 package net.systemeD.potlatch2 {
2     import net.systemeD.halcyon.Map;
3     import net.systemeD.halcyon.MapController;
4     import net.systemeD.halcyon.connection.*;
5     import net.systemeD.potlatch2.controller.*;
6         import mx.managers.CursorManager;
7         import flash.events.*;
8         import flash.geom.*;
9
10     public class EditController implements MapController {
11
12         private var _map:Map;
13         private var tagViewer:TagViewer;
14                 private var toolbox:Toolbox;
15         
16         public var state:ControllerState;
17         private var _connection:Connection;
18         
19                 private var keys:Object={};
20                 public var clipboards:Object={};
21                 public var imagery:Array=[];
22                 public var stylesheets:Array=[];
23                 public var cursorsEnabled:Boolean=true;
24
25                 [Embed(source="../../../embedded/pen.png")]             public var pen:Class;
26                 [Embed(source="../../../embedded/pen_x.png")]           public var pen_x:Class;
27                 [Embed(source="../../../embedded/pen_o.png")]           public var pen_o:Class;
28                 [Embed(source="../../../embedded/pen_so.png")]          public var pen_so:Class;
29                 [Embed(source="../../../embedded/pen_plus.png")]        public var pen_plus:Class;
30                 
31         public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
32             this._map = map;
33             this.tagViewer = tagViewer;
34                         this.toolbox = toolbox;
35                         this.toolbox.init(this);
36             setState(new NoSelection());
37             
38             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
39             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
40             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
41             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
42             map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
43             map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
44         }
45
46         public function setActive():void {
47             map.setController(this);
48             _connection = map.connection;
49         }
50
51         public function get map():Map {
52             return _map;
53         }
54         
55         public function get connection():Connection {
56             return _connection;
57         }
58         
59         public function setSelectedEntity(entity:Entity):void {
60             tagViewer.setEntity(entity);
61                         toolbox.setEntity(entity);
62         }
63         
64         private function keyDownHandler(event:KeyboardEvent):void {
65                         keys[event.keyCode]=true;
66                 }
67
68         private function keyUpHandler(event:KeyboardEvent):void {
69             trace("key code "+event.keyCode);
70                         if (keys[event.keyCode]) { delete keys[event.keyCode]; }
71             var newState:ControllerState = state.processKeyboardEvent(event);
72             setState(newState);            
73                 }
74
75                 public function keyDown(key:Number):Boolean {
76                         return Boolean(keys[key]);
77                 }
78
79         private function mapMouseEvent(event:MouseEvent):void {
80             map.stage.focus = map.parent;
81             if (event.type==MouseEvent.MOUSE_UP && map.dragstate==map.DRAGGING) { return; }
82             
83             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
84             event.localX = mapLoc.x;
85             event.localY = mapLoc.y;
86
87             var newState:ControllerState = state.processMouseEvent(event, null);
88             setState(newState);
89         }
90         
91         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
92             map.stage.focus = map.parent;
93             //if ( event.type == MouseEvent.MOUSE_DOWN )
94             event.stopPropagation();
95                 
96             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
97             event.localX = mapLoc.x;
98             event.localY = mapLoc.y;
99
100             var newState:ControllerState = state.processMouseEvent(event, entity);
101             setState(newState);
102         }
103         
104         public function setState(newState:ControllerState):void {
105             if ( newState == state )
106                 return;
107                 
108             if ( state != null )
109                 state.exitState(newState);
110             newState.setController(this);
111             newState.setPreviousState(state);
112             state = newState;
113             state.enterState();
114         }
115
116                 public function setCursor(cursor:Class):void {
117                         CursorManager.removeAllCursors();
118                         if (cursor && cursorsEnabled) { CursorManager.setCursor(cursor,2,-4,0); }
119                 }
120
121     }
122
123     
124 }
125