| 1 | package com.longtailvideo.jwplayer.view { |
|---|
| 2 | import com.longtailvideo.jwplayer.player.Player; |
|---|
| 3 | import com.longtailvideo.jwplayer.plugins.PluginConfig; |
|---|
| 4 | |
|---|
| 5 | import flash.geom.Rectangle; |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | public class PlayerLayoutManager { |
|---|
| 9 | |
|---|
| 10 | public static var LEFT:String = "left"; |
|---|
| 11 | public static var RIGHT:String = "right"; |
|---|
| 12 | public static var TOP:String = "top"; |
|---|
| 13 | public static var BOTTOM:String = "bottom"; |
|---|
| 14 | public static var NONE:String = "none"; |
|---|
| 15 | |
|---|
| 16 | private var _player:Player; |
|---|
| 17 | |
|---|
| 18 | private var toLayout:Array; |
|---|
| 19 | private var noLayout:Array; |
|---|
| 20 | |
|---|
| 21 | private var remainingSpace:Rectangle; |
|---|
| 22 | |
|---|
| 23 | public function PlayerLayoutManager(player:Player) { |
|---|
| 24 | _player = player; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | public function resize(width:Number, height:Number):void { |
|---|
| 28 | toLayout = []; |
|---|
| 29 | noLayout = []; |
|---|
| 30 | |
|---|
| 31 | for each (var plugin:String in _player.config.pluginNames) { |
|---|
| 32 | addLayout(plugin); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | addLayout('playlist'); |
|---|
| 36 | addLayout('controlbar'); |
|---|
| 37 | addLayout('display'); |
|---|
| 38 | addLayout('dock'); |
|---|
| 39 | |
|---|
| 40 | remainingSpace = new Rectangle(0, 0, width, height); |
|---|
| 41 | generateLayout(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | private function addLayout(plugin:String):void { |
|---|
| 46 | var cfg:PluginConfig = _player.config.pluginConfig(plugin); |
|---|
| 47 | if (!_player.fullscreen && testPosition(cfg['position']) && Number(cfg['size']) > 0 ) { |
|---|
| 48 | toLayout.push(cfg); |
|---|
| 49 | } else { |
|---|
| 50 | noLayout.push(cfg); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public static function testPosition(pos:String):String { |
|---|
| 55 | if (!pos) { return ""; } |
|---|
| 56 | |
|---|
| 57 | switch (pos.toLowerCase()) { |
|---|
| 58 | case LEFT: |
|---|
| 59 | case RIGHT: |
|---|
| 60 | case TOP: |
|---|
| 61 | case BOTTOM: |
|---|
| 62 | return pos.toLowerCase(); |
|---|
| 63 | break; |
|---|
| 64 | default: |
|---|
| 65 | return ""; |
|---|
| 66 | break; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | protected function generateLayout():void { |
|---|
| 71 | if (toLayout.length == 0) { |
|---|
| 72 | for each(var item:PluginConfig in noLayout) { |
|---|
| 73 | item['visible'] = !(_player.fullscreen && testPosition(item['position'])); |
|---|
| 74 | assignSpace(item, remainingSpace); |
|---|
| 75 | } |
|---|
| 76 | _player.config.width = remainingSpace.width; |
|---|
| 77 | _player.config.height = remainingSpace.height; |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | var config:PluginConfig = toLayout.shift() as PluginConfig; |
|---|
| 82 | var pluginSpace:Rectangle = new Rectangle(); |
|---|
| 83 | var position:String = testPosition(config['position']); |
|---|
| 84 | var size:Number = config['size']; |
|---|
| 85 | |
|---|
| 86 | switch (position) { |
|---|
| 87 | case LEFT: |
|---|
| 88 | pluginSpace.x = remainingSpace.x; |
|---|
| 89 | pluginSpace.y = remainingSpace.y; |
|---|
| 90 | pluginSpace.width = size; |
|---|
| 91 | pluginSpace.height = remainingSpace.height; |
|---|
| 92 | remainingSpace.width -= size; |
|---|
| 93 | remainingSpace.x += size; |
|---|
| 94 | break; |
|---|
| 95 | case RIGHT: |
|---|
| 96 | pluginSpace.x = remainingSpace.x + remainingSpace.width - size; |
|---|
| 97 | pluginSpace.y = remainingSpace.y; |
|---|
| 98 | pluginSpace.width = size; |
|---|
| 99 | pluginSpace.height = remainingSpace.height; |
|---|
| 100 | remainingSpace.width -= size; |
|---|
| 101 | break; |
|---|
| 102 | case TOP: |
|---|
| 103 | pluginSpace.x = remainingSpace.x; |
|---|
| 104 | pluginSpace.y = remainingSpace.y; |
|---|
| 105 | pluginSpace.width = remainingSpace.width; |
|---|
| 106 | pluginSpace.height = size; |
|---|
| 107 | remainingSpace.height -= size; |
|---|
| 108 | remainingSpace.y += size; |
|---|
| 109 | break; |
|---|
| 110 | case BOTTOM: |
|---|
| 111 | pluginSpace.x = remainingSpace.x; |
|---|
| 112 | pluginSpace.y = remainingSpace.y + remainingSpace.height - size; |
|---|
| 113 | pluginSpace.width = remainingSpace.width; |
|---|
| 114 | pluginSpace.height = size; |
|---|
| 115 | remainingSpace.height -= size; |
|---|
| 116 | break; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | config['visible'] = true; |
|---|
| 120 | assignSpace(config, pluginSpace); |
|---|
| 121 | |
|---|
| 122 | generateLayout(); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | protected function assignSpace(cfg:PluginConfig, space:Rectangle):void { |
|---|
| 126 | cfg['width'] = space.width; |
|---|
| 127 | cfg['height'] = space.height; |
|---|
| 128 | cfg['x'] = space.x; |
|---|
| 129 | cfg['y'] = space.y; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | } |
|---|
| 134 | } |
|---|