/** * The class gives an easy interface to align movieclips on Stage or to other movieclips, * and register -or not- these alignments to the Stage.onResize event. * @usage import sdf.alignManager * var aligner=alignManager.getInstance(); * aligner._align(mc,Stage,'R',2,0,true); * @author Sergio Daroca Fernández (funlab) * @version 1.0.1 * @since 06-02-2007 */ class sdf.alignManager { private var _alignments : Array = new Array (); private static var _instance : alignManager; /** * Private Constructor function, since alignManager is a singleton class. * It sets Stage.scaleMode to noScale and Stage.align to 'TL' (Top Left), * and it also makes the class the listener to the Stage.onResize Event. * * @usage Called by the getInstance method. * @return Nothing */ private function alignManager () { Stage.scaleMode = "noscale"; Stage.align = "TL"; Stage.addListener (this); } /** * Used to instantiate the class, returns a 'singleton' class instance. * Follows Collin Moock's Singleton example * @usage var aligner=sdf.alignManager.getInstance(); * @return alignManager instance */ public static function getInstance () : alignManager { if (!_instance) { _instance = new alignManager (); } return _instance; } /** * Aligns a movieclip to Stage or other scope (MovieClip), and registers (or not) the alignment to Stage onResize event. * Uses 'T' 'M' 'B' and 'L' 'C' 'R' strings to set the alignment (case unsensitive). * ('Top, Middle, Bottom' for vertical alignment) * ('Left, Center, Right' for horizontal alignment) * @usage sdf.alignManager.getInstance()._align(mc,Stage,'MR',6,0,true); * This will instantiate the class, align mc to the Stage vertical center * and to the right of the stage with a 6 pixels margin, and trigger the align action * anytime the Stage is resized * @param mc MovieClip to be aligned * @param scope Alignment's scope (Stage or MovieClip) * @param alignTo One of: 'T' 'M' 'B', and/or one of: 'L' 'C' 'R' * @param xmargin Horizontal offset/margin for alignment * @param ymargin Vertical offset/margin for alignment * @param register Wether to register the alignment for Stage.onResize method * @return Void */ public function _align (mc : MovieClip, scope : MovieClip, alignTo : String, xmargin : Number, ymargin : Number, register : Boolean) : Void { var xmargin = isNaN (xmargin) ? 0 : xmargin; var ymargin = isNaN (ymargin) ? 0 : ymargin; switch (scope) { case Stage : var _topHeight = Stage.height; var _topWidth = Stage.width; break; default : var _topHeight = scope._height; var _topWidth = scope._width; break; } var l = alignTo.length; while ( -- l > - 1) { var _alignTo = alignTo.charAt (l); switch (_alignTo) { case 'T' : case 't' : mc._y = Math.floor(0 + ymargin); break; case 'M' : case 'm' : mc._y = Math.floor(_topHeight / 2 - mc._height / 2 + ymargin); break; case 'B' : case 'b' : mc._y = Math.floor((_topHeight - mc._height) - ymargin); break; case 'L' : case 'l' : mc._x = Math.floor(0 + xmargin); break; case 'C' : case 'c' : mc._x = Math.floor(_topWidth / 2 - mc._width / 2 + xmargin); break; case 'R' : case 'r' : mc._x = Math.floor((_topWidth - mc._width) - xmargin); break; } } if (register) { var alignment : Object = {mc:mc, scope:scope, alignTo:alignTo, xmargin:xmargin, ymargin:ymargin}; _alignments.push(alignment); } } /** * Event Listener Method for Stage.onResize: * Alingments that have been registered will trigger on Stage.onResize event through this function * */ private function onResize () { var l = _alignments.length; while ( -- l > - 1) { var _args=_alignments[l]; var _args_array=new Array(); for(var a in _args) _args_array.push(_args[a]); _align (_args_array[0],_args_array[1],_args_array[2],_args_array[3],_args_array[4],false); } } }