Guest User

SpaceContainer.as

a guest
Dec 4th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sfxworks
  2. {
  3.     import flash.display.DisplayObject;
  4.     import flash.display.Loader;
  5.     import flash.display.MovieClip;
  6.     import flash.display.Sprite;
  7.     import flash.display.Stage;
  8.     import flash.events.Event;
  9.     import flash.events.MouseEvent;
  10.     import flash.filesystem.File;
  11.     import flash.filesystem.FileMode;
  12.     import flash.filesystem.FileStream;
  13.     /**
  14.      * ...
  15.      * @author Samuel Jacob Walker
  16.      */
  17.     public class SpaceContainer extends MovieClip
  18.     {
  19.         //Stage focus
  20.         private var stagee:Stage;
  21.        
  22.         private static const editframe:int = 7;
  23.         private static const mainFrame:int = 8;
  24.         private static const displayFrame:int = 9;
  25.        
  26.         private var currentSpaceObject:Space;
  27.         private var selectedSpaceFile:File;
  28.        
  29.         private static const desktopFile:File = File.applicationStorageDirectory.resolvePath(".spaces");
  30.        
  31.         public function SpaceContainer(stage:Stage) //Intro frames - > Editor frame -> Display Frame
  32.         {
  33.             stop();
  34.             stagee = stage;
  35.            
  36.             if (desktopFile.exists)
  37.             {
  38.                 gotoAndStop(mainFrame);
  39.                 this.x = (stagee.stageWidth - this.width) / 2;
  40.                 navbar_mc.editor_btn.addEventListener(MouseEvent.CLICK, handleGotoEditorClick);
  41.                 navbar_mc.loadspace_btn.addEventListener(MouseEvent.CLICK, handleSpaceLoadClick);
  42.             }
  43.             else
  44.             {
  45.                 next_btn.addEventListener(MouseEvent.CLICK, handleNextButton);
  46.                 back_btn.addEventListener(MouseEvent.CLICK, handleBackButton);
  47.             }
  48.         }
  49.        
  50.         public function initSpaceMenu():void //Main menu bar [Triggered on space on exiting]
  51.         {
  52.             //For first runs
  53.             var fs:FileStream = new FileStream();
  54.             fs.open(desktopFile, FileMode.WRITE);
  55.             fs.writeInt(1);
  56.             fs.close();
  57.            
  58.             //Remove current space object
  59.             removeChild(currentSpaceObject);
  60.             gotoAndStop(mainFrame); //Goto MainFrame
  61.            
  62.             this.x = (stagee.stageWidth - this.width) / 2;
  63.             //NavBar Event listeners
  64.             navbar_mc.editor_btn.addEventListener(MouseEvent.CLICK, handleGotoEditorClick);
  65.             navbar_mc.loadspace_btn.addEventListener(MouseEvent.CLICK, handleSpaceLoadClick);
  66.         }
  67.        
  68.         //Handle Display Space
  69.         private function handleSpaceLoadClick(e:MouseEvent):void
  70.         {
  71.             gotoAndStop(displayFrame);
  72.             selectedSpaceFile = File.applicationStorageDirectory.resolvePath("spaces");
  73.             selectedSpaceFile.browseForOpen("Select a space file.");
  74.             selectedSpaceFile.addEventListener(Event.SELECT, handleSpaceSelection);
  75.             selectedSpaceFile.addEventListener(Event.CANCEL, handleSpaceCancel);
  76.         }
  77.        
  78.         private function handleSpaceSelection(e:Event):void
  79.         {
  80.             try
  81.             {
  82.                 removeChild(currentSpaceObject);
  83.             }
  84.             catch (e:Error)
  85.             {
  86.                 trace("Nothing to remove but its ok");
  87.             }
  88.            
  89.             selectedSpaceFile.removeEventListener(Event.SELECT, handleSpaceSelection);
  90.             var space:Space = new Space(stagee, selectedSpaceFile.nativePath);
  91.             addChild(space);
  92.             swapChildren(navbar_mc, space);
  93.             currentSpaceObject = space;
  94.            
  95.             this.x = 0;
  96.             this.y = 0;
  97.         }
  98.        
  99.        
  100.         //Handle Cancel
  101.         private function handleSpaceCancel(e:Event):void
  102.         {
  103.             selectedSpaceFile.removeEventListener(Event.CANCEL, handleSpaceCancel);
  104.             gotoAndStop(mainFrame);
  105.         }
  106.        
  107.         //Handle Editor
  108.        
  109.         private function handleGotoEditorClick(e:MouseEvent):void
  110.         {
  111.             gotoAndStop(editframe);
  112.             selectedSpaceFile = File.applicationStorageDirectory.resolvePath("spaces");
  113.             selectedSpaceFile.browseForOpen("Select a space file to start editing.");
  114.             selectedSpaceFile.addEventListener(Event.SELECT, handleSpaceSelectionForEditing);
  115.             selectedSpaceFile.addEventListener(Event.CANCEL, handleSpaceCancel);
  116.         }
  117.        
  118.         private function handleSpaceSelectionForEditing(e:Event):void
  119.         {
  120.             try
  121.             {
  122.                 removeChild(currentSpaceObject);
  123.             }
  124.             catch (e:Error)
  125.             {
  126.                 trace("Nothing to remove but its ok");
  127.             }
  128.            
  129.             selectedSpaceFile.removeEventListener(Event.SELECT, handleSpaceSelectionForEditing);
  130.             gotoAndStop(editframe);
  131.             var space:Space = new Space(stagee, selectedSpaceFile.nativePath);
  132.             space.editMode();
  133.             addChild(space);
  134.             currentSpaceObject = space;
  135.             this.x = 0;
  136.             this.y = 0;
  137.         }
  138.        
  139.         //First desktop editor launch
  140.         private function beginFirstDesktop(e:MouseEvent):void
  141.         {
  142.             //Right click to launch menu
  143.             removeEventListener(MouseEvent.CLICK, beginFirstDesktop);
  144.             gotoAndStop(editframe);
  145.             var space:Space = new Space(stagee);
  146.             space.editMode();
  147.             addChild(space);
  148.             this.x = 0;
  149.             this.y = 0;
  150.             currentSpaceObject = space;
  151.         }
  152.        
  153.         //Navigation
  154.         private function handleNextButton(e:MouseEvent):void
  155.         {
  156.             if (currentFrame == 5)
  157.             {
  158.                 next_btn.removeEventListener(MouseEvent.CLICK, handleNextButton);
  159.                 back_btn.removeEventListener(MouseEvent.CLICK, handleBackButton);
  160.                
  161.                 addEventListener(MouseEvent.CLICK, beginFirstDesktop);
  162.             }
  163.             if (currentFrame != 1) //Back button is visible
  164.             {
  165.                 back_btn.visible = true;
  166.             }
  167.             nextFrame();
  168.         }
  169.        
  170.         private function handleBackButton(e:MouseEvent):void
  171.         {
  172.             prevFrame();
  173.             if (currentFrame == 1) //Back button goes invis if it's on the first frame
  174.             {
  175.                 back_btn.visible = false;
  176.             }
  177.         }
  178.         //Resize UTIL
  179.         private function resize(mc:DisplayObject, maxW:Number, maxH:Number = 0, constrainProportions:Boolean = true):void
  180.         {
  181.             maxH = maxH == 0 ? maxW : maxH;
  182.             mc.width = maxW;
  183.             mc.height = maxH;
  184.             if (constrainProportions)
  185.             {
  186.                 mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
  187.             }
  188.         }
  189.        
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment