1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  import org.aswing.geom.Point;
     6  import org.aswing.geom.Rectangle;
     7  
     8  /**
     9   * The Dimension class encapsulates the width and height of a componentin a single object.
    10   * @author iiley
    11   */
    12  class org.aswing.geom.Dimension{
    13  	public var width:Number;
    14  	public var height:Number;
    15  	
    16  	/**
    17  	 * Dimension(width:Number, height:Number)<br>
    18  	 * Dimension(dim:Dimension)<br>
    19  	 * Dimension()<br>
    20  	 */
    21  	public function Dimension(width, height:Number){
    22  		setSize(width, height);
    23  	}
    24  	
    25  	/**
    26  	 * <br>
    27  	 * setSize(width:Number, height:Number)<br>
    28  	 * setSize(dim:Dimension)<br>
    29  	 */
    30  	public function setSize(width, height:Number):Void{
    31  		if(width instanceof Dimension){
    32  			this.width = width.width;
    33  			this.height = width.height;
    34  		}else{
    35  			if(width == undefined) width = 0;
    36  			if(height == undefined) height = 0;
    37  			this.width = width;
    38  			this.height = height;
    39  		}
    40  	}
    41  	
    42  	/**
    43  	 * Increases the size by s and return its self(<code>this</code>).
    44  	 * @return <code>this</code>.
    45  	 */
    46  	public function increaseSize(s:Dimension):Dimension{
    47  		width += s.width;
    48  		height += s.height;
    49  		return this;
    50  	}
    51  	
    52  	/**
    53  	 * Decreases the size by s and return its self(<code>this</code>).
    54  	 * @return <code>this</code>.
    55  	 */
    56  	public function decreaseSize(s:Dimension):Dimension{
    57  		width -= s.width;
    58  		height -= s.height;
    59  		return this;
    60  	}
    61  	
    62  	/**
    63  	 * modify the size and return itself. 
    64  	 * <br>
    65  	 * change(d:Number)<br>
    66  	 * change(dw:Number, dh:Number)<br>
    67  	 */
    68  	public function change(d:Number, h:Number):Dimension{
    69  		width += d;
    70  		h = (h==undefined ? d:h);
    71  		height += h;
    72  		return this;
    73  	}
    74  	
    75  	/**
    76  	 * return a new size with this size with a change
    77  	 * <br>
    78  	 * changedSize(d:Number)<br>
    79  	 * changedSize(dw:Number, dh:Number)<br>
    80  	 */
    81  	public function changedSize(d:Number, h:Number):Dimension{
    82  		var s:Dimension = new Dimension(width, height);
    83  		return s.change(d, h);
    84  	}
    85  	
    86  	/**
    87  	 * return a new bounds with this size with a pos
    88  	 * <br>
    89  	 * getBounds(x:Number, y:Number)<br>
    90  	 * getBounds(p:Point)<br>
    91  	 * getBounds() pos (0,0)<br> 
    92  	 */
    93  	public function getBounds(x, y:Number):Rectangle{
    94  		var p:Point = new Point(x, y);
    95  		var r:Rectangle = new Rectangle();
    96  		r.setLocation(p);
    97  		r.setSize(width, height);
    98  		return r;
    99  	}
   100  	
   101  	public function equals(o:Object):Boolean{
   102  		var d:Dimension = Dimension(o);
   103  		return width===d.width && height===d.height;
   104  	}	
   105  	
   106  	public function toString():String{
   107  		return "Dimension("+width+","+height+")";
   108  	}
   109  }
   110