home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / livebrush / graphics / CanvasView.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  5.9 KB  |  216 lines

  1. package com.livebrush.graphics
  2. {
  3.    import com.livebrush.events.UpdateEvent;
  4.    import com.livebrush.graphics.canvas.BitmapLayer;
  5.    import com.livebrush.graphics.canvas.Canvas;
  6.    import com.livebrush.graphics.canvas.CanvasManager;
  7.    import com.livebrush.graphics.canvas.Layer;
  8.    import com.livebrush.graphics.canvas.LineLayer;
  9.    import com.livebrush.ui.UI;
  10.    import com.livebrush.utils.Model;
  11.    import com.livebrush.utils.Update;
  12.    import com.livebrush.utils.View;
  13.    import flash.display.Bitmap;
  14.    import flash.utils.getTimer;
  15.    
  16.    public class CanvasView extends View
  17.    {
  18.       public var canvas:Canvas;
  19.       
  20.       public function CanvasView(canvasManager:Model)
  21.       {
  22.          super(canvasManager);
  23.          this.init();
  24.       }
  25.       
  26.       override public function die() : void
  27.       {
  28.          controller.die();
  29.       }
  30.       
  31.       override protected function createView() : void
  32.       {
  33.          this.canvas = new Canvas(this.canvasManager);
  34.       }
  35.       
  36.       public function get layers() : Array
  37.       {
  38.          return this.canvasManager.layers;
  39.       }
  40.       
  41.       public function get aboveLayerComp() : Bitmap
  42.       {
  43.          return this.canvas.aboveLayerComp;
  44.       }
  45.       
  46.       public function toggleMasterComp(b:Boolean) : void
  47.       {
  48.          this.canvas.masterComp.visible = b;
  49.       }
  50.       
  51.       override protected function registerController() : void
  52.       {
  53.       }
  54.       
  55.       private function updateCanvasDisplay() : void
  56.       {
  57.          var d:int = 0;
  58.          var layer:Layer = null;
  59.          var s:int = getTimer();
  60.          var layersChanged:Boolean = false;
  61.          this.canvas.clearBitmap(this.masterComp);
  62.          this.canvas.clearBitmap(this.belowLayerComp);
  63.          this.canvas.clearBitmap(this.aboveLayerComp);
  64.          this.canvas.clearBitmap(this.sampleLayerComp);
  65.          for(d = 0; d < this.layers.length; d++)
  66.          {
  67.             layer = this.getLayer(d);
  68.             try
  69.             {
  70.                if(layer is LineLayer)
  71.                {
  72.                   if(layer.enabled)
  73.                   {
  74.                      if(Layer.isLineLayer(layer))
  75.                      {
  76.                         this.cacheLayer(layer as LineLayer);
  77.                      }
  78.                      else
  79.                      {
  80.                         this.cacheLayer(layer as BitmapLayer);
  81.                      }
  82.                      layersChanged = true;
  83.                   }
  84.                }
  85.                layer.visible = true;
  86.             }
  87.             catch(e:Error)
  88.             {
  89.             }
  90.             if(layer.enabled)
  91.             {
  92.                layer.drawTo(this.masterComp);
  93.             }
  94.             if(d < this.canvasManager.bottomActiveLayerDepth)
  95.             {
  96.                if(layer.enabled)
  97.                {
  98.                   layer.drawTo(this.belowLayerComp);
  99.                }
  100.                if(layer.enabled)
  101.                {
  102.                   layer.drawTo(this.sampleLayerComp);
  103.                }
  104.                layer.visible = false;
  105.             }
  106.             else if(d >= this.canvasManager.bottomActiveLayerDepth && d <= this.canvasManager.topActiveLayerDepth)
  107.             {
  108.                if(layer.enabled)
  109.                {
  110.                   layer.drawTo(this.sampleLayerComp);
  111.                }
  112.                layer.visible = true;
  113.             }
  114.             else if(d > this.canvasManager.topActiveLayerDepth)
  115.             {
  116.                if(layer.enabled)
  117.                {
  118.                   layer.drawTo(this.aboveLayerComp);
  119.                }
  120.                layer.visible = false;
  121.             }
  122.          }
  123.          this.belowLayerComp.visible = true;
  124.          this.aboveLayerComp.visible = true;
  125.          this.toggleMasterComp(true);
  126.          UI.setStatus("Ready");
  127.       }
  128.       
  129.       override public function update(update:Update = null) : void
  130.       {
  131.          if(update.type == UpdateEvent.CANVAS)
  132.          {
  133.             this.updateCanvasDisplay();
  134.          }
  135.       }
  136.       
  137.       public function showAllLayers() : void
  138.       {
  139.          var layer:Layer = null;
  140.          this.toggleMasterComp(false);
  141.          this.belowLayerComp.visible = this.aboveLayerComp.visible = false;
  142.          for(var d:int = 0; d < this.layers.length; d++)
  143.          {
  144.             this.getLayer(d).visible = true;
  145.          }
  146.       }
  147.       
  148.       public function get belowLayerComp() : Bitmap
  149.       {
  150.          return this.canvas.belowLayerComp;
  151.       }
  152.       
  153.       public function get masterComp() : Bitmap
  154.       {
  155.          return this.canvas.masterComp;
  156.       }
  157.       
  158.       public function resize() : void
  159.       {
  160.          this.canvas.x = 0.7 + this.canvas.offset.x;
  161.          this.canvas.y = 30.7 + this.canvas.offset.y;
  162.       }
  163.       
  164.       public function get canvasManager() : CanvasManager
  165.       {
  166.          return CanvasManager(model);
  167.       }
  168.       
  169.       public function get sampleLayerComp() : Bitmap
  170.       {
  171.          return this.canvas.sampleLayerComp;
  172.       }
  173.       
  174.       override protected function init() : void
  175.       {
  176.          this.createView();
  177.       }
  178.       
  179.       private function getLayer(depth:int) : Layer
  180.       {
  181.          var layer:Layer = null;
  182.          for(var i:int = 0; i < this.layers.length; i++)
  183.          {
  184.             if(this.layers[i].depth == depth)
  185.             {
  186.                layer = this.layers[i];
  187.             }
  188.          }
  189.          return layer;
  190.       }
  191.       
  192.       public function setup() : void
  193.       {
  194.          this.createController();
  195.          this.registerController();
  196.       }
  197.       
  198.       private function cacheLayer(layer:Layer) : void
  199.       {
  200.          if(layer.loaded)
  201.          {
  202.             if(!layer.cached)
  203.             {
  204.                layer.updateDisplay();
  205.             }
  206.          }
  207.       }
  208.       
  209.       override protected function createController() : void
  210.       {
  211.          controller = new CanvasController(this);
  212.       }
  213.    }
  214. }
  215.  
  216.