package SDTMods { import flash.events.*; import flash.display.*; import flash.geom.*; import flash.net.*; import SDTMods.ModTypes; import SDTMods.ModElements; //import SDTMods.ModPackage; public dynamic class ModPackage extends MovieClip { // standard ModPackage Properties public const MOD_VERSION:String = "SDTMOD_0.2"; public var modType:String = ModTypes.MOD_PACKAGE; private var _mods:Array = new Array(); public var modName:String = "Hair Loader"; public var modCreator:String = "Hugo_Boss"; // unused Properties, we keep them anyway public var charData:String = ""; public var dialogueName:String = ""; public var clearAllDialogue:Boolean = false; public var targetElement:String; public var overwrite:Boolean; private var warningsGenerated:Boolean = false; // my new kickass Properties for the loader functionality public var main; public var hairldr: HairLoader; public var hairTop: Hair; public var hairUnder:Hair; public var hairBack:Hair; public var ready = false // true, when everything is instanciated // some standard PublicModPackage functions. better keep those public function registerMod(newMod:*):void { _mods.push(newMod); } public function get mods():Array { return _mods; } // public ModPackage Functions, we won't need. Better keep the name, but no need to implement public function registerDialogue(lineType:String, dialogueLine:String, soundName:String = null, vol:Number = 0):void {} public function setupDialogue(dialogueLibraryName:String, clearAll:Boolean = true):void {} public function checkMod():void{} // Modified Constructor public function ModPackage() { super(); // initialize Hair Parts and Hairloader this.hairTop = new Hair(ModElements.HAIR_TOP); this.hairUnder = new Hair(ModElements.HAIR_UNDER); this.hairBack = new Hair(ModElements.HAIR_BACK); this.registerMod(this.hairTop); this.registerMod(this.hairUnder); this.registerMod(this.hairBack); this.hairldr = new HairLoader(); // parent is not defined in constructor yet, and therefor we are not ready yet this.addEventListener(Event.ADDED, this.onAdded); } function onAdded (e) { // Executes when MpodPackage is added to sdt or anobjec is added to ModPackage // chack, if Loaded by sdt if (this.parent != null && this.parent.parent!= null) { // package was loaded, we can stop listening this.removeEventListener(Event.ADDED, this.onAdded); this.main = this.parent.parent; if (this.main.loaderMenu != null) { // main is loader this.hairldr.setProperties(this.main, this); this.hairTop.setProperties(this.main, this.hairldr); this.hairUnder.setProperties(this.main, this.hairldr); this.hairBack.setProperties(this.main, this.hairldr); this.ready = true; //this.main.updateStatus("Loader found"); this.hairldr.loadHair(); } else { throw new Error("Error loading Hair Costume Loader: You don't seem to be using ModGuy's Loader. This Mod will not work with Vanilla SDT!"); } } else { // wrong alarm, some object was added to package, keep listening } } } public dynamic class Hair extends MovieClip { public var modType:String = ModTypes.HAIR; public var targetElement:String; public var overwrite:Boolean = true; public var hairldr; public var main; var rect; public var added = false; public var ready = false; // true, when added and all Properties defined public var complete = false; // true, when all operatiions are done public function Hair(targetElement:String) { super(); this.targetElement = targetElement; Debug.checkUndefined(this.targetElement, "this.targetElement"); this.addEventListener(Event.ADDED, this.onAdded); } public function setProperties (main, hairldr) { this.main = main; this.hairldr = hairldr; Debug.checkUndefined(this.hairldr, "this.hairldr"); Debug.checkUndefined(this.main, "this.main"); //this.main.updateStatus("Hair Properties set"); this.tryToSetReady(); } function onAdded (e) { // Executes when Hair is added to sdt or a child is added to Hair // chack if loaded if (this.parent != null) { // hair was loaded, we can stop listening //throw new Error (this + this.name + this.parent + this.parent.name); this.removeEventListener(Event.ADDED, this.onAdded); this.added= true; this.addRect(); this.tryToSetReady(); //throw new Error("Hair added"); } else { // wrong alarm, some object was added to hair, keep listening } } public function tryToSetReady() { //throw new Error("try to set ready"); if (this.main != null) { //this.main.updateStatus(" main= "+ this.main + " hairldr=" + this.hairldr + " added=" + this.added); if (this.added != null) { //this.main.updateStatus("check parent"); if (this.hairldr != null) { //this.main.updateStatus("check hairldr"); //throw new Error("Hair is Ready"); this.readyToLoad(); } } } } public function readyToLoad() { // executed after hair is added to sdt and all properties were defined this.ready = true; //throw new Error("Hair is Ready"); //this.main.updateStatus("Hair is ready"); this.tryToAddBMP(); } public function tryToAddBMP() { //this.main.updateStatus("try to add bmp"); if ( this.ready && this.hairldr.complete) { // this. addBMP(); } else if ( this.ready && this.hairldr.fail) { // image not found this.complete = true; this.ready = false; throw new Error("Failed to load AddHair.png"); } else { // keep waiting } } public function addRect() : void { this.rect = new Shape(); rect.graphics.beginFill(0xFFFFFF); rect.graphics.drawRect(0, 0, 100, 100); rect.graphics.endFill(); this.addChild(this.rect); } public function addBMP() { //this.main.updateStatus("Adding bmp"); Debug.checkUndefined(this.hairldr, "this.hairldr"); var bmp = null; switch (this.targetElement) { case ModElements.HAIR_TOP: bmp = this.hairldr.topBMP; break; case ModElements.HAIR_UNDER: bmp = this.hairldr.underBMP; break; case ModElements.HAIR_BACK: bmp = this.hairldr.backBMP; break; } Debug.checkUndefined(bmp, "bmp"); this.addChild(bmp); this.complete = true; /* at his point this.parent point to null * and this.rect point to null */ //this.main.updateStatus(this); //this.main.updateStatus(this.numChildren); //this.main.updateStatus(this.parent); //this.main.updateStatus(this.rect); //this.main.updateStatus(this.rect.parent); } } public class HairLoader { public var main; public var modPKG; public var ready = false; // true, when ready to load public var loading = false; public var complete = false; public var fail = false; public var topBMP; public var underBMP; public var backBMP; private var hairImportSource; private var hairTemplate; public function setProperties (main, modPKG) { this.main = main; this.modPKG = modPKG; this.ready = true; } public function loadHair() { if (this.ready) { var ldr = new Loader(); ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.blank); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,this.fileFound); this.loading = true; this.ready = false; ldr.load(new URLRequest("Mods/" + this.main.cData + "/HairAdd.png")); } else { throw new Error("Error: HairLoader is not ready to Load yet"); } } private function blank(e) { // file not found this.loading = false; this.fail = true; this.main.updateStatus ("HairAdd.png not found"); } private function fileFound(loader) { var content = loader.target.content; var contentSize = new Point(content.width,content.height); if(contentSize.x <= 1200 && contentSize.y <= 1200) { hairTemplate = new Rectangle(0,0,600,600); } else { hairTemplate = new Rectangle(0,0,Math.ceil(contentSize.x * 1/2),Math.ceil(contentSize.y * 1/2)); } hairImportSource = new BitmapData(contentSize.x,contentSize.y,true,0); hairImportSource.draw(content); var cutFrame = new Rectangle(0,0,hairTemplate.width,hairTemplate.height); topBMP = createHairPart(cutFrame); cutFrame.x = hairTemplate.width; this.underBMP = createHairPart(cutFrame); cutFrame.x = 0; cutFrame.y = hairTemplate.height; this.backBMP = createHairPart(cutFrame); this.loading = false; this.complete = true; this.main.updateStatus("HairAdd.png loaded"); this.informHairParts(); } private function createHairPart(cutFrame : Rectangle) : Bitmap { var bmpData:* = new BitmapData(hairTemplate.width,hairTemplate.height,true,0); var bmp = new Bitmap(bmpData,"auto",true); bmpData.copyPixels(hairImportSource,cutFrame,new Point()); bmp.x = -360; bmp.y = -560; bmp.rotation = 15; bmp.scaleX = 20/17; bmp.scaleY = 20/17; //this.main.g.hairTop.addChild(bmp); //this.hairTop.addChild(topBMP); return bmp; } private function informHairParts() { this.modPKG.hairTop.tryToAddBMP(); this.modPKG.hairUnder.tryToAddBMP(); this.modPKG.hairBack.tryToAddBMP(); } } public class Debug { public static function checkUndefined(obj, n:String = "object") : Boolean { if (obj == null) { throw new Error("Error: " + n + " undefined "); return true; } return false; } } }