386daf8d384310fbd5b9ec667e23bd03d31be790
[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                     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 ( entity is Node ) {
47                                         // *** select node
48                                         Globals.vars.root.addDebug("- selected POI from SelectedWay");
49                     trace("select poi");
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                     return new DragWayNode(selectedWay, Node(entity), event, false);
61                                 }
62             }
63
64             return this;
65         }
66         
67         protected function addNode(event:MouseEvent):Node {
68             trace("add node");
69             var lat:Number = controller.map.coord2lat(event.localY);
70             var lon:Number = controller.map.coord2lon(event.localX);
71             var node:Node = controller.connection.createNode({}, lat, lon);
72             selectedWay.insertNodeAtClosestPosition(node, true);
73                         return node;
74         }
75         
76         override public function enterState():void {
77             selectWay(initWay);
78                         Globals.vars.root.addDebug("**** -> "+this);
79         }
80         override public function exitState():void {
81             clearSelection();
82                         Globals.vars.root.addDebug("**** <- "+this);
83         }
84
85         override public function toString():String {
86             return "SelectedWay";
87         }
88
89     }
90 }