e0e4a1f363e37369a56631e81bd46f6d7c182b42
[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 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D
51                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z
52                         }
53                         return null;
54                 }
55                 
56                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
57                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
58             var focus:Entity = getTopLevelFocusEntity(entity);
59
60                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
61                                 if ( entity is Way && event.altKey && paint.isBackground ) {
62                                         // pull way out of vector background layer
63                                         return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
64                                 } else if ( entity is Way ) {
65                                         // click way
66                                         return new DragWay(focus as Way, event);
67                                 } else if ( focus is Node ) {
68                                         // select POI node
69                                         return new DragPOINode(entity as Node,event,false);
70                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
71                                         // select node within this way
72                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
73                                 } else if ( entity is Node && focus is Way ) {
74                                         // select way node
75                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
76                                 } else if ( controller.keyDown(32) ) {
77                                         // drag map
78                                         return new DragBackground(event);
79                                 }
80                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
81                                 return new NoSelection();
82                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
83                                 controller.map.setHighlight(focus, { hover: true });
84                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
85                                 controller.map.setHighlight(focus, { hover: false });
86                         }
87                         return null;
88                 }
89
90                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
91                         if ( entity is Node ) {
92                                 for each (var parent:Entity in entity.parentWays) {
93                                         return parent;
94                                 }
95                                 return entity;
96                         } else if ( entity is Way ) {
97                                 return entity;
98                         } else {
99                                 return null;
100                         }
101                 }
102
103                 protected function getMapPaint(d:DisplayObject):MapPaint {
104                         while (d) {
105                                 if (d is MapPaint) { return MapPaint(d); }
106                                 d=d.parent;
107                         }
108                         return null;
109                 }
110                 
111                 protected function getNodeIndex(way:Way,node:Node):uint {
112                         for (var i:uint=0; i<way.length; i++) {
113                                 if (way.getNode(i)==node) { return i; }
114                         }
115                         return null;
116                 }
117                 
118
119     }
120 }