home *** CD-ROM | disk | FTP | other *** search
Wrap
package com.livebrush { import com.formatlos.as3.lib.display.BitmapDataUnlimited; import com.formatlos.as3.lib.display.events.BitmapDataUnlimitedEvent; import com.livebrush.data.FileManager; import com.livebrush.data.GlobalSettings; import com.livebrush.data.StateManager; import com.livebrush.events.*; import com.livebrush.graphics.canvas.CanvasManager; import com.livebrush.styles.StyleManager; import com.livebrush.tools.ToolManager; import com.livebrush.ui.UI; import flash.desktop.NativeApplication; import flash.display.MovieClip; import flash.events.Event; import flash.events.InvokeEvent; import flash.filesystem.*; import flash.utils.setTimeout; public class Main extends MovieClip { 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>"; public static const MAJOR_VERSION:int = 1; public static const HELP_LINK:String = "http://www.livebrush.com/help/index.html"; public static const SUPPORT_LINK:String = "http://www.livebrush.com/support.html"; public static const FEEDBACK_LINK:String = "http://www.livebrush.com/feedback.html"; public static const FORUM_LINK:String = "http://www.livebrush.com/forumCommunity.html"; public static const FORUM_STYLES_LINK:String = "http://www.livebrush.com/forumCommunity.html"; public static const FORUM_DECOS_LINK:String = "http://www.livebrush.com/forumCommunity.html"; public static const FORUM_REMIX_LINK:String = "http://www.livebrush.com/forumCommunity.html"; public static const FORUM_DEVELOP_LINK:String = "http://www.livebrush.com/forumCommunity.html"; public static const HOME_LINK:String = "http://www.livebrush.com"; public static const UPDATE_LINK:String = "http://www.livebrush.com/update.aspx"; public static const FACEBOOK_LINK:String = "http://www.facebook.com/Livebrush"; public static const TWITTER_LINK:String = "http://www.twitter.com/Livebrush"; public static const BUY_LINK:String = "http://www.Livebrush.com/Buy.aspx"; private var fnAfterSave:Function = null; private var ui:UI; private var fileManager:FileManager; private var _invokeEnabled:Boolean = false; private var canvasManager:CanvasManager; private var _stateManager:StateManager; private var styleManager:StyleManager; 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."; private var _allLayers:Boolean = true; private var invokeFilePath:String = ""; private var _outputRes:int = 0; private var propsAfterSave:Array = null; private var toolManager:ToolManager; public function Main() { super(); NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE,this.appInvoked); this.initUI(); stage.stageFocusRect = false; setTimeout(this.mainInit,1000); } private function closeProject() : void { StateManager.lock(); this.reset(); } private function fileIOListener(e:FileEvent) : void { var data:XML = null; if(e.type == FileEvent.SAVE) { switch(e.fileType) { case FileManager.PROJECT: this.ui.toggle(true); this.ui.setProject(this.fileManager.projectName); StateManager.changed = false; this.runFnAfterSave(); } } else if(e.type == FileEvent.IO_ERROR) { switch(e.fileType) { case FileManager.PROJECT: this.ui.toggle(true); this.ui.showErrorDialog({"message":"SAVE ERROR\nProject assets open or in-use by another application.\nA backup has been saved to your desktop."}); } } else if(e.type == FileEvent.BEGIN_LOAD) { switch(e.fileType) { case FileManager.PROJECT: UI.setStatus("Loading..."); this.ui.toggle(false); } } else if(e.type != FileEvent.FILE_NOT_FOUND) { if(e.type != FileEvent.WRONG_FILE) { if(e.action == FileEvent.OPEN || e.action == FileEvent.BATCH_OPEN) { switch(e.fileType) { case FileManager.PROJECT: this.ui.setProject(this.fileManager.projectName); data = new XML(e.data); if(data.@size.length() > 0) { this.canvasManager.canvas.setSize(int(data.@size)); } else { this.canvasManager.canvas.setSize(4); } setTimeout(this.canvasManager.setXML,100,data.layers); break; case FileManager.STYLE: this.styleManager.setStyleXML(new XML(e.data),this.fileManager.lastFileLoaded == FileManager.STYLE); break; case FileManager.INPUT_SWF: this.styleManager.setDynamicInput(new File(e.data.url).name); break; case FileManager.DECO: this.styleManager.activeStyle.decoStyle.addDeco(new File(e.data.url).name); this.styleManager.pushStyle(); break; case FileManager.LAYER_IMAGE: this.canvasManager.loadImageLayer(String(e.data),e.action == FileEvent.BATCH_OPEN); break; case FileManager.SWF: this.canvasManager.loadSWFLayer(String(e.data),e.action == FileEvent.BATCH_OPEN); } } else if(e.action == FileEvent.CLOSE) { this.closeProject(); } else if(e.action == FileEvent.IMPORT) { switch(e.fileType) { case FileManager.PROJECT: setTimeout(this.canvasManager.setXML,100,new XML(e.data).layers); } } } } } private function runFnAfterSave() : void { if(this.fnAfterSave != null) { if(this.propsAfterSave != null) { this.fnAfterSave.apply(this,this.propsAfterSave); } else { this.fnAfterSave(); } this.fnAfterSave = null; this.propsAfterSave = null; } } private function appInvoked(e:InvokeEvent) : * { if(e.arguments[0] is String) { this.invokeFilePath = e.arguments[0]; if(this.invokeEnabled) { this.closeProject(); this.saveFirst(this.fileManager.loadProject,[this.invokeFilePath]); } } } public function saveAsImage(res:int, allLayers:Boolean) : void { this._outputRes = res; this._allLayers = allLayers; this.ui.toggle(false); this.ui.showProcessDialog({ "message":"Preparing Image", "id":"saveImage" }); setTimeout(this.canvasManager.canvas.generateCanvasBitmap,100,res,this.bitmapReady,this.bitmapError); } public function revertProject() : void { this.ui.confirmActionDialog({ "message":"This action cannot be undone.\n\nWould you like to continue?", "yesFunction":this.fileManager.revertProject, "id":"revertConfirm" }); } public function bitmapError(e:BitmapDataUnlimitedEvent) : void { this.ui.toggle(true); } public function importInputSWF() : void { this.fileManager.openInputSWF(); } public function exit() : void { this.saveFirst(this.closeAndExit); } public function stateChange(e:StateEvent) : void { this.canvasManager.stateChange(e); } private function initUI() : void { this.ui = new UI(this); this.ui.addEventListener(FileEvent.IO_EVENT,this.fileIOListener); stage.nativeWindow.addEventListener(Event.CLOSE,this.windowClose); } private function get invokeEnabled() : Boolean { var b:Boolean = false; try { b = this._invokeEnabled && !this._stateManager.locked && !this.ui.locked; } catch(e:Error) { b = false; } return b; } public function saveProjectAs() : void { if(GlobalSettings.FIRST_SAVE) { this.ui.alert({ "message":"Your first Livebrush project!\nTIP: Your project file will be created within a project folder. Livebrush will always create this structure for you.", "yesFunction":this.fileManager.saveAsProject, "yesProps":[this.canvasManager.getXML(),this.styleManager.getStyleXML()], "id":"firstSaveAlert" }); GlobalSettings.FIRST_SAVE = false; } else { this.fileManager.saveAsProject(this.canvasManager.getXML(),this.styleManager.getStyleXML()); } } public function saveFirst(fnAfterSave:Function, props:Array = null) : void { this.fnAfterSave = fnAfterSave; this.propsAfterSave = props; if(StateManager.changed) { this.ui.confirmActionDialog({ "message":"There have been changes since you last saved.\n\nWould you like save first?", "yesFunction":this.saveProject, "noFunction":this.runFnAfterSave, "id":"saveConfirm" }); } else { this.runFnAfterSave(); } } public function export(type:String) : void { var name:String = null; if(type == FileManager.DECOSET && GlobalSettings.FIRST_DECOSET_EXPORT || type == FileManager.STYLE && GlobalSettings.FIRST_STYLE_EXPORT) { name = type == FileManager.DECOSET ? "current style decoration list" : "current style"; this.ui.alert({ "message":"TIP: Your " + name + " and its assets will be created within a folder. Livebrush will always create this structure for you.", "yesFunction":this._exportAssets, "yesProps":[type], "id":"firstExportAlert" }); if(type == FileManager.DECOSET) { GlobalSettings.FIRST_DECOSET_EXPORT = false; } else { GlobalSettings.FIRST_STYLE_EXPORT = false; } } else { this._exportAssets(type); } } private function _exportAssets(type:String) : void { switch(type) { case FileManager.DECOSET: this.fileManager.saveDecoSet(this.styleManager.activeStyle.decoStyle.decoSet.getXML()); break; case FileManager.STYLE: this.fileManager.saveStyle(this.styleManager.activeStyle.getXML()); break; case FileManager.LAYER_IMAGE: this.fileManager.saveImage(this.canvasManager.getImage(this.canvasManager.activeLayerDepths)); break; case FileManager.SVG: this.ui.alert({ "message":"PRO FEATURE\n\nPlease upgrade Livebrush to use this professional feature.", "yesFunction":FileManager.getURL, "yesProps":[Main.BUY_LINK], "id":"proFeature" }); } } public function bitmapReady(e:BitmapDataUnlimitedEvent) : void { this.ui.closeDialogID("saveImage"); this.ui.toggle(true); setTimeout(this.fileManager.saveImage,100,this.canvasManager.getCanvasImage(e.target as BitmapDataUnlimited,this._outputRes,this._allLayers)); this._outputRes = 0; } private function initToolManager() : void { this.toolManager = new ToolManager(this.ui,this.canvasManager,this.styleManager); this.toolManager.addEventListener(UpdateEvent.LAYER,this.canvasManager.updateLayers); this.toolManager.addEventListener(UpdateEvent.SELECTION,this.canvasManager.selectionUpdate); this.toolManager.addEventListener(UpdateEvent.BEGIN,this.canvasManager.toolUpdate); this.toolManager.addEventListener(UpdateEvent.FINISH,this.canvasManager.toolUpdate); } private function projectInit(e:Event) : void { this.ui.toggleDrawMode(GlobalSettings.DRAW_MODE); this.ui.toggle(true); this._stateManager = StateManager.getInstance(this,this.canvasManager); this.invokeEnabled = true; } private function reset() : void { StateManager.reset(); this.canvasManager.reset(); this.styleManager.reset(); } public function cleanupProject() : void { this.saveFirst(this.fileManager.cleanupProject,[this.canvasManager.getXML(),this.styleManager.getStyleXML()]); } public function newProject(sizeIndex:int = 2) : void { this.closeProject(); this.fileManager.newProject(sizeIndex); } public function saveProject() : void { if(GlobalSettings.FIRST_SAVE) { this.ui.alert({ "message":"Your first Livebrush project!\nTIP: Your project file will be created within a project folder. Livebrush will always create this structure for you.", "yesFunction":this.fileManager.saveProject, "yesProps":[this.canvasManager.getXML(),this.styleManager.getStyleXML()], "id":"firstSaveAlert" }); GlobalSettings.FIRST_SAVE = false; } else { this.fileManager.saveProject(this.canvasManager.getXML(),this.styleManager.getStyleXML()); } } private function initFileManager() : void { this.fileManager = FileManager.getInstance(); this.fileManager.addEventListener(FileEvent.BEGIN_LOAD,this.fileIOListener); this.fileManager.addEventListener(FileEvent.FILE_NOT_FOUND,this.fileIOListener); this.fileManager.addEventListener(FileEvent.WRONG_FILE,this.fileIOListener); this.fileManager.addEventListener(FileEvent.IO_EVENT,this.fileIOListener,false,1); this.fileManager.addEventListener(FileEvent.IO_EVENT,this.ui.fileIO); this.fileManager.addEventListener(FileEvent.SAVE,this.fileIOListener); this.fileManager.addEventListener(FileEvent.IO_ERROR,this.fileIOListener); this.fileManager.addEventListener(FileEvent.VERSION_UPDATE,this.ui.promptForNewVersion); } private function canvasManagerInit(e:CanvasEvent) : void { this.canvasManager.removeEventListener(CanvasEvent.INIT,this.canvasManagerInit); this.initToolManager(); this.canvasManager.toolManager = this.toolManager; this.canvasManager.styleManager = this.styleManager; this.ui.toolManager = this.toolManager; this.ui.canvasManager = this.canvasManager; this.ui.styleManager = this.styleManager; this.ui.initCanvasDependantViews(); this.fileManager.loadAppSettings(); this.fileManager.checkFirstRun(); this.ui.showLoadDialog({ "message":"Opening Project", "loadPercent":0.1, "id":"loadProject" }); if(this.invokeFilePath != "") { setTimeout(this.fileManager.loadProject,1000,this.invokeFilePath); } else { setTimeout(this.fileManager.loadProject,1000,this.fileManager.getRecentProjectPath()); } } private function windowClose(e:Event) : void { this.fileManager.saveGlobalSettings(); this.fileManager.cleanup(); } private function mainInit() : void { this.canvasManager = new CanvasManager(this,this.ui); this.canvasManager.addEventListener(Event.COMPLETE,this.projectInit); this.canvasManager.addEventListener(CanvasEvent.INIT,this.canvasManagerInit); this.styleManager = new StyleManager(this.ui); this.initFileManager(); this.canvasManager.setup(); } private function set invokeEnabled(b:Boolean) : void { this._invokeEnabled = b; } public function openProject() : void { this.saveFirst(this.fileManager.openProject); } public function importToProject(type:String) : void { switch(type) { case FileManager.DECO: this.fileManager.openDeco(); break; case FileManager.DECOSET: this.fileManager.openDecoSet(); break; case FileManager.STYLE: this.fileManager.openStyle(); break; case FileManager.LAYER_IMAGE: this.fileManager.openLayerImage(); break; case FileManager.PROJECT: this.canvasManager.omitBg = true; this.fileManager.importProject(); } } private function closeAndExit() : void { this.closeProject(); stage.nativeWindow.close(); } } }