ab8321c2bf36947b2c1675cba76fa91ada5dc723
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3     import net.systemeD.potlatch2.EditController;
4     import net.systemeD.potlatch2.tools.Quadrilateralise;
5     import net.systemeD.halcyon.connection.*;
6         import net.systemeD.halcyon.Globals;
7
8     public class SelectedWay extends ControllerState {
9         protected var selectedWay:Way;
10         protected var initWay:Way;
11         
12         public function SelectedWay(way:Way) {
13             initWay = way;
14         }
15  
16         protected function selectWay(way:Way):void {
17             if ( way == selectedWay )
18                 return;
19
20             clearSelection();
21             controller.setTagViewer(way);
22             controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
23             selectedWay = way;
24             initWay = way;
25         }
26
27         protected function clearSelection():void {
28             if ( selectedWay != null ) {
29                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
30                 controller.setTagViewer(null);
31                 selectedWay = null;
32             }
33         }
34         
35         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
36                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.MOUSE_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
37             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
38
39             if ( event.type == MouseEvent.MOUSE_UP ) {
40                                 if ( entity is Node && event.shiftKey ) {
41                                         // start new way
42                     var way:Way = controller.connection.createWay({}, [entity]);
43                     return new DrawWay(way, true, false);
44                                 } else if ( entity is Way && event.ctrlKey ) {
45                                         // merge way
46                                         mergeWith(entity as Way);
47                                 } else if ( entity is Way ) {
48                                         // select way
49                     selectWay(entity as Way);
50                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
51                     return new NoSelection();
52                                 }
53             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
54                                 if ( entity is Way && focus==selectedWay && event.shiftKey) {
55                                         // insert node within way (shift-click)
56                     var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
57                                         d.forceDragStart();
58                                         return d;
59                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
60                                         // select node within this way
61                     return new DragWayNode(selectedWay, Node(entity), event, false);
62                 } else if ( focus is Node ) {
63                                         // select POI node
64                                         return new DragPOINode(entity as Node,event,false);
65                 } else if ( entity is Node && focus is Way ) {
66                                         // select way node
67                                         return new DragWayNode(focus as Way,entity as Node,event,false);
68                                 }
69             }
70
71             return this;
72         }
73         
74         override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
75           if (event.keyCode == 81) { // 'q' or 'Q'
76             var success:Boolean = Quadrilateralise.quadrilateralise(selectedWay);
77             if (!success) {
78               trace("Quadrilateralise failed.");
79             }
80           }
81
82           return this;
83         }
84
85         protected function addNode(event:MouseEvent):Node {
86             trace("add node");
87             var lat:Number = controller.map.coord2lat(event.localY);
88             var lon:Number = controller.map.coord2lon(event.localX);
89             var node:Node = controller.connection.createNode({}, lat, lon);
90             selectedWay.insertNodeAtClosestPosition(node, true);
91                         return node;
92         }
93
94                 protected function mergeWith(way:Way):Boolean {
95                         // ** needs to prefer positive to negative IDs
96                         // find common point
97                         if (way==selectedWay) { return false; }
98                         if      (selectedWay.getNode(0)   ==way.getNode(0)   ) { selectedWay.mergeWith(way,0,0); }
99                         else if (selectedWay.getNode(0)   ==way.getLastNode()) { selectedWay.mergeWith(way,0,way.length-1); }
100                         else if (selectedWay.getLastNode()==way.getNode(0)   ) { selectedWay.mergeWith(way,selectedWay.length-1,0); }
101                         else if (selectedWay.getLastNode()==way.getLastNode()) { selectedWay.mergeWith(way,selectedWay.length-1,way.length-1); }
102                         return true;
103                 }
104         
105         override public function enterState():void {
106             selectWay(initWay);
107                         Globals.vars.root.addDebug("**** -> "+this);
108         }
109         override public function exitState():void {
110             clearSelection();
111                         Globals.vars.root.addDebug("**** <- "+this);
112         }
113
114         override public function toString():String {
115             return "SelectedWay";
116         }
117
118     }
119 }