1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package MyControls
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.core.UIComponent;
public class itemSelector
{
public var _view:itemsSelector; // MVC (view connector)
public var _x1:uint; // selecting box start coordinates
public var _y1:uint;
public var _x2:uint; // selecting box end coordinates
public var _y2:uint;
public var _clickFlag:Boolean = false; // is Mousebutton down?
public var s:Sprite = new Sprite(); // to draw the rectangular box
public var c:UIComponent = new UIComponent(); // a surface to draw the sprite (box) inside
public var _SelectedItems:Array = new Array();
public var _totalItems:uint = 0; // number of items in the view
public var _ItemNo:uint;
// properties
public var _BoxColor:uint = 0x000000; // color of the Box (default = black)
public var _BoxThickness:uint = 1; // thickness of the Box (default = 1px)
public var _BoxAlpha:Number = 1.0; // alpha of the Box (default = 1.0)
// ------- Constructor -------
public function itemSelector(view:itemsSelector)
{
_x1 = 100;
_y1 = 100;
_x2 = 200;
_y2 = 200;
_view = view;
_view.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
_view.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
_view.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
}
public function DrawSelectBox():void
{
s.graphics.clear();
s.graphics.lineStyle(_BoxThickness, _BoxColor, _BoxAlpha);
s.graphics.drawRect(_x1,_y1, (_x2-_x1), (_y2-_y1));
c.addChild(s);
//_view.myPanel.addElement(c);
_view.addElement(c);
}
///////////////////////// Listening Events CALLBACK functions //////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
private function mouseDownHandler(e:MouseEvent):void
{
// get selection box start coordinates
_x1 = e.stageX;
_y1 = e.stageY;
_clickFlag = true;
}
private function mouseMove(e:MouseEvent):void
{
// get selection box start coordinates while Mouse is moving
_x2 = e.stageX;
_y2 = e.stageY;
if (_clickFlag == true)
DrawSelectBox(); // redraw this box, while mouse button is down
}
private function mouseUpHandler(e:MouseEvent):void
{
var i:uint = 0;
var j:uint = 0;
// clear selection box
_clickFlag = false;
s.graphics.clear();
// now get selected objects
_totalItems = _view.objArray.length;
_SelectedItems = []; // flush selected components array
for (i=0; i<_totalItems; i++)
{
if ( (_view.objArray[i].x > _x1 && _view.objArray[i].x < _x2 && _view.objArray[i].y > _y1 && _view.objArray[i].y < _y2)
|| (_view.objArray[i].x > _x1 && _view.objArray[i].x < _x2 && _view.objArray[i].y > _y2 && _view.objArray[i].y < _y1)
|| (_view.objArray[i].x > _x2 && _view.objArray[i].x < _x1 && _view.objArray[i].y > _y1 && _view.objArray[i].y < _y2)
|| (_view.objArray[i].x > _x2 && _view.objArray[i].x < _x1 && _view.objArray[i].y > _y2 && _view.objArray[i].y < _y1) )
{
_SelectedItems[j] = "Objet "+i.toString();
j++;
}
}
// fill datagrid with selected componenents...
_view.dgrid.dataProvider = _SelectedItems;
}
}
} |