start of turn restriction editor
[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.WayUI;
12         import net.systemeD.halcyon.Globals;
13
14     public class SelectedWay extends ControllerState {
15         protected var initWay:Way;
16         
17         public function SelectedWay(way:Way) {
18             initWay = way;
19         }
20  
21         protected function selectWay(way:Way):void {
22             if ( way == selectedWay )
23                 return;
24
25             clearSelection(this);
26             controller.setSelectedEntity(way);
27             controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
28             selectedWay = way;
29             initWay = way;
30         }
31
32         protected function clearSelection(newState:ControllerState):void {
33             if ( selectedWay != null ) {
34                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
35                 if (!newState.isSelectionState()) { controller.setSelectedEntity(null); }
36                 selectedWay = null;
37             }
38         }
39         
40         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
41                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
42             var focus:Entity = getTopLevelFocusEntity(entity);
43
44             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
45                                 // start new way
46                                 var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
47                                 return new DrawWay(way, true, false);
48                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
49                                 // insert node within way (shift-click)
50                 var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
51                                 d.forceDragStart();
52                                 return d;
53                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && event.shiftKey ) {
54                                 // merge way
55                                 return mergeWith(entity as Way);
56                         }
57                         var cs:ControllerState = sharedMouseEvents(event, entity);
58                         return cs ? cs : this;
59         }
60         
61                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
62                         switch (event.keyCode) {
63                                 case 80:                                        return new SelectedParallelWay(selectedWay);
64                                 case 81:                                        Quadrilateralise.quadrilateralise(selectedWay); return this;
65                                 case 82:                                        repeatTags(selectedWay); return this;
66                 case 86:                    selectedWay.reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
67                 case 89:                    Simplify.simplify(selectedWay, controller.map, true); return this;         
68                                 case Keyboard.BACKSPACE:        if (event.shiftKey) { return deleteWay(); } break;
69                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
70                         }
71                         var cs:ControllerState = sharedKeyboardEvents(event);
72                         return cs ? cs : this;
73                 }
74
75         protected function addNode(event:MouseEvent):int {
76                         // find which other ways are under the mouse
77                         var ways:Array=[]; var w:Way;
78                         for each (var wayui:WayUI in controller.map.paint.wayuis) {
79                                 w=wayui.hitTest(event.stageX, event.stageY);
80                                 if (w && w!=selectedWay) { ways.push(w); }
81                         }
82
83             var lat:Number = controller.map.coord2lat(event.localY);
84             var lon:Number = controller.map.coord2lon(event.localX);
85             var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
86             var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
87             var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
88                         for each (w in ways) { w.insertNodeAtClosestPosition(node, true, undo.push); }
89             MainUndoStack.getGlobalStack().addAction(undo);
90                         return index;
91         }
92
93                 protected function mergeWith(otherWay:Way):ControllerState {
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 this; }
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 new SelectedWay(way1);
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(newState:ControllerState):void {
125                         controller.clipboards['way']=selectedWay.getTagsCopy();
126             clearSelection(newState);
127                         Globals.vars.root.addDebug("**** <- "+this);
128         }
129
130         override public function toString():String {
131             return "SelectedWay";
132         }
133
134     }
135 }