//----------------------------------------------------------------------------- // Copyright (c) 2002 Ryerson University. All Rights Reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. //----------------------------------------------------------------------------- // This software was written in order to experiment with the Macromedia // Flash Communication Server MX and is not intended to demonstrate the best // way to write code for Flash or FlashCom. //----------------------------------------------------------------------------- // // HashCodeManager.asc and HashCodeManager.as Version 1.00 // Written by Brian Lesser // September/2002 //----------------------------------------------------------------------------- /** * A HashCodeManager hands out unique "_hash_Code_" values to any object that * needs one. * The HashCodeManagerClass shoud be instantiated as a single object this way: * * hashCodeManager = new HashCodeManagerClass(); // Do this early in the first * frame. * * To use it with movie clips write constructor and onUnload handlers * this way: * * function MyClip(){ * hashCodeManager.setID(this); * } * * MyClip.prototype.onUnload = function(){ * hashCodeManager.freeID(this); * } * * Note: If you use a constructor and manually place the clip on the stage * the hashCodeManager must be instantiated on a previous frame. * Otherwise use the onLoad method to call setID. For example: * * MyClip.prototype.onLoad = function(){ * hashCodeManager.setID(this); * } * * The HashCodeManager maintains a simple numbering system and a free pool. * It is not meant as a robust way to hand out unique hash names for objects * but it works if used carefully. It is designed to be used in both * sclient-side as files and server-side asc files. * See the HashCodeManager.fla movie for a simple test example. */ function HashCodeManagerClass(){ this.nextID = 1; this.freeIDs = new Array(); // Make it difficult to create more than one instance. HashCodeManagerClass = null; } /** * setID is passed a reference to an object and provides it with a unique * _hash_Code_ property. It uses ids from the freeIDs pool if there are any. * Try calling this within a movie clip's constructor or onLoad() method. */ HashCodeManagerClass.prototype.setID = function(obj){ if (obj._hash_Code_ == undefined){ if (this.freeIDs.length > 0){ obj._hash_Code_ = this.freeIDs.pop(); } else{ obj._hash_Code_ = this.nextID++; } } } /** * freeID is passed an object reference and reclaims the _hash_Code_ of * the object if it has one. Try calling this when a movie clip is * about to be unloaded or disposed of. */ HashCodeManagerClass.prototype.freeID = function(obj){ if (obj._hash_Code_ != undefined){ this.freeIDs.push(obj._hash_Code_); } } /** * showAll returns a string showing the current state of the * oHashCodeManager that can be displayed in a text window or * using trace. */ HashCodeManagerClass.prototype.showAll = function(){ var buffer = "Free pool length: " + this.freeIDs.length + "\nNext free id is: " + this.nextID + "\n"; if (this.freeIDs.length > 0) buffer += "Free IDs:\n"; for (var i = 0; i < this.freeIDs.length; i++){ buffer += "Slot: " + i + ": " + this.freeIDs[i] + "\n";; } return buffer; } // Create the one global hashCodeManager. if (typeof hashCodeManager == undefined){ if (typeof _global != undefined){ _global.hashCodeManager = new HashCodeManagerClass(); } else if(application){ application.hashCodeManager = new HashCodeManagerClass(); hashCodeManager = application.hashCodeManager; } }