keyboard shortcut for Undo, plus gratuitous use of ternary operator
[potlatch2.git] / net / systemeD / potlatch2 / controller / ControllerState.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.display.*;
4     import net.systemeD.halcyon.Map;
5     import net.systemeD.halcyon.MapPaint;
6     import net.systemeD.halcyon.connection.*;
7     import net.systemeD.potlatch2.EditController;
8         import net.systemeD.halcyon.Globals;
9
10     public class ControllerState {
11
12         protected var controller:EditController;
13         protected var previousState:ControllerState;
14
15         protected var selectedNode:Node;
16         public var selectedWay:Way;
17
18         public function ControllerState() {}
19  
20         public function setController(controller:EditController):void {
21             this.controller = controller;
22         }
23
24         public function setPreviousState(previousState:ControllerState):void {
25             if ( this.previousState == null )
26                 this.previousState = previousState;
27         }
28    
29         public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
30             return this;
31         }
32         
33         public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
34             return this;
35         }
36
37                 public function get map():Map {
38                         return controller.map;
39                 }
40
41         public function enterState():void {}
42         public function exitState():void {}
43
44                 public function toString():String {
45                         return "(No state)";
46                 }
47                 
48                 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
49                         switch (event.keyCode) {
50                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;             // Z
51                         }
52                         return null;
53                 }
54                 
55                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
56                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
57             var focus:Entity = getTopLevelFocusEntity(entity);
58
59                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
60                                 if ( entity is Way && event.altKey && paint.isBackground ) {
61                                         // pull way out of vector background layer
62                                         return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
63                                 } else if ( entity is Way ) {
64                                         // click way
65                                         return new DragWay(focus as Way, event);
66                                 } else if ( focus is Node ) {
67                                         // select POI node
68                                         return new DragPOINode(entity as Node,event,false);
69                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
70                                         // select node within this way
71                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
72                                 } else if ( entity is Node && focus is Way ) {
73                                         // select way node
74                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
75                                 }
76                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
77                                 return new NoSelection();
78                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
79                                 controller.map.setHighlight(focus, { hover: true });
80                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
81                                 controller.map.setHighlight(focus, { hover: false });
82                         }
83                         return null;
84                 }
85
86                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
87                         if ( entity is Node ) {
88                                 for each (var parent:Entity in entity.parentWays) {
89                                         return parent;
90                                 }
91                                 return entity;
92                         } else if ( entity is Way ) {
93                                 return entity;
94                         } else {
95                                 return null;
96                         }
97                 }
98
99                 protected function getMapPaint(d:DisplayObject):MapPaint {
100                         while (d) {
101                                 if (d is MapPaint) { return MapPaint(d); }
102                                 d=d.parent;
103                         }
104                         return null;
105                 }
106                 
107                 protected function getNodeIndex(way:Way,node:Node):uint {
108                         for (var i:uint=0; i<way.length; i++) {
109                                 if (way.getNode(i)==node) { return i; }
110                         }
111                         return null;
112                 }
113                 
114
115     }
116 }