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 / Main.as
Encoding:
Text File  |  2009-10-26  |  19.1 KB  |  515 lines

  1. package com.livebrush
  2. {
  3.    import com.formatlos.as3.lib.display.BitmapDataUnlimited;
  4.    import com.formatlos.as3.lib.display.events.BitmapDataUnlimitedEvent;
  5.    import com.livebrush.data.FileManager;
  6.    import com.livebrush.data.GlobalSettings;
  7.    import com.livebrush.data.StateManager;
  8.    import com.livebrush.events.*;
  9.    import com.livebrush.graphics.canvas.CanvasManager;
  10.    import com.livebrush.styles.StyleManager;
  11.    import com.livebrush.tools.ToolManager;
  12.    import com.livebrush.ui.UI;
  13.    import flash.desktop.NativeApplication;
  14.    import flash.display.MovieClip;
  15.    import flash.events.Event;
  16.    import flash.events.InvokeEvent;
  17.    import flash.filesystem.*;
  18.    import flash.utils.setTimeout;
  19.    
  20.    public class Main extends MovieClip
  21.    {
  22.       public static const ABOUT_INFO:String = "<font size=\'16\'><b>LiveBrush</b></font>\n<font size=\'10\'>Version 1.1.0</font>\n\n<b>Created By:</b> David Fasullo (MoreMeYou.com)\n\n<b>Libraries Used:</b> CASA Lib, Core Lib\n\n<font size=\'11\'>Copyright(c) 2007-2009 David Fasullo.</font>";
  23.       
  24.       public static const MAJOR_VERSION:int = 1;
  25.       
  26.       public static const HELP_LINK:String = "http://www.livebrush.com/help/index.html";
  27.       
  28.       public static const SUPPORT_LINK:String = "http://www.livebrush.com/support.html";
  29.       
  30.       public static const FEEDBACK_LINK:String = "http://www.livebrush.com/feedback.html";
  31.       
  32.       public static const FORUM_LINK:String = "http://www.livebrush.com/forumCommunity.html";
  33.       
  34.       public static const FORUM_STYLES_LINK:String = "http://www.livebrush.com/forumCommunity.html";
  35.       
  36.       public static const FORUM_DECOS_LINK:String = "http://www.livebrush.com/forumCommunity.html";
  37.       
  38.       public static const FORUM_REMIX_LINK:String = "http://www.livebrush.com/forumCommunity.html";
  39.       
  40.       public static const FORUM_DEVELOP_LINK:String = "http://www.livebrush.com/forumCommunity.html";
  41.       
  42.       public static const HOME_LINK:String = "http://www.livebrush.com";
  43.       
  44.       public static const UPDATE_LINK:String = "http://www.livebrush.com/update.aspx";
  45.       
  46.       public static const FACEBOOK_LINK:String = "http://www.facebook.com/Livebrush";
  47.       
  48.       public static const TWITTER_LINK:String = "http://www.twitter.com/Livebrush";
  49.       
  50.       public static const BUY_LINK:String = "http://www.Livebrush.com/Buy.aspx";
  51.       
  52.       private var fnAfterSave:Function = null;
  53.       
  54.       private var ui:UI;
  55.       
  56.       private var fileManager:FileManager;
  57.       
  58.       private var _invokeEnabled:Boolean = false;
  59.       
  60.       private var canvasManager:CanvasManager;
  61.       
  62.       private var _stateManager:StateManager;
  63.       
  64.       private var styleManager:StyleManager;
  65.       
  66.       public var NOTICE_TO_DECOMPILERS:String = "Hi there. Looks like you\'re interested in my code. May I suggest livebrush.org? I only say this because you\'re violating the EULA. But moreso because I don\'t think this code is very good. But if you must poke around, let me just mention that I\'m not a developer. This was a project driven by passion and brute force. And after this experience I have even more respect for software developers. I just wish I could have done better. Maybe you can set me straight. In any case, thanks for your time.";
  67.       
  68.       private var _allLayers:Boolean = true;
  69.       
  70.       private var invokeFilePath:String = "";
  71.       
  72.       private var _outputRes:int = 0;
  73.       
  74.       private var propsAfterSave:Array = null;
  75.       
  76.       private var toolManager:ToolManager;
  77.       
  78.       public function Main()
  79.       {
  80.          super();
  81.          NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE,this.appInvoked);
  82.          this.initUI();
  83.          stage.stageFocusRect = false;
  84.          setTimeout(this.mainInit,1000);
  85.       }
  86.       
  87.       private function closeProject() : void
  88.       {
  89.          StateManager.lock();
  90.          this.reset();
  91.       }
  92.       
  93.       private function fileIOListener(e:FileEvent) : void
  94.       {
  95.          var data:XML = null;
  96.          if(e.type == FileEvent.SAVE)
  97.          {
  98.             switch(e.fileType)
  99.             {
  100.                case FileManager.PROJECT:
  101.                   this.ui.toggle(true);
  102.                   this.ui.setProject(this.fileManager.projectName);
  103.                   StateManager.changed = false;
  104.                   this.runFnAfterSave();
  105.             }
  106.          }
  107.          else if(e.type == FileEvent.IO_ERROR)
  108.          {
  109.             switch(e.fileType)
  110.             {
  111.                case FileManager.PROJECT:
  112.                   this.ui.toggle(true);
  113.                   this.ui.showErrorDialog({"message":"SAVE ERROR\nProject assets open or in-use by another application.\nA backup has been saved to your desktop."});
  114.             }
  115.          }
  116.          else if(e.type == FileEvent.BEGIN_LOAD)
  117.          {
  118.             switch(e.fileType)
  119.             {
  120.                case FileManager.PROJECT:
  121.                   UI.setStatus("Loading...");
  122.                   this.ui.toggle(false);
  123.             }
  124.          }
  125.          else if(e.type != FileEvent.FILE_NOT_FOUND)
  126.          {
  127.             if(e.type != FileEvent.WRONG_FILE)
  128.             {
  129.                if(e.action == FileEvent.OPEN || e.action == FileEvent.BATCH_OPEN)
  130.                {
  131.                   switch(e.fileType)
  132.                   {
  133.                      case FileManager.PROJECT:
  134.                         this.ui.setProject(this.fileManager.projectName);
  135.                         data = new XML(e.data);
  136.                         if(data.@size.length() > 0)
  137.                         {
  138.                            this.canvasManager.canvas.setSize(int(data.@size));
  139.                         }
  140.                         else
  141.                         {
  142.                            this.canvasManager.canvas.setSize(4);
  143.                         }
  144.                         setTimeout(this.canvasManager.setXML,100,data.layers);
  145.                         break;
  146.                      case FileManager.STYLE:
  147.                         this.styleManager.setStyleXML(new XML(e.data),this.fileManager.lastFileLoaded == FileManager.STYLE);
  148.                         break;
  149.                      case FileManager.INPUT_SWF:
  150.                         this.styleManager.setDynamicInput(new File(e.data.url).name);
  151.                         break;
  152.                      case FileManager.DECO:
  153.                         this.styleManager.activeStyle.decoStyle.addDeco(new File(e.data.url).name);
  154.                         this.styleManager.pushStyle();
  155.                         break;
  156.                      case FileManager.LAYER_IMAGE:
  157.                         this.canvasManager.loadImageLayer(String(e.data),e.action == FileEvent.BATCH_OPEN);
  158.                         break;
  159.                      case FileManager.SWF:
  160.                         this.canvasManager.loadSWFLayer(String(e.data),e.action == FileEvent.BATCH_OPEN);
  161.                   }
  162.                }
  163.                else if(e.action == FileEvent.CLOSE)
  164.                {
  165.                   this.closeProject();
  166.                }
  167.                else if(e.action == FileEvent.IMPORT)
  168.                {
  169.                   switch(e.fileType)
  170.                   {
  171.                      case FileManager.PROJECT:
  172.                         setTimeout(this.canvasManager.setXML,100,new XML(e.data).layers);
  173.                   }
  174.                }
  175.             }
  176.          }
  177.       }
  178.       
  179.       private function runFnAfterSave() : void
  180.       {
  181.          if(this.fnAfterSave != null)
  182.          {
  183.             if(this.propsAfterSave != null)
  184.             {
  185.                this.fnAfterSave.apply(this,this.propsAfterSave);
  186.             }
  187.             else
  188.             {
  189.                this.fnAfterSave();
  190.             }
  191.             this.fnAfterSave = null;
  192.             this.propsAfterSave = null;
  193.          }
  194.       }
  195.       
  196.       private function appInvoked(e:InvokeEvent) : *
  197.       {
  198.          if(e.arguments[0] is String)
  199.          {
  200.             this.invokeFilePath = e.arguments[0];
  201.             if(this.invokeEnabled)
  202.             {
  203.                this.closeProject();
  204.                this.saveFirst(this.fileManager.loadProject,[this.invokeFilePath]);
  205.             }
  206.          }
  207.       }
  208.       
  209.       public function saveAsImage(res:int, allLayers:Boolean) : void
  210.       {
  211.          this._outputRes = res;
  212.          this._allLayers = allLayers;
  213.          this.ui.toggle(false);
  214.          this.ui.showProcessDialog({
  215.             "message":"Preparing Image",
  216.             "id":"saveImage"
  217.          });
  218.          setTimeout(this.canvasManager.canvas.generateCanvasBitmap,100,res,this.bitmapReady,this.bitmapError);
  219.       }
  220.       
  221.       public function revertProject() : void
  222.       {
  223.          this.ui.confirmActionDialog({
  224.             "message":"This action cannot be undone.\n\nWould you like to continue?",
  225.             "yesFunction":this.fileManager.revertProject,
  226.             "id":"revertConfirm"
  227.          });
  228.       }
  229.       
  230.       public function bitmapError(e:BitmapDataUnlimitedEvent) : void
  231.       {
  232.          this.ui.toggle(true);
  233.       }
  234.       
  235.       public function importInputSWF() : void
  236.       {
  237.          this.fileManager.openInputSWF();
  238.       }
  239.       
  240.       public function exit() : void
  241.       {
  242.          this.saveFirst(this.closeAndExit);
  243.       }
  244.       
  245.       public function stateChange(e:StateEvent) : void
  246.       {
  247.          this.canvasManager.stateChange(e);
  248.       }
  249.       
  250.       private function initUI() : void
  251.       {
  252.          this.ui = new UI(this);
  253.          this.ui.addEventListener(FileEvent.IO_EVENT,this.fileIOListener);
  254.          stage.nativeWindow.addEventListener(Event.CLOSE,this.windowClose);
  255.       }
  256.       
  257.       private function get invokeEnabled() : Boolean
  258.       {
  259.          var b:Boolean = false;
  260.          try
  261.          {
  262.             b = this._invokeEnabled && !this._stateManager.locked && !this.ui.locked;
  263.          }
  264.          catch(e:Error)
  265.          {
  266.             b = false;
  267.          }
  268.          return b;
  269.       }
  270.       
  271.       public function saveProjectAs() : void
  272.       {
  273.          if(GlobalSettings.FIRST_SAVE)
  274.          {
  275.             this.ui.alert({
  276.                "message":"Your first Livebrush project!\nTIP: Your project file will be created within a project folder. Livebrush will always create this structure for you.",
  277.                "yesFunction":this.fileManager.saveAsProject,
  278.                "yesProps":[this.canvasManager.getXML(),this.styleManager.getStyleXML()],
  279.                "id":"firstSaveAlert"
  280.             });
  281.             GlobalSettings.FIRST_SAVE = false;
  282.          }
  283.          else
  284.          {
  285.             this.fileManager.saveAsProject(this.canvasManager.getXML(),this.styleManager.getStyleXML());
  286.          }
  287.       }
  288.       
  289.       public function saveFirst(fnAfterSave:Function, props:Array = null) : void
  290.       {
  291.          this.fnAfterSave = fnAfterSave;
  292.          this.propsAfterSave = props;
  293.          if(StateManager.changed)
  294.          {
  295.             this.ui.confirmActionDialog({
  296.                "message":"There have been changes since you last saved.\n\nWould you like save first?",
  297.                "yesFunction":this.saveProject,
  298.                "noFunction":this.runFnAfterSave,
  299.                "id":"saveConfirm"
  300.             });
  301.          }
  302.          else
  303.          {
  304.             this.runFnAfterSave();
  305.          }
  306.       }
  307.       
  308.       public function export(type:String) : void
  309.       {
  310.          var name:String = null;
  311.          if(type == FileManager.DECOSET && GlobalSettings.FIRST_DECOSET_EXPORT || type == FileManager.STYLE && GlobalSettings.FIRST_STYLE_EXPORT)
  312.          {
  313.             name = type == FileManager.DECOSET ? "current style decoration list" : "current style";
  314.             this.ui.alert({
  315.                "message":"TIP: Your " + name + " and its assets will be created within a folder. Livebrush will always create this structure for you.",
  316.                "yesFunction":this._exportAssets,
  317.                "yesProps":[type],
  318.                "id":"firstExportAlert"
  319.             });
  320.             if(type == FileManager.DECOSET)
  321.             {
  322.                GlobalSettings.FIRST_DECOSET_EXPORT = false;
  323.             }
  324.             else
  325.             {
  326.                GlobalSettings.FIRST_STYLE_EXPORT = false;
  327.             }
  328.          }
  329.          else
  330.          {
  331.             this._exportAssets(type);
  332.          }
  333.       }
  334.       
  335.       private function _exportAssets(type:String) : void
  336.       {
  337.          switch(type)
  338.          {
  339.             case FileManager.DECOSET:
  340.                this.fileManager.saveDecoSet(this.styleManager.activeStyle.decoStyle.decoSet.getXML());
  341.                break;
  342.             case FileManager.STYLE:
  343.                this.fileManager.saveStyle(this.styleManager.activeStyle.getXML());
  344.                break;
  345.             case FileManager.LAYER_IMAGE:
  346.                this.fileManager.saveImage(this.canvasManager.getImage(this.canvasManager.activeLayerDepths));
  347.                break;
  348.             case FileManager.SVG:
  349.                this.ui.alert({
  350.                   "message":"PRO FEATURE\n\nPlease upgrade Livebrush to use this professional feature.",
  351.                   "yesFunction":FileManager.getURL,
  352.                   "yesProps":[Main.BUY_LINK],
  353.                   "id":"proFeature"
  354.                });
  355.          }
  356.       }
  357.       
  358.       public function bitmapReady(e:BitmapDataUnlimitedEvent) : void
  359.       {
  360.          this.ui.closeDialogID("saveImage");
  361.          this.ui.toggle(true);
  362.          setTimeout(this.fileManager.saveImage,100,this.canvasManager.getCanvasImage(e.target as BitmapDataUnlimited,this._outputRes,this._allLayers));
  363.          this._outputRes = 0;
  364.       }
  365.       
  366.       private function initToolManager() : void
  367.       {
  368.          this.toolManager = new ToolManager(this.ui,this.canvasManager,this.styleManager);
  369.          this.toolManager.addEventListener(UpdateEvent.LAYER,this.canvasManager.updateLayers);
  370.          this.toolManager.addEventListener(UpdateEvent.SELECTION,this.canvasManager.selectionUpdate);
  371.          this.toolManager.addEventListener(UpdateEvent.BEGIN,this.canvasManager.toolUpdate);
  372.          this.toolManager.addEventListener(UpdateEvent.FINISH,this.canvasManager.toolUpdate);
  373.       }
  374.       
  375.       private function projectInit(e:Event) : void
  376.       {
  377.          this.ui.toggleDrawMode(GlobalSettings.DRAW_MODE);
  378.          this.ui.toggle(true);
  379.          this._stateManager = StateManager.getInstance(this,this.canvasManager);
  380.          this.invokeEnabled = true;
  381.       }
  382.       
  383.       private function reset() : void
  384.       {
  385.          StateManager.reset();
  386.          this.canvasManager.reset();
  387.          this.styleManager.reset();
  388.       }
  389.       
  390.       public function cleanupProject() : void
  391.       {
  392.          this.saveFirst(this.fileManager.cleanupProject,[this.canvasManager.getXML(),this.styleManager.getStyleXML()]);
  393.       }
  394.       
  395.       public function newProject(sizeIndex:int = 2) : void
  396.       {
  397.          this.closeProject();
  398.          this.fileManager.newProject(sizeIndex);
  399.       }
  400.       
  401.       public function saveProject() : void
  402.       {
  403.          if(GlobalSettings.FIRST_SAVE)
  404.          {
  405.             this.ui.alert({
  406.                "message":"Your first Livebrush project!\nTIP: Your project file will be created within a project folder. Livebrush will always create this structure for you.",
  407.                "yesFunction":this.fileManager.saveProject,
  408.                "yesProps":[this.canvasManager.getXML(),this.styleManager.getStyleXML()],
  409.                "id":"firstSaveAlert"
  410.             });
  411.             GlobalSettings.FIRST_SAVE = false;
  412.          }
  413.          else
  414.          {
  415.             this.fileManager.saveProject(this.canvasManager.getXML(),this.styleManager.getStyleXML());
  416.          }
  417.       }
  418.       
  419.       private function initFileManager() : void
  420.       {
  421.          this.fileManager = FileManager.getInstance();
  422.          this.fileManager.addEventListener(FileEvent.BEGIN_LOAD,this.fileIOListener);
  423.          this.fileManager.addEventListener(FileEvent.FILE_NOT_FOUND,this.fileIOListener);
  424.          this.fileManager.addEventListener(FileEvent.WRONG_FILE,this.fileIOListener);
  425.          this.fileManager.addEventListener(FileEvent.IO_EVENT,this.fileIOListener,false,1);
  426.          this.fileManager.addEventListener(FileEvent.IO_EVENT,this.ui.fileIO);
  427.          this.fileManager.addEventListener(FileEvent.SAVE,this.fileIOListener);
  428.          this.fileManager.addEventListener(FileEvent.IO_ERROR,this.fileIOListener);
  429.          this.fileManager.addEventListener(FileEvent.VERSION_UPDATE,this.ui.promptForNewVersion);
  430.       }
  431.       
  432.       private function canvasManagerInit(e:CanvasEvent) : void
  433.       {
  434.          this.canvasManager.removeEventListener(CanvasEvent.INIT,this.canvasManagerInit);
  435.          this.initToolManager();
  436.          this.canvasManager.toolManager = this.toolManager;
  437.          this.canvasManager.styleManager = this.styleManager;
  438.          this.ui.toolManager = this.toolManager;
  439.          this.ui.canvasManager = this.canvasManager;
  440.          this.ui.styleManager = this.styleManager;
  441.          this.ui.initCanvasDependantViews();
  442.          this.fileManager.loadAppSettings();
  443.          this.fileManager.checkFirstRun();
  444.          this.ui.showLoadDialog({
  445.             "message":"Opening Project",
  446.             "loadPercent":0.1,
  447.             "id":"loadProject"
  448.          });
  449.          if(this.invokeFilePath != "")
  450.          {
  451.             setTimeout(this.fileManager.loadProject,1000,this.invokeFilePath);
  452.          }
  453.          else
  454.          {
  455.             setTimeout(this.fileManager.loadProject,1000,this.fileManager.getRecentProjectPath());
  456.          }
  457.       }
  458.       
  459.       private function windowClose(e:Event) : void
  460.       {
  461.          this.fileManager.saveGlobalSettings();
  462.          this.fileManager.cleanup();
  463.       }
  464.       
  465.       private function mainInit() : void
  466.       {
  467.          this.canvasManager = new CanvasManager(this,this.ui);
  468.          this.canvasManager.addEventListener(Event.COMPLETE,this.projectInit);
  469.          this.canvasManager.addEventListener(CanvasEvent.INIT,this.canvasManagerInit);
  470.          this.styleManager = new StyleManager(this.ui);
  471.          this.initFileManager();
  472.          this.canvasManager.setup();
  473.       }
  474.       
  475.       private function set invokeEnabled(b:Boolean) : void
  476.       {
  477.          this._invokeEnabled = b;
  478.       }
  479.       
  480.       public function openProject() : void
  481.       {
  482.          this.saveFirst(this.fileManager.openProject);
  483.       }
  484.       
  485.       public function importToProject(type:String) : void
  486.       {
  487.          switch(type)
  488.          {
  489.             case FileManager.DECO:
  490.                this.fileManager.openDeco();
  491.                break;
  492.             case FileManager.DECOSET:
  493.                this.fileManager.openDecoSet();
  494.                break;
  495.             case FileManager.STYLE:
  496.                this.fileManager.openStyle();
  497.                break;
  498.             case FileManager.LAYER_IMAGE:
  499.                this.fileManager.openLayerImage();
  500.                break;
  501.             case FileManager.PROJECT:
  502.                this.canvasManager.omitBg = true;
  503.                this.fileManager.importProject();
  504.          }
  505.       }
  506.       
  507.       private function closeAndExit() : void
  508.       {
  509.          this.closeProject();
  510.          stage.nativeWindow.close();
  511.       }
  512.    }
  513. }
  514.  
  515.