天天看点

flex 开发卡片主类一

  1. package code
  2. {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Loader;
  6.     import flash.events.Event;
  7.     import flash.events.MouseEvent;
  8.     import flash.geom.Matrix;
  9.     import flash.net.URLRequest;
  10.     import module.TextEditview;
  11.     import module.photEditview;
  12.     import mx.containers.Canvas;
  13.     import mx.controls.Image;
  14.     import mx.controls.TextArea;
  15.     import mx.core.Container;
  16.     import mx.managers.PopUpManager;
  17.     import mx.rpc.events.ResultEvent;
  18.     import mx.rpc.http.mxml.HTTPService;  
  19.     public class ProductHandlerBasic extends Container
  20.     {
  21.         private var _currentStage:Container ;
  22.         private var _productID:Number ;
  23.         private var _recordID:Number=-1 ;  ; 
  24.         private var _defaulModuleUrl:String  ;
  25.         private var _productDataContent:XML ;
  26.         private var _productInfo:XML ;
  27.         public var _testTextArea:TextArea ;
  28.         private var _photoCoordinateData:XML ; 
  29.         private var _httpservice:HTTPService ;
  30.         private var _currentphotoNum:Number =0 ; ;
  31.         private var _currentpageNum:Number =0 ;
  32.         private var _currentpageData:XML ;
  33.         private var _currentcoordinateData:XMLList ;
  34.         private var _userstage:Canvas ;
  35.         private var _backgroundImage:Image= new Image() ;
  36.         private var _productInnerNum:Number = 0 ;
  37.         public function ProductHandlerBasic()
  38.         {
  39.             _httpservice = new HTTPService();
  40.             _httpservice.showBusyCursor = true ; 
  41.             _httpservice.method = "post" ;
  42.             _httpservice.url = "/HttpServiceSlt";
  43.             _httpservice.resultFormat="xml";
  44.             _httpservice.addEventListener(ResultEvent.RESULT,createEditProudctContent);
  45.             _backgroundImage.addEventListener(MouseEvent.CLICK,photoClickHandler);
  46.             _backgroundImage.addEventListener(MouseEvent.DOUBLE_CLICK,photoDoubleClickHandler);
  47.             _backgroundImage.doubleClickEnabled = true ; 
  48.         }
  49.         public function get previewURL():String{
  50.             return _productInfo.product.(@id==_productID).previewURL ;
  51.         }
  52.         public function get price():Number{
  53.             return _productInfo.product.(@id==_productID).price ;
  54.         }
  55.         public function get unit():String{
  56.             return _productInfo.product.(@id==_productID).unit ;
  57.         }
  58.         public function get pages():Number{ 
  59.             return _productInfo.product.(@id==_productID)..label.length();
  60.         }
  61.         public function get stepPrice():Number{
  62.             return _productInfo.product.(@id==_productID)..stepprice;
  63.         }
  64.         public function get duplexPrice():Number{
  65.             return Math.round((this.productInnerNum*stepPrice+price)*100)/100 ;
  66.         } 
  67.         public function get mbClass():XMLList{ 
  68.             return _productInfo.product.(@id==_productID)..subject;
  69.         }
  70.         public function get typename():String{
  71.             return _productInfo.product.(@id==_productID).file ;
  72.         }
  73.         public function get productInnerNum():Number{
  74.             return _productDataContent.innernumber;
  75.         }
  76.         public function set productInnerNum(p:Number):void{
  77.             _productDataContent.innernumber = p;
  78.         }
  79.         public function get classid():String{
  80.             return [email protected] ;
  81.         }
  82.         public function set currentStage(c:Container):void{
  83.             this._currentStage = c ;
  84.         }
  85.         public function get currentStage():Container{
  86.             return this._currentStage ;
  87.         }
  88.         public function set productID(p:Number):void{
  89.             this._productID = p ;
  90.         }
  91.         public function get productID():Number{
  92.             return this._productID ;
  93.         }
  94.         public function set recordID(r:Number):void{
  95.             this._recordID= r ;
  96.         }
  97.         public function get recordID():Number{
  98.             return this._recordID ;
  99.         } 
  100.         public function set defaulModuleUrl(url:String):void{
  101.             this._defaulModuleUrl = url ;
  102.         }  
  103.         public function get defaulModuleUrl():String{
  104.             return this._defaulModuleUrl ;
  105.         }   
  106.         public function set productInfo(info:XML):void{
  107.             this._productInfo = info ;
  108.         }    
  109.         public function get productInfo():XML{
  110.             return this._productInfo ;
  111.         }   
  112.         public function set httpservice(h:HTTPService):void{
  113.             this._httpservice = h ;
  114.         }   
  115.         public function get httpservice():HTTPService{
  116.             return this._httpservice ;
  117.         }
  118.         public function set photoCoordinateData(p:XML):void{
  119.             this._photoCoordinateData = p ;
  120.         }
  121.         public function get photoCoordinateData():XML{
  122.             return this._photoCoordinateData ;
  123.         }
  124.         public function set userstage(s:Canvas):void{
  125.             this._userstage = s ;
  126.         }  
  127.         public function get userstage():Canvas{
  128.             return this._userstage ;
  129.         }
  130.         public function set currentpageNum(c:Number):void{
  131.             this._currentpageNum = c ;
  132.         }
  133.         public function get currentpageNum():Number{
  134.             return this._currentpageNum ;
  135.         } 
  136.         public function set currentphotoNum(p:Number):void{
  137.             this.currentphotoNum = p ;
  138.         } 
  139.         public function get currentphotoNum():Number{
  140.             return this._currentphotoNum ;
  141.         }
  142.         public function set productDataContent(p:XML):void{
  143.             this.productDataContent = p ;
  144.         } 
  145.         public function get productDataContent():XML{
  146.             return this._productDataContent ;
  147.         }
  148.         private function photoClickHandler(e:MouseEvent):void{
  149.             var img :Image = null ;
  150.             for(var i:int=0;i<this._currentcoordinateData.pic.length();i++){
  151.                 img =  this._userstage.getChildAt(i) as Image ;
  152.                 revertPhotoAlpha();
  153.                 if(img.hitTestPoint(mouseX,mouseY)){   
  154.                     this._currentphotoNum = i ;//当前照片的号码
  155.                     img.alpha = 0.5 ;
  156.                     break ;
  157.                 }
  158.             }    
  159.         }
  160.         private function photoDoubleClickHandler(e:MouseEvent):void{
  161.             var img_ :Image = null ;
  162.             for(var i:int=0;i<this._currentcoordinateData.pic.length();i++){
  163.                 img_ =  this._userstage.getChildAt(i) as Image ;
  164.                 revertPhotoAlpha();
  165.                 if(img_.hitTestPoint(mouseX,mouseY)){   
  166.                     this._currentphotoNum = i ;
  167.                     this.revertPhotoAlpha() ;
  168.                     var url_:String =     this._productDataContent.item[_currentpageNum]..img[i][email protected] ;
  169.                     var x_  :Number =     this._productDataContent.item[_currentpageNum]..img[i][email protected] ;
  170.                     var y_  :Number =     this._productDataContent.item[_currentpageNum]..img[i][email protected] ;
  171.                     var w_  :Number =     this._productDataContent.item[_currentpageNum]..img[i][email protected] ;
  172.                     var h_  :Number =     this._productDataContent.item[_currentpageNum]..img[i][email protected] ;
  173.                     var photoEdit_:photEditview = new photEditview();
  174.                     photoEdit_.producthandlerBase = this ;  
  175.                     PopUpManager.addPopUp(photoEdit_,this._currentStage,true);
  176.                     photoEdit_.initphotoEditParams(img_,url_,x_,y_,w_,h_);           
  177.                     break ;
  178.                 } 
  179.             }   
  180.         }
  181.         private function revertPhotoAlpha():void{
  182.             var img:Image = null ;
  183.             for(var i:int=0;i<this._currentcoordinateData.pic.length();i++){
  184.                 img =  this._userstage.getChildAt(i) as Image ;
  185.                 if(img.alpha==0.5){
  186.                     img.alpha = 1 ; 
  187.                     break ; 
  188.                 }    
  189.             }
  190.         }
  191.         public function createProductContent():void{
  192.             if(this._recordID==-1){
  193.                 this.createNewProudctContent() ;    
  194.                 return ;            
  195.             }
  196.             //编辑产品
  197.             var params:Object = new Object() ;
  198.             params.methodName = "getProductContent" ;
  199.             params.random= Math.random() ; 
  200.             params.pid= this._recordID ; 
  201.             //this._testTextArea.text = "recordid=="+this._recordID ; 
  202.             this._httpservice.send(params) ;    
  203.         }   
  204.         private function createNewProudctContent():void{
  205.             _productDataContent = <wdxc></wdxc> ; 
  206.             var xmllist_t:XML = new XML(this._productInfo.product.(@id==_productID));       
  207.             var count:Number = xmllist_t.count ;
  208.             for(var i:int=0;i<count;i++){ 
  209.                 var itemNode:XML  = <item></item>;
  210.                 var photoNode:XML =<photo></photo>;
  211.                 for(var j:int=0;j<13;j++){
  212.                     photoNode.appendChild(<img url="" x="-1" y="" width="" height=""/>);
  213.                 }  
  214.                 if(this._productID==598){               
  215.                     itemNode.appendChild(<bs>{_defaulModuleUrl.replace(".gif","_"+(i+1)+".gif")}</bs>);
  216.                 }else{
  217.                     itemNode.appendChild(<bs>{this._defaulModuleUrl}</bs>);
  218.                 }
  219.                 itemNode.appendChild(photoNode) ;  
  220.                 itemNode.appendChild(<txt><info></info></txt>);
  221.                 _productDataContent.appendChild(itemNode);      
  222.             }   
  223.             _productDataContent.appendChild(<innernumber>0</innernumber>);  
  224.             //创建是否需要邮票和信封组件
  225.             createOtherCom();
  226.             this.initCurrentPageInfoToStage(true);
  227.         }
  228.         private function createEditProudctContent(e:ResultEvent):void{ 
  229.             _productDataContent = new XML(e.result);
  230.             createOtherCom(); 
  231.             this.initCurrentPageInfoToStage(false);
  232.         }   
  233.         private var _defaultText:String = "单击此处输入文字" ;
  234.         public function initCurrentPageInfoToStage(recompute:Boolean):void{
  235.             this._currentphotoNum =  0 ;  
  236.             _currentpageData = new XML(this._productDataContent.item[this._currentpageNum]) ;
  237.             this._userstage.removeAllChildren();
  238.             for(var i:int=0;i<getCurrentPhotoCount(_currentpageNum);i++){
  239.                 var img1_t:Image = new Image() ;
  240.                 this._userstage.addChild(img1_t);
  241.                 img1_t.name="img_"+i;
  242.                 img1_t.x = _currentcoordinateData.pic[i][email protected] ;
  243.                 img1_t.y = _currentcoordinateData.pic[i][email protected] ;
  244.                 img1_t.width = _currentcoordinateData.pic[i][email protected] ;
  245.                 img1_t.height = _currentcoordinateData.pic[i][email protected] ; 
  246.                 img1_t.scaleContent = true ;   
  247.                 this.loadphotoToStage(img1_t,i,recompute,_productDataContent.item[_currentpageNum].photo.img[i][email protected]);          
  248.             }           
  249.             _backgroundImage.source = (this._productDataContent.item[this._currentpageNum].bs).toString(); 
  250.             this._userstage.addChild(_backgroundImage);  
  251.             for(var k:int=0;k<_currentcoordinateData.txt.length();k++){
  252.                 var textInfo_:TextArea = new TextArea() ;
  253.                 textInfo_.alpha = 0 ;
  254.                 textInfo_.setStyle("fontSize","12"); 
  255.                 textInfo_.name = "txt_"+k ;
  256.                 textInfo_.x = _currentcoordinateData.txt[k][email protected] ;
  257.                 textInfo_.y = _currentcoordinateData.txt[k][email protected]  ; 
  258.                 textInfo_.width= _currentcoordinateData.txt[k][email protected] ;
  259.                 textInfo_.height = _currentcoordinateData.txt[k][email protected] ;  
  260.                 textInfo_.editable = false ;
  261.                 if(_productDataContent.item[_currentpageNum].txt.info[k]==""||
  262.                     _productDataContent.item[_currentpageNum].txt.info[k]==null){
  263.                     textInfo_.text = _defaultText ;
  264.                 }else{
  265.                     textInfo_.htmlText = _productDataContent.item[_currentpageNum].txt.info[k] ;
  266.                 }  
  267.                 textInfo_.addEventListener(MouseEvent.CLICK,openTextEditview) ;
  268.                 this._userstage.addChild(textInfo_); 
  269.             }    
  270.         }
  271.         public function openTextEditview(e:MouseEvent):void{
  272.             var textInfo_:TextArea = TextArea(e.currentTarget);
  273.             var tev_ :TextEditview = new TextEditview() ;
  274.             PopUpManager.addPopUp(tev_,_currentStage,true,null);
  275.             tev_.targetLabelName = textInfo_.name ;
  276.             tev_.phander = this ;
  277.             var n_:Number = textInfo_.name.split("_")[1]; ;
  278.             tev_.my_rte.htmlText =_productDataContent.item[_currentpageNum].txt.info[n_] ;
  279.             tev_.my_rte.width = textInfo_.width ;
  280.             tev_.my_rte.height =textInfo_.height ;
  281.             tev_.my_rte.maxWidth = textInfo_.width ;
  282.             tev_.my_rte.maxHeight = textInfo_.height ;
  283.             tev_.x = 100  ;  
  284.             tev_.y = 100  ; 
  285.         } 
  286.         public function saveUserInputText(html_:String,txt_:String,labelname_:String):void{
  287.             var lab_:TextArea = this._userstage.getChildByName(labelname_) as TextArea;
  288.             if(txt_==null||txt_.length==0||txt_==""){
  289.                lab_.text= _defaultText ;
  290.            } else{ 
  291.                 lab_.htmlText = html_ ;  
  292.                 var n_:Number = labelname_.split("_")[1]; ;
  293.                 _productDataContent.item[_currentpageNum].txt.info[n_] = html_ ;
  294.            }
  295.         }
  296.         public function getCurrentPhotoCount(num_:Number):Number{
  297.             var a1:Array = this._productDataContent.item[num_].bs.split("/"); 
  298.             var picname:String = a1[a1.length-1];
  299.             _currentcoordinateData=_photoCoordinateData.subject.mb.(@picname==picname);
  300.             return  this._currentcoordinateData.pic.length();       
  301.         }
  302.         public function getCurrentTextCount(num_:Number):Number{
  303.             var a1:Array = this._productDataContent.item[num_].bs.split("/"); 
  304.             var picname:String = a1[a1.length-1];
  305.             _currentcoordinateData=_photoCoordinateData.subject.mb.(@picname==picname);
  306.             return  this._currentcoordinateData.txt.length();       
  307.         }
  308.         public function loadphotoToStage(targetimg:Image,num:Number,recompute:Boolean,targerurl:String):void{
  309.             if(targerurl==null||targerurl==""){
  310.                 return ;
  311.             }           
  312.             //this._testTextArea.text ="url=="+targerurl+"targerurl.length"+targerurl.length;    
  313.             var ph_t:ProductHandlerBasic = this ;
  314.             var loader_t:Loader = new Loader();   
  315.             loader_t.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
  316.                 var width1:Number=0 ;
  317.                 var height1:Number=0;
  318.                 var x1:Number =0 ; 
  319.                 var y1:Number =0 ; 
  320.                 x1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] ;
  321.                 y1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] ;
  322.                 width1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] ;
  323.                 height1 = ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] ;
  324.                 var oldbitmap_t:Bitmap = new Bitmap() ;
  325.                 var l:Loader = e.target.loader as Loader;  
  326.                 var oldbitmap_tdata:BitmapData = new  BitmapData (l.width,l.height) ;
  327.                 oldbitmap_tdata.draw(l); 
  328.                 if(recompute||x1==-1){ 
  329.                     var scale_t:Number =targetimg.width/targetimg.height ;
  330.                     var h2:Number = l.width/scale_t ;                   
  331.                     if(h2<=l.height){
  332.                         height1 = h2 ; 
  333.                         width1 = l.width ;
  334.                     }else{    
  335.                         height1 = l.height ; 
  336.                         width1 = height1*scale_t ;
  337.                     }
  338.                     x1 = (l.width-width1)/2 ; 
  339.                     y1 = (l.height-height1)/2;      
  340.                     ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] = x1 ;  
  341.                     ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] = y1 ; 
  342.                     ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] = width1 ;  
  343.                     ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] =height1 ; 
  344.                     ph_t._productDataContent.item[ph_t._currentpageNum]..img[num][email protected] = targerurl ;
  345.                 } 
  346.                 var newbitmap_t:BitmapData  = new BitmapData(width1,height1);
  347.                 //坐标转化,把坐标移到裁剪区域的位置,宽度和高度在cutbitmapdata里指定。  
  348.                 var matrix:Matrix = new Matrix(1, 0, 0, 1, -x1, -y1);   
  349.                 newbitmap_t.draw(oldbitmap_tdata, matrix);    
  350.                 oldbitmap_t.bitmapData = newbitmap_t ;    
  351.                 targetimg.source = oldbitmap_t ; 
  352.                 targetimg.scaleContent  = true ;
  353.                 ph_t._currentphotoNum++ ;
  354.                 }); 
  355.             loader_t.load(new URLRequest(targerurl)); 
  356.         } 
  357.         public function moduleChange(url:String):void{
  358.             //豪华折叠卡一次换3张
  359.             if(this.typename=="replicatecard"){
  360.                 this._productDataContent.item[0].bs = url.replace(".gif","_1.gif") ;
  361.                 this._productDataContent.item[1].bs = url.replace(".gif","_2.gif") ;
  362.                 this._productDataContent.item[2].bs = url.replace(".gif","_3.gif") ;                
  363.             } else{
  364.                 this._productDataContent.item[this._currentpageNum].bs = url ;
  365.             }
  366.             initCurrentPageInfoToStage(true);
  367.         }
  368.         public function albumPhotoClickHandler(url:String):void{
  369.             //======当前模板的图片个数  
  370.             var t:Number = _currentcoordinateData.pic.length();
  371.             if(this._currentphotoNum>t-1){
  372.                 this._currentphotoNum = 0 ;
  373.             }    
  374.             var img:Image = this._userstage.getChildByName("img_"+this._currentphotoNum) as Image ;
  375.             if(img==null){
  376.                 return ;
  377.             }
  378.             this.loadphotoToStage(img,this._currentphotoNum,true,url);
  379.         }
  380.         public function saveCurrentPageData(p1_:Number,p2_:Number,url_:String,x_:Number,y_:Number,w_:Number,h_:Number):void{
  381.             _productDataContent.item[p1_]..img[p2_][email protected] =x_ ;    
  382.             _productDataContent.item[p1_]..img[p2_][email protected] =y_ ; 
  383.             _productDataContent.item[p1_]..img[p2_][email protected] =w_ ;
  384.             _productDataContent.item[p1_]..img[p2_][email protected] =h_ ;
  385.             _productDataContent.item[p1_]..img[p2_][email protected]=url_ ;
  386.         } 
  387.         public function createOtherCom():void{
  388.             //card.MAIN.right_id.sendAlbumInfoRequest();
  389.             if(this.productInnerNum==1){
  390.                 card.MAIN.radbut1.selected = true ; 
  391.             }else{
  392.                 card.MAIN.radbut2.selected = true ;
  393.             } 
  394.             card.MAIN.header_id.recomputePrice();
  395.             card.MAIN.right_id.initAlbum();
  396.         }
  397.     }
  398. }