0dfe13b09fb85bc59eaa4cd6fc43f449efa62f01
[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
6     public class SelectedWay extends ControllerState {
7         protected var selectedWay:Way;
8         protected var selectedNode:Node;
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);
22             controller.map.setHighlight(way, "showNodes", true);
23             selectedWay = way;
24             initWay = way;
25         }
26
27         protected function selectNode(node:Node):void {
28             if ( node == selectedNode )
29                 return;
30             
31             clearSelectedNode();
32             controller.setTagViewer(node);
33             controller.map.setHighlight(node, "selected", true);
34             selectedNode = node;
35         }
36                 
37         protected function clearSelectedNode():void {
38             if ( selectedNode != null ) {
39                 controller.map.setHighlight(selectedNode, "selected", false);
40                 controller.setTagViewer(selectedWay);
41                 selectedNode = null;
42             }
43         }
44         protected function clearSelection():void {
45             if ( selectedWay != null ) {
46                 controller.map.setHighlight(selectedWay, "selected", false);
47                 controller.map.setHighlight(selectedWay, "showNodes", false);
48                 controller.setTagViewer(null);
49                 selectedWay = null;
50             }
51         }
52         
53         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
54             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
55             if ( event.type == MouseEvent.CLICK ) {
56                 if ( (entity is Node && entity.hasParent(selectedWay)) || focus == selectedWay )
57                     return clickOnWay(event, entity);
58                 else if ( focus is Way )
59                     selectWay(focus as Way);
60                 else if ( focus is Node )
61                     trace("select poi");
62                 else if ( focus == null )
63                     return new NoSelection();
64             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
65                 if ( entity is Node && entity.hasParent(selectedWay) )
66                     return new DragWayNode(selectedWay, Node(entity), event);
67             }
68
69             return this;
70         }
71
72         public function clickOnWay(event:MouseEvent, entity:Entity):ControllerState {
73             if ( event.shiftKey ) {
74                 if ( entity is Way )
75                     addNode(event);
76                 else
77                     trace("start new way");
78             } else {
79                 if ( entity is Node ) {
80                     if ( selectedNode == entity ) {
81                         var i:uint = selectedWay.indexOfNode(selectedNode);
82                         if ( i == 0 )
83                             return new DrawWay(selectedWay, false);
84                         else if ( i == selectedWay.length - 1 )
85                             return new DrawWay(selectedWay, true);
86                     } else {
87                         selectNode(entity as Node);
88                     }
89                 }
90             }
91             
92             return this;
93         }
94         
95         private function addNode(event:MouseEvent):void {
96             trace("add node");
97             var lat:Number = controller.map.coord2lat(event.localY);
98             var lon:Number = controller.map.coord2lon(event.localX);
99             var node:Node = controller.connection.createNode({}, lat, lon);
100             selectedWay.insertNodeAtClosestPosition(node, true);
101         }
102         
103         override public function enterState():void {
104             selectWay(initWay);
105         }
106         override public function exitState():void {
107             clearSelection();
108         }
109     }
110 }