slight refactoring of controller states
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.display.DisplayObject;
4         import flash.ui.Keyboard;
5     import net.systemeD.potlatch2.EditController;
6     import net.systemeD.potlatch2.tools.Quadrilateralise;
7     import net.systemeD.halcyon.connection.*;
8         import net.systemeD.halcyon.MapPaint;
9         import net.systemeD.halcyon.Globals;
10
11     public class SelectedWay extends ControllerState {
12         protected var initWay:Way;
13         
14         public function SelectedWay(way:Way) {
15             initWay = way;
16         }
17  
18         protected function selectWay(way:Way):void {
19             if ( way == selectedWay )
20                 return;
21
22             clearSelection();
23             controller.setSelectedEntity(way);
24             controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
25             selectedWay = way;
26             initWay = way;
27         }
28
29         protected function clearSelection():void {
30             if ( selectedWay != null ) {
31                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
32                 controller.setSelectedEntity(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], MainUndoStack.getGlobalStack().addAction);
44                                 return new DrawWay(way, true, false);
45                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Way && event.ctrlKey ) {
46                                 // merge way
47                                 mergeWith(entity as Way);
48                                 return this;
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 81:                                        Quadrilateralise.quadrilateralise(selectedWay); return this;
63                 case 82:                    selectedWay.reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;         
64                                 case Keyboard.BACKSPACE:        if (event.shiftKey) { return deleteWay(); } break;
65                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
66                         }
67                         return this;
68                 }
69
70         protected function addNode(event:MouseEvent):Node {
71             trace("add node");
72             var lat:Number = controller.map.coord2lat(event.localY);
73             var lon:Number = controller.map.coord2lon(event.localX);
74             var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
75             var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
76             selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
77             MainUndoStack.getGlobalStack().addAction(undo);
78                         return node;
79         }
80
81                 protected function mergeWith(otherWay:Way):Boolean {
82                         var way1:Way;
83                         var way2:Way;
84                         if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
85                             way1 = selectedWay;
86                             way2 = otherWay;
87                         } else {
88                             way1 = otherWay;
89                             way2 = selectedWay;
90                         }
91                         
92                         var undo:Function = MainUndoStack.getGlobalStack().addAction;
93                         
94                         // find common point
95                         if (way1 == way2) { return false; }
96                         if      (way1.getNode(0)   ==way2.getNode(0)   ) { way1.mergeWith(way2,0,0,undo); }
97                         else if (way1.getNode(0)   ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
98                         else if (way1.getLastNode()==way2.getNode(0)   ) { way1.mergeWith(way2,way1.length-1,0,undo); }
99                         else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
100                         return true;
101                 }
102         
103                 public function deleteWay():ControllerState {
104                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
105                         return new NoSelection();
106                 }
107
108         override public function enterState():void {
109             selectWay(initWay);
110                         Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
111         }
112         override public function exitState():void {
113             clearSelection();
114                         Globals.vars.root.addDebug("**** <- "+this);
115         }
116
117         override public function toString():String {
118             return "SelectedWay";
119         }
120
121     }
122 }