Fix two map-panning bugs - when drawing a way (doesn't do sharedmouseevents) and...
[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                         }
107                         return null;
108                 }
109
110                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
111                         if ( entity is Node ) {
112                                 for each (var parent:Entity in entity.parentWays) {
113                                         return parent;
114                                 }
115                                 return entity;
116                         } else if ( entity is Way ) {
117                                 return entity;
118                         } else {
119                                 return null;
120                         }
121                 }
122
123                 protected function getMapPaint(d:DisplayObject):MapPaint {
124                         while (d) {
125                                 if (d is MapPaint) { return MapPaint(d); }
126                                 d=d.parent;
127                         }
128                         return null;
129                 }
130                 
131                 protected function getNodeIndex(way:Way,node:Node):uint {
132                         for (var i:uint=0; i<way.length; i++) {
133                                 if (way.getNode(i)==node) { return i; }
134                         }
135                         return null;
136                 }
137                 
138                 protected function repeatTags(object:Entity):void {
139                         if (!controller.clipboards[object.getType()]) { return; }
140                         object.suspend();
141
142                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
143                         for (var k:String in controller.clipboards[object.getType()]) {
144                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
145                         }
146                         MainUndoStack.getGlobalStack().addAction(undo);
147
148                         object.resume();
149                 }
150     }
151 }