1  /**
     2   * com.sekati.utils.AlignUtils
     3   * @version 1.0.6
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8   
     9  import com.sekati.display.StageDisplay;
    10  
    11  /**
    12   * Static class wrapping various Alignment and Scale utilities.
    13   */
    14  class com.sekati.utils.AlignUtils {
    15  
    16  	/**
    17  	 * center align object to target
    18  	 */
    19  	public static  function alignCenter(item:Object,target:Object):Void {
    20  		xAlignCenter( item, target );
    21  		yAlignCenter( item, target );
    22  	}
    23  
    24  	/**
    25  	 * horizontal center align object to target
    26  	 */
    27  	public static  function xAlignCenter(item:Object,target:Object):Void {
    28  		item._x = int( target._width / 2 - item._width / 2 );
    29  	}
    30  
    31  	/**
    32  	 * vertical  center  align object to target
    33  	 */
    34  	public static  function yAlignCenter(item:Object,target:Object):Void {
    35  		item._y = int( target._height / 2 - item._height / 2 );
    36  	}
    37  
    38  	/**
    39  	 * right align object to target
    40  	 */
    41  	public static  function alignRight(item:Object,target:Object):Void {
    42  		xAlignRight( item, target );
    43  		yAlignRight( item, target );
    44  	}
    45  
    46  	/**
    47  	 * horizontal right align object to target
    48  	 */
    49  	public static  function xAlignRight(item:Object,target:Object):Void {
    50  		item._x = int( target._width - item._width );
    51  	}
    52  
    53  	/**
    54  	 * vertical right align object to target
    55  	 */
    56  	public static  function yAlignRight(item:Object,target:Object):Void {
    57  		item._y = int( target._height - item._height );
    58  	}
    59  
    60  	/**
    61  	 * left align object to target
    62  	 */
    63  	public static  function alignLeft(item:Object,target:Object):Void {
    64  		xAlignLeft( item, target );
    65  		yAlignLeft( item, target );
    66  	}
    67  
    68  	/**
    69  	 * horizontal left align object to target
    70  	 */	
    71  	public static  function xAlignLeft(item:Object,target:Object):Void {
    72  		item._x = int( target._x );
    73  	}
    74  
    75  	/**
    76  	 * vertical left  align object to target
    77  	 */	
    78  	public static  function yAlignLeft(item:Object,target:Object):Void {
    79  		item._y = int( target._y );
    80  	}
    81  
    82  	/**
    83  	 * center align object to Stage
    84  	 */
    85  	public static  function stageAlignCenter(item:Object):Void {
    86  		stageAlignXCenter( item );
    87  		stageAlignYCenter( item );
    88  	}
    89  
    90  	/**
    91  	 * horizontal center align object to Stage
    92  	 */
    93  	public static  function stageAlignXCenter(item:Object):Void {
    94  		item._x = int( StageDisplay.$._width / 2 - item._width / 2 );
    95  	}
    96  
    97  	/**
    98  	 * vertical  center align object to Stage
    99  	 */
   100  	public static  function stageAlignYCenter(item:Object):Void {
   101  		item._y = int( StageDisplay.$._height / 2 - item._height / 2 );
   102  	}
   103  
   104  	/**
   105  	 * Align object to Stage right
   106  	 */
   107  	public static  function stageAlignRight(item:Object):Void {
   108  		item._x = int( StageDisplay.$._width - item._width );
   109  	}
   110  
   111  	/**
   112  	 * Align object to Stage bottom
   113  	 */
   114  	public static  function stageAlignBottom(item:Object):Void {
   115  		item._y = int( StageDisplay.$._height - item._height );
   116  	}
   117  
   118  	/**
   119  	 * set scale wrapper
   120  	 * @param item (Object) item to be scaled 
   121  	 * @param scale (Number) scale percentage [0-100]
   122  	 * @return Void
   123  	 */
   124  	public static  function scale(item:Object,scale:Number):Void {
   125  		item._xscale = scale,
   126  		item._yscale = scale;
   127  	}
   128  
   129  	/**
   130  	 * scale target item to fit within target confines
   131  	 * @param item (Object) item to be aligned 
   132  	 * @param targetW (Number) target item width
   133  	 * @param targetH (Number) target item height
   134  	 * @param center (Boolean) center object
   135  	 * @return Void
   136  	 */
   137  	public static  function scaleToFit(item:Object,targetW:Number,targetH:Number,center:Boolean):Void {
   138  		if (item._width < targetW && item._width > item._height) {
   139  			item._width = targetW;
   140  			item._yscale = item._xscale;
   141  		} else {
   142  			item._height = targetH;
   143  			item._xscale = item._yscale;
   144  		}
   145  		if (center) {
   146  			item._x = int( targetW / 2 - item._width / 2 );
   147  			item._y = int( targetH / 2 - item._height / 2 );
   148  		}
   149  	}
   150  
   151  	/**
   152  	 * scale while retaining original w:h ratio
   153  	 * @param item (Object) item to be scaled
   154  	 * @param targetW (Number) target item width
   155  	 * @param targetH (Number) target item height
   156  	 * @return Void
   157  	 */
   158  	public static  function scaleRatio(item:Object,targetW:Number,targetH:Number):Void {
   159  		if (targetW / targetH < item._height / item._width) {
   160  			targetW = targetH * item._width / item._height;
   161  		} else {
   162  			targetH = targetW * item._height / item._width;
   163  		}
   164  		item._width = targetW;
   165  		item._height = targetH;
   166  	}
   167  
   168  	/**
   169  	 * flip object on an axis
   170  	 * @param obj (Object) item to flip
   171  	 * @param axis (String) axis to flip on ["_x" or "_y"]
   172  	 * @return Void
   173  	 * @throws Error on invalid axis 
   174  	 */
   175  	public static  function flip(obj:Object,axis:String):Void {
   176  		if (axis != "_x" && axis != "_y") {
   177  			throw new Error( "@@@ com.sekati.utils.AlignUtils.flip() Error: expects axis param: '_x' or '_y'." );
   178  			return;
   179  		}
   180  		var _scale:String = axis == "_x" ? "_xscale" : "_yscale";
   181  		var _prop:String = axis == "_x" ? "_width" : "_height";
   182  		obj[_scale] = -obj[_scale];
   183  		obj[axis] -= obj[_prop];
   184  	}
   185  
   186  	private function AlignUtils() {
   187  	}
   188  }