cursor-key panning, and fix "infinite drag" bug
[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 - dim
55                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
56                                 case 87:        if (selectedWay) { return new SelectedWay(selectedWay); }; return null; // W - select way
57                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z - undo
58                                 case 187:       controller.tagViewer.addNewTag(); return null;                                                  // + - add tag
59                         }
60                         return null;
61                 }
62                 
63                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
64                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
65             var focus:Entity = getTopLevelFocusEntity(entity);
66
67                         if ( paint && paint.isBackground ) {
68                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
69                                         // alt-click to pull data out of vector background layer
70                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
71                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
72                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
73                                 } else if ( event.type == MouseEvent.MOUSE_UP ) { 
74                                         return (this is NoSelection) ? null : new NoSelection();
75                                 } else { return null; }
76                         }
77
78                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
79                                 if ( entity is Way ) {
80                                         // click way
81                                         return new DragWay(focus as Way, event);
82                                 } else if ( focus is Node ) {
83                                         // select POI node
84                                         return new DragPOINode(entity as Node,event,false);
85                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
86                                         // select node within this way
87                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
88                                 } else if ( entity is Node && focus is Way ) {
89                                         // select way node
90                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
91                                 } else if ( controller.keyDown(32) ) {
92                                         // drag map
93                                         return new DragBackground(event);
94                                 }
95                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
96                                 return (this is NoSelection) ? null : new NoSelection();
97                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate==map.DRAGGING) {
98                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
99                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
100                                 controller.map.setHighlight(focus, { hover: true });
101                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
102                                 controller.map.setHighlight(focus, { hover: false });
103                         }
104                         return null;
105                 }
106
107                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
108                         if ( entity is Node ) {
109                                 for each (var parent:Entity in entity.parentWays) {
110                                         return parent;
111                                 }
112                                 return entity;
113                         } else if ( entity is Way ) {
114                                 return entity;
115                         } else {
116                                 return null;
117                         }
118                 }
119
120                 protected function getMapPaint(d:DisplayObject):MapPaint {
121                         while (d) {
122                                 if (d is MapPaint) { return MapPaint(d); }
123                                 d=d.parent;
124                         }
125                         return null;
126                 }
127                 
128                 protected function getNodeIndex(way:Way,node:Node):uint {
129                         for (var i:uint=0; i<way.length; i++) {
130                                 if (way.getNode(i)==node) { return i; }
131                         }
132                         return null;
133                 }
134                 
135                 protected function repeatTags(object:Entity):void {
136                         if (!controller.clipboards[object.getType()]) { return; }
137                         object.suspend();
138
139                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
140                         for (var k:String in controller.clipboards[object.getType()]) {
141                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
142                         }
143                         MainUndoStack.getGlobalStack().addAction(undo);
144
145                         object.resume();
146                 }
147     }
148 }