DragWayAction - undoing dragging a way now moves all the nodes together, instead...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.ui.Keyboard;
4     import net.systemeD.potlatch2.EditController;
5     import net.systemeD.halcyon.connection.*;
6     import net.systemeD.halcyon.connection.actions.*;
7         import net.systemeD.halcyon.Globals;
8
9     public class SelectedWayNode extends SelectedWay {
10         protected var initNode:Node;
11         
12         public function SelectedWayNode(way:Way,node:Node) {
13                         super (way);
14             initNode = node;
15         }
16  
17         protected function selectNode(way:Way,node:Node):void {
18             if ( way == selectedWay && node == selectedNode )
19                 return;
20
21             clearSelection();
22             controller.setSelectedEntity(node);
23             controller.map.setHighlight(way, { showNodes: true, nodeSelected: node.id });
24             selectedWay = way;   initWay  = way;
25             selectedNode = node; initNode = node;
26         }
27                 
28         override protected function clearSelection():void {
29             if ( selectedNode != null ) {
30                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
31                 controller.setSelectedEntity(null);
32                 selectedNode = null;
33                                 selectedWay = null;
34             }
35         }
36         
37         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
38                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
39             var focus:Entity = getTopLevelFocusEntity(entity);
40
41             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
42                                 // start new way
43                 var way:Way = controller.connection.createWay({}, [entity],
44                     MainUndoStack.getGlobalStack().addAction);
45                 return new DrawWay(way, true, false);
46                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == selectedWay ) {
47                                 // select node within way
48                                 return selectOrEdit(selectedWay, Node(entity));
49             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
50                                 // insert node within way (shift-click)
51                         var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
52                                 d.forceDragStart();
53                                 return d;
54                         }
55                         var cs:ControllerState = sharedMouseEvents(event, entity);
56                         if (cs) return cs;
57                         return this;
58         }
59
60                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
61                         switch (event.keyCode) {
62                                 case 88:                                        return splitWay();
63                                 case Keyboard.BACKSPACE:        return deleteNode();
64                                 case Keyboard.DELETE:           return deleteNode();
65                         }
66                         return this;
67                 }
68                 
69                 override public function enterState():void {
70             selectNode(initWay,initNode);
71                         Globals.vars.root.addDebug("**** -> "+this);
72         }
73                 override public function exitState():void {
74             clearSelection();
75                         Globals.vars.root.addDebug("**** <- "+this);
76         }
77
78         override public function toString():String {
79             return "SelectedWayNode";
80         }
81
82         public static function selectOrEdit(selectedWay:Way, entity:Node):ControllerState {
83                 var isFirst:Boolean = false;
84                         var isLast:Boolean = false;
85                         isFirst = selectedWay.getNode(0) == entity;
86                         isLast = selectedWay.getNode(selectedWay.length - 1) == entity;
87                         if ( isFirst == isLast )    // both == looped, none == central node 
88                             return new SelectedWayNode(selectedWay, entity);
89                         else
90                             return new DrawWay(selectedWay, isLast, true);
91         }
92
93                 public function splitWay():ControllerState {
94                         // abort if start or end
95                         if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
96                         if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
97
98             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(selectedWay, selectedNode));
99
100                         return new SelectedWay(selectedWay);
101                 }
102                 
103                 public function deleteNode():ControllerState {
104                         selectedNode.remove(MainUndoStack.getGlobalStack().addAction);
105                         return new SelectedWay(selectedWay);
106                 }
107
108     }
109 }