08f57df2a4b4b3b61010bcd11c4b115328e7711d
[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         import net.systemeD.potlatch2.save.SaveManager;
10
11     public class ControllerState {
12
13         protected var controller:EditController;
14         protected var previousState:ControllerState;
15
16         protected var selectedNode:Node;
17         public var selectedWay:Way;
18
19         public function ControllerState() {}
20  
21         public function setController(controller:EditController):void {
22             this.controller = controller;
23         }
24
25         public function setPreviousState(previousState:ControllerState):void {
26             if ( this.previousState == null )
27                 this.previousState = previousState;
28         }
29    
30                 public function isSelectionState():Boolean {
31                         return true;
32                 }
33
34         public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
35             return this;
36         }
37         
38         public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
39             return this;
40         }
41
42                 public function get map():Map {
43                         return controller.map;
44                 }
45
46         public function enterState():void {}
47         public function exitState(newState:ControllerState):void {}
48
49                 public function toString():String {
50                         return "(No state)";
51                 }
52                 
53                 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
54                         switch (event.keyCode) {
55                                 case 67:        controller.connection.closeChangeset(); break;                                                  // C - close changeset
56                                 case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D - dim
57                                 case 83:        SaveManager.saveChanges(); break;                                                                               // S - save
58                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
59                                 case 87:        if (selectedWay) { return new SelectedWay(selectedWay); }; return null; // W - select way
60                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z - undo
61                                 case 187:       controller.tagViewer.addNewTag(); return null;                                                  // + - add tag
62                         }
63                         return null;
64                 }
65                 
66                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
67                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
68             var focus:Entity = getTopLevelFocusEntity(entity);
69
70                         if ( paint && paint.isBackground ) {
71                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
72                                         // alt-click to pull data out of vector background layer
73                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
74                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
75                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
76                                 } else if ( event.type == MouseEvent.MOUSE_UP ) { 
77                                         return (this is NoSelection) ? null : new NoSelection();
78                                 } else { return null; }
79                         }
80
81                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
82                                 if ( entity is Way ) {
83                                         // click way
84                                         return new DragWay(focus as Way, event);
85                                 } else if ( focus is Node ) {
86                                         // select POI node
87                                         return new DragPOINode(entity as Node,event,false);
88                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
89                                         // select node within this way
90                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
91                                 } else if ( entity is Node && focus is Way ) {
92                                         // select way node
93                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
94                                 } else if ( controller.keyDown(32) ) {
95                                         // drag map
96                                         return new DragBackground(event);
97                                 }
98                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
99                                 return (this is NoSelection) ? null : new NoSelection();
100                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
101                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
102                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
103                                 controller.map.setHighlight(focus, { hover: true });
104                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
105                                 controller.map.setHighlight(focus, { hover: false });
106             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
107                 if (event.delta > 0) {
108                   map.zoomIn();
109                 } else if (event.delta < 0) {
110                   map.zoomOut();
111                 }
112             }
113                         return null;
114                 }
115
116                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
117                         if ( entity is Node ) {
118                                 for each (var parent:Entity in entity.parentWays) {
119                                         return parent;
120                                 }
121                                 return entity;
122                         } else if ( entity is Way ) {
123                                 return entity;
124                         } else {
125                                 return null;
126                         }
127                 }
128
129                 protected function getMapPaint(d:DisplayObject):MapPaint {
130                         while (d) {
131                                 if (d is MapPaint) { return MapPaint(d); }
132                                 d=d.parent;
133                         }
134                         return null;
135                 }
136                 
137                 protected function getNodeIndex(way:Way,node:Node):uint {
138                         for (var i:uint=0; i<way.length; i++) {
139                                 if (way.getNode(i)==node) { return i; }
140                         }
141                         return null;
142                 }
143                 
144                 protected function repeatTags(object:Entity):void {
145                         if (!controller.clipboards[object.getType()]) { return; }
146                         object.suspend();
147
148                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
149                         for (var k:String in controller.clipboards[object.getType()]) {
150                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
151                         }
152                         MainUndoStack.getGlobalStack().addAction(undo);
153
154                         object.resume();
155                 }
156     }
157 }