Merge branch 'refactor'
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedPOINode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.display.*;
4         import flash.ui.Keyboard;
5     import net.systemeD.potlatch2.EditController;
6     import net.systemeD.halcyon.connection.*;
7     import net.systemeD.halcyon.MapPaint;
8
9     public class SelectedPOINode extends ControllerState {
10         protected var initNode:Node;
11
12         public function SelectedPOINode(node:Node, layer:MapPaint=null) {
13                         if (layer) this.layer=layer;
14             initNode = node;
15         }
16  
17         protected function selectNode(node:Node):void {
18             if ( firstSelected is Node && Node(firstSelected)==node )
19                 return;
20
21             clearSelection(this);
22             layer.setHighlight(node, { selected: true });
23             selection = [node];
24             controller.updateSelectionUI();
25             initNode = node;
26         }
27                 
28         protected function clearSelection(newState:ControllerState):void {
29             if ( selectCount ) {
30                 layer.setHighlight(firstSelected, { selected: false });
31                 selection = [];
32                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
33             }
34         }
35         
36         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
37                         if (event.type==MouseEvent.MOUSE_MOVE) { return this; }
38                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
39
40                         if (event.type==MouseEvent.MOUSE_DOWN && event.ctrlKey && entity && entity!=firstSelected && paint==layer) {
41                                 return new SelectedMultiple([firstSelected,entity],layer);
42                         } else if (event.type==MouseEvent.MOUSE_DOWN && event.shiftKey && !entity && !layer.isBackground) {
43                                 return new DrawQuadrilateral(firstSelected as Node);
44                         } else if ( event.type == MouseEvent.MOUSE_UP && entity==firstSelected ) {
45                                 return this;
46                         }
47                         var cs:ControllerState = sharedMouseEvents(event, entity);
48                         return cs ? cs : this;
49         }
50
51                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
52                         switch (event.keyCode) {
53                                 case Keyboard.BACKSPACE:        return deletePOI();
54                                 case Keyboard.DELETE:           return deletePOI();
55                                 case 82:                                        repeatTags(firstSelected); return this; // 'R'
56                         }
57                         var cs:ControllerState = sharedKeyboardEvents(event);
58                         return cs ? cs : this;
59                 }
60
61                 public function deletePOI():ControllerState {
62                         firstSelected.connection.unregisterPOI(firstSelected as Node);
63                         firstSelected.remove(MainUndoStack.getGlobalStack().addAction);
64                         return new NoSelection();
65                 }
66
67         override public function enterState():void {
68             selectNode(initNode);
69                         layer.setPurgable(selection,false);
70         }
71         override public function exitState(newState:ControllerState):void {
72             if(firstSelected.hasTags()) {
73               controller.clipboards['node']=firstSelected.getTagsCopy();
74             }
75                         layer.setPurgable(selection,true);
76             clearSelection(newState);
77         }
78
79         override public function toString():String {
80             return "SelectedPOINode";
81         }
82
83     }
84 }