ae94708938c4c38079dac283a8e3c11bc3f2959c
[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();
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():void {
33             if ( selectedWay != null ) {
34                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
35                 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 && event.ctrlKey ) {
49                                 // merge way
50                                 return mergeWith(entity as Way);
51                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
52                                 // insert node within way (shift-click)
53                 var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
54                                 d.forceDragStart();
55                                 return d;
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:                    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                         // find which other ways are under the mouse
76                         var ways:Array=[]; var w:Way;
77                         for each (var wayui:WayUI in controller.map.paint.wayuis) {
78                                 w=wayui.hitTest(event.stageX, event.stageY);
79                                 if (w && w!=selectedWay) { ways.push(w); }
80                         }
81
82             var lat:Number = controller.map.coord2lat(event.localY);
83             var lon:Number = controller.map.coord2lon(event.localX);
84             var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
85             var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
86             var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
87                         for each (w in ways) { w.insertNodeAtClosestPosition(node, true, undo.push); }
88             MainUndoStack.getGlobalStack().addAction(undo);
89                         return index;
90         }
91
92                 protected function mergeWith(otherWay:Way):ControllerState {
93                         var way1:Way;
94                         var way2:Way;
95                         if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
96                             way1 = selectedWay;
97                             way2 = otherWay;
98                         } else {
99                             way1 = otherWay;
100                             way2 = selectedWay;
101                         }
102                         
103                         var undo:Function = MainUndoStack.getGlobalStack().addAction;
104                         
105                         // find common point
106                         if (way1 == way2) { return this; }
107                         if      (way1.getNode(0)   ==way2.getNode(0)   ) { way1.mergeWith(way2,0,0,undo); }
108                         else if (way1.getNode(0)   ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
109                         else if (way1.getLastNode()==way2.getNode(0)   ) { way1.mergeWith(way2,way1.length-1,0,undo); }
110                         else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
111                         return new SelectedWay(way1);
112                 }
113         
114                 public function deleteWay():ControllerState {
115                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
116                         return new NoSelection();
117                 }
118
119         override public function enterState():void {
120             selectWay(initWay);
121                         Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
122         }
123         override public function exitState():void {
124             clearSelection();
125                         Globals.vars.root.addDebug("**** <- "+this);
126         }
127
128         override public function toString():String {
129             return "SelectedWay";
130         }
131
132     }
133 }