| 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(idx:Number):Boolean { |
|---|
| 50 | var min = stack[idx].x; |
|---|
| 51 | var max = stack[idx].x+stack[idx].w; |
|---|
| 52 | for (var i in stack) { |
|---|
| 53 | if(i!=idx && stack[i].c.visible==true && stack[i].w < _width && |
|---|
| 54 | stack[i].x < max && stack[i].x+stack[i].w > min) { |
|---|
| 55 | //trace(stack[idx].n+'overlaps with'+stack[i].n); |
|---|
| 56 | //trace(stack[i].x+'-'+max+' / '+(stack[i].x+stack[i].w)+'-'+min); |
|---|
| 57 | return true; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | return false; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Rearrange the contents of the clip. |
|---|
| 66 | * |
|---|
| 67 | * @param wid The target width of the clip. |
|---|
| 68 | **/ |
|---|
| 69 | public function rearrange(wid:Number=undefined) { |
|---|
| 70 | if(wid) { latest = wid; } |
|---|
| 71 | var rdf = latest-width; |
|---|
| 72 | var ldf = 0; |
|---|
| 73 | // first run through the entire stack, closing the gaps. |
|---|
| 74 | for(var i=0; i<stack.length; i++) { |
|---|
| 75 | if(stack[i].x > width/2) { |
|---|
| 76 | stack[i].c.x = stack[i].x + rdf; |
|---|
| 77 | if(stack[i].c.visible == false && overlaps(i) == false) { |
|---|
| 78 | rdf -= stack[i+1].x - stack[i].x; |
|---|
| 79 | } |
|---|
| 80 | } else { |
|---|
| 81 | stack[i].c.x = stack[i].x-ldf; |
|---|
| 82 | if(stack[i].c.visible == false && overlaps(i) == false) { |
|---|
| 83 | ldf += stack[i+1].x - stack[i].x; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | if(stack[i].w > width/3) { |
|---|
| 87 | stack[i].c.width = stack[i].w+rdf+ldf; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | // if gaps were closed, move all rightside stuff to fill the width. |
|---|
| 91 | var dif = latest-width-rdf; |
|---|
| 92 | if(dif>0) { |
|---|
| 93 | for(var j=0; j<stack.length; j++) { |
|---|
| 94 | if(stack[j].x > width/2) { |
|---|
| 95 | stack[j].c.x += dif; |
|---|
| 96 | } |
|---|
| 97 | if(stack[j].w>width/4 && stack[j].n!='back') { |
|---|
| 98 | stack[j].c.width += dif; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** Getter for the original width of the MC. **/ |
|---|
| 106 | public function get width():Number { |
|---|
| 107 | return _width; |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | } |
|---|