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