PageRenderTime 7221ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Jaris/src/jaris/display/Loader.hx

https://gitlab.com/dali99/shimmie2-Material-Theme
Haxe | 181 lines | 110 code | 22 blank | 49 comment | 4 complexity | 36a3f7114306a312bfd08a9fd733e63c MD5 | raw file
  1. /**
  2. * @author Jefferson González
  3. * @copyright 2010 Jefferson González
  4. *
  5. * @license
  6. * This file is part of Jaris FLV Player.
  7. *
  8. * Jaris FLV Player is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License or GNU LESSER GENERAL
  10. * PUBLIC LICENSE as published by the Free Software Foundation, either version
  11. * 3 of the License, or (at your option) any later version.
  12. *
  13. * Jaris FLV Player is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License and
  19. * GNU LESSER GENERAL PUBLIC LICENSE along with Jaris FLV Player. If not,
  20. * see <http://www.gnu.org/licenses/>.
  21. */
  22. package jaris.display;
  23. import flash.display.MovieClip;
  24. import flash.display.Sprite;
  25. import flash.display.Stage;
  26. import flash.events.Event;
  27. import flash.Lib;
  28. /**
  29. * Draws a loading bar
  30. */
  31. class Loader extends Sprite
  32. {
  33. private var _stage:Stage;
  34. private var _movieClip:MovieClip;
  35. private var _background:Sprite;
  36. private var _loaderTrack:Sprite;
  37. private var _loaderThumb:Sprite;
  38. private var _visible:Bool;
  39. private var _brightColor:UInt;
  40. private var _controlColor:UInt;
  41. private var _forward:Bool;
  42. public function new()
  43. {
  44. super();
  45. _stage = Lib.current.stage;
  46. _movieClip = Lib.current;
  47. _background = new Sprite();
  48. addChild(_background);
  49. _loaderTrack = new Sprite();
  50. addChild(_loaderTrack);
  51. _loaderThumb = new Sprite();
  52. addChild(_loaderThumb);
  53. _brightColor = 0x4c4c4c;
  54. _controlColor = 0xFFFFFF;
  55. _forward = true;
  56. _visible = true;
  57. addEventListener(Event.ENTER_FRAME, onEnterFrame);
  58. _stage.addEventListener(Event.RESIZE, onResize);
  59. drawLoader();
  60. }
  61. /**
  62. * Animation of a thumb moving on the track
  63. * @param event
  64. */
  65. private function onEnterFrame(event:Event):Void
  66. {
  67. if (_visible)
  68. {
  69. if (_forward)
  70. {
  71. if ((_loaderThumb.x + _loaderThumb.width) >= (_loaderTrack.x + _loaderTrack.width))
  72. {
  73. _forward = false;
  74. }
  75. else
  76. {
  77. _loaderThumb.x += 10;
  78. }
  79. }
  80. else
  81. {
  82. if (_loaderThumb.x <= _loaderTrack.x)
  83. {
  84. _forward = true;
  85. }
  86. else
  87. {
  88. _loaderThumb.x -= 10;
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Redraws the loader to match new stage size
  95. * @param event
  96. */
  97. private function onResize(event:Event):Void
  98. {
  99. drawLoader();
  100. }
  101. /**
  102. * Draw loader graphics
  103. */
  104. private function drawLoader():Void
  105. {
  106. //Clear graphics
  107. _background.graphics.clear();
  108. _loaderTrack.graphics.clear();
  109. _loaderThumb.graphics.clear();
  110. //Draw background
  111. var backgroundWidth:Float = (65 / 100) * _stage.stageWidth;
  112. var backgroundHeight:Float = 30;
  113. _background.x = (_stage.stageWidth / 2) - (backgroundWidth / 2);
  114. _background.y = (_stage.stageHeight / 2) - (backgroundHeight / 2);
  115. _background.graphics.lineStyle();
  116. _background.graphics.beginFill(_brightColor, 0.5);
  117. _background.graphics.drawRoundRect(0, 0, backgroundWidth, backgroundHeight, 6, 6);
  118. _background.graphics.endFill();
  119. //Draw track
  120. var trackWidth:Float = (50 / 100) * _stage.stageWidth;
  121. var trackHeight:Float = 15;
  122. _loaderTrack.x = (_stage.stageWidth / 2) - (trackWidth / 2);
  123. _loaderTrack.y = (_stage.stageHeight / 2) - (trackHeight / 2);
  124. _loaderTrack.graphics.lineStyle(2, _controlColor);
  125. _loaderTrack.graphics.drawRect(0, 0, trackWidth, trackHeight);
  126. //Draw thumb
  127. _loaderThumb.x = _loaderTrack.x;
  128. _loaderThumb.y = _loaderTrack.y;
  129. _loaderThumb.graphics.lineStyle();
  130. _loaderThumb.graphics.beginFill(_controlColor, 1);
  131. _loaderThumb.graphics.drawRect(0, 0, trackHeight, trackHeight);
  132. }
  133. /**
  134. * Stops drawing the loader
  135. */
  136. public function hide():Void
  137. {
  138. this.visible = false;
  139. _visible = false;
  140. }
  141. /**
  142. * Starts drawing the loader
  143. */
  144. public function show():Void
  145. {
  146. this.visible = true;
  147. _visible = true;
  148. }
  149. /**
  150. * Set loader colors
  151. * @param colors
  152. */
  153. public function setColors(colors:Array<String>):Void
  154. {
  155. _brightColor = colors[0].length > 0? Std.parseInt("0x" + colors[0]) : 0x4c4c4c;
  156. _controlColor = colors[1].length > 0? Std.parseInt("0x" + colors[1]) : 0xFFFFFF;
  157. drawLoader();
  158. }
  159. }