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