various click-handling improvements/fixes
[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 selectedNode:Node;
10         protected var initWay:Way;
11         
12         public function SelectedWay(way:Way) {
13             initWay = way;
14         }
15  
16         protected function selectWay(way:Way):void {
17             if ( way == selectedWay )
18                 return;
19
20             clearSelection();
21             controller.setTagViewer(way);
22             controller.map.setHighlight(way, "selected", true);
23             controller.map.setHighlight(way, "showNodes", true);
24             selectedWay = way;
25             initWay = way;
26         }
27
28         protected function selectNode(node:Node):void {
29             if ( node == selectedNode )
30                 return;
31             
32             clearSelectedNode();
33             controller.setTagViewer(node);
34             controller.map.setHighlight(node, "selected", true);
35             selectedNode = node;
36         }
37                 
38         protected function clearSelectedNode():void {
39             if ( selectedNode != null ) {
40                 controller.map.setHighlight(selectedNode, "selected", false);
41                 controller.setTagViewer(selectedWay);
42                 selectedNode = null;
43             }
44         }
45         protected function clearSelection():void {
46             if ( selectedWay != null ) {
47                 controller.map.setHighlight(selectedWay, "selected", false);
48                 controller.map.setHighlight(selectedWay, "showNodes", false);
49                 controller.setTagViewer(null);
50                 selectedWay = null;
51             }
52         }
53         
54         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
55                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.MOUSE_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
56             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
57
58             if ( event.type == MouseEvent.MOUSE_UP ) {
59                                 if ( entity is Node && event.shiftKey ) {
60                                         // start new way
61                                         Globals.vars.root.addDebug("- start new way");
62                     var way:Way = controller.connection.createWay({}, [entity, entity]);
63                     return new DrawWay(way, true);
64                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
65                                         // select node within way
66                                         Globals.vars.root.addDebug("- clicked on place within way");
67                     return clickOnWay(event, entity);
68                 } else if ( focus is Way ) {
69                                         // select way
70                                         Globals.vars.root.addDebug("- selected way");
71                     selectWay(focus as Way);
72                 } else if ( focus is Node ) {
73                                         // *** select node
74                                         Globals.vars.root.addDebug("- selected POI");
75                     trace("select poi");
76                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
77                     return new NoSelection();
78                                 }
79             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
80                                 if ( entity is Way && focus==selectedWay && event.shiftKey) {
81                                         // insert node within way (shift-click)
82                     var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event);
83                                         d.forceDragStart();
84                                         return d;
85                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
86                     return new DragWayNode(selectedWay, Node(entity), event);
87                                 }
88             }
89
90             return this;
91         }
92
93         public function clickOnWay(event:MouseEvent, entity:Entity):ControllerState {
94             if ( entity is Node ) {
95                 if ( selectedNode == entity ) {
96                     var i:uint = selectedWay.indexOfNode(selectedNode);
97                     if ( i == 0 )
98                         return new DrawWay(selectedWay, false);
99                     else if ( i == selectedWay.length - 1 )
100                         return new DrawWay(selectedWay, true);
101                 } else {
102                     selectNode(entity as Node);
103                 }
104             }
105             
106             return this;
107         }
108         
109         private function addNode(event:MouseEvent):Node {
110             trace("add node");
111             var lat:Number = controller.map.coord2lat(event.localY);
112             var lon:Number = controller.map.coord2lon(event.localX);
113             var node:Node = controller.connection.createNode({}, lat, lon);
114             selectedWay.insertNodeAtClosestPosition(node, true);
115                         return node;
116         }
117         
118         override public function enterState():void {
119             selectWay(initWay);
120         }
121         override public function exitState():void {
122             clearSelection();
123         }
124
125         override public function toString():String {
126             return "SelectedWay";
127         }
128
129     }
130 }