- 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();
- }
- }
- }
flex 開發卡片主類一
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.