| 1 | /** |
|---|
| 2 | * Parses children of a MovieClip and docks them to the left & right. |
|---|
| 3 | **/ |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | package com.jeroenwijering.utils { |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import flash.display.MovieClip; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | public class Stacker { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | /** Reference to the clip to stack. **/ |
|---|
| 16 | public var clip:MovieClip; |
|---|
| 17 | /** SWF skin loader reference **/ |
|---|
| 18 | private var stack:Array; |
|---|
| 19 | /** Original width of the clip. **/ |
|---|
| 20 | private var _width:Number; |
|---|
| 21 | /** Latest width of the clip. **/ |
|---|
| 22 | private var latest:Number; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Constructor. |
|---|
| 27 | * |
|---|
| 28 | * @param skn The MovieClip to manage stacking of. |
|---|
| 29 | **/ |
|---|
| 30 | public function Stacker(clp:MovieClip) { |
|---|
| 31 | clip = clp; |
|---|
| 32 | analyze(); |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** Analyze the MovieClip and save its children. **/ |
|---|
| 37 | private function analyze() { |
|---|
| 38 | _width = clip.width; |
|---|
| 39 | stack = new Array(); |
|---|
| 40 | for(var i=0; i<clip.numChildren; i++) { |
|---|
| 41 | var clp = clip.getChildAt(i); |
|---|
| 42 | stack.push({c:clp,x:clp.x,n:clp.name,w:clp.width}); |
|---|
| 43 | } |
|---|
| 44 | stack.sortOn(['x','n'],[Array.NUMERIC,Array.CASEINSENSITIVE]); |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** Check if an child overlaps with others. **/ |
|---|
| 49 | private function overlaps() { |
|---|
| 50 | // working on this... |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Rearrange the contents of the clip. |
|---|
| 56 | * |
|---|
| 57 | * @param wid The target width of the clip. |
|---|
| 58 | **/ |
|---|
| 59 | public function rearrange(wid:Number=undefined) { |
|---|
| 60 | if(wid) { latest = wid; } |
|---|
| 61 | var rdf = latest-width; |
|---|
| 62 | var ldf = 0; |
|---|
| 63 | for(var i=0; i<stack.length; i++) { |
|---|
| 64 | if(stack[i].x > stack[0].w/2) { |
|---|
| 65 | stack[i].c.x = stack[i].x + rdf; |
|---|
| 66 | } else { |
|---|
| 67 | stack[i].c.x = stack[i].x+ldf; |
|---|
| 68 | } |
|---|
| 69 | if(stack[i].w > width/3) { |
|---|
| 70 | stack[i].c.width = stack[i].w+rdf+ldf; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | /** Getter for the original width of the MC. **/ |
|---|
| 77 | public function get width():Number { |
|---|
| 78 | return _width; |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | } |
|---|