don't show tag editor when multiple items are selected
[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                         tagViewer.setEntity(state.selection);
62                         toolbox.updateSelectionUI();
63                 }
64         
65         private function keyDownHandler(event:KeyboardEvent):void {
66                         keys[event.keyCode]=true;
67                 }
68
69         private function keyUpHandler(event:KeyboardEvent):void {
70             trace("key code "+event.keyCode);
71                         if (keys[event.keyCode]) { delete keys[event.keyCode]; }
72             var newState:ControllerState = state.processKeyboardEvent(event);
73             setState(newState);            
74                 }
75
76                 public function keyDown(key:Number):Boolean {
77                         return Boolean(keys[key]);
78                 }
79
80         private function mapMouseEvent(event:MouseEvent):void {
81             if (event.type!=MouseEvent.ROLL_OVER) map.stage.focus = map.parent;
82             if (event.type==MouseEvent.MOUSE_UP && map.dragstate==map.DRAGGING) { return; }
83             
84             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
85             event.localX = mapLoc.x;
86             event.localY = mapLoc.y;
87
88             var newState:ControllerState = state.processMouseEvent(event, null);
89             setState(newState);
90         }
91         
92         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
93             if (event.type!=MouseEvent.ROLL_OVER) map.stage.focus = map.parent;
94             //if ( event.type == MouseEvent.MOUSE_DOWN )
95             event.stopPropagation();
96                 
97             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
98             event.localX = mapLoc.x;
99             event.localY = mapLoc.y;
100
101             var newState:ControllerState = state.processMouseEvent(event, entity);
102             setState(newState);
103         }
104         
105         public function setState(newState:ControllerState):void {
106             if ( newState == state )
107                 return;
108                 
109             if ( state != null )
110                 state.exitState(newState);
111             newState.setController(this);
112             newState.setPreviousState(state);
113             state = newState;
114             state.enterState();
115         }
116
117                 public function setCursor(cursor:Class):void {
118                         CursorManager.removeAllCursors();
119                         if (cursor && cursorsEnabled) { CursorManager.setCursor(cursor,2,-4,0); }
120                 }
121
122     }
123
124     
125 }
126