| 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.ViewEvent; |
|---|
| 3 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 4 | |
|---|
| 5 | import flash.display.DisplayObject; |
|---|
| 6 | import flash.display.Sprite; |
|---|
| 7 | import flash.events.MouseEvent; |
|---|
| 8 | import flash.geom.ColorTransform; |
|---|
| 9 | import flash.geom.Rectangle; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | public class Slider extends Sprite { |
|---|
| 13 | public static var HORIZONTAL:String = "horizontal"; |
|---|
| 14 | public static var VERTICAL:String = "vertical"; |
|---|
| 15 | protected var _rail:Sprite; |
|---|
| 16 | protected var _buffer:Sprite; |
|---|
| 17 | protected var _progress:Sprite; |
|---|
| 18 | protected var _thumb:Sprite; |
|---|
| 19 | protected var _orientation:String; |
|---|
| 20 | protected var _currentThumb:Number = 0; |
|---|
| 21 | protected var _currentProgress:Number = 0; |
|---|
| 22 | protected var _currentBuffer:Number = 0; |
|---|
| 23 | /** Color object for frontcolor. **/ |
|---|
| 24 | protected var _front:ColorTransform; |
|---|
| 25 | /** Color object for lightcolor. **/ |
|---|
| 26 | protected var _light:ColorTransform; |
|---|
| 27 | /** Current width and height **/ |
|---|
| 28 | protected var _width:Number; |
|---|
| 29 | protected var _height:Number; |
|---|
| 30 | /** Currently dragging thumb **/ |
|---|
| 31 | protected var _dragging:Boolean; |
|---|
| 32 | /** Lock state of the slider **/ |
|---|
| 33 | protected var _lock:Boolean; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | //protected var _height:Number; |
|---|
| 37 | public function Slider(rail:Sprite, buffer:Sprite, progress:Sprite, thumb:Sprite, orientation:String) { |
|---|
| 38 | super(); |
|---|
| 39 | addEventListener(MouseEvent.MOUSE_DOWN, downHandler); |
|---|
| 40 | addEventListener(MouseEvent.MOUSE_OVER, overHandler); |
|---|
| 41 | addEventListener(MouseEvent.MOUSE_OUT, outHandler); |
|---|
| 42 | _rail = rail; |
|---|
| 43 | addElement(_rail, "rail", true); |
|---|
| 44 | _buffer = buffer; |
|---|
| 45 | addElement(_buffer, "buffer"); |
|---|
| 46 | _progress = progress; |
|---|
| 47 | addElement(_progress, "progress"); |
|---|
| 48 | _thumb = thumb ? thumb : new Sprite(); |
|---|
| 49 | addElement(_thumb, "thumb"); |
|---|
| 50 | _orientation = orientation; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | private function addElement(element:DisplayObject, name:String, visible:Boolean=false):void { |
|---|
| 55 | if (element) { |
|---|
| 56 | element.visible = visible; |
|---|
| 57 | addChild(element); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | protected function setThumb(progress:Number):void { |
|---|
| 63 | _currentThumb = progress; |
|---|
| 64 | if (_thumb) { |
|---|
| 65 | _thumb.visible = true; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | public function setProgress(progress:Number):void { |
|---|
| 71 | _currentProgress = progress; |
|---|
| 72 | if (_progress) { |
|---|
| 73 | _progress.visible = true; |
|---|
| 74 | } |
|---|
| 75 | setThumb(progress); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | public function setBuffer(buffer:Number):void { |
|---|
| 80 | _currentBuffer = buffer; |
|---|
| 81 | if (_buffer) { |
|---|
| 82 | _buffer.visible = true; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | public function resize(width:Number, height:Number):void { |
|---|
| 88 | var scale:Number = this.scaleX; |
|---|
| 89 | this.scaleX = 1; |
|---|
| 90 | _width = width * scale; |
|---|
| 91 | _height = height; |
|---|
| 92 | _rail.getChildByName("bitmap").width = _width; |
|---|
| 93 | if (_buffer) { |
|---|
| 94 | _buffer.getChildByName("bitmap").width = _width; |
|---|
| 95 | resizeElement(_buffer, _currentBuffer); |
|---|
| 96 | } |
|---|
| 97 | if (_progress && !_dragging) { |
|---|
| 98 | _progress.getChildByName("bitmap").width = _width; |
|---|
| 99 | resizeElement(_progress, _currentProgress); |
|---|
| 100 | } |
|---|
| 101 | if (_thumb && !_dragging) { |
|---|
| 102 | _thumb.x = _width * _currentThumb / 100; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | private function resizeElement(element:Sprite, maskpercentage:Number=100):void { |
|---|
| 108 | if (element) { |
|---|
| 109 | element.y = (_height - element.height) / 2; |
|---|
| 110 | if (_width && _height) { |
|---|
| 111 | var mask:Sprite; |
|---|
| 112 | if (element.mask) { |
|---|
| 113 | mask = element.mask as Sprite; |
|---|
| 114 | } else { |
|---|
| 115 | mask = new Sprite(); |
|---|
| 116 | mask.name = "mask"; |
|---|
| 117 | element.addChild(mask); |
|---|
| 118 | element.mask = mask; |
|---|
| 119 | } |
|---|
| 120 | mask.x = 0; |
|---|
| 121 | mask.graphics.clear(); |
|---|
| 122 | mask.graphics.beginFill(0x000000, 0); |
|---|
| 123 | mask.graphics.drawRect(element.x, 0, _width * maskpercentage / 100, _height); |
|---|
| 124 | mask.graphics.endFill(); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /** Handle mouse downs. **/ |
|---|
| 131 | private function downHandler(evt:MouseEvent):void { |
|---|
| 132 | if (_thumb && !_lock) { |
|---|
| 133 | var rct:Rectangle = new Rectangle(_rail.x, _thumb.y, _rail.width - _thumb.width, 0); |
|---|
| 134 | _thumb.startDrag(true, rct); |
|---|
| 135 | _dragging = true; |
|---|
| 136 | RootReference.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | /** Handle mouse releases. **/ |
|---|
| 142 | private function upHandler(evt:MouseEvent):void { |
|---|
| 143 | RootReference.stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler); |
|---|
| 144 | _thumb.stopDrag(); |
|---|
| 145 | _dragging = false; |
|---|
| 146 | var percent:Number = (_thumb.x - _rail.x) / (_rail.width - _thumb.width); |
|---|
| 147 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_CLICK, percent)); |
|---|
| 148 | setThumb(percent * 100); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /** Handle mouseouts. **/ |
|---|
| 153 | private function outHandler(evt:MouseEvent):void { |
|---|
| 154 | //slider.transform.colorTransform = front; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | /** Handle mouseovers. **/ |
|---|
| 159 | private function overHandler(evt:MouseEvent):void { |
|---|
| 160 | //slider.transform.colorTransform = light; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** Reset the slider to it's original state**/ |
|---|
| 164 | public function reset():void { |
|---|
| 165 | setBuffer(0); |
|---|
| 166 | setProgress(0); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | public function lock():void { |
|---|
| 170 | _lock = true; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | public function unlock():void{ |
|---|
| 174 | _lock = false; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | } |
|---|