e5abe68344a39795e4bac2f837b32569ce55046e
[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         protected 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.setTagViewer(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.setTagViewer(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.MOUSE_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(way:Way):Boolean {
94                         // ** needs to prefer positive to negative IDs
95                         // find common point
96                         if (way==selectedWay) { return false; }
97                         if      (selectedWay.getNode(0)   ==way.getNode(0)   ) { selectedWay.mergeWith(way,0,0); }
98                         else if (selectedWay.getNode(0)   ==way.getLastNode()) { selectedWay.mergeWith(way,0,way.length-1); }
99                         else if (selectedWay.getLastNode()==way.getNode(0)   ) { selectedWay.mergeWith(way,selectedWay.length-1,0); }
100                         else if (selectedWay.getLastNode()==way.getLastNode()) { selectedWay.mergeWith(way,selectedWay.length-1,way.length-1); }
101                         return true;
102                 }
103         
104                 public function deleteWay():ControllerState {
105                         selectedWay.remove();
106                         return new NoSelection();
107                 }
108
109         override public function enterState():void {
110             selectWay(initWay);
111                         Globals.vars.root.addDebug("**** -> "+this);
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 }