Flex ObjectHandles 构建绘图程序!
模型
主画布组件:com/components/graph/GraphContainer.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init(event)"
width="100%" height="100%">
<fx:Script>
<![CDATA[
import com.components.model.ShapeModel;
import com.roguedevelopment.objecthandles.Flex4ChildManager;
import com.roguedevelopment.objecthandles.Flex4HandleFactory;
import com.roguedevelopment.objecthandles.HandleClickedEvent;
import com.roguedevelopment.objecthandles.ObjectChangedEvent;
import com.roguedevelopment.objecthandles.ObjectHandles;
import com.roguedevelopment.objecthandles.SelectionEvent;
import com.components.shape.BaseShape;
import com.components.shape.IShape;
import com.components.shape.ShapeLoad;
import com.components.shape.SimpleRect; import mx.controls.Alert;
import mx.controls.Button;
import mx.core.UIComponent;
import mx.events.DragEvent;
import mx.events.FlexEvent;
import mx.managers.DragManager;
[Bindable]
public var objecthandles:ObjectHandles = null; override protected function initializationComplete() : void
{
objecthandles = new ObjectHandles( mainGroup , null, new Flex4HandleFactory() ,
new Flex4ChildManager() );
super.initializationComplete();
} private function init(event:FlexEvent):void
{
this.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
if(event.target is IShape) {
if(objecthandles.selectionManager.currentlySelected.length > 1){
objecthandles.selectionManager.clearSelection();
}
}else{
objecthandles.selectionManager.clearSelection();
}
event.stopPropagation();
});
} protected function mainGroup_dragDropHandler(event:DragEvent):void
{
// TODO Auto-generated method stub } public function addShapeUI(shape:ShapeModel):void
{
var ishape:IShape = ShapeLoad.getShape(shape.shapeType);
ishape.graph = this;
ishape.model = shape; shape.ishape = ishape;
if (shape.owner) {
var container:IShape = shape.owner.ishape if (container) {
// Alert.show(container.model.label);//alert Test
var oh:ObjectHandles = new ObjectHandles(container as Sprite, objecthandles.selectionManager, new Flex4HandleFactory(), new Flex4ChildManager());
oh.registerComponent(shape, ishape, []); container.addShape(ishape);
} else {
Alert.show("null");
}
} else { mainGroup.addElement(ishape); objecthandles.registerComponent(shape, ishape); //shapeModel Bindable #1069
} if (shape.children && shape.children.length > 0) {
for (var i:int=0;i<shape.children.length;i++) {
var child:ShapeModel = shape.children[i];
addShapeUI(child);
}
} ishape.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
// ishape.objectHandles.selectionManager.setSelected(shape);
event.stopPropagation(); //important
}); ishape.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void{
// ishape.objectHandles.selectionManager.setSelected(shape);
event.stopPropagation(); //important
}); } public function removeShapeUI(shape:ShapeModel):void
{
if (shape.owner) {
var container:IShape = shape.owner.ishape
container.removeChildByModel(shape);
} else {
for (var i:int=0;i<mainGroup.numElements;i++) {
var ishape:IShape = mainGroup.getElementAt(i) as IShape; if (ishape.model == shape) {
objecthandles.unregisterComponent(ishape);
mainGroup.removeElement(ishape);
}
}
}
} ]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations> <s:Scroller width="100%" height="100%" >
<s:Group width="100%" height="100%" id="mainGroup"> </s:Group>
</s:Scroller>
</s:Group>
GraphModel.as: com/components/model/
package com.components.model
{ public class GraphModel
{
public function GraphModel()
{ }
}
}
ShapeModel.as: 图形模型类,用于设置图形宽、高、坐标等参数
package com.components.model
{
import com.roguedevelopment.objecthandles.IMoveable;
import com.roguedevelopment.objecthandles.IResizeable;
import com.components.shape.IShape; import flash.events.EventDispatcher;
import flash.events.MouseEvent; import mx.messaging.AbstractConsumer; public class ShapeModel extends EventDispatcher implements IResizeable, IMoveable
{
[Bindable]
private var _width:Number = 0;
[Bindable]
private var _height:Number = 0;
[Bindable]
private var _x:Number = 0;
[Bindable]
private var _y:Number = 0;
[Bindable]
private var _rotation:Number = 0;
[Bindable]
private var _label:String = '';
[Bindable]
private var _isLocked:Boolean = false;
[Bindable]
private var _color:uint = 0xffffff;
[Bindable]
private var _image:String;
[Bindable]
private var _ishape:IShape;
[Bindable]
private var _owner:ShapeModel = null;
[Bindable]
private var _children:Array;
[Bindable]
private var _shapeType:int = 0; public function ShapeModel()
{ } public function set width(value:Number):void
{
this._width = value;
} [Bindable]
public function get width():Number
{
return this._width;
} public function set height(value:Number):void
{
this._height = value;
} [Bindable]
public function get height():Number
{
return this._height;
} public function set x(value:Number):void
{
this._x = value;
} [Bindable]
public function get x():Number
{
return this._x;
} public function set y(value:Number):void
{
this._y = value;
} [Bindable]
public function get y():Number
{
return this._y;
} public function set rotation(value:Number):void
{
this._rotation = value;
} [Bindable]
public function get rotation():Number
{
return this._rotation;
} public function set label(value:String):void
{
this._label = value;
} [Bindable]
public function get label():String
{
return this._label;
} public function set isLocked(value:Boolean):void
{
this._isLocked = value;
} [Bindable]
public function get isLocked():Boolean
{
return this._isLocked;
} //-------------------- [Bindable]
public function get ishape():IShape
{
return this._ishape;
} public function set ishape(value:IShape):void
{
this._ishape = value;
} public function set children(childs:Array):void
{
this._children = childs; if (childs && childs.length > 0) {
var c:* = childs[0]; //检测是不是ShapeModel类型
if (c is ShapeModel){
for each(var itm:ShapeModel in childs) {
itm.owner = this;
}
}
}
} [Bindable]
public function get children():Array
{
return this._children;
} public function set owner(value:ShapeModel):void
{
this._owner = value;
} [Bindable]
public function get owner():ShapeModel
{
return this._owner;
} // public function set color(value:uint):void
{
this._color = value;
} [Bindable]
public function get color():uint
{
return this._color;
} public function set image(value:String):void
{
this._image = value;
} [Bindable]
public function get image():String
{
return this._image;
} public function set shapeType(value:int):void
{
this._shapeType = value;
} [Bindable]
public function get shapeType():int {
return this._shapeType;
} public function setSelected(flag:Boolean):void {
if (this._ishape) {
if (flag) {
this._ishape.objectHandles.selectionManager.setSelected(this);
} else {
this._ishape.objectHandles.selectionManager.removeFromSelected(this);
}
}
}
}
}
图形基组件: com/components/shape/BaseShape.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
implements="com.components.shape.IShape"
creationComplete="init(event)"
width="{_model.width}"
height="{_model.height}"
x="{_model.x}"
y="{_model.y}"
rotation="{_model.rotation}" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:SolidColor id="backColor" color="0xdcdcdc" alpha="1" />
<s:SolidColorStroke id="borderStroke" color="0x000000" weight="1" alpha="1" />
</fx:Declarations> <fx:Script>
<![CDATA[
import com.components.graph.GraphContainer;
import com.components.model.ShapeModel;
import com.events.ShapeEvent;
import com.roguedevelopment.objecthandles.Flex4ChildManager;
import com.roguedevelopment.objecthandles.Flex4HandleFactory;
import com.roguedevelopment.objecthandles.ObjectHandles;
import com.utils.ShapeUtils; import mx.controls.Alert;
import mx.events.FlexEvent; [Bindable]private var _objectHandles:ObjectHandles ;
[Bindable]private var _graph:GraphContainer;
[Bindable]private var _model:ShapeModel;
[Bindable]private var _showLabel:Boolean;
[Bindable]private var _selected:Boolean; private var _state:int = 0;
private var _rx:Number = 0;
private var _ry:Number = 0; private function init(event:FlexEvent):void
{
var that:IShape = this;
this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandle);
this.addEventListener(MouseEvent.MOUSE_OUT, mousOutHandle);
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandle);
//this.addEventListener(MouseEvent.MOUSE_WHEEL, mouWheelHandle); //从没出现
this.addEventListener(MouseEvent.ROLL_OUT, mouRolOutHandle);
this.addEventListener(MouseEvent.ROLL_OVER, mouRolOverHandle);
//this.addEventListener(MouseEvent.RELEASE_OUTSIDE, mouseOutsideHandle);//从没出现
} private function mouseDownHandle(event:MouseEvent):void { _rx = this.mouseX;
_ry = this.mouseY;
this.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandle);
this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandle); } //
private function mousOutHandle(event:MouseEvent):void
{
var cm:ShapeModel = this.model;
if (cm) {
if (cm.x < 0) {
cm.x = 0;
} if (cm.y < 0) {
cm.y = 0
}
}
this.model.label = "MouseOut"; } private function mouseOutsideHandle(event:MouseEvent):void
{
this.model.label = "Outside";
} private function mouseOverHandle(event:MouseEvent):void
{
this.model.label = "MouseOver";
ResizeContainer(this);
} private function mouWheelHandle(event:MouseEvent):void
{
this.model.label = "MouseWheel";
} private function mouRolOutHandle(event:MouseEvent):void
{
var cm:ShapeModel = this.model;
if (cm) {
if (cm.x < 0) {
cm.x = 0;
} if (cm.y < 0) {
cm.y = 0
}
} this.model.label = "RollOut";
ResizeContainer(this); } private function mouRolOverHandle(event:MouseEvent):void
{
this.model.label = "MouseRollOver";
} private function mouseMoveHandle(event:MouseEvent):void {
if (_state != 6) {
_state = 6; }
this.model.label = "MouseMove"; ResizeContainer(this, event);
} private function mouseUpHandle(event:MouseEvent):void {
if (_state != 7) {
_state = 7;
} this.model.label = "UP";
this.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandle);
this.removeEventListener(MouseEvent.MOUSE_DOWN, mouseMoveHandle);
// this.removeEventListener(MouseEvent.MOUSE_OUT, mousOutHandle);
// this.removeEventListener(MouseEvent.ROLL_OUT, mouRolOutHandle);
// this.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandle);
var cm:ShapeModel = this.model; if (!cm || !cm.owner)
return; /*
var xy:Object = ShapeUtils.getRealShapePosition(cm); var _pw:Number = cm.owner.width;
var _ph:Number = cm.owner.height;
var _px:Number = cm.owner.x;
var _py:Number = cm.owner.y; if (cm.x<0) {
cm.owner.x = Number(xy.x);
cm.owner.width = _pw + (_px - Number(xy.x));
cm.x = 0;
} if (cm.y <0) {
cm.owner.y = Number(xy.y);
cm.owner.height = _ph + (_py - Number(xy.y));
cm.y = 0;
}*/
} private function ResizeContainer(shapeui:IShape, event:MouseEvent = null):void
{
var cm:ShapeModel = shapeui.model;
if (cm && cm.owner) {
var _x:Number = cm.x;
var _y:Number = cm.y;
var _w:Number = cm.width;
var _h:Number = cm.height; var _pw:Number = cm.owner.width;
var _ph:Number = cm.owner.height;
var _px:Number = cm.owner.x;
var _py:Number = cm.owner.y; var xy:Object = ShapeUtils.getRealShapePosition(cm);
this.model.label = "Rs"; /*
//Resize Right, Down
if ((_x+_w) > _pw) {
cm.owner.width = _x + _w;
} if ((_y + _h) > _ph) {
cm.owner.height = _y + _h;
} //Resize Left , UP
if (_x < 0) {
cm.owner.x = _px + (_x);
cm.owner.width = _pw + (-_x);
} if (_y < 0) {
cm.owner.y = _py + (_y);
cm.owner.height = _ph + (-_y);
} */ } else {
this.model.label = "Where";
}
} public function get objectHandles():ObjectHandles
{
if (!this._objectHandles) {
this._objectHandles = new ObjectHandles(this, null, new Flex4HandleFactory(), new Flex4ChildManager());
} return this._objectHandles;
} public function set graph(value:GraphContainer):void
{
this._graph = value;
if (this._graph) {
this._objectHandles = this._graph.objecthandles;
}
} public function get graph():GraphContainer
{
return this._graph;
} public function set model(value:ShapeModel):void
{
this._model = value;
} [Bindable]
public function get model():ShapeModel
{
return this._model;
} public function set selected(value:Boolean):void
{
this._selected = value;
} public function get selected():Boolean
{
return this._selected;
} public function addShape(shapeui:IShape):void
{
ResizeContainer(shapeui);
} public function removeChildByModel(shape:ShapeModel):void { } public function set showLabel(value:Boolean):void
{
this._showLabel = value;
} public function get showLabel():Boolean
{
return this._showLabel;
} ]]>
</fx:Script> </s:Group>
Shape图形接口: IShape.as
package com.components.shape
{
import com.components.graph.GraphContainer;
import com.components.model.ShapeModel;
import com.roguedevelopment.objecthandles.ObjectHandles; import mx.core.IUIComponent;
import mx.core.IVisualElement;
import mx.core.IVisualElementContainer; public interface IShape extends IUIComponent, IVisualElement, IVisualElementContainer
{
function set graph(value:GraphContainer):void;
function get graph():GraphContainer;
function set model(value:ShapeModel):void;
function get model():ShapeModel;
function addShape(shapeui:IShape):void;
function removeChildByModel(shape:ShapeModel):void;
function set showLabel(value:Boolean):void;
function get showLabel():Boolean;
function get objectHandles():ObjectHandles;
//
function set selected(value:Boolean):void;
function get selected():Boolean;
}
}
图形ShapeModel与具体图形如SimpleRect.mxml[IShape]转换: ShapeLoad.as
package com.components.shape
{
public class ShapeLoad
{
public function ShapeLoad()
{
} public static function getShape(stype:int):IShape
{
var shape:BaseShape;
switch (stype) {
case 0:
shape = new SimpleRect();
break;
case 1:
shape = new SimpleEllipse();
break;
default:
break;
} return shape;
}
}
}
一个矩形图组件: com/components/shape/SimpleRect.mxml
<?xml version="1.0" encoding="utf-8"?>
<shape:BaseShape
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:shape="com.components.shape.*"
width="{model.width}"
height="{model.height}"
x="{model.x}"
y="{model.y}"
rotation="{model.rotation}" >
<fx:Script>
<![CDATA[
import com.components.model.ShapeModel; import mx.controls.Alert;
import mx.core.IVisualElement;
import mx.core.UIComponent;
import mx.events.FlexEvent; override public function addShape(shapeui:IShape):void
{
container.addElement(shapeui);
super.addShape(shapeui);
} override public function removeChildByModel(shape:ShapeModel):void
{
for (var i:int = 0; i<container.numElements;i++) {
var ishape:IShape = container.getElementAt(i) as IShape;
if (ishape && ishape.model == shape) {
objectHandles.unregisterComponent(ishape);
container.removeElement(ishape);
}
}
} override public function set showLabel(value:Boolean):void
{
super.showLabel = value;
} [Bindable]
override public function get showLabel():Boolean
{
return true;
} protected function scrollDown_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var itm:Object = event.target as DisplayObject; var tyName:String = itm.name;
if (tyName && tyName.indexOf('Button') >= 0) {
event.stopPropagation();
objectHandles.selectionManager.clearSelection();
} } protected function scrollUp_clickHandler(event:MouseEvent):void
{ } ]]>
</fx:Script> <s:Rect width="100%" height="100%" stroke="{borderStroke}" fill="{backColor}" />
<s:Scroller width="100%" height="100%" mouseDown="scrollDown_clickHandler(event)" mouseUp="scrollUp_clickHandler(event)">
<s:Group id="container" width="100%" height="100%" horizontalCenter="0" verticalCenter="0" />
</s:Scroller>
<s:Label text="{model.label}" horizontalCenter="0" verticalCenter="0" color="#000000" />
</shape:BaseShape>
使用方法Application: Flex4objectHandles.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%"
xmlns:graph="com.components.graph.*"
creationComplete="init(event)" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.components.model.ShapeModel; import mx.controls.Alert;
import mx.events.FlexEvent; private function init(event:FlexEvent):void
{
var shape:ShapeModel = new ShapeModel();
shape.shapeType = 1;
shape.width = 400;
shape.height = 300;
shape.x = 15;
shape.y = 30;
shape.label = "Test";
shape.rotation = 0; var childs:Array = [];
var child:ShapeModel = new ShapeModel();
child.shapeType = 0;
child.width = 100;
child.height = 100;
child.x = 60;
child.y = 90;
child.label = "child1";
child.rotation = 0;
childs.push(child); var _child:ShapeModel = new ShapeModel();
_child.shapeType = 0;
_child.width = 50;
_child.height = 50;
_child.x = 60;
_child.y = 90;
_child.label = "child1";
_child.rotation = 0;
child.children = [_child]; child = new ShapeModel();
child.shapeType = 1;
child.width = 50;
child.height = 50;
child.x = 70;
child.y = 10;
child.label = "child2";
child.rotation = 0;
childs.push(child); shape.children = childs; graph.addShapeUI(shape);
}
]]>
</fx:Script>
<s:SpriteVisualElement width="100%" height="100%" id="drawingLayer" />
<s:Group width="100%" height="100%">
<graph:GraphContainer id="graph" width="100%" height="100%"></graph:GraphContainer>
</s:Group>
</s:Application>
资源源码下载
Flex ObjectHandles 构建绘图程序!的更多相关文章
- 用html5的canvas和JavaScript创建一个绘图程序
本文将引导你使用canvas和JavaScript创建一个简单的绘图程序. 创建canvas元素 首先准备容器Canvas元素,接下来所有的事情都会在JavaScript里面. <canvas ...
- iOS-马上着手开发iOS应用应用程序-第二部分构建应用程序
第二部分构建应用程序 1,应用程序开发过程 2,设计用户界面 3,定义交互 4,教程:串联图 1,应用程序开发过程 定义概念 设计用户界面 定义交互 实现行为整合数据 对象是应用程序的基石 类是对象的 ...
- 实战案例--Grunt构建Web程序
GruntJS构建Web程序.使用Gruntjs来搭建一个前端项目,然后使用grunt合并,压缩JS文件,熟练了node.js安装和grunt.js安装后,接下来来实战一个案例,案例是根据snandy ...
- 简单的QT绘图程序(把全部的点都记录下来,然后在paintEvent里使用drawLine函数进行绘制,貌似效率很低。。。)
当初在学MFC时,最经典的入门实例就是绘图程序,其作用相当于Console Application 下的Hello World了吧. 如今入手QT,不免怀旧,于是也写了一个绘图程序,虽然简单,却也是入 ...
- Go基础系列:构建go程序
hello world 从一个简单的程序开始解释,将下面的内容放进test.go文件中,路径随意: package main import ( "fmt" ) func main( ...
- Maven构建应用程序常用配置(转)
来自:http://shiyanjun.cn/archives/180.html 使用Maven来构建应用程序,可以非常方便地管理应用相关的资源.众所周知,应用程序中涉及到的一些依赖关系,如Java应 ...
- wepy框架构建小程序(1)
wepy框架构建小程序(1) 基本操作: # 安装脚手架工具 npm install wepy-cli -g # 创建一个新的项目 npm init standard myproject # 进入新项 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发、集成和构建应用程序 总结
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发.集成和构建应用程序 总结 SharePoint开发已经 ...
- flex布局构建大屏框架并支持翻页动画、滚动表格功能
本文将利用flex属性构建大屏可视化界面.界面主要分标题栏.工具栏.数据可视化窗口.其中,翻页动画以及滚动表格功能分别分布在数据可视化界面两侧. 鼠标点击标题,可看到左侧窗口翻转动画: 整体布局效 ...
随机推荐
- iOS开发-微博客户端-基本界面搭建(01)
1>创建程序载入界面 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...
- db2 常用命令(一)
DB2数据库常用命令小结 ========操作数据库命令========== -- 启动数据库实例 #db2start -- 停止数据库实例 #db2sto # 如 ...
- linux—【用户和组的管理操作】(5)
用户:user 组:group 增加:add 修改:modify mod 删除:delete del useradd 增加用户 usermod 修改用户 userdel ...
- Oracle 性能维护一点
大数据量表的维护原则 1.如果此表经常CRUD,最好的办法是定期收集统计信息传递给oracle优化器,提高性能.例如dbms_stats.gather_table_stats命令. 可以做在ETL中, ...
- linux网络相关配置文件
linux系统一般来说分为两大类:1.RedHat系列:Redhat.Centos.Fedora等:2.Debian系列:Debian.Ubuntu等. linux系统中,TCP/IP网络是通过若干个 ...
- Expect 入门
一, Expect的作用 Expect广泛应用于交互式操作和自动化测试的场景之中,用来实现自动和交互式任务进行通信,而无需人的干预. 二, Ubuntu安装Expect sudo ...
- WinCE应用程序崩溃提示框的处理
WinCE的开发人员和WinCE设备的用户应该对下面这两个错误不陌生,"Application encountered a serious error and must shut down& ...
- JetBrain WebStorm 注册码
webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA ...
- ajax载入数据是小细节
今天看了一个点子: 在 ajax 导入数据的 div中添加一些样式,比如:我们正紧急抢救 增加趣味性,有解决数据卡壳问题
- Mac快捷键、命令行
睡眠:option + command + 电源键 立即关机:Cmd-Opt-Ctrl-Eject 立即重启:Cmd-Ctrl-Eject 弹出关机提示 :Ctrl + 关机 正常关机快捷键 : C ...