1
8
9 import com.sekati.display.StageDisplay;
10
11
14 class com.sekati.utils.AlignUtils {
15
16
19 public static function alignCenter(item:Object,target:Object):Void {
20 xAlignCenter( item, target );
21 yAlignCenter( item, target );
22 }
23
24
27 public static function xAlignCenter(item:Object,target:Object):Void {
28 item._x = int( target._width / 2 - item._width / 2 );
29 }
30
31
34 public static function yAlignCenter(item:Object,target:Object):Void {
35 item._y = int( target._height / 2 - item._height / 2 );
36 }
37
38
41 public static function alignRight(item:Object,target:Object):Void {
42 xAlignRight( item, target );
43 yAlignRight( item, target );
44 }
45
46
49 public static function xAlignRight(item:Object,target:Object):Void {
50 item._x = int( target._width - item._width );
51 }
52
53
56 public static function yAlignRight(item:Object,target:Object):Void {
57 item._y = int( target._height - item._height );
58 }
59
60
63 public static function alignLeft(item:Object,target:Object):Void {
64 xAlignLeft( item, target );
65 yAlignLeft( item, target );
66 }
67
68
71 public static function xAlignLeft(item:Object,target:Object):Void {
72 item._x = int( target._x );
73 }
74
75
78 public static function yAlignLeft(item:Object,target:Object):Void {
79 item._y = int( target._y );
80 }
81
82
85 public static function stageAlignCenter(item:Object):Void {
86 stageAlignXCenter( item );
87 stageAlignYCenter( item );
88 }
89
90
93 public static function stageAlignXCenter(item:Object):Void {
94 item._x = int( StageDisplay.$._width / 2 - item._width / 2 );
95 }
96
97
100 public static function stageAlignYCenter(item:Object):Void {
101 item._y = int( StageDisplay.$._height / 2 - item._height / 2 );
102 }
103
104
107 public static function stageAlignRight(item:Object):Void {
108 item._x = int( StageDisplay.$._width - item._width );
109 }
110
111
114 public static function stageAlignBottom(item:Object):Void {
115 item._y = int( StageDisplay.$._height - item._height );
116 }
117
118
124 public static function scale(item:Object,scale:Number):Void {
125 item._xscale = scale,
126 item._yscale = scale;
127 }
128
129
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
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
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 }