PageRenderTime 936ms CodeModel.GetById 34ms RepoModel.GetById 3ms app.codeStats 1ms

/src/com/clink/ui/ScrollBar.as

https://bitbucket.org/nidinthb/clink_virtual_classroom
ActionScript | 423 lines | 267 code | 68 blank | 88 comment | 22 complexity | 4f12bfc6a6ad5a6f57b049543791dd69 MD5 | raw file
  1. package com.clink.ui
  2. {
  3. import com.clink.controllers.Controller_button;
  4. import com.clink.factories.Factory_prettyBox;
  5. import com.clink.factories.Factory_triangle;
  6. import com.clink.utils.DrawingUtils;
  7. import flash.display.Sprite;
  8. import flash.events.Event;
  9. import flash.events.MouseEvent;
  10. /**
  11. * This class creates a scrollbar component with arrow buttons. This draws the scrollbar programatically instead of importing movieClips.
  12. * The static functions can be used to change the color of the different scrollBar parts.
  13. * @author adamrensel
  14. *
  15. */
  16. public class ScrollBar extends Sprite
  17. {
  18. private var _button1:Controller_button;
  19. private var _button2:Controller_button;
  20. private var _handle:Sprite;
  21. private var _track:Sprite;
  22. private var _slider:Slider;
  23. private var _length:Number;
  24. private var _isVertical:Boolean;
  25. private var _handlePotential:Number;
  26. private var _handleLength:Number;
  27. private var _handlePos:Number;
  28. private var _trackLength:Number;
  29. private var _thisValue:Number;
  30. private var _direction:Number;
  31. private const BUTTON_SIZE:Number = 16;
  32. private static var _buttonColor:uint;
  33. private static var _trackColor:uint;
  34. private static var _buttonArrowColor:uint;
  35. private static var _handleColor:uint;
  36. private static var _scrollBarList:Array;
  37. /**
  38. * Instantiates a new scrollBar
  39. *
  40. * @param length:Number Length of the scrollBar
  41. * @param isVertical:Boolean whether or not the scrollBar is vertical or not.
  42. *
  43. */
  44. public function ScrollBar(length:Number,isVertical:Boolean = true)
  45. {
  46. super();
  47. if(!_scrollBarList)
  48. {
  49. throw new Error("Please initialize this class by calling ScrollBar.initScrollBars() at the beginning of your application");
  50. }
  51. _length = length;
  52. _isVertical = isVertical;
  53. init();
  54. }
  55. private function init():void
  56. {
  57. _scrollBarList.push(this);
  58. _handlePotential = 1;
  59. _thisValue = 0;
  60. drawScrollBar();
  61. }
  62. //draws the handle of the scrollBar
  63. private function drawHandle():void
  64. {
  65. //if its vertical draw this
  66. if(_isVertical)
  67. {
  68. _handleLength = (_length - BUTTON_SIZE*2) * _handlePotential;
  69. _handle = Factory_prettyBox.drawPrettyBox(BUTTON_SIZE,_handleLength,_handleColor);
  70. _handle.graphics.moveTo(3,_handle.height/2);
  71. _handle.graphics.lineTo(BUTTON_SIZE - 3,_handle.height/2);
  72. _handle.graphics.moveTo(3,_handle.height/2 - 3);
  73. _handle.graphics.lineTo(BUTTON_SIZE - 3,_handle.height/2 - 3);
  74. _handle.graphics.moveTo(3,_handle.height/2 + 3);
  75. _handle.graphics.lineTo(BUTTON_SIZE - 3,_handle.height/2 + 3);
  76. }else{//if its horizontal draw this
  77. _handleLength = (_length - BUTTON_SIZE*2) * _handlePotential;
  78. _handle = Factory_prettyBox.drawPrettyBox(_handleLength,BUTTON_SIZE,_handleColor);
  79. _handle.graphics.moveTo(_handle.width/2,3);
  80. _handle.graphics.lineTo(_handle.width/2,BUTTON_SIZE - 3);
  81. _handle.graphics.moveTo(_handle.width/2 - 3,3);
  82. _handle.graphics.lineTo(_handle.width/2 - 3,BUTTON_SIZE - 3);
  83. _handle.graphics.moveTo(_handle.width/2 + 3,3);
  84. _handle.graphics.lineTo(_handle.width/2 + 3,BUTTON_SIZE - 3);
  85. }
  86. _handle.buttonMode = true;
  87. _track.addChild(_handle);
  88. }
  89. //draw the track of the scrollBar
  90. private function drawTrack():void
  91. {
  92. //if vertical
  93. if(_isVertical)
  94. {
  95. _trackLength = _length - BUTTON_SIZE*2;
  96. _track = Factory_prettyBox.drawPrettyBox(BUTTON_SIZE,_trackLength,_trackColor,0,false,false,true);
  97. _track.y = BUTTON_SIZE;
  98. }else{//if horizontal
  99. _trackLength = _length - BUTTON_SIZE*2;
  100. _track = Factory_prettyBox.drawPrettyBox(_trackLength,BUTTON_SIZE,_trackColor,0,false,false,true);
  101. _track.x = BUTTON_SIZE;
  102. }
  103. this.addChild(_track);
  104. }
  105. //draw the arrow buttons of the scrollBar
  106. private function drawButtons():void
  107. {
  108. var button1Dir:String;
  109. var button2Dir:String;
  110. //determines the values of the button directions if its horizontal or vertical, these values get passed to the triangle Factory
  111. if(_isVertical)
  112. {
  113. button1Dir = "up";
  114. button2Dir = "down";
  115. }else{
  116. button1Dir = "left";
  117. button2Dir = "right";
  118. }
  119. //up/left arrow button
  120. var btn1Up:Sprite = Factory_prettyBox.drawPrettyBox(16,16,_buttonColor,0,true);
  121. var arrow1:Sprite = Factory_triangle.drawEqTriangle(8,3.5,_buttonArrowColor,button1Dir);
  122. arrow1.x = 8;
  123. arrow1.y = 8;
  124. btn1Up.addChild(arrow1);
  125. var btn1Down:Sprite = Factory_prettyBox.drawPrettyBox(16,16,_buttonColor,0,true,true,true);
  126. var arrow2:Sprite = Factory_triangle.drawEqTriangle(8,3.5,_buttonArrowColor,button1Dir);
  127. arrow2.x = 9;
  128. arrow2.y = 9;
  129. btn1Down.addChild(arrow2);
  130. _button1 = new Controller_button(btn1Up);
  131. _button1.downState = btn1Down;
  132. //down/right arrow button
  133. var btn2Up:Sprite = Factory_prettyBox.drawPrettyBox(16,16,_buttonColor,0,true);
  134. var arrow3:Sprite = Factory_triangle.drawEqTriangle(8,3.5,_buttonArrowColor,button2Dir);
  135. arrow3.x = 8;
  136. arrow3.y = 8;
  137. btn2Up.addChild(arrow3);
  138. var btn2Down:Sprite = Factory_prettyBox.drawPrettyBox(16,16,_buttonColor,0,true,true,true);
  139. var arrow4:Sprite = Factory_triangle.drawEqTriangle(8,3.5,_buttonArrowColor,button2Dir);
  140. arrow4.x = 9;
  141. arrow4.y = 9;
  142. btn2Down.addChild(arrow4);
  143. _button2 = new Controller_button(btn2Up);
  144. _button2.downState = btn2Down;
  145. //position the buttons based on if vertical or horizontal
  146. if(_isVertical)
  147. {
  148. _button2.y = _length - BUTTON_SIZE;
  149. }else{
  150. _button2.x = _length - BUTTON_SIZE;
  151. }
  152. this.addChild(_button1);
  153. this.addChild(_button2);
  154. //add event listeners to control handle movement
  155. _button1.addEventListener(MouseEvent.MOUSE_DOWN,onButtonDown);
  156. _button1.addEventListener(MouseEvent.MOUSE_UP,onButtonUp);
  157. _button2.addEventListener(MouseEvent.MOUSE_DOWN,onButtonDown);
  158. _button2.addEventListener(MouseEvent.MOUSE_UP,onButtonUp);
  159. }
  160. ///////////////////Callbacks///////////////////////
  161. //slider changes position
  162. private function onChange(e:Event):void
  163. {
  164. _thisValue = _slider.value;
  165. var e:Event = new Event(Event.CHANGE);
  166. this.dispatchEvent(e);
  167. }
  168. //mouse down on buttons
  169. private function onButtonDown(e:MouseEvent):void
  170. {
  171. _direction = 1;
  172. if(e.target == _button1)
  173. {
  174. _direction = -1
  175. }
  176. this.addEventListener(Event.ENTER_FRAME,onFrame);
  177. }
  178. //mouse up
  179. private function onButtonUp(e:MouseEvent):void
  180. {
  181. this.removeEventListener(Event.ENTER_FRAME,onFrame);
  182. }
  183. //when mouse is down this listner gets activated and makes the bar scroll
  184. private function onFrame(e:Event):void
  185. {
  186. var incr:Number = this.value + .1 * _direction;
  187. if(incr >=0 && incr <=1)
  188. {
  189. if(incr > 1)
  190. {
  191. this.value = 1;
  192. }else if(incr < 0){
  193. this.value = 0;
  194. }else if(this.value != incr){
  195. this.value = incr;
  196. var e:Event = new Event(Event.CHANGE);
  197. this.dispatchEvent(e);
  198. }
  199. }
  200. }
  201. //////////////////Public methods//////////////////
  202. /**
  203. * Redraw the entire scrollbar
  204. *
  205. */
  206. public function drawScrollBar():void
  207. {
  208. //remove all the children - helps with memory
  209. while(this.numChildren > 0)
  210. {
  211. this.removeChildAt(0);
  212. }
  213. drawTrack();
  214. drawButtons();
  215. drawHandle();
  216. //remove listeners
  217. if(_slider)
  218. {
  219. _slider.removeEventListener(Event.CHANGE,onChange);
  220. }
  221. //clear the _slider object
  222. _slider = null;
  223. //create a new slider object
  224. if(_isVertical)
  225. {
  226. _slider = new Slider(_handle,_track);
  227. }else{
  228. _slider = new Slider(_handle,_track,true);
  229. }
  230. _slider.value = _thisValue;
  231. _slider.addEventListener(Event.CHANGE,onChange);
  232. }
  233. ////////////////Getters/Setters///////////////////
  234. /**
  235. * SETTER
  236. * Changes the size of the handle, usually used to indicate how many items are in whatever the scrollBar is controling
  237. * @param percent:Number percent value of the handle size 1 being the size of the track
  238. *
  239. */
  240. public function set handleSize(percent:Number):void
  241. {
  242. _thisValue = _slider.value;
  243. if(percent < .15)
  244. {
  245. _handlePotential = .15
  246. }else if(percent > 1){
  247. _handlePotential = 1;
  248. }else{
  249. _handlePotential = percent;
  250. }
  251. _slider.potential = _handlePotential;
  252. }
  253. /**
  254. * GETTER for handle size returns a percent
  255. * @return Number
  256. *
  257. */
  258. public function get handleSize():Number
  259. {
  260. return _handlePotential;
  261. }
  262. /**
  263. * SETTER
  264. * sets a manual value for the slider, takes a percent value
  265. * @param percent:Number percent value between 1 and 0
  266. *
  267. */
  268. public function set value(percent:Number):void
  269. {
  270. if(_slider)
  271. {
  272. _slider.value = percent;
  273. }
  274. }
  275. /**
  276. * GETTER gets value as a percent between 0 and 1
  277. * @return Number
  278. *
  279. */
  280. public function get value():Number
  281. {
  282. if(_slider)
  283. {
  284. if(_slider.value > .94)
  285. {
  286. return 1;
  287. }else{
  288. return _slider.value;
  289. }
  290. }else{
  291. return -1;
  292. }
  293. }
  294. /**
  295. * SETTER for scrollBar length
  296. * @param value:Number sets length
  297. *
  298. */
  299. public function set length(value:Number):void
  300. {
  301. _length = value;
  302. drawScrollBar();
  303. }
  304. /**
  305. * GETTER for scrollBar length
  306. * @return Number
  307. *
  308. */
  309. public function get length():Number
  310. {
  311. return _length
  312. }
  313. ////////////////Static Methods///////////////////
  314. /**
  315. * This method needs to be called before any scrollBars are instantiated.
  316. *
  317. *
  318. */
  319. public static function initScrollBars():void
  320. {
  321. setScrollBarColors();
  322. if(!_scrollBarList)
  323. {
  324. _scrollBarList = [];
  325. }
  326. }
  327. /**
  328. * This method can set/change all scrollBar colors of all instantiated scrollBars
  329. * @param buttonColor:String Arrow Button color can be passed in as a css value ex. #FFFFFF or as an actionscript hex value 0xFFFFFF, either will work
  330. * @param trackColor:String Track color can be passed in as a css value ex. #FFFFFF or as an actionscript hex value 0xFFFFFF, either will work
  331. * @param buttonArrowColor:String Arrow color on the buttons can be passed in as a css value ex. #FFFFFF or as an actionscript hex value 0xFFFFFF, either will work
  332. * @param handleColor:String handle color can be passed in as a css value ex. #FFFFFF or as an actionscript hex value 0xFFFFFF, either will work
  333. *
  334. */
  335. public static function setScrollBarColors(buttonColor:String = "#aaaaaa", trackColor:String = "#ffffff", buttonArrowColor:String = "#333333", handleColor:String = "#aaaaaa"):void
  336. {
  337. _buttonColor = uint(DrawingUtils.fixColorCode(buttonColor));
  338. _trackColor = uint(DrawingUtils.fixColorCode(trackColor));
  339. _buttonArrowColor = uint(DrawingUtils.fixColorCode(buttonArrowColor));
  340. _handleColor = uint(DrawingUtils.fixColorCode(handleColor));
  341. redDrawAll();
  342. }
  343. /**
  344. * redraws all scrollbars instantiated
  345. *
  346. */
  347. public static function redDrawAll():void
  348. {
  349. for each(var sb:ScrollBar in _scrollBarList)
  350. {
  351. sb.drawScrollBar();
  352. }
  353. }
  354. }
  355. }