| 1 | package com.longtailvideo.jwplayer.view { |
|---|
| 2 | import flash.display.DisplayObject; |
|---|
| 3 | import flash.display.MovieClip; |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | public class Stacker { |
|---|
| 7 | /** Reference to the clip to stack. **/ |
|---|
| 8 | public var clip:MovieClip; |
|---|
| 9 | /** SWF skin loader reference **/ |
|---|
| 10 | private var stack:Array; |
|---|
| 11 | /** Original width of the clip. **/ |
|---|
| 12 | private var _width:Number; |
|---|
| 13 | /** Latest width of the clip. **/ |
|---|
| 14 | private var latest:Number = 0; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Constructor. |
|---|
| 19 | * |
|---|
| 20 | * @param clp The MovieClip to manage. |
|---|
| 21 | **/ |
|---|
| 22 | public function Stacker(clp:MovieClip):void { |
|---|
| 23 | clip = clp; |
|---|
| 24 | analyze(); |
|---|
| 25 | }; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | /** Analyze the MovieClip and save its children. **/ |
|---|
| 29 | private function analyze():void { |
|---|
| 30 | _width = clip.width; |
|---|
| 31 | stack = new Array(); |
|---|
| 32 | for (var i:Number = 0; i < clip.numChildren; i++) { |
|---|
| 33 | var clp:DisplayObject = clip.getChildAt(i); |
|---|
| 34 | stack.push({c: clp, x: clp.x, n: clp.name, w: clp.width}); |
|---|
| 35 | } |
|---|
| 36 | stack.sortOn(['x', 'n'], [Array.NUMERIC, Array.CASEINSENSITIVE]); |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | /** Check if an child overlaps with others. **/ |
|---|
| 41 | private function overlaps(idx:Number):Boolean { |
|---|
| 42 | var min:Number = stack[idx].x; |
|---|
| 43 | var max:Number = stack[idx].x + stack[idx].w; |
|---|
| 44 | for (var i:Number = 0; i < stack.length; i++) { |
|---|
| 45 | if (i != idx && stack[i].c.visible == true && stack[i].w < _width && stack[i].x < max && stack[i].x + stack[i].w > min) { |
|---|
| 46 | //trace(stack[idx].n+' overlaps with '+stack[i].n); |
|---|
| 47 | //trace(stack[i].x+'-'+max+' / '+(stack[i].x+stack[i].w)+'-'+min); |
|---|
| 48 | return true; |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | return false; |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Rearrange the contents of the clip. |
|---|
| 57 | * |
|---|
| 58 | * @param wid The target width of the clip. |
|---|
| 59 | **/ |
|---|
| 60 | public function rearrange(wid:Number = undefined):void { |
|---|
| 61 | if (wid) { |
|---|
| 62 | latest = wid; |
|---|
| 63 | } |
|---|
| 64 | var rdf:Number = latest - width; |
|---|
| 65 | var ldf:Number = 0; |
|---|
| 66 | // first run through the entire stack, closing the gaps. |
|---|
| 67 | for (var i:Number = 0; i < stack.length; i++) { |
|---|
| 68 | if (stack[i].x > width / 2) { |
|---|
| 69 | stack[i].c.x = stack[i].x + rdf; |
|---|
| 70 | if (stack[i].c.visible == false && overlaps(i) == false) { |
|---|
| 71 | if (i < (stack.length - 1)) { |
|---|
| 72 | rdf -= stack[i].w + stack[i].x - stack[i - 1].x - stack[i - 1].w; |
|---|
| 73 | } else { |
|---|
| 74 | rdf -= stack[i].w + stack[i].x - stack[i - 1].x - stack[i - 1].w; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | } else { |
|---|
| 78 | stack[i].c.x = stack[i].x - ldf; |
|---|
| 79 | if (stack[i].c.visible == false && overlaps(i) == false) { |
|---|
| 80 | if (stack[i - 1].w > width / 4) { |
|---|
| 81 | ldf += stack[i].w + stack[i].x; |
|---|
| 82 | } else { |
|---|
| 83 | ldf += stack[i].w + stack[i].x - stack[i - 1].x - stack[i - 1].w; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | if (stack[i].w > width / 4) { |
|---|
| 88 | stack[i].c.width = Math.abs(stack[i].w + rdf + ldf); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | // if gaps were closed, move all rightside stuff to fill the width. |
|---|
| 92 | var dif:Number = latest - width - rdf; |
|---|
| 93 | if (dif > 0) { |
|---|
| 94 | for (var j:Number = 0; j < stack.length; j++) { |
|---|
| 95 | if (stack[j].x > width / 2) { |
|---|
| 96 | stack[j].c.x += dif; |
|---|
| 97 | } |
|---|
| 98 | if (stack[j].w > width / 4 && stack[j].w < width) { |
|---|
| 99 | stack[j].c.width += dif; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** Getter for the original width of the MC. **/ |
|---|
| 107 | public function get width():Number { |
|---|
| 108 | return _width; |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | public function insert(clp:MovieClip, nxt:MovieClip):void { |
|---|
| 113 | var fnd:Number = 0; |
|---|
| 114 | for (var j:Number = 0; j < stack.length; j++) { |
|---|
| 115 | if (stack[j].w >= _width) { |
|---|
| 116 | stack[j].w += clp.width; |
|---|
| 117 | } |
|---|
| 118 | if (stack[j].c == nxt && !fnd) { |
|---|
| 119 | fnd = j; |
|---|
| 120 | stack.splice(j, 0, {c: clp, x: stack[j].x, n: clp.name, w: clp.width}); |
|---|
| 121 | } else if (fnd) { |
|---|
| 122 | stack[j].x += clp.width; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | _width += clp.width; |
|---|
| 126 | rearrange(); |
|---|
| 127 | }; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|