refactor node stateClasses and stylelist calculation
[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 selectedIndex:int;
11                 protected var initIndex:int;
12         
13         public function SelectedWayNode(way:Way,index:int) {
14                         super (way);
15                         initIndex = index;
16         }
17  
18         protected function selectNode(way:Way,index:int):void {
19                         var node:Node=way.getNode(index);
20             if ( way == selectedWay && node == selectedNode )
21                 return;
22
23             clearSelection(this);
24             controller.setSelectedEntity(node);
25             controller.map.setHighlight(way, { hover: false });
26             controller.map.setHighlight(node, { selected: true });
27             controller.map.setHighlightOnNodes(way, { selectedway: true });
28             selectedWay = way; initWay = way;
29                         selectedIndex = index; initIndex = index;
30             selectedNode = node;
31         }
32                 
33         override protected function clearSelection(newState:ControllerState):void {
34             if ( selectedNode != null ) {
35                 controller.map.setHighlight(selectedWay, { selected: false });
36                                 controller.map.setHighlight(selectedNode, { selected: false });
37                                 controller.map.setHighlightOnNodes(selectedWay, { selectedway: false });
38                 if (!newState.isSelectionState()) { controller.setSelectedEntity(null); }
39                 selectedNode = null;
40                                 selectedWay = null;
41             }
42         }
43         
44         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
45                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
46             var focus:Entity = getTopLevelFocusEntity(entity);
47
48             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
49                                 // start new way
50                 var way:Way = controller.connection.createWay({}, [entity],
51                     MainUndoStack.getGlobalStack().addAction);
52                 return new DrawWay(way, true, false);
53                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == selectedWay ) {
54                                 // select node within way
55                                 return selectOrEdit(selectedWay, getNodeIndex(selectedWay,Node(entity)));
56             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
57                                 // insert node within way (shift-click)
58                         var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
59                                 d.forceDragStart();
60                                 return d;
61                         }
62                         var cs:ControllerState = sharedMouseEvents(event, entity);
63                         return cs ? cs : this;
64         }
65
66                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
67                         switch (event.keyCode) {
68                                 case 189:                                       return removeNode();                                    // '-'
69                                 case 88:                                        return splitWay();                                              // 'X'
70                                 case 82:                                        repeatTags(selectedNode); return this;  // 'R'
71                                 case Keyboard.BACKSPACE:        return deleteNode();
72                                 case Keyboard.DELETE:           return deleteNode();
73                         }
74                         var cs:ControllerState = sharedKeyboardEvents(event);
75                         return cs ? cs : this;
76                 }
77                 
78                 override public function enterState():void {
79             selectNode(initWay,initIndex);
80                         controller.map.setPurgable(selectedNode,false);
81                         Globals.vars.root.addDebug("**** -> "+this);
82         }
83                 override public function exitState(newState:ControllerState):void {
84                         controller.clipboards['node']=selectedNode.getTagsCopy();
85                         controller.map.setPurgable(selectedNode,true);
86             clearSelection(newState);
87                         Globals.vars.root.addDebug("**** <- "+this);
88         }
89
90         override public function toString():String {
91             return "SelectedWayNode";
92         }
93
94         public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
95                 var isFirst:Boolean = false;
96                         var isLast:Boolean = false;
97                         var node:Node = selectedWay.getNode(index);
98                         isFirst = selectedWay.getNode(0) == node;
99                         isLast = selectedWay.getNode(selectedWay.length - 1) == node;
100                         if ( isFirst == isLast )    // both == looped, none == central node 
101                             return new SelectedWayNode(selectedWay, index);
102                         else
103                             return new DrawWay(selectedWay, isLast, true);
104         }
105
106                 public function splitWay():ControllerState {
107                         // abort if start or end
108                         if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
109                         if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
110
111                         controller.map.setHighlightOnNodes(selectedWay, { selectedway: false } );
112                         controller.map.setPurgable(selectedWay,true);
113             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(selectedWay, selectedNode));
114
115                         return new SelectedWay(selectedWay);
116                 }
117                 
118                 public function removeNode():ControllerState {
119                         if (selectedNode.numParentWays==1 && selectedWay.hasOnceOnly(selectedNode)) {
120                                 return deleteNode();
121                         }
122                         selectedWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
123                         return new SelectedWay(selectedWay);
124                 }
125                 
126                 public function deleteNode():ControllerState {
127                         controller.map.setPurgable(selectedNode,true);
128                         selectedNode.remove(MainUndoStack.getGlobalStack().addAction);
129                         return new SelectedWay(selectedWay);
130                 }
131
132     }
133 }