more selection fixes
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.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 SelectedWayNode extends SelectedWay {
8         protected var selectedNode:Node;
9         protected var initNode:Node;
10         
11         public function SelectedWayNode(way:Way,node:Node) {
12                         super (way);
13             initNode = node;
14         }
15  
16         protected function selectNode(way:Way,node:Node):void {
17             if ( way == selectedWay && node == selectedNode )
18                 return;
19
20             clearSelection();
21             controller.setTagViewer(node);
22             controller.map.setHighlight(way, { showNodes: true, nodeSelected: node.id });
23             selectedWay = way;   initWay  = way;
24             selectedNode = node; initNode = node;
25         }
26                 
27         override protected function clearSelection():void {
28             if ( selectedNode != null ) {
29                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
30                 controller.setTagViewer(null);
31                 selectedNode = null;
32                                 selectedWay = null;
33             }
34         }
35         
36         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
37                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.MOUSE_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
38             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
39
40             if ( event.type == MouseEvent.MOUSE_UP ) {
41                                 if ( entity is Node && event.shiftKey ) {
42                                         // start new way
43                     var way:Way = controller.connection.createWay({}, [entity, entity]);
44                     return new DrawWay(way, true);
45                                 } else if ( entity is Node ) {
46                                         // select node within way
47                                         return new SelectedWayNode(selectedWay,Node(entity));
48                 } else if ( entity is Way ) {
49                                         // select way
50                                         return new SelectedWay(selectedWay);
51                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
52                     return new NoSelection();
53                                 }
54             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
55                                 if ( entity is Way && focus==selectedWay && event.shiftKey) {
56                                         // insert node within way (shift-click)
57                         var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
58                                         d.forceDragStart();
59                                         return d;
60                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
61                     return new DragWayNode(selectedWay, Node(entity), event, false);
62                                 }
63             }
64
65             return this;
66         }
67
68                 override public function enterState():void {
69             selectNode(initWay,initNode);
70                         Globals.vars.root.addDebug("**** -> "+this);
71         }
72                 override public function exitState():void {
73             clearSelection();
74                         Globals.vars.root.addDebug("**** <- "+this);
75         }
76
77         override public function toString():String {
78             return "SelectedWayNode";
79         }
80
81     }
82 }