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