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