| [1] | 1 | /** |
|---|
| 2 | * Simple class that handles stretching of displayelements. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.utils { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import flash.display.DisplayObject; |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | public class Stretcher { |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | /** Stretches the clip nonuniform to fit the container. **/ |
|---|
| 14 | public static var EXACTFIT:String = "exactfit"; |
|---|
| 15 | /** Stretches the clip uniform to fill the container, with parts being cut off. **/ |
|---|
| 16 | public static var FILL:String = "fill"; |
|---|
| 17 | /** No stretching, but the clip is placed in the center of the container. **/ |
|---|
| 18 | public static var NONE:String = "none"; |
|---|
| 19 | /** Stretches the clip uniform to fit the container, with bars added. **/ |
|---|
| 20 | public static var UNIFORM:String = "uniform"; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Resize a displayobject to the display, depending on the stretching. |
|---|
| 25 | * |
|---|
| 26 | * @param clp The display element to resize. |
|---|
| 27 | * @param wid The target width. |
|---|
| 28 | * @param hei The target height. |
|---|
| 29 | * @param typ The stretching type. |
|---|
| 30 | **/ |
|---|
| 31 | public static function stretch(clp:DisplayObject,wid:Number,hei:Number,typ:String='uniform') { |
|---|
| 32 | var xsc = wid/clp.width; |
|---|
| 33 | var ysc = hei/clp.height; |
|---|
| 34 | switch(typ.toLowerCase()) { |
|---|
| 35 | case 'exactfit': |
|---|
| 36 | clp.width = wid; |
|---|
| 37 | clp.height = hei; |
|---|
| 38 | break; |
|---|
| 39 | case 'fill': |
|---|
| 40 | if(xsc > ysc) { |
|---|
| 41 | clp.width *= xsc; |
|---|
| 42 | clp.height *= xsc; |
|---|
| 43 | } else { |
|---|
| 44 | clp.width *= ysc; |
|---|
| 45 | clp.height *= ysc; |
|---|
| 46 | } |
|---|
| 47 | break; |
|---|
| 48 | case 'none': |
|---|
| 49 | break; |
|---|
| 50 | case 'uniform': |
|---|
| 51 | if(xsc > ysc) { |
|---|
| 52 | clp.width *= ysc; |
|---|
| 53 | clp.height *= ysc; |
|---|
| 54 | } else { |
|---|
| 55 | clp.width *= xsc; |
|---|
| 56 | clp.height *= xsc; |
|---|
| 57 | } |
|---|
| 58 | break; |
|---|
| 59 | } |
|---|
| 60 | clp.x = Math.round(wid/2 - clp.width/2); |
|---|
| 61 | clp.y = Math.round(hei/2 - clp.height/2); |
|---|
| 62 | clp.width = Math.ceil(clp.width); |
|---|
| 63 | clp.height = Math.ceil(clp.height); |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | } |
|---|