DragWayAction - undoing dragging a way now moves all the nodes together, instead...
[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 sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
49                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
50             var focus:Entity = getTopLevelFocusEntity(entity);
51
52                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
53                                 if ( entity is Way && event.altKey && paint.isBackground ) {
54                                         // pull way out of vector background layer
55                                         return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
56                                 } else if ( entity is Way ) {
57                                         // click way
58                                         return new DragWay(focus as Way, event);
59                                 } else if ( focus is Node ) {
60                                         // select POI node
61                                         return new DragPOINode(entity as Node,event,false);
62 //                              } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
63 //                                      // select node within this way
64 //                  return new DragWayNode(selectedWay, Node(entity), event, false);
65                                 } else if ( entity is Node && focus is Way ) {
66                                         // select way node
67                                         return new DragWayNode(focus as Way,entity as Node,event,false);
68                                 }
69                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING ) {
70                                 return new NoSelection();
71                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
72                                 controller.map.setHighlight(focus, { hover: true });
73                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
74                                 controller.map.setHighlight(focus, { hover: false });
75                         }
76                         return null;
77                 }
78
79                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
80                         if ( entity is Node ) {
81                                 for each (var parent:Entity in entity.parentWays) {
82                                         return parent;
83                                 }
84                                 return entity;
85                         } else if ( entity is Way ) {
86                                 return entity;
87                         } else {
88                                 return null;
89                         }
90                 }
91
92                 protected function getMapPaint(d:DisplayObject):MapPaint {
93                         while (d) {
94                                 if (d is MapPaint) { return MapPaint(d); }
95                                 d=d.parent;
96                         }
97                         return null;
98                 }
99                 
100
101     }
102 }