first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.ui.Keyboard;
4     import net.systemeD.potlatch2.EditController;
5     import net.systemeD.potlatch2.tools.Quadrilateralise;
6     import net.systemeD.halcyon.connection.*;
7         import net.systemeD.halcyon.Globals;
8
9     public class SelectedWay extends ControllerState {
10         public var selectedWay:Way;
11         protected var initWay:Way;
12         
13         public function SelectedWay(way:Way) {
14             initWay = way;
15         }
16  
17         protected function selectWay(way:Way):void {
18             if ( way == selectedWay )
19                 return;
20
21             clearSelection();
22             controller.setSelectedEntity(way);
23             controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
24             selectedWay = way;
25             initWay = way;
26         }
27
28         protected function clearSelection():void {
29             if ( selectedWay != null ) {
30                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
31                 controller.setSelectedEntity(null);
32                 selectedWay = null;
33             }
34         }
35         
36         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
37                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
38             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
39
40             if ( event.type == MouseEvent.MOUSE_UP ) {
41                                 if ( entity is Node && event.shiftKey ) {
42                                         // start new way
43                     var way:Way = controller.connection.createWay({}, [entity]);
44                     return new DrawWay(way, true, false);
45                                 } else if ( entity is Way && event.ctrlKey ) {
46                                         // merge way
47                                         mergeWith(entity as Way);
48                                 } else if ( entity is Way ) {
49                                         // select way
50                     selectWay(entity as Way);
51                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
52                     return new NoSelection();
53                                 }
54             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
55                                 if ( entity is Way && focus==selectedWay && event.shiftKey) {
56                                         // insert node within way (shift-click)
57                     var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
58                                         d.forceDragStart();
59                                         return d;
60                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
61                                         // select node within this way
62                     return new DragWayNode(selectedWay, Node(entity), event, false);
63                 } else if ( focus is Node ) {
64                                         // select POI node
65                                         return new DragPOINode(entity as Node,event,false);
66                 } else if ( entity is Node && focus is Way ) {
67                                         // select way node
68                                         return new DragWayNode(focus as Way,entity as Node,event,false);
69                                 }
70             }
71
72             return this;
73         }
74         
75                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
76                         switch (event.keyCode) {
77                                 case 81:                                        Quadrilateralise.quadrilateralise(selectedWay); return this;
78                                 case Keyboard.BACKSPACE:        if (event.shiftKey) { return deleteWay(); } break;
79                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
80                         }
81                         return this;
82                 }
83
84         protected function addNode(event:MouseEvent):Node {
85             trace("add node");
86             var lat:Number = controller.map.coord2lat(event.localY);
87             var lon:Number = controller.map.coord2lon(event.localX);
88             var node:Node = controller.connection.createNode({}, lat, lon);
89             selectedWay.insertNodeAtClosestPosition(node, true);
90                         return node;
91         }
92
93                 protected function mergeWith(otherWay:Way):Boolean {
94                         var way1:Way;
95                         var way2:Way;
96                         if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
97                             way1 = selectedWay;
98                             way2 = otherWay;
99                         } else {
100                             way1 = otherWay;
101                             way2 = selectedWay;
102                         }
103                         
104                         var undo:Function = MainUndoStack.getGlobalStack().addAction;
105                         
106                         // find common point
107                         if (way1 == way2) { return false; }
108                         if      (way1.getNode(0)   ==way2.getNode(0)   ) { way1.mergeWith(way2,0,0,undo); }
109                         else if (way1.getNode(0)   ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
110                         else if (way1.getLastNode()==way2.getNode(0)   ) { way1.mergeWith(way2,way1.length-1,0,undo); }
111                         else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
112                         return true;
113                 }
114         
115                 public function deleteWay():ControllerState {
116                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
117                         return new NoSelection();
118                 }
119
120         override public function enterState():void {
121             selectWay(initWay);
122                         Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
123         }
124         override public function exitState():void {
125             clearSelection();
126                         Globals.vars.root.addDebug("**** <- "+this);
127         }
128
129         override public function toString():String {
130             return "SelectedWay";
131         }
132
133     }
134 }