Improve selection for background objects and tidy ControllerState layer handling
[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
16                 public function SelectArea(x:Number,y:Number) {
17                         startX=endX=x;
18                         startY=endY=y;
19                 }
20
21         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
22             if (event.type==MouseEvent.MOUSE_MOVE) { 
23                                 // ** FIXME: weird things happen if you mouse-over the drag-and-drop panel
24                                 endX=event.localX;
25                                 endY=event.localY;
26                                 drawSelectionBox();
27                         } else if (event.type==MouseEvent.MOUSE_UP) { 
28                                 // select everything within boundary
29                                 var a:Number;
30                                 if (startX>endX) { a=startX; startX=endX; endX=a; }
31                                 if (startY>endY) { a=startY; startY=endY; endY=a; }
32                                 if (endX-startX<TOLERANCE && endY-startY<TOLERANCE) { return new NoSelection(); }
33                                 var left:Number=controller.map.coord2lon(startX);
34                                 var right:Number=controller.map.coord2lon(endX);
35                                 var top:Number=controller.map.coord2lat(startY);
36                                 var bottom:Number=controller.map.coord2lat(endY);
37                                 var entities:Object=layer.connection.getObjectsByBbox(left,right,top,bottom);
38                                 for each (var way:Way  in entities.waysInside) { if (way.intersects(left,right,top,bottom)) toggleSelection(way); }
39                                 for each (var poi:Node in entities.poisInside) { toggleSelection(poi); }
40                                 return controller.findStateForSelection(selection);
41                         }
42             return this;
43         }
44
45                 private function drawSelectionBox():void {
46                         box.graphics.clear();
47                         box.graphics.beginFill(0xDDDDFF,0.5);
48                         box.graphics.lineStyle(1,0xFF0000);
49                         box.graphics.drawRect(startX,startY,endX-startX,endY-startY);
50                 }
51                 
52                 override public function enterState():void {
53                         box=new Shape();
54                         var l:DisplayObject=layer.getPaintSpriteAt(layer.maxlayer);
55                         var o:DisplayObject=Sprite(l).getChildAt(3);
56                         (o as Sprite).addChild(box);
57                         controller.map.draggable=false;
58                 }
59                 override public function exitState(newState:ControllerState):void {
60                         box.parent.removeChild(box);
61                         controller.map.draggable=true;
62                         if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
63                 }
64                 override public function toString():String {
65                         return "SelectArea";
66                 }
67         }
68
69 }