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 / styles / StyleManager.as < prev   
Encoding:
Text File  |  2009-10-26  |  10.2 KB  |  332 lines

  1. package com.livebrush.styles
  2. {
  3.    import com.livebrush.data.Exchangeable;
  4.    import com.livebrush.data.FileManager;
  5.    import com.livebrush.data.Settings;
  6.    import com.livebrush.graphics.canvas.StylePreviewLayer;
  7.    import com.livebrush.ui.UI;
  8.    import com.livebrush.utils.Update;
  9.    
  10.    public class StyleManager implements Exchangeable
  11.    {
  12.       public var alphaLocked:Boolean = false;
  13.       
  14.       private var _copiedStyle:Style = null;
  15.       
  16.       public var ui:UI;
  17.       
  18.       public var stylePreviewAutoRefresh:Boolean = true;
  19.       
  20.       public var stylePreview:StylePreviewLayer;
  21.       
  22.       public var colorsLocked:Boolean = false;
  23.       
  24.       public var styleGroup:Array;
  25.       
  26.       public var styles:Array;
  27.       
  28.       private var _locked:Boolean = false;
  29.       
  30.       public var showStylePreview:Boolean = false;
  31.       
  32.       public var lockedColorSettings:Settings;
  33.       
  34.       public function StyleManager(ui:UI)
  35.       {
  36.          super();
  37.          this.styleGroup = [];
  38.          this.styles = [];
  39.          this.ui = ui;
  40.          this.lockColors(false);
  41.       }
  42.       
  43.       public function updateStylePreview(style:Style) : void
  44.       {
  45.          if(this.showStylePreview)
  46.          {
  47.             this.stylePreviewLayer.drawStyle();
  48.          }
  49.       }
  50.       
  51.       public function lockColors(b:Boolean) : void
  52.       {
  53.          this.colorsLocked = b;
  54.          if(b)
  55.          {
  56.             this.lockedColorSettings = this.ui.globalColorView.settings;
  57.          }
  58.          try
  59.          {
  60.             this.alphaLocked = this.lockedColorSettings.alphaLocked;
  61.          }
  62.          catch(e:Error)
  63.          {
  64.             alphaLocked = false;
  65.          }
  66.       }
  67.       
  68.       public function copyStyle(i:int) : void
  69.       {
  70.          if(this._copiedStyle != null)
  71.          {
  72.             this._copiedStyle.die();
  73.          }
  74.          this._copiedStyle = this.styles[i].clone();
  75.       }
  76.       
  77.       public function set settings(settings:Settings) : void
  78.       {
  79.          this.styles = settings.styles;
  80.          this.setStyleGroup(settings.styleGroup);
  81.       }
  82.       
  83.       private function getStyle(id:int) : Style
  84.       {
  85.          var style:Style = null;
  86.          for(var i:int = 0; i < this.styles.length; i++)
  87.          {
  88.             if(this.styles[i].id == id)
  89.             {
  90.                style = this.styles[i];
  91.             }
  92.          }
  93.          return style;
  94.       }
  95.       
  96.       public function get activeStyle() : Style
  97.       {
  98.          return this.styleGroup[0];
  99.       }
  100.       
  101.       public function reset() : void
  102.       {
  103.          for(var i:int = 0; i < this.styles.length; i++)
  104.          {
  105.             this.styles[i].die();
  106.             delete this.styles[i];
  107.          }
  108.          for(var j:int = 0; j < this.styleGroup.length; j++)
  109.          {
  110.             delete this.styleGroup[j];
  111.          }
  112.          this.styles = [];
  113.          this.styleGroup = [];
  114.       }
  115.       
  116.       public function removeDeco(index:int) : void
  117.       {
  118.          this.activeStyle.decoStyle.decoSet.removeDeco(index);
  119.          this.pushStyle();
  120.       }
  121.       
  122.       public function removeStyle(id:int) : void
  123.       {
  124.          var index:int = 0;
  125.          for(var i:int = 0; i < this.styles.length; i++)
  126.          {
  127.             if(this.styles[i].id == id)
  128.             {
  129.                index = i;
  130.                this.styles[i].die();
  131.                this.styles.splice(i,1);
  132.             }
  133.          }
  134.          this.activeStyle = this.styles[Math.min(this.styles.length - 1,index)];
  135.          this.pushStyle();
  136.       }
  137.       
  138.       public function set activeStyle(s:Style) : void
  139.       {
  140.          this.setStyleGroup([s]);
  141.       }
  142.       
  143.       public function selectStyle(i:int) : void
  144.       {
  145.          this.activeStyle = this.styles[i];
  146.       }
  147.       
  148.       public function get activeStyleId() : int
  149.       {
  150.          return this.activeStyle.id;
  151.       }
  152.       
  153.       private function setStyleGroup(bGroup:Array) : void
  154.       {
  155.          var style:Style = null;
  156.          var i:int = 0;
  157.          try
  158.          {
  159.             for(i = 0; i < this.styleGroup.length; i++)
  160.             {
  161.                style = this.getStyle(this.styleGroup[i].id);
  162.                if(style.lineStyle.type == LineStyle.DYNAMIC)
  163.                {
  164.                   style.lineStyle.unloadInputSWF();
  165.                }
  166.             }
  167.          }
  168.          catch(error:Error)
  169.          {
  170.          }
  171.          finally
  172.          {
  173.             this.styleGroup = [];
  174.             for(i = 0; i < bGroup.length; i++)
  175.             {
  176.                style = this.getStyle(bGroup[i].id);
  177.                if(style.lineStyle.type == LineStyle.DYNAMIC)
  178.                {
  179.                   style.lineStyle.loadInputSWF();
  180.                }
  181.                this.styleGroup.push(style);
  182.             }
  183.             this.pushStyle();
  184.          }
  185.          if(this.stylePreviewAutoRefresh)
  186.          {
  187.             this.updateStylePreview(this.activeStyle);
  188.          }
  189.       }
  190.       
  191.       public function setDynamicInput(fileName:String) : void
  192.       {
  193.          this.activeStyle.lineStyle.setDynamicInput(fileName);
  194.          this.setStyleGroup([this.activeStyle]);
  195.       }
  196.       
  197.       public function createStyle(name:String) : void
  198.       {
  199.          var style:Style = this.activeStyle.clone();
  200.          style.name = FileManager.createNameDup(this.styles,name,"name");
  201.          this.styles.splice(this.styles.indexOf(this.activeStyle) + 1,0,style);
  202.          this.activeStyle = style;
  203.          this.pushStyle();
  204.       }
  205.       
  206.       public function pasteStyle(i:int, all:Boolean, b:Boolean = false, l:Boolean = false, d:Boolean = false, lc:Boolean = false, dc:Boolean = false, dl:Boolean = false, t:Boolean = false) : void
  207.       {
  208.          var settingsMix:Settings = null;
  209.          var style:Style = this.styles[i];
  210.          if(this._copiedStyle != null)
  211.          {
  212.             if(all)
  213.             {
  214.                style.settings = this._copiedStyle.settings;
  215.             }
  216.             else if(b)
  217.             {
  218.                style.strokeStyle.thresholds = this._copiedStyle.strokeStyle.settings.thresholds;
  219.                style.lineStyle.settings = this._copiedStyle.lineStyle.settings;
  220.             }
  221.             else if(l)
  222.             {
  223.                style.lineStyle.smoothing = this._copiedStyle.lineStyle.settings.smoothing;
  224.                style.strokeStyle.settings = this._copiedStyle.strokeStyle.settings;
  225.             }
  226.             else if(d)
  227.             {
  228.                style.decoStyle.settings = this._copiedStyle.decoStyle.settings;
  229.             }
  230.             else if(lc)
  231.             {
  232.                settingsMix = style.strokeStyle.settings;
  233.                settingsMix.colorObjList = this._copiedStyle.strokeStyle.settings.colorObjList;
  234.                style.strokeStyle.settings = settingsMix;
  235.             }
  236.             else if(dc)
  237.             {
  238.                settingsMix = style.decoStyle.settings;
  239.                settingsMix.colorObjList = this._copiedStyle.decoStyle.settings.colorObjList;
  240.                style.decoStyle.settings = settingsMix;
  241.             }
  242.             else if(dl)
  243.             {
  244.                settingsMix = style.decoStyle.settings;
  245.                settingsMix.decos = this._copiedStyle.decoStyle.settings.decos;
  246.                style.decoStyle.settings = settingsMix;
  247.             }
  248.             else if(t)
  249.             {
  250.                style.strokeStyle.thresholds = this._copiedStyle.strokeStyle.settings.thresholds;
  251.             }
  252.             if(style == this.activeStyle)
  253.             {
  254.                this.pushStyle();
  255.             }
  256.          }
  257.       }
  258.       
  259.       public function getStyleXML() : XML
  260.       {
  261.          var stylesXML:XML = new XML(<styles></styles>);
  262.          for(var i:int = 0; i < this.styles.length; i++)
  263.          {
  264.             stylesXML.appendChild(this.styles[i].getXML());
  265.          }
  266.          return stylesXML;
  267.       }
  268.       
  269.       public function pullStyle(settings:Settings) : void
  270.       {
  271.          var settingsMix:Settings = null;
  272.          if(!this._locked)
  273.          {
  274.             this.activeStyle.decoStyle.settings = settings.deco;
  275.             settingsMix = settings.behavior;
  276.             settingsMix.smoothing = settings.line.smoothing;
  277.             this.activeStyle.lineStyle.settings = settingsMix;
  278.             settingsMix = settings.line;
  279.             settingsMix.thresholds = settings.behavior.thresholds;
  280.             this.activeStyle.strokeStyle.settings = settingsMix;
  281.             this.settings = settings.list;
  282.          }
  283.       }
  284.       
  285.       public function moveDeco(index:int, dir:int) : void
  286.       {
  287.          this.activeStyle.decoStyle.decoSet.moveDeco(index,dir);
  288.          this.pushStyle();
  289.       }
  290.       
  291.       public function get settings() : Settings
  292.       {
  293.          var settings:Settings = new Settings();
  294.          settings.styles = this.styles;
  295.          settings.styleGroup = this.styleGroup;
  296.          return settings;
  297.       }
  298.       
  299.       public function setStyleXML(xml:XML, importBool:Boolean = false) : Style
  300.       {
  301.          this._locked = true;
  302.          var styleXML:XML = xml.copy();
  303.          var style:Style = new Style(this);
  304.          style.setXML(Settings.attrToElement(styleXML));
  305.          style.name = FileManager.createNameDup(this.styles,style.name,"name");
  306.          this.styles.push(style);
  307.          this.setStyleGroup([this.styleGroup.length == 0 ? this.styles[0] : (importBool ? this.styles[this.styles.length - 1] : this.activeStyle)]);
  308.          this._locked = false;
  309.          return style;
  310.       }
  311.       
  312.       public function get stylePreviewLayer() : StylePreviewLayer
  313.       {
  314.          return this.ui.canvasManager.stylePreviewLayer;
  315.       }
  316.       
  317.       public function pushStyle() : void
  318.       {
  319.          this._locked = true;
  320.          var settings:Settings = this.settings;
  321.          settings.behavior = this.activeStyle.lineStyle.settings;
  322.          settings.behavior.thresholds = this.activeStyle.strokeStyle.thresholds;
  323.          settings.line = this.activeStyle.strokeStyle.settings;
  324.          settings.line.smoothing = settings.behavior.smoothing;
  325.          settings.deco = this.activeStyle.decoStyle.settings;
  326.          this.ui.update(Update.brushStyleUpdate(settings));
  327.          this._locked = false;
  328.       }
  329.    }
  330. }
  331.  
  332.