您的位置:首页 > 其它

flex 开发卡片主类一

2008-11-11 10:09 162 查看
package code

{

/**

* 产品开发基本类

*/

import flash.display.Bitmap;

import flash.display.BitmapData;

import flash.display.Loader;

import flash.events.Event;

import flash.events.MouseEvent;

import flash.geom.Matrix;

import flash.net.URLRequest;

import module.TextEditview;

import module.photEditview;

import mx.containers.Canvas;

import mx.controls.Image;

import mx.controls.TextArea;

import mx.core.Container;

import mx.managers.PopUpManager;

import mx.rpc.events.ResultEvent;

import mx.rpc.http.mxml.HTTPService;

public class ProductHandlerBasic extends Container

{

/*当前所编辑产品*/

private var _currentStage:Container ;

private var _productID:Number ;

/*所保存作品的ID,如果此产品没保存过这为-1*/

private var _recordID:Number=-1 ; ;

/*用户选择的模板图片,在进入操作页面之前,用户需要选择模板*/

private var _defaulModuleUrl:String ;

/*保存产品所以页面的内容,例如模板,照片,文字。以xml到形式存放*/

private var _productDataContent:XML ;

/*产品信息数据,每个型号所对应的价格,名称等等*/

private var _productInfo:XML ;

public var _testTextArea:TextArea ;

/*当前主题的所有数据坐标数据*/

private var _photoCoordinateData:XML ;

private var _httpservice:HTTPService ;

/*照片在模板上的位置(顺序)*/

private var _currentphotoNum:Number =0 ; ;

/*每一张的顺序,如:上一张,下一张*/

private var _currentpageNum:Number =0 ;

/*当前页面内容数据,由版式,照片文件组成*/

private var _currentpageData:XML ;

/*当前页面的坐标数据*/

private var _currentcoordinateData:XMLList ;

/*显示模板,照片及一些操作*/

private var _userstage:Canvas ;

/*当前的模板图片*/

private var _backgroundImage:Image= new Image() ;

/*产品innerNumber*/

private var _productInnerNum:Number = 0 ;

public function ProductHandlerBasic()

{

_httpservice = new HTTPService();

_httpservice.showBusyCursor = true ;

_httpservice.method = "post" ;

_httpservice.url = "/HttpServiceSlt";

_httpservice.resultFormat="xml";

_httpservice.addEventListener(ResultEvent.RESULT,createEditProudctContent);

/*加入事件监听,照片切换及照片编辑时,要用到此*/

_backgroundImage.addEventListener(MouseEvent.CLICK,photoClickHandler);

_backgroundImage.addEventListener(MouseEvent.DOUBLE_CLICK,photoDoubleClickHandler);

_backgroundImage.doubleClickEnabled = true ;

}

/**

* 作品封面图片地址

*/

public function get previewURL():String{

return _productInfo.product.(@id==_productID).previewURL ;

}

/**

* 产品单价

*/

public function get price():Number{

return _productInfo.product.(@id==_productID).price ;

}

/**

* 价格单位

*/

public function get unit():String{

return _productInfo.product.(@id==_productID).unit ;

}

/**

* 得到型号的总页数

*/

public function get pages():Number{

return _productInfo.product.(@id==_productID)..label.length();

}

/**

* 增加邮票或信封的价格

*/

public function get stepPrice():Number{

return _productInfo.product.(@id==_productID)..stepprice;

}

/**

* 复合价格。例如加上信封和邮票的价格。

* 计算方法:基价+步价*innernumber

*/

public function get duplexPrice():Number{

return Math.round((this.productInnerNum*stepPrice+price)*100)/100 ;

}

/**

* 获取型号下的主题信息.包括名称,数量等等.

*/

public function get mbClass():XMLList{

return _productInfo.product.(@id==_productID)..subject;

}

/**

* 型号。如:明星片,长贺卡

*

*/

public function get typename():String{

return _productInfo.product.(@id==_productID).file ;

}

public function get productInnerNum():Number{

return _productDataContent.innernumber;

}

public function set productInnerNum(p:Number):void{

_productDataContent.innernumber = p;

}

/**

* 产品所属类别

*/

public function get classid():String{

return _productInfo.classid.@id ;

}

public function set currentStage(c:Container):void{

this._currentStage = c ;

}

public function get currentStage():Container{

return this._currentStage ;

}

public function set productID(p:Number):void{

this._productID = p ;

}

public function get productID():Number{

return this._productID ;

}

public function set recordID(r:Number):void{

this._recordID= r ;

}

public function get recordID():Number{

return this._recordID ;

}

public function set defaulModuleUrl(url:String):void{

this._defaulModuleUrl = url ;

}

public function get defaulModuleUrl():String{

return this._defaulModuleUrl ;

}

public function set productInfo(info:XML):void{

this._productInfo = info ;

}

public function get productInfo():XML{

return this._productInfo ;

}

public function set httpservice(h:HTTPService):void{

this._httpservice = h ;

}

public function get httpservice():HTTPService{

return this._httpservice ;

}

public function set photoCoordinateData(p:XML):void{

this._photoCoordinateData = p ;

}

public function get photoCoordinateData():XML{

return this._photoCoordinateData ;

}

public function set userstage(s:Canvas):void{

this._userstage = s ;

}

public function get userstage():Canvas{

return this._userstage ;

}

public function set currentpageNum(c:Number):void{

this._currentpageNum = c ;

}

public function get currentpageNum():Number{

return this._currentpageNum ;

}

public function set currentphotoNum(p:Number):void{

this.currentphotoNum = p ;

}

public function get currentphotoNum():Number{

return this._currentphotoNum ;

}

public function set productDataContent(p:XML):void{

this.productDataContent = p ;

}

public function get productDataContent():XML{

return this._productDataContent ;

}

/**

* 模板单击处理,检测鼠标的位置计算出,具体单击的照片

*/

private function photoClickHandler(e:MouseEvent):void{

var img :Image = null ;

for(var i:int=0;i<this._currentcoordinateData.pic.length();i++){

img = this._userstage.getChildAt(i) as Image ;

revertPhotoAlpha();

if(img.hitTestPoint(mouseX,mouseY)){

this._currentphotoNum = i ;//当前照片的号码

img.alpha = 0.5 ;

break ;

}

}

}

/**

* 双击照片,弹出照片编辑窗口

*/

private function photoDoubleClickHandler(e:MouseEvent):void{

var img_ :Image = null ;

for(var i:int=0;i<this._currentcoordinateData.pic.length();i++){

img_ = this._userstage.getChildAt(i) as Image ;

revertPhotoAlpha();

if(img_.hitTestPoint(mouseX,mouseY)){

this._currentphotoNum = i ;

this.revertPhotoAlpha() ;

var url_:String = this._productDataContent.item[_currentpageNum]..img[i].@url ;

var x_ :Number = this._productDataContent.item[_currentpageNum]..img[i].@x ;

var y_ :Number = this._productDataContent.item[_currentpageNum]..img[i].@y ;

var w_ :Number = this._productDataContent.item[_currentpageNum]..img[i].@width ;

var h_ :Number = this._productDataContent.item[_currentpageNum]..img[i].@height ;

/*照片编辑框*/

var photoEdit_:photEditview = new photEditview();

photoEdit_.producthandlerBase = this ;

PopUpManager.addPopUp(photoEdit_,this._currentStage,true);

photoEdit_.initphotoEditParams(img_,url_,x_,y_,w_,h_);

break ;

}

}

}

/**

* 恢复照片透明度。用户单击照片,改变单击的照片的透明度 ,以此来做标识 。

* 当用户切换照片时,恢复照片透明度,再改变所选照片的透明度。

*/

private function revertPhotoAlpha():void{

var img:Image = null ;

for(var i:int=0;i<this._currentcoordinateData.pic.length();i++){

img = this._userstage.getChildAt(i) as Image ;

if(img.alpha==0.5){

img.alpha = 1 ;

break ;

}

}

}

/**

* 生成默认的产品内容,没过型号都不相同,所以动态生成。

* 如果产品已经保存过的话,内容从服务端加载过来

*/

public function createProductContent():void{

if(this._recordID==-1){

this.createNewProudctContent() ;

return ;

}

//编辑产品

var params:Object = new Object() ;

params.methodName = "getProductContent" ;

params.random= Math.random() ;

params.pid= this._recordID ;

//this._testTextArea.text = "recordid=="+this._recordID ;

this._httpservice.send(params) ;

}

/*创建默认产品数据结构*/

private function createNewProudctContent():void{

_productDataContent = <wdxc></wdxc> ;

/*得到此种型号的产品有多少页数*/

var xmllist_t:XML = new XML(this._productInfo.product.(@id==_productID));

var count:Number = xmllist_t.count ;

for(var i:int=0;i<count;i++){

/*每一张记录节点*/

var itemNode:XML = <item></item>;

var photoNode:XML =<photo></photo>;

for(var j:int=0;j<13;j++){

photoNode.appendChild(<img url="" x="-1" y="" width="" height=""/>);

}

/*豪华折叠卡没办法,这能这样*/

if(this._productID==598){

itemNode.appendChild(<bs>{_defaulModuleUrl.replace(".gif","_"+(i+1)+".gif")}</bs>);

}else{

itemNode.appendChild(<bs>{this._defaulModuleUrl}</bs>);

}

itemNode.appendChild(photoNode) ;

itemNode.appendChild(<txt><info></info></txt>);

_productDataContent.appendChild(itemNode);

}

_productDataContent.appendChild(<innernumber>0</innernumber>);

//创建是否需要邮票和信封组件

createOtherCom();

this.initCurrentPageInfoToStage(true);

}

/**如果产品已经保存,将从productRecord表中,加载数据。

* 而不是用订单表中取得信息【编辑产品时】

*/

private function createEditProudctContent(e:ResultEvent):void{

_productDataContent = new XML(e.result);

createOtherCom();

this.initCurrentPageInfoToStage(false);

}

private var _defaultText:String = "单击此处输入文字" ;

/**

* 初始化当前参数信息

* 1:加载模板图片

* 2:加载对应的图片

* @param recompute

* 左边是否重新计算

*/

public function initCurrentPageInfoToStage(recompute:Boolean):void{

this._currentphotoNum = 0 ;

/*得到当前页的数据信息.*/

_currentpageData = new XML(this._productDataContent.item[this._currentpageNum]) ;

this._userstage.removeAllChildren();

/*根据图片在模板那的坐标,生成容器,可单击图片而放入容器中*/

for(var i:int=0;i<getCurrentPhotoCount(_currentpageNum);i++){

var img1_t:Image = new Image() ;

this._userstage.addChild(img1_t);

img1_t.name="img_"+i;

img1_t.x = _currentcoordinateData.pic[i].@x ;

img1_t.y = _currentcoordinateData.pic[i].@y ;

img1_t.width = _currentcoordinateData.pic[i].@width ;

img1_t.height = _currentcoordinateData.pic[i].@height ;

img1_t.scaleContent = true ;

/*加载图片到模板相应的位置*/

this.loadphotoToStage(img1_t,i,recompute,_productDataContent.item[_currentpageNum].photo.img[i].@url);

}

_backgroundImage.source = (this._productDataContent.item[this._currentpageNum].bs).toString();

this._userstage.addChild(_backgroundImage);

/*加入用户输入的文字信息*/

for(var k:int=0;k<_currentcoordinateData.txt.length();k++){

var textInfo_:TextArea = new TextArea() ;

textInfo_.alpha = 0 ;

textInfo_.setStyle("fontSize","12");

textInfo_.name = "txt_"+k ;

textInfo_.x = _currentcoordinateData.txt[k].@x ;

textInfo_.y = _currentcoordinateData.txt[k].@y ;

textInfo_.width= _currentcoordinateData.txt[k].@width ;

textInfo_.height = _currentcoordinateData.txt[k].@height ;

textInfo_.editable = false ;

if(_productDataContent.item[_currentpageNum].txt.info[k]==""||

_productDataContent.item[_currentpageNum].txt.info[k]==null){

textInfo_.text = _defaultText ;

}else{

textInfo_.htmlText = _productDataContent.item[_currentpageNum].txt.info[k] ;

}

textInfo_.addEventListener(MouseEvent.CLICK,openTextEditview) ;

this._userstage.addChild(textInfo_);

}

}

/**

* 打开文字编辑窗口

*

*/

public function openTextEditview(e:MouseEvent):void{

var textInfo_:TextArea = TextArea(e.currentTarget);

var tev_ :TextEditview = new TextEditview() ;

PopUpManager.addPopUp(tev_,_currentStage,true,null);

tev_.targetLabelName = textInfo_.name ;

tev_.phander = this ;

var n_:Number = textInfo_.name.split("_")[1]; ;

/*打开文字编辑窗口*/

tev_.my_rte.htmlText =_productDataContent.item[_currentpageNum].txt.info[n_] ;

tev_.my_rte.width = textInfo_.width ;

tev_.my_rte.height =textInfo_.height ;

tev_.my_rte.maxWidth = textInfo_.width ;

tev_.my_rte.maxHeight = textInfo_.height ;

tev_.x = 100 ;

tev_.y = 100 ;

}

/**

* 文字编辑完毕保存用户输入的文字 。

*

*/

public function saveUserInputText(html_:String,txt_:String,labelname_:String):void{

var lab_:TextArea = this._userstage.getChildByName(labelname_) as TextArea;

if(txt_==null||txt_.length==0||txt_==""){

lab_.text= _defaultText ;

} else{

lab_.htmlText = html_ ;

var n_:Number = labelname_.split("_")[1]; ;

_productDataContent.item[_currentpageNum].txt.info[n_] = html_ ;

}

}

/**

* 当前模板最大能填充照片的张数。 自动填充照片时,需要用此 。

*/

public function getCurrentPhotoCount(num_:Number):Number{

/*得到当前的模板名称,通过此查询其坐标数据*/

var a1:Array = this._productDataContent.item[num_].bs.split("/");

var picname:String = a1[a1.length-1];

/*得到当前的坐标数据.例如:<pic x="98" y="10" width="112" height="111"/>*/

_currentcoordinateData=_photoCoordinateData.subject.mb.(@picname==picname);

return this._currentcoordinateData.pic.length();

}

/**

* 当前模板文字输入框坐标个数。

*/

public function getCurrentTextCount(num_:Number):Number{

/*得到当前的模板名称,通过此查询其坐标数据*/

var a1:Array = this._productDataContent.item[num_].bs.split("/");

var picname:String = a1[a1.length-1];

/*得到当前的坐标数据.例如:<pic x="98" y="10" width="112" height="111"/>*/

_currentcoordinateData=_photoCoordinateData.subject.mb.(@picname==picname);

return this._currentcoordinateData.txt.length();

}

/**

* 加载图片到舞台上,图片显示区域由模板框大小决定。模板的最大边为准。

* 用户照片显示区域的坐标.照片编辑对照片裁剪的坐标,首次没有坐标,则

* 取用户图片的中心位置如果用户已经裁剪,则有坐标,根据坐标裁剪图片。

* @param img:当前操作的图片对象。相册列表中所选择的照片将在此显示

* @param num:当前图片在模板上的顺序号码。

* @param recompute 坐标是否需要重新计算。如:改变模板时,许重算

* @param targerurl 用户在相册列表选择的图片地址。

*

*/

public function loadphotoToStage(targetimg:Image,num:Number,recompute:Boolean,targerurl:String):void{

if(targerurl==null||targerurl==""){

return ;

}

/*把当前的所操作的图片地址放入产品内容xml中*/

//this._testTextArea.text ="url=="+targerurl+"targerurl.length"+targerurl.length;

/*得到当前的对象的.以在匿名函数中使用*/

var ph_t:ProductHandlerBasic = this ;

var loader_t:Loader = new Loader();

loader_t.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{

/*从产品内容数据中取得裁剪框在图片上的坐标*/

var width1:Number=0 ;

var height1:Number=0;

var x1:Number =0 ;

var y1:Number =0 ;

x1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@x ;

y1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@y ;

width1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@width ;

height1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@height ;

/*图片原始数据*/

var oldbitmap_t:Bitmap = new Bitmap() ;

var l:Loader = e.target.loader as Loader;

var oldbitmap_tdata:BitmapData = new BitmapData (l.width,l.height) ;

oldbitmap_tdata.draw(l);

/*是否需要重新计算坐标*/

if(recompute||x1==-1){

/*计算出模板中图片框的比例。根据此比例才能算出裁剪框的最大值*/

var scale_t:Number =targetimg.width/targetimg.height ;

/*根据比例求出高*/

var h2:Number = l.width/scale_t ;

if(h2<=l.height){

height1 = h2 ;

width1 = l.width ;

}else{

height1 = l.height ;

width1 = height1*scale_t ;

}

/*裁剪框在照片的坐标*/

x1 = (l.width-width1)/2 ;

y1 = (l.height-height1)/2;

ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@x = x1 ;

ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@y = y1 ;

ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@width = width1 ;

ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@height =height1 ;

ph_t._productDataContent.item[ph_t._currentpageNum]..img[num].@url = targerurl ;

}

/*存放从原始图片数据里裁剪出来的图片数据*/

var newbitmap_t:BitmapData = new BitmapData(width1,height1);

//坐标转化,把坐标移到裁剪区域的位置,宽度和高度在cutbitmapdata里指定。

var matrix:Matrix = new Matrix(1, 0, 0, 1, -x1, -y1);

newbitmap_t.draw(oldbitmap_tdata, matrix);

oldbitmap_t.bitmapData = newbitmap_t ;

targetimg.source = oldbitmap_t ;

targetimg.scaleContent = true ;

ph_t._currentphotoNum++ ;

});

loader_t.load(new URLRequest(targerurl));

}

/**

* 更换模板,改变之后重新导入当前页面信息

* 先改变内容数据,再重新加载

*/

public function moduleChange(url:String):void{

//豪华折叠卡一次换3张

if(this.typename=="replicatecard"){

this._productDataContent.item[0].bs = url.replace(".gif","_1.gif") ;

this._productDataContent.item[1].bs = url.replace(".gif","_2.gif") ;

this._productDataContent.item[2].bs = url.replace(".gif","_3.gif") ;

} else{

this._productDataContent.item[this._currentpageNum].bs = url ;

}

initCurrentPageInfoToStage(true);

}

/**

* 单击相册照片,按顺序填充照片到模板相应的位置 。

* @param url :被单击照片url

*/

public function albumPhotoClickHandler(url:String):void{

//======当前模板的图片个数

var t:Number = _currentcoordinateData.pic.length();

if(this._currentphotoNum>t-1){

this._currentphotoNum = 0 ;

}

var img:Image = this._userstage.getChildByName("img_"+this._currentphotoNum) as Image ;

if(img==null){

return ;

}

this.loadphotoToStage(img,this._currentphotoNum,true,url);

}

/**

* 保存当前页数据,如:图片坐标

* @param p1_ 当前页号码

* @param p2_ 当前照片号码

* @param x_ 起点坐标

* @param y

* @param w_

* @param h_

*/

public function saveCurrentPageData(p1_:Number,p2_:Number,url_:String,x_:Number,y_:Number,w_:Number,h_:Number):void{

_productDataContent.item[p1_]..img[p2_].@x =x_ ;

_productDataContent.item[p1_]..img[p2_].@y =y_ ;

_productDataContent.item[p1_]..img[p2_].@width =w_ ;

_productDataContent.item[p1_]..img[p2_].@height =h_ ;

_productDataContent.item[p1_]..img[p2_].@url=url_ ;

}

/**

* 创建其他额外的组件,此处初始化信封和邮件列表

*/

public function createOtherCom():void{

//card.MAIN.right_id.sendAlbumInfoRequest();

if(this.productInnerNum==1){

card.MAIN.radbut1.selected = true ;

}else{

card.MAIN.radbut2.selected = true ;

}

card.MAIN.header_id.recomputePrice();

card.MAIN.right_id.initAlbum();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: