Fixed Stuff
[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                                         // alt-click to 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                                 } else if ( event.type == MouseEvent.MOUSE_UP ) { 
72                                         return (this is NoSelection) ? null : new NoSelection();
73                                 } else { return null; }
74                         }
75
76                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
77                                 if ( entity is Way ) {
78                                         // click way
79                                         return new DragWay(focus as Way, event);
80                                 } else if ( focus is Node ) {
81                                         // select POI node
82                                         return new DragPOINode(entity as Node,event,false);
83                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
84                                         // select node within this way
85                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
86                                 } else if ( entity is Node && focus is Way ) {
87                                         // select way node
88                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
89                                 } else if ( controller.keyDown(32) ) {
90                                         // drag map
91                                         return new DragBackground(event);
92                                 }
93                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
94                                 return (this is NoSelection) ? null : new NoSelection();
95                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
96                                 controller.map.setHighlight(focus, { hover: true });
97                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
98                                 controller.map.setHighlight(focus, { hover: false });
99                         }
100                         return null;
101                 }
102
103                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
104                         if ( entity is Node ) {
105                                 for each (var parent:Entity in entity.parentWays) {
106                                         return parent;
107                                 }
108                                 return entity;
109                         } else if ( entity is Way ) {
110                                 return entity;
111                         } else {
112                                 return null;
113                         }
114                 }
115
116                 protected function getMapPaint(d:DisplayObject):MapPaint {
117                         while (d) {
118                                 if (d is MapPaint) { return MapPaint(d); }
119                                 d=d.parent;
120                         }
121                         return null;
122                 }
123                 
124                 protected function getNodeIndex(way:Way,node:Node):uint {
125                         for (var i:uint=0; i<way.length; i++) {
126                                 if (way.getNode(i)==node) { return i; }
127                         }
128                         return null;
129                 }
130                 
131                 protected function repeatTags(object:Entity):void {
132                         if (!controller.clipboards[object.getType()]) { return; }
133                         object.suspend();
134
135                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
136                         for (var k:String in controller.clipboards[object.getType()]) {
137                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
138                         }
139                         MainUndoStack.getGlobalStack().addAction(undo);
140
141                         object.resume();
142                 }
143     }
144 }