b753843ccb1a5ce8a13da8c33e61f710a195756c
[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.Parallelise;
7     import net.systemeD.potlatch2.tools.Simplify;
8     import net.systemeD.halcyon.connection.*;
9         import net.systemeD.halcyon.MapPaint;
10         import net.systemeD.halcyon.WayUI;
11         import net.systemeD.halcyon.Globals;
12
13     public class SelectedWay extends ControllerState {
14         protected var initWay:Way;
15         
16         public function SelectedWay(way:Way) {
17             initWay = way;
18         }
19  
20         protected function selectWay(way:Way):void {
21             if ( firstSelected is Way && Way(firstSelected)==way )
22                 return;
23
24             clearSelection(this);
25             controller.map.setHighlight(way, { selected: true, hover: false });
26             controller.map.setHighlightOnNodes(way, { selectedway: true });
27             selection = [way];
28             controller.updateSelectionUI();
29             initWay = way;
30         }
31
32         protected function clearSelection(newState:ControllerState):void {
33             if ( selectCount ) {
34                 controller.map.setHighlight(firstSelected, { selected: false, hover: false });
35                 controller.map.setHighlightOnNodes(firstSelected as Way, { selectedway: false });
36                 selection = [];
37                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
38             }
39         }
40         
41         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
42                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
43             var focus:Entity = getTopLevelFocusEntity(entity);
44
45             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
46                                 // start new way
47                                 var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
48                                 return new DrawWay(way, true, false);
49                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==firstSelected && event.shiftKey) {
50                                 // insert node within way (shift-click)
51                 var d:DragWayNode=new DragWayNode(firstSelected as Way, -1, event, true);
52                                 d.forceDragStart();
53                                 return d;
54                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && event.shiftKey ) {
55                                 // merge way
56                                 return mergeWith(entity as Way);
57                         } else if ( event.type == MouseEvent.MOUSE_DOWN && event.ctrlKey && entity!=firstSelected) {
58                                 // multiple selection
59                                 return new SelectedMultiple([firstSelected,entity]);
60                         }
61                         var cs:ControllerState = sharedMouseEvents(event, entity);
62                         return cs ? cs : this;
63         }
64         
65                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
66                         switch (event.keyCode) {
67                                 case 80:                                        return new SelectedParallelWay(firstSelected as Way);
68                                 case 82:                                        repeatTags(firstSelected); return this;
69                 case 86:                    Way(firstSelected).reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
70                 case 89:                    Simplify.simplify(firstSelected as Way, controller.map, true); return this;         
71                                 case Keyboard.BACKSPACE:        if (event.shiftKey) { return deleteWay(); } break;
72                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
73                         }
74                         var cs:ControllerState = sharedKeyboardEvents(event);
75                         return cs ? cs : this;
76                 }
77
78                 protected function mergeWith(otherWay:Way):ControllerState {
79                         var way1:Way;
80                         var way2:Way;
81                         if ( firstSelected.id < otherWay.id && firstSelected.id >= 0 ) {
82                             way1 = firstSelected as Way;
83                             way2 = otherWay;
84                         } else {
85                             way1 = otherWay;
86                             way2 = firstSelected as Way;
87                         }
88                         
89                         var undo:Function = MainUndoStack.getGlobalStack().addAction;
90                         
91                         // find common point
92                         if (way1 == way2) { return this; }
93                         if      (way1.getNode(0)   ==way2.getNode(0)   ) { way1.mergeWith(way2,0,0,undo); }
94                         else if (way1.getNode(0)   ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
95                         else if (way1.getLastNode()==way2.getNode(0)   ) { way1.mergeWith(way2,way1.length-1,0,undo); }
96                         else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
97                         return new SelectedWay(way1);
98                 }
99         
100                 public function deleteWay():ControllerState {
101                         controller.map.setHighlightOnNodes(firstSelected as Way, {selectedway: false});
102                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
103                         return new NoSelection();
104                 }
105
106         override public function enterState():void {
107             selectWay(initWay);
108                         controller.map.setPurgable(selection,false);
109                         Globals.vars.root.addDebug("**** -> "+this+" "+firstSelected.id);
110         }
111         override public function exitState(newState:ControllerState):void {
112                         controller.clipboards['way']=firstSelected.getTagsCopy();
113                         controller.map.setPurgable(selection,true);
114             clearSelection(newState);
115                         Globals.vars.root.addDebug("**** <- "+this);
116         }
117
118         override public function toString():String {
119             return "SelectedWay";
120         }
121
122     }
123 }