refactoring and some bugfixes
[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         public 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.MOUSE_WHEEL, mapMouseEvent);
42             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
43             map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
44             map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
45         }
46
47         public function setActive():void {
48             map.setController(this);
49             _connection = map.connection;
50         }
51
52         public function get map():Map {
53             return _map;
54         }
55         
56         public function get connection():Connection {
57             return _connection;
58         }
59         
60         public function updateSelectionUI():void {
61                         var entity:Entity=state.firstSelected;
62             tagViewer.setEntity(entity);
63                         toolbox.setEntity(entity);
64         }
65         
66         private function keyDownHandler(event:KeyboardEvent):void {
67                         keys[event.keyCode]=true;
68                 }
69
70         private function keyUpHandler(event:KeyboardEvent):void {
71             trace("key code "+event.keyCode);
72                         if (keys[event.keyCode]) { delete keys[event.keyCode]; }
73             var newState:ControllerState = state.processKeyboardEvent(event);
74             setState(newState);            
75                 }
76
77                 public function keyDown(key:Number):Boolean {
78                         return Boolean(keys[key]);
79                 }
80
81         private function mapMouseEvent(event:MouseEvent):void {
82             if (event.type!=MouseEvent.ROLL_OVER) map.stage.focus = map.parent;
83             if (event.type==MouseEvent.MOUSE_UP && map.dragstate==map.DRAGGING) { return; }
84             
85             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
86             event.localX = mapLoc.x;
87             event.localY = mapLoc.y;
88
89             var newState:ControllerState = state.processMouseEvent(event, null);
90             setState(newState);
91         }
92         
93         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
94             if (event.type!=MouseEvent.ROLL_OVER) map.stage.focus = map.parent;
95             //if ( event.type == MouseEvent.MOUSE_DOWN )
96             event.stopPropagation();
97                 
98             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
99             event.localX = mapLoc.x;
100             event.localY = mapLoc.y;
101
102             var newState:ControllerState = state.processMouseEvent(event, entity);
103             setState(newState);
104         }
105         
106         public function setState(newState:ControllerState):void {
107             if ( newState == state )
108                 return;
109                 
110             if ( state != null )
111                 state.exitState(newState);
112             newState.setController(this);
113             newState.setPreviousState(state);
114             state = newState;
115             state.enterState();
116         }
117
118                 public function setCursor(cursor:Class):void {
119                         CursorManager.removeAllCursors();
120                         if (cursor && cursorsEnabled) { CursorManager.setCursor(cursor,2,-4,0); }
121                 }
122
123     }
124
125     
126 }
127