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