refactor EntityUI, better stylesheet selection, etc.
[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             
82             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
83             event.localX = mapLoc.x;
84             event.localY = mapLoc.y;
85
86             var newState:ControllerState = state.processMouseEvent(event, null);
87             setState(newState);
88         }
89         
90         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
91             map.stage.focus = map.parent;
92             //if ( event.type == MouseEvent.MOUSE_DOWN )
93             event.stopPropagation();
94                 
95             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
96             event.localX = mapLoc.x;
97             event.localY = mapLoc.y;
98
99             var newState:ControllerState = state.processMouseEvent(event, entity);
100             setState(newState);
101         }
102         
103         public function setState(newState:ControllerState):void {
104             if ( newState == state )
105                 return;
106                 
107             if ( state != null )
108                 state.exitState(newState);
109             newState.setController(this);
110             newState.setPreviousState(state);
111             state = newState;
112             state.enterState();
113         }
114
115                 public function setCursor(cursor:Class):void {
116                         CursorManager.removeAllCursors();
117                         if (cursor && cursorsEnabled) { CursorManager.setCursor(cursor,2,-4,0); }
118                 }
119
120     }
121
122     
123 }
124