select and move POI nodes, and a handful of smaller fixes too
[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 ) {
44                                         // select way
45                     selectWay(entity as Way);
46                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
47                     return new NoSelection();
48                                 }
49             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
50                                 if ( entity is Way && focus==selectedWay && event.shiftKey) {
51                                         // insert node within way (shift-click)
52                     var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
53                                         d.forceDragStart();
54                                         return d;
55                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
56                                         // select node within this way
57                     return new DragWayNode(selectedWay, Node(entity), event, false);
58                 } else if ( focus is Node ) {
59                                         // select POI node
60                                         return new DragPOINode(entity as Node,event,false);
61                 } else if ( entity is Node && focus is Way ) {
62                                         // select way node
63                                         return new DragWayNode(focus as Way,entity as Node,event,false);
64                                 }
65             }
66
67             return this;
68         }
69         
70         protected function addNode(event:MouseEvent):Node {
71             trace("add node");
72             var lat:Number = controller.map.coord2lat(event.localY);
73             var lon:Number = controller.map.coord2lon(event.localX);
74             var node:Node = controller.connection.createNode({}, lat, lon);
75             selectedWay.insertNodeAtClosestPosition(node, true);
76                         return node;
77         }
78         
79         override public function enterState():void {
80             selectWay(initWay);
81                         Globals.vars.root.addDebug("**** -> "+this);
82         }
83         override public function exitState():void {
84             clearSelection();
85                         Globals.vars.root.addDebug("**** <- "+this);
86         }
87
88         override public function toString():String {
89             return "SelectedWay";
90         }
91
92     }
93 }