home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / qs / controls / CachedLabel.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  4.6 KB  |  162 lines

  1. package qs.controls
  2. {
  3.    import flash.display.Bitmap;
  4.    import flash.display.BitmapData;
  5.    import flash.geom.Point;
  6.    import flash.geom.Rectangle;
  7.    import flash.text.TextLineMetrics;
  8.    import mx.core.IDataRenderer;
  9.    import mx.core.UIComponent;
  10.    import mx.core.UITextField;
  11.    
  12.    public class CachedLabel extends UIComponent implements IDataRenderer
  13.    {
  14.       private static var xUnit:Point = new Point(1,0);
  15.       
  16.       private static var origin:Point = new Point(0,0);
  17.       
  18.       private var _cachePolicy:String = "auto";
  19.       
  20.       private var _data:Object;
  21.       
  22.       private var _text:String = "";
  23.       
  24.       private var _bitmap:Bitmap;
  25.       
  26.       private var _label:UITextField;
  27.       
  28.       private var _capturedText:BitmapData;
  29.       
  30.       public function CachedLabel()
  31.       {
  32.          super();
  33.       }
  34.       
  35.       public function set data(param1:Object) : void
  36.       {
  37.          if(param1 == this._data)
  38.          {
  39.             return;
  40.          }
  41.          this._data = param1;
  42.          this._text = String(param1);
  43.          if(this._label != null)
  44.          {
  45.             this._label.text = this._text == null ? "" : this._text;
  46.          }
  47.          this.invalidateSize();
  48.          invalidateDisplayList();
  49.       }
  50.       
  51.       override protected function createChildren() : void
  52.       {
  53.          super.createChildren();
  54.          this._label = new UITextField();
  55.          this._label.multiline = true;
  56.          this._label.selectable = false;
  57.          this._label.autoSize = "left";
  58.          this._label.text = this._text;
  59.          this._label.backgroundColor = 6720395;
  60.          addChild(this._label);
  61.       }
  62.       
  63.       public function get data() : Object
  64.       {
  65.          return this._data;
  66.       }
  67.       
  68.       override public function invalidateSize() : void
  69.       {
  70.          super.invalidateSize();
  71.       }
  72.       
  73.       public function set useCache(param1:String) : void
  74.       {
  75.          this._cachePolicy = param1;
  76.          invalidateDisplayList();
  77.       }
  78.       
  79.       override protected function updateDisplayList(param1:Number, param2:Number) : void
  80.       {
  81.          var _loc3_:Point = null;
  82.          var _loc4_:Point = null;
  83.          this._label.validateNow();
  84.          this._label.setActualSize(param1,param2);
  85.          var _loc5_:Boolean = true;
  86.          switch(this._cachePolicy)
  87.          {
  88.             case "off":
  89.                _loc5_ = true;
  90.                break;
  91.             case "on":
  92.                _loc5_ = false;
  93.                break;
  94.             case "auto":
  95.                if(this._label.embedFonts == false && param1 > 0 && param2 > 0)
  96.                {
  97.                   _loc3_ = globalToLocal(xUnit);
  98.                   _loc4_ = globalToLocal(origin);
  99.                   _loc5_ = _loc3_.x - _loc4_.x == 1 && _loc3_.y - _loc4_.y == 0;
  100.                }
  101.          }
  102.          if(param2 <= 1 || param1 <= 1)
  103.          {
  104.             _loc5_ = true;
  105.          }
  106.          if(_loc5_)
  107.          {
  108.             if(this._bitmap != null)
  109.             {
  110.                removeChild(this._bitmap);
  111.                this._bitmap = null;
  112.             }
  113.             this._label.visible = true;
  114.          }
  115.          else
  116.          {
  117.             this._label.visible = false;
  118.             if(this._capturedText == null || this._capturedText.width != param1 || this._capturedText.height != param2)
  119.             {
  120.                this._capturedText = new BitmapData(param1,param2);
  121.                if(this._bitmap != null)
  122.                {
  123.                   removeChild(this._bitmap);
  124.                   this._bitmap = null;
  125.                }
  126.             }
  127.             if(this._bitmap == null)
  128.             {
  129.                this._bitmap = new Bitmap(this._capturedText);
  130.                this._bitmap.smoothing = true;
  131.                addChild(this._bitmap);
  132.             }
  133.             this._capturedText.fillRect(new Rectangle(0,0,param1,param2),0);
  134.             this._capturedText.draw(this._label);
  135.          }
  136.       }
  137.       
  138.       public function get useCache() : String
  139.       {
  140.          return this._cachePolicy;
  141.       }
  142.       
  143.       override protected function measure() : void
  144.       {
  145.          var _loc1_:TextLineMetrics = null;
  146.          this._label.validateNow();
  147.          if(this._label.embedFonts)
  148.          {
  149.             measuredWidth = this._label.measuredWidth + 6;
  150.             measuredHeight = this._label.measuredHeight + 4;
  151.          }
  152.          else
  153.          {
  154.             _loc1_ = measureText(this._text);
  155.             measuredWidth = _loc1_.width + 6;
  156.             measuredHeight = _loc1_.height + 4;
  157.          }
  158.       }
  159.    }
  160. }
  161.  
  162.