PageRenderTime 40ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/cw/view/interfaceElements/VolumeController.as

https://bitbucket.org/404assassin/musicplayer
ActionScript | 240 lines | 154 code | 3 blank | 83 comment | 3 complexity | c500b11e850b53d1200cd655e1983044 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Christian Worley Development & Design
  3. // Copyright 2012 Christian Worley Development & Design
  4. // All Rights Reserved.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  7. // Package Declaration
  8. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  9. package com.cw.view.interfaceElements {
  10. /**
  11. * :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  12. * class description: VolumeController
  13. * language version: ActionScript 3.0
  14. * player version: Flash 10.0
  15. * author: christian
  16. * created: Jan 27, 2012
  17. * TODO:
  18. * :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  19. */
  20. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  21. // Imports
  22. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  23. import com.cw.control.observer.ISubject;
  24. import com.cw.utilities.math.DiagonalValue;
  25. import com.cw.view.shapeCreators.CreateShape;
  26. import com.greensock.TimelineLite;
  27. import com.greensock.TweenMax;
  28. import com.greensock.easing.Linear;
  29. import com.greensock.easing.Sine;
  30. import com.greensock.loading.LoaderMax;
  31. import com.greensock.loading.MP3Loader;
  32. import flash.display.Sprite;
  33. import flash.events.MouseEvent;
  34. import flash.geom.Rectangle;
  35. import org.casalib.util.StageReference;
  36. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  37. // Class characteristics
  38. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  39. public class VolumeController implements ISubject {
  40. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  41. // Private Variables
  42. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  43. private var theVolumeControllHolder:Sprite;
  44. private var theVolumeControllerWidth:int = 44;
  45. private var theVolumeControllerHeight:int = 22;
  46. private var levelBar:Sprite;
  47. private var volumeSliderSymbol:Sprite;
  48. private var volumeSliderSymbolClickArea:Sprite;
  49. private var diagonal:Number;
  50. private var volumeScaleY:Number;
  51. private var myTimeline:TimelineLite;
  52. private var observer:ISubject;
  53. private var currentTrack:String;
  54. private var currentTrackLoader:MP3Loader;
  55. private var volumeSlider:Sprite;
  56. private var theVolumeControlls:Sprite;
  57. private var volumeParam:Number;
  58. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  59. // Constructor
  60. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  61. public function VolumeController () {}
  62. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  63. // Public Interfaces
  64. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  65. public function initVolumeController ():void {
  66. addVolumeControllHolder();
  67. }
  68. public function getVolumeController ():Sprite {
  69. return theVolumeControlls;
  70. }
  71. /**
  72. * InvokedObserver interface, reference update, and subscription with
  73. * updated observer and adding subscription with addObserver(this).
  74. */
  75. public function addObserver (observer:ISubject):void {
  76. this.observer = observer;
  77. observer.addObserver(this);
  78. }
  79. /**
  80. * InvokedObserver notification
  81. */
  82. public function notifyObservers (infoObject:String):void {
  83. observer.notifyObservers(infoObject);
  84. }
  85. /**
  86. * remove an observer refrence from InvokedObserver
  87. */
  88. public function removeObserver (observer:ISubject):void {
  89. this.observer = observer;
  90. observer.removeObserver(this);
  91. }
  92. /**
  93. * Updates to all registared observers with string @param infoObject.
  94. */
  95. public function update (infoObject:String):void {
  96. if(infoObject.substring(0, 5) == 'track') {
  97. this.currentTrack = infoObject;
  98. newTrackVolumeUpdate();
  99. }
  100. if(hasOwnProperty(infoObject)) {
  101. this[infoObject]();
  102. }
  103. }
  104. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  105. // Private Methods
  106. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  107. private function newTrackVolumeUpdate():void{
  108. volumeParam = Math.abs(volumeScaleY);
  109. currentTrackLoader = LoaderMax.getLoader(currentTrack);
  110. TweenMax.to(currentTrackLoader, .01, {volume:Math.abs(volumeParam)});
  111. }
  112. /**
  113. * Create and add progress bar holder sprite which will act as our
  114. * returned Object. Next create and add a mask for all included objects.
  115. */
  116. private function addVolumeControllHolder ():void {
  117. theVolumeControllHolder = new Sprite();
  118. var volumeSymbolMask:Sprite = new Sprite();
  119. TweenMax.to(volumeSymbolMask, 0, {y:-theVolumeControllerHeight});
  120. theVolumeControllHolder.addChild(volumeSymbolMask);
  121. var theShapeCreator:CreateShape = new CreateShape();
  122. theShapeCreator.draw(CreateShape.SQUARE_FILLED, volumeSymbolMask, 0, 0, theVolumeControllerWidth, theVolumeControllerHeight);
  123. theVolumeControllHolder.mask = volumeSymbolMask;
  124. addLevelBar();
  125. }
  126. /**
  127. * Create the Progress Bar, set its initial state, and add to the
  128. * progressBarHolder sprite.
  129. */
  130. private function addLevelBar ():void {
  131. volumeScaleY = 1;
  132. volumeParam = Math.abs(volumeScaleY)
  133. levelBar = new Sprite();
  134. var theShapeCreator2:CreateShape = new CreateShape();
  135. theShapeCreator2.draw(CreateShape.SQUARE_FILLED, levelBar, 0, 0, theVolumeControllerWidth, theVolumeControllerHeight);
  136. TweenMax.to(levelBar, 0, {alpha:.25, x:theVolumeControllerWidth, y:0, height:theVolumeControllerHeight, rotation:180});
  137. theVolumeControllHolder.addChild(levelBar);
  138. addVolumeSlider();
  139. }
  140. /**
  141. * Create and add the scrubber symbol, set its initial state, and add
  142. * to the progressBarHolder sprite.
  143. */
  144. private function addVolumeSlider ():void {
  145. var shapeWidth:int = 5;
  146. var shapeHeight:int = 5;
  147. volumeSliderSymbol = new Sprite();
  148. var theShapeCreator:CreateShape = new CreateShape();
  149. theShapeCreator.draw(CreateShape.SQUARE_FILLED, volumeSliderSymbol, 0, 0, shapeHeight, shapeWidth);
  150. var theDiagonalValue:DiagonalValue = new DiagonalValue();
  151. theDiagonalValue.setShapeValue(shapeWidth, shapeHeight);
  152. diagonal = theDiagonalValue.getDiagonalValue();
  153. TweenMax.to(volumeSliderSymbol, 0, {alpha:.25, tint:0xFFFFFF, z:0, x:(theVolumeControllerWidth * .5) - (diagonal * .5), y:-theVolumeControllerHeight+volumeParam, rotation:-45});
  154. theVolumeControllHolder.addChild(volumeSliderSymbol);
  155. volumeControlls();
  156. }
  157. private function volumeControlls():void{
  158. theVolumeControlls = new Sprite();
  159. theVolumeControlls.addChild(theVolumeControllHolder);
  160. TweenMax.to(theVolumeControllHolder, 0, {y:theVolumeControllerHeight});
  161. clickAreaShape();
  162. }
  163. /**
  164. * Create and add the scrubber symbol click area.
  165. */
  166. private function clickAreaShape ():void {
  167. var clickAreaShapeWidth:int = 14;
  168. var clickAreaShapeHeight:int = 14;
  169. volumeSliderSymbolClickArea = new Sprite();
  170. var clickAreaShapeCreator:CreateShape = new CreateShape();
  171. clickAreaShapeCreator.draw(CreateShape.SQUARE_FILLED, volumeSliderSymbolClickArea, 0, 0, clickAreaShapeHeight, clickAreaShapeWidth);
  172. var theClickAreaDiagonalValue:DiagonalValue = new DiagonalValue();
  173. theClickAreaDiagonalValue.setShapeValue(clickAreaShapeWidth, clickAreaShapeHeight);
  174. var clickAreaDiagonal:Number = theClickAreaDiagonalValue.getDiagonalValue();
  175. TweenMax.to(volumeSliderSymbolClickArea, 0, {alpha:0, tint:0x000000, z:0, x:-(clickAreaDiagonal * .25), y:-(clickAreaDiagonal * .25)});
  176. volumeSliderSymbol.addChild(volumeSliderSymbolClickArea);
  177. volumeSlider = new Sprite();
  178. addDragEvents();
  179. }
  180. /**
  181. * Add mouse events for the scrubber.
  182. */
  183. private function addDragEvents ():void {
  184. volumeSliderSymbol.buttonMode = true;
  185. volumeSliderSymbol.doubleClickEnabled = true;
  186. volumeSliderSymbol.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  187. volumeSliderSymbol.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  188. volumeSliderSymbol.addEventListener(MouseEvent.MOUSE_OUT, placementTargetOut);
  189. volumeSliderSymbol.addEventListener(MouseEvent.MOUSE_OVER, placementTargetOver);
  190. }
  191. private function placementTargetOver (overEvent:MouseEvent):void {
  192. TweenMax.to(volumeSliderSymbol, .25, {alpha:1, ease:Sine.easeOut});
  193. }
  194. private function placementTargetOut (outEvent:MouseEvent):void {
  195. TweenMax.to(volumeSliderSymbol, .75, {alpha:.5, ease:Sine.easeOut});
  196. }
  197. /**
  198. * On mouse down;
  199. * add stage event listener for on release outside events.
  200. * pause the play loop update so we can move the scrubber without interruption.
  201. * asign startDrag to scrubber and update the progress bar width to match.
  202. */
  203. private function mouseDownHandler (event:MouseEvent):void {
  204. TweenMax.to(volumeSliderSymbol, .25, {alpha:1, tint:0x3399FF, ease:Sine.easeOut});
  205. StageReference.getStage().addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  206. var dragConstraintX:Number = (theVolumeControllerWidth * .5) - ((diagonal * .5) - .5);
  207. var dragConstraints:Rectangle = new Rectangle(dragConstraintX, 0, 0, -theVolumeControllerHeight);
  208. volumeSliderSymbol.startDrag(false, dragConstraints);
  209. volumeUpdate();
  210. }
  211. /**
  212. * Animate the progress bar with a loop to match the current postion
  213. * of the scrubber.
  214. */
  215. private function volumeUpdate ():void {
  216. volumeParam = Math.abs(volumeScaleY);
  217. volumeScaleY = (volumeSliderSymbol.y / theVolumeControllerHeight);
  218. myTimeline = new TimelineLite({onComplete:volumeUpdate});
  219. myTimeline.append(new TweenMax(levelBar, .01, {scaleY:Math.abs(volumeScaleY), ease:Linear}));
  220. currentTrackLoader = LoaderMax.getLoader(currentTrack);
  221. myTimeline.append(new TweenMax(currentTrackLoader, .01, {volume:Math.abs(volumeScaleY)}));
  222. }
  223. /**
  224. * On mouse up;
  225. * remove stage event listener for on release outside events.
  226. * stopDrag to the volume slider.
  227. * cancel update loop.
  228. */
  229. private function mouseUpHandler (event:MouseEvent):void {
  230. TweenMax.to(volumeSliderSymbol, .5, {alpha:.5, tint:0xFFFFFF, ease:Sine.easeOut});
  231. StageReference.getStage().removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  232. var currentTrackLoader:MP3Loader = LoaderMax.getLoader(currentTrack);
  233. volumeSliderSymbol.stopDrag();
  234. myTimeline.stop();
  235. }
  236. }
  237. }