Unify selection ControllerStates so they can work on either background or editable...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectArea.as
1 package net.systemeD.potlatch2.controller {
2
3         import flash.display.*;
4         import flash.events.*;
5         import net.systemeD.halcyon.connection.*;
6
7     public class SelectArea extends ControllerState {
8
9                 private var startX:Number;
10                 private var startY:Number;
11                 private var endX:Number;
12                 private var endY:Number;
13                 private var box:Shape;
14                 private const TOLERANCE:uint=4;
15                 private var originalSelection:Array;
16
17                 public function SelectArea(x:Number,y:Number,sel:Array) {
18                         selection = sel.concat();
19                         originalSelection = sel.concat();
20                         startX=endX=x;
21                         startY=endY=y;
22                 }
23
24         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
25             if (event.type==MouseEvent.MOUSE_MOVE) { 
26                                 // ** FIXME: weird things happen if you mouse-over the drag-and-drop panel
27                                 endX=event.localX;
28                                 endY=event.localY;
29                                 drawSelectionBox();
30                         } else if (event.type==MouseEvent.MOUSE_UP) { 
31                                 // select everything within boundary
32                                 var a:Number;
33                                 if (startX>endX) { a=startX; startX=endX; endX=a; }
34                                 if (startY>endY) { a=startY; startY=endY; endY=a; }
35                                 if (endX-startX<TOLERANCE && endY-startY<TOLERANCE) { return controller.findStateForSelection(originalSelection); }
36                                 var left:Number=controller.map.coord2lon(startX);
37                                 var right:Number=controller.map.coord2lon(endX);
38                                 var top:Number=controller.map.coord2lat(startY);
39                                 var bottom:Number=controller.map.coord2lat(endY);
40                                 var entities:Object=layer.connection.getObjectsByBbox(left,right,top,bottom);
41                                 for each (var way:Way  in entities.waysInside) { if (way.intersects(left,right,top,bottom)) toggleSelection(way); }
42                                 for each (var poi:Node in entities.poisInside) { toggleSelection(poi); }
43                                 return controller.findStateForSelection(selection);
44                         }
45             return this;
46         }
47
48                 private function drawSelectionBox():void {
49                         box.graphics.clear();
50                         box.graphics.beginFill(0xDDDDFF,0.5);
51                         box.graphics.lineStyle(1,0xFF0000);
52                         box.graphics.drawRect(startX,startY,endX-startX,endY-startY);
53                 }
54                 
55                 override public function enterState():void {
56                         for each (var entity:Entity in selection) {
57                                 layer.setHighlight(entity, { selected: true });
58                         }
59                         box=new Shape();
60                         var l:DisplayObject=layer.getPaintSpriteAt(layer.maxlayer);
61                         var o:DisplayObject=Sprite(l).getChildAt(3);
62                         (o as Sprite).addChild(box);
63                         controller.map.draggable=false;
64                 }
65                 override public function exitState(newState:ControllerState):void {
66                         for each (var entity:Entity in originalSelection) {
67                                 layer.setHighlight(entity, { selected: false });
68                         }
69                         box.parent.removeChild(box);
70                         controller.map.draggable=true;
71                         if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
72                 }
73                 override public function toString():String {
74                         return "SelectArea";
75                 }
76         }
77
78 }