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