topical media & game development
graphic-flex-diagram-class-Designer.ax
graphic-flex-diagram-class-Designer.ax
[swf]
flex
package
{
import mx.collections.ArrayCollection;
import mx.containers.Canvas;
import flash.events.MouseEvent;
import mx.core.UIComponent;
import mx.rpc.events.AbstractEvent;
import mx.controls.Alert;
public class @ax-graphic-flex-diagram-class-Designer
{
private var boxes:ArrayCollection = new ArrayCollection();
private var lines:ArrayCollection = new ArrayCollection();
private var designArea:Canvas;
private var isDrawEnable:Boolean = false;
private var isDrawing:Boolean = false;
private var templateLine:graphic_flex_diagram_class_Line;
private var currentFromBox:graphic_flex_diagram_class_Box;
private var currentToBox:graphic_flex_diagram_class_Box;
// designer is a manager class.
public function @ax-graphic-flex-diagram-class-Designer(){
templateLine = new graphic_flex_diagram_class_Line();
templateLine.name="templateLine";
templateLine.visible = false;
}
public function setIsDrawEnable(value:Boolean):void{
this.isDrawEnable = value;
}
public function getIsDrawEnable():Boolean{
return this.isDrawEnable;
}
public function setCurrentFromBox(value:graphic_flex_diagram_class_Box):void{
this.currentFromBox = value;
}
public function setCurrentToBox(value:graphic_flex_diagram_class_Box):void{
this.currentToBox = value;
}
public function getBoxList():ArrayCollection{
return boxes;
}
public function getLineList():ArrayCollection{
return lines;
}
private function getId(type:String):String{
var idString:String = type + Math.round(Math.random()*10000).toString();
return idString;
}
// set design area and adding desing area events.
public function setDesignArea(value:Canvas):void{
this.designArea = value;
designArea.addChild(templateLine);
designArea.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
designArea.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
}
// this method checks mouse up event on the box or not .
// if not cancel drawing.
// if there is no excpation add lines
public function mouseUp(event:MouseEvent):void{
if (isDrawEnable){
try{
if (graphic_flex_diagram_class_Box(event.currentTarget)!= null){
addLine();
}
}
catch(e:Error){
cancelDrawing();
}
}
}
//when mouse move on the design area drawing template line if isDrawing true
public function mouseMove(event:MouseEvent):void{
if (isDrawing){
drawLine();
}
}
// create box
public function addBox(x:int, y:int):void{
var newBox:graphic_flex_diagram_class_Box = new graphic_flex_diagram_class_Box();
var id:String = getId("Box");
newBox.setDesigner(this);
newBox.create(x,y,id);
boxes.addItem(newBox);
designArea.addChild(newBox);
}
// this method works when mouse down on the box
// it sets first point coordinae of template line
public function prepareDrawing():void{
if (isDrawEnable){
templateLine.graphics.clear();
templateLine.setX1(designArea.mouseX);
templateLine.setY1(designArea.mouseY);
templateLine.visible = true;
isDrawing = true;
}
}
// this methods set second coordinate of template line and draw
public function drawLine():void{
if (isDrawing){
templateLine.setX2(designArea.mouseX);
templateLine.setY2(designArea.mouseY);
templateLine.draw();
}
}
// this methods create new line and add line to box and designer
public function addLine():void{
if (isDrawing){
var newLine:graphic_flex_diagram_class_Line = new graphic_flex_diagram_class_Line();
newLine.setId(getId("Line"));
newLine.setFromBox(currentFromBox);
newLine.setToBox(currentToBox);
newLine.draw();
currentFromBox.addFromLine(newLine);
currentToBox.addToLine(newLine);
lines.addItem(newLine);
designArea.addChild(newLine);
templateLine.visible = false;
isDrawEnable = false;
isDrawing = false;
}
else{
cancelDrawing();
}
}
// this methods cancel drawing
public function cancelDrawing():void{
templateLine.visible = false;
isDrawEnable = false;
isDrawing = false;
}
}
}
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.