obviously
[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 isSelectionState():Boolean {
30                         return true;
31                 }
32
33         public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
34             return this;
35         }
36         
37         public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
38             return this;
39         }
40
41                 public function get map():Map {
42                         return controller.map;
43                 }
44
45         public function enterState():void {}
46         public function exitState(newState:ControllerState):void {}
47
48                 public function toString():String {
49                         return "(No state)";
50                 }
51                 
52                 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
53                         switch (event.keyCode) {
54                                 case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D
55                                 case 87:        if (selectedWay) { return new SelectedWay(selectedWay); }; return null; // W
56                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z
57                         }
58                         return null;
59                 }
60                 
61                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
62                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
63             var focus:Entity = getTopLevelFocusEntity(entity);
64
65                         if ( paint && paint.isBackground ) {
66                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
67                                         // pull data out of vector background layer
68                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
69                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
70                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
71                                 }
72                                 return (this is NoSelection) ? this : new NoSelection();
73                         }
74
75                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
76                                 if ( entity is Way ) {
77                                         // click way
78                                         return new DragWay(focus as Way, event);
79                                 } else if ( focus is Node ) {
80                                         // select POI node
81                                         return new DragPOINode(entity as Node,event,false);
82                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
83                                         // select node within this way
84                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
85                                 } else if ( entity is Node && focus is Way ) {
86                                         // select way node
87                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
88                                 } else if ( controller.keyDown(32) ) {
89                                         // drag map
90                                         return new DragBackground(event);
91                                 }
92                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
93                                 return new NoSelection();
94                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
95                                 controller.map.setHighlight(focus, { hover: true });
96                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
97                                 controller.map.setHighlight(focus, { hover: false });
98                         }
99                         return null;
100                 }
101
102                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
103                         if ( entity is Node ) {
104                                 for each (var parent:Entity in entity.parentWays) {
105                                         return parent;
106                                 }
107                                 return entity;
108                         } else if ( entity is Way ) {
109                                 return entity;
110                         } else {
111                                 return null;
112                         }
113                 }
114
115                 protected function getMapPaint(d:DisplayObject):MapPaint {
116                         while (d) {
117                                 if (d is MapPaint) { return MapPaint(d); }
118                                 d=d.parent;
119                         }
120                         return null;
121                 }
122                 
123                 protected function getNodeIndex(way:Way,node:Node):uint {
124                         for (var i:uint=0; i<way.length; i++) {
125                                 if (way.getNode(i)==node) { return i; }
126                         }
127                         return null;
128                 }
129                 
130                 protected function repeatTags(object:Entity):void {
131                         if (!controller.clipboards[object.getType()]) { return; }
132                         object.suspend();
133
134                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
135                         for (var k:String in controller.clipboards[object.getType()]) {
136                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
137                         }
138                         MainUndoStack.getGlobalStack().addAction(undo);
139
140                         object.resume();
141                 }
142     }
143 }