Monday, October 31, 2011

Multi Selection


<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas  clipContent="false" doubleClickEnabled="true" doubleClick="SelectionManager.instance.selectNone()"  xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="400" backgroundColor="#FFFFFF" backgroundAlpha="1.0" xmlns:view="be.vip.marchingantsdemo.view.*" xmlns:objecthandles="com.roguedevelopment.objecthandles.*">
<mx:Script>
<![CDATA[
import com.roguedevelopment.objecthandles.SelectionManager;
import com.roguedevelopment.objecthandles.ObjectHandleEvent;
import mx.utils.ObjectUtil;
import mx.controls.Label;
import mx.controls.Button;
import com.roguedevelopment.objecthandles.OHMouseCursors;
import com.roguedevelopment.objecthandles.ObjectHandles;
import mx.core.Application;
import mx.core.UIComponent;
private var selectionRect:Rectangle;
private var clickPoint:Point;
private var selection:Boolean=false;
private var selectionX:int=0;
       private var selectionY:int=0;

    

private var isDragCanvas:Boolean=false;
private var selectMode:Boolean=false;

private var selectionWidth:int=10;
private var selectionHeight:int=10;

private var ctrlArray:Array=new Array();
[Bindable]
private var ctrlPressed:Boolean=false;

private function creationComplete_handler():void
{
                this.addEventListener(MouseEvent.MOUSE_DOWN, startSelectionDrawing_handler);
                this.addEventListener(MouseEvent.MOUSE_UP, stopSelectionDrawing_handler);
            }
public function startSelectionDrawing_handler(evt:MouseEvent):void{
if(this.hasEventListener(MouseEvent.MOUSE_DOWN))
{
this.removeEventListener(MouseEvent.MOUSE_DOWN, startSelectionDrawing_handler);
}
if(selection==false)
{
selectionRect=new Rectangle();
selectionX=this.mouseX;
selectionY=this.mouseY;
selectionRect.x=this.mouseX;
selectionRect.y=this.mouseY;
selectionRect.width=1;
selectionRect.height=1;
//this.addChild(marchingAntsSelectionRect);
this.addEventListener(Event.ENTER_FRAME,updateSelection_handler);
if(this.hasEventListener(MouseEvent.MOUSE_UP))
{
this.removeEventListener(MouseEvent.MOUSE_UP, stopSelectionDrawing_handler);
}

this.addEventListener(MouseEvent.MOUSE_UP, stopSelectionDrawing_handler);
}
}
public function updateSelection_handler(event:Event):void{

selectionRect.width=this.mouseX-selectionRect.x;
selectionRect.height=this.mouseY-selectionRect.y;
marchingAntsSelectionRect.rect=selectionRect;

}
public var dragCanvas:OBHContainer=new OBHContainer();
public var swapRect:Rectangle;
public function stopSelectionDrawing_handler(evt:MouseEvent):void{
if(isDragCanvas&&this.contains(dragCanvas))
{
this.removeChild(dragCanvas);
isDragCanvas=false;
}
if(this.hasEventListener(MouseEvent.MOUSE_UP))
{
this.removeEventListener(MouseEvent.MOUSE_UP, stopSelectionDrawing_handler);
}
if(this.hasEventListener(Event.ENTER_FRAME))
{
this.removeEventListener(Event.ENTER_FRAME,updateSelection_handler);
}
swapRect=marchingAntsSelectionRect.rect;
marchingAntsSelectionRect.x=0;
marchingAntsSelectionRect.y=0;
marchingAntsSelectionRect.clear();
dragCanvas=new OBHContainer();
this.addChild(dragCanvas);
//dragCanvas.contextMenu=pageMenu1;
isDragCanvas=true;
dragCanvas.x=selectionX;
dragCanvas.y=selectionY;
dragCanvas.height=swapRect.height;
dragCanvas.width=swapRect.width;
/* dragCanvas.addEventListener(MouseEvent.MOUSE_DOWN,dragUpdateLocation);
dragCanvas.addEventListener(MouseEvent.MOUSE_UP,dropUpdateLocation);  */
//dragCanvas.addEventListener(KeyboardEvent.KEY_DOWN,onPressDel);
dragCanvas.addEventListener(MouseEvent.DOUBLE_CLICK,clearSelection);
selectItems();
//selectionRect=null;

}
[Bindable]
    public var _left:int=0;
    [Bindable]
    public var _right:int=0;
    [Bindable]
    public var _top:int=0;
    [Bindable]
    public var _bottom:int=0;
public function selectionResize(event:ObjectHandleEvent):void
{
if(dragCanvas.x<=_left)
{
dragCanvas.x=_left-1;
}
if(dragCanvas.y<=_top)
{
dragCanvas.y=_top-1;
}
if(dragCanvas.x+dragCanvas.width>this.width)
{
var diff:int=dragCanvas.x+dragCanvas.width-this.width;
diff=dragCanvas.width-diff;
dragCanvas.width=diff;
}
if(dragCanvas.y+dragCanvas.height>this.height)
{
var diff:int=dragCanvas.y+dragCanvas.height-this.height;
diff=dragCanvas.height-diff;
dragCanvas.height=diff;
}
selectionRect=new Rectangle(dragCanvas.x-3,dragCanvas.y-3,dragCanvas.width+3,dragCanvas.height+3);
selectionRect.x=dragCanvas.x-3;
selectionRect.y=dragCanvas.y-3;
selectionRect.width=dragCanvas.width+3;
selectionRect.height=dragCanvas.height+3;

selectedItemsArr=new Array();
for (var i:Number=0;i<this.numChildren;i++)
{
if(selectionRect.containsRect((this.getChildAt(i)).getRect(this)))
{
if(this.getChildAt(i).name=='test1')
{
this.getChildAt(i).filters=[new GlowFilter(0x8fcef6,0.7,2,2,2,1)];
var coordinates:Object=new Object();
coordinates.index=i;
coordinates.lock=(this.getChildAt(i) as ObjectHandles);
coordinates.x=this.getChildAt(i).x-dragCanvas.x;
coordinates.y=this.getChildAt(i).y-dragCanvas.y;
selectedItemsArr.push(coordinates);
}

}
         else
       {
     this.getChildAt(i).filters=[];
     trace('no object');
        }
}
}
private var selectedItemsArr:Array=new Array();
public function selectItems():void
{
selectedItemsArr=new Array();
if(swapRect!=null)
{
selectionRect=swapRect;
for(var u:int=0;u<this.numChildren;u++)
{
if(selectionRect.intersects((this.getChildAt(u)).getRect(this)))
{
//if{this.getChildAt(u).name!="MAnt"}
selectionRect=selectionRect.union((this.getChildAt(u)).getRect(this));
}
}
if(selectionRect.width<0)
{
selectionRect.x=selectionRect.x+selectionRect.width;
selectionRect.width=selectionRect.width*-1;
}
if(selectionRect.height<0)
{
selectionRect.y=selectionRect.y+selectionRect.height;
selectionRect.height=selectionRect.height*-1;
}
dragCanvas.x=selectionRect.x;
dragCanvas.y=selectionRect.y;
dragCanvas.height=selectionRect.height;
dragCanvas.width=selectionRect.width;
for (var i:Number=0;i<this.numChildren;i++)
{
if(this.getChildAt(i).name=="test1")
{
if(this.getChildAt(i) as ObjectHandles)
{
this.getChildAt(i).filters=[new GlowFilter(0x8fcef6,0.7,2,2,2,1)];
var coordinates:Object=new Object();
coordinates.index=i;
coordinates.x=this.getChildAt(i).x-dragCanvas.x;
coordinates.y=this.getChildAt(i).y-dragCanvas.y;
selectedItemsArr.push(coordinates);
}

}
}
}
else
{
this.getChildAt(i).filters=[];
}
if(selectedItemsArr.length>0)
{

selection=true;
isDragCanvas=true;
stage.focus=dragCanvas;
this.removeEventListener(MouseEvent.MOUSE_DOWN,startSelectionDrawing_handler);
}
else if(selectedItemsArr.length<=0)
{
for (var i:Number=0;i<selectedItemsArr.length;i++)
{
if(this.getChildAt(selectedItemsArr[i].index).name=="test1")
{
(this.getChildAt(selectedItemsArr[i].index) as ObjectHandles).select();
}
this.getChildAt(selectedItemsArr[i].index).filters=[];
}
marchingAntsSelectionRect.clear();
marchingAntsSelectionRect.x=0;
marchingAntsSelectionRect.y=0;
selection=false;
selectedItemsArr=new Array();
if(isDragCanvas)
{
this.removeChild(dragCanvas);
isDragCanvas=false;
}
}
}
private var dev:int=0;
public function clear():void
{
dev=0;
for (var i:Number=0;i<selectedItemsArr.length;i++)
{
this.removeChild(this.getChildAt(selectedItemsArr[i].index-dev));
dev=dev+1;
}
selectedItemsArr=new Array();
clearControl();
selectionRect=null;

}
public function dragUpdateLocation(event:MouseEvent):void
{
dragCanvas.startDrag();
dragCanvas.addEventListener(MouseEvent.MOUSE_MOVE,updatePosition);
for (var i:Number=0;i<selectedItemsArr.length;i++)
{
this.getChildAt(selectedItemsArr[i].index).x=selectedItemsArr[i].x+dragCanvas.x;
this.getChildAt(selectedItemsArr[i].index).y=selectedItemsArr[i].y+dragCanvas.y;

stage.focus=dragCanvas;
dragCanvas.setFocus();
}
public function updatePosition(event:MouseEvent):void
{
for (var i:Number=0;i<selectedItemsArr.length;i++)
{
this.getChildAt(selectedItemsArr[i].index).x=selectedItemsArr[i].x+dragCanvas.x;
this.getChildAt(selectedItemsArr[i].index).y=selectedItemsArr[i].y+dragCanvas.y;

//stage.focus=dragCanvas;
//dragCanvas.setFocus();
}
public function dropUpdateLocation(event:MouseEvent):void
{
dragCanvas.stopDrag();
dragCanvas.removeEventListener(MouseEvent.MOUSE_MOVE,updatePosition);
for (var i:Number=0;i<selectedItemsArr.length;i++)
{
this.getChildAt(selectedItemsArr[i].index).x=selectedItemsArr[i].x+dragCanvas.x;
this.getChildAt(selectedItemsArr[i].index).y=selectedItemsArr[i].y+dragCanvas.y;
}
//stage.focus=dragCanvas;
//dragCanvas.setFocus();

}
private function clearControl():void
{
for (var i:Number=0;i<selectedItemsArr.length;i++)
{
this.getChildAt(selectedItemsArr[i].index).filters=[];
}
selectionRect=null;
selectedItemsArr=new Array();
selection=false;
if(isDragCanvas)
{
this.removeChild(dragCanvas);
isDragCanvas=false;
}
if(this.hasEventListener(MouseEvent.DOUBLE_CLICK))
{
this.removeEventListener(MouseEvent.DOUBLE_CLICK,clearSelection);
}
if(dragCanvas.hasEventListener(MouseEvent.DOUBLE_CLICK))
{
dragCanvas.removeEventListener(MouseEvent.DOUBLE_CLICK,clearSelection);
}

}
private function clearSelection(event:MouseEvent):void
{

clearControl();
//selectMode=false;
}

public function multiselectionevent(event:MouseEvent):void
{
if(ctrlPressed)
{
// removeMultiSelector();
if(ctrlArray.length>0)
{
var ind:int = this.getChildIndex(event.currentTarget as DisplayObject);
var chBool:Boolean=false;
   for (var i:int=0; i<ctrlArray.length; i++) 
   {
   if (ctrlArray[i].index==ind) 
   {
    ind = i;
    chBool=true;
   }

}
if(chBool==true)
{
ctrlArray.splice(ind,1);
(event.currentTarget as UIComponent).filters=[];
/* (event.currentTarget as UIComponent).setStyle("borderStyle",'solid');
(event.currentTarget as UIComponent).setStyle("borderThickness",0);
(event.currentTarget as UIComponent).setStyle("borderColor",0x000000); */
}
else
{
(event.currentTarget as UIComponent).filters=[new GlowFilter(0x8fcef6,0.7,2,2,2,1)];
/* (event.currentTarget as UIComponent).setStyle("borderStyle",'solid');
(event.currentTarget as UIComponent).setStyle("borderThickness",2);
(event.currentTarget as UIComponent).setStyle("borderColor",0x000000); */
var coordinates:Object=new Object();
coordinates.x=(event.currentTarget as DisplayObject).x;
coordinates.y=(event.currentTarget as DisplayObject).y;
coordinates.index=this.getChildIndex(event.currentTarget as DisplayObject);
ctrlArray.push(coordinates);
}
}
else
{
(event.currentTarget as UIComponent).filters=[new GlowFilter(0x8fcef6,0.7,2,2,2,1)];
/* (event.currentTarget as UIComponent).setStyle("borderStyle",'solid');
(event.currentTarget as UIComponent).setStyle("borderThickness",2);
(event.currentTarget as UIComponent).setStyle("borderColor",0x000000); */
var coordinates:Object=new Object();
coordinates.x=(event.currentTarget as DisplayObject).x;
coordinates.y=(event.currentTarget as DisplayObject).y;
coordinates.index=this.getChildIndex(event.currentTarget as DisplayObject);
ctrlArray.push(coordinates);
}
}
else
{

for(var c:int=0;c<ctrlArray.length;c++)
{
this.getChildAt(ctrlArray[c].index).filters=[];
/* (bgCan.getChildAt(ctrlArray[c].index) as UIComponent).setStyle("borderStyle",'solid');
(bgCan.getChildAt(ctrlArray[c].index) as UIComponent).setStyle("borderThickness",0);
(bgCan.getChildAt(ctrlArray[c].index) as UIComponent).setStyle("borderColor",0x000000); */
}
ctrlArray=new Array();
}
}
public var shape:String;
public function addGroupSelection(event:MouseEvent):void
{
//removeSelection();
shape=new String();
cursorManager.removeAllCursors();
//mainCan.clear();
//removeConnector(event);
//clearControl();
if(selectMode==false&&selection==false)
{
if(this.hasEventListener(MouseEvent.MOUSE_DOWN))
{
this.removeEventListener(MouseEvent.MOUSE_DOWN, startSelectionDrawing_handler);
}
if(this.hasEventListener(MouseEvent.DOUBLE_CLICK))
{
this.removeEventListener(MouseEvent.DOUBLE_CLICK,clearSelection);
}
this.addEventListener(MouseEvent.MOUSE_DOWN, startSelectionDrawing_handler);
this.addEventListener(MouseEvent.DOUBLE_CLICK,clearSelection);
for(var c:int=0;c<ctrlArray.length;c++)
{
this.getChildAt(ctrlArray[c].index).filters=[];
}
ctrlArray=new Array();
}
}


override public function addChild(child:DisplayObject):DisplayObject
{
   super.addChild(child);
child.addEventListener(MouseEvent.CLICK,multiselectionevent);
updateSelection();
   return child;
}
override public function addChildAt(child:DisplayObject, index:int):DisplayObject
{
super.addChildAt(child,index);

child.addEventListener(MouseEvent.CLICK,multiselectionevent);

updateSelection();
return child;
}


private function updateSelection():void
{
for(var i:int=0;i<this.numChildren;i++)
{
if(this.getChildAt(i).name=="MAnt")
{
this.setChildIndex((this.getChildAt(i) as DisplayObject),this.numChildren-1);
return;
}
}
}
public var  but:Button;
public var subObj:ObjectHandles;
public var ObjMain:Object;
public function add():void
{
but=new Button();
but.label='Object';
but.name='test1'
but.percentHeight=100;
but.percentWidth=100;
 
subObj=new ObjectHandles();
subObj.name='test1';
subObj.x=40;
subObj.y=40;
subObj.addChild(but);
this.addChild(subObj);
ObjMain=subObj;
ObjMain.name='test1';
trace(ObjMain.name);
}
public function remove():void
{
if(selectedItemsArr.length>0)
        {
        clear();
        }
        else if(subObj.name=='test1')
{
 this.removeChild((subObj as Button).parent as ObjectHandles);
}
}
]]>
</mx:Script>



<mx:Button click="add()"   x="6" y="4" label="Add Button"/>
    <mx:Button click="creationComplete_handler()"   x="105" y="4" label="Multi Selection"/>
<!--<mx:CheckBox id="chek" name="test1" x="120" y="121" label="Checkbox"/>
<mx:ColorPicker id="colr" name="test2" x="120" y="151"/>
<mx:ComboBox id="comb" name="test3" x="120" y="181"></mx:ComboBox>
<mx:Label id="lab" name="test4" x="120" y="211" text="Label"/>
<mx:NumericStepper id="numb" name="test5" x="120" y="237"/>
<mx:TextInput id="textin" name="test6" x="120" y="267"/>-->

<view:MarchingAntsSelectionRectangle  name="MAnt" lineThickness="1" x="0" y="0" id="marchingAntsSelectionRect"/>
<mx:Button click="remove()"   x="226" y="4" label="Delete"/>

</mx:Canvas>