refactor EditController to make it stateful, and add addWayNodes functionality with...
[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         private var selectedWay:Way;
8         private var initWay:Way;
9         
10         public function SelectedWay(way:Way) {
11             initWay = way;
12         }
13  
14         protected function selectWay(way:Way):void {
15             if ( way == selectedWay )
16                 return;
17
18             clearSelection();
19             controller.setTagViewer(way);
20             controller.map.setHighlight(way, "selected", true);
21             controller.map.setHighlight(way, "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);
29                 controller.map.setHighlight(selectedWay, "showNodes", false);
30                 controller.setTagViewer(null);
31                 selectedWay = null;
32             }
33         }
34         
35         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
36             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
37             if ( event.type == MouseEvent.CLICK ) {
38                 if ( (entity is Node && entity.hasParent(selectedWay)) || focus == selectedWay )
39                     return clickOnWay(event, entity);
40                 else if ( focus is Way )
41                     selectWay(focus as Way);
42                 else if ( focus is Node )
43                     trace("select poi");
44                 else if ( focus == null )
45                     return previousState;
46             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
47                 if ( entity is Node && entity.hasParent(selectedWay) )
48                     return new DragWayNode(selectedWay, Node(entity), event);
49             }
50
51             return this;
52         }
53
54         public function clickOnWay(event:MouseEvent, entity:Entity):ControllerState {
55             if ( event.shiftKey ) {
56                 if ( entity is Way )
57                     addNode(event);
58                 else
59                     trace("start new way");
60             } else {
61                 if ( entity is Node )
62                     trace("select way node");
63             }
64             
65             return this;
66         }
67         
68         public function addNode(event:MouseEvent):void {
69             trace("add node");
70             var lat:Number = controller.map.coord2lat(event.localY);
71             var lon:Number = controller.map.coord2lon(event.localX);
72             var node:Node = controller.connection.createNode({}, lat, lon);
73             selectedWay.insertNodeAtClosestPosition(node, true);
74         }
75         
76         override public function enterState():void {
77             selectWay(initWay);
78         }
79         override public function exitState():void {
80             clearSelection();
81         }
82     }
83 }