width can be defined based on maximum width so far
[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]);
44                     return new DrawWay(way, true, false);
45                                 } else if ( entity is Node && focus == selectedWay ) {
46                                         // select node within way
47                                         return selectOrEdit(selectedWay, Node(entity));
48                 } else if ( entity is Way ) {
49                                         // select way
50                                         return new SelectedWay(Way(entity));
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                                 } else if ( focus is Node ) {
63                                         return new DragPOINode(entity as Node,event,false);
64                                 }
65             }
66
67             return this;
68         }
69
70                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
71                         switch (event.keyCode) {
72                                 case 88:        return splitWay();
73                         }
74                         return this;
75                 }
76                 
77                 override public function enterState():void {
78             selectNode(initWay,initNode);
79                         Globals.vars.root.addDebug("**** -> "+this);
80         }
81                 override public function exitState():void {
82             clearSelection();
83                         Globals.vars.root.addDebug("**** <- "+this);
84         }
85
86         override public function toString():String {
87             return "SelectedWayNode";
88         }
89
90         public static function selectOrEdit(selectedWay:Way, entity:Node):ControllerState {
91                 var isFirst:Boolean = false;
92                         var isLast:Boolean = false;
93                         isFirst = selectedWay.getNode(0) == entity;
94                         isLast = selectedWay.getNode(selectedWay.length - 1) == entity;
95                         if ( isFirst == isLast )    // both == looped, none == central node 
96                             return new SelectedWayNode(selectedWay, entity);
97                         else
98                             return new DrawWay(selectedWay, isLast, true);
99         }
100
101                 public function splitWay():ControllerState {
102                         // abort if start or end
103                         if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
104                         if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
105
106                         // create new way
107                         var newWay:Way = controller.connection.createWay(
108                                 selectedWay.getTagsCopy(), 
109                                 selectedWay.sliceNodes(selectedWay.indexOfNode(selectedNode),selectedWay.length));
110                         selectedWay.deleteNodesFrom(selectedWay.indexOfNode(selectedNode)+1);
111                         
112                         // copy relations
113                         for each (var r:Relation in selectedWay.parentRelations) {
114                                 // ** needs to copy roles as well
115                                 r.appendMember(new RelationMember(newWay, ''));
116                         }
117                         controller.map.ways[newWay.id].redraw();
118
119                         return new SelectedWay(selectedWay);
120                 }
121
122     }
123 }