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