/src/com/axiomalaska/integratedlayers/views/test/timeline/AHRulerGraphicElement.as

https://github.com/axiomalaska/crks · ActionScript · 222 lines · 96 code · 44 blank · 82 comment · 6 complexity · 19c0ac8601a3e1d60fd4fb839525b656 MD5 · raw file

  1. package com.axiomalaska.integratedlayers.views.test.timeline
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.GradientType;
  5. import flash.display.Graphics;
  6. import flash.display.GraphicsPathCommand;
  7. import flash.display.Sprite;
  8. import flash.geom.Matrix;
  9. import spark.core.IGraphicElement;
  10. import spark.core.SpriteVisualElement;
  11. import spark.primitives.supportClasses.FilledElement;
  12. import spark.primitives.supportClasses.GraphicElement;
  13. /**
  14. * The AHRulerGraphicElement class displays a ruler made of tick lines
  15. * for each division and each subdivision.
  16. *
  17. * The number of divisions and subdivisions can be set,
  18. * the tick lines are drawn based on the strokeWeight and
  19. * color property.
  20. *
  21. * TODO: clean up code, optimize, test.
  22. *
  23. * @author andy andreas hulstkamp
  24. *
  25. */
  26. public class AHRulerGraphicElement extends FilledElement
  27. {
  28. /**
  29. * Commands to draw the tick lines
  30. */
  31. private var _commands:Vector.<int> = new Vector.<int>();
  32. /**
  33. * Data to draw the tick lines
  34. */
  35. private var _data:Vector.<Number> = new Vector.<Number>();
  36. /**
  37. * Index for drawing commands
  38. */
  39. private var _ci:int = 0;
  40. /**
  41. * Index for data commands
  42. */
  43. private var _di:int = 0;
  44. /**
  45. * Number of divisions
  46. */
  47. private var _divisions:int = 23;
  48. /**
  49. * Number of subdivisions
  50. */
  51. private var _subdivisions:int = 4;
  52. /**
  53. * Ratio of line height, subdivisions to division
  54. */
  55. private var _subdivisionToDivisionLineHeightRatio:Number = .75;
  56. public function AHRulerGraphicElement()
  57. {
  58. super();
  59. }
  60. //------------------------------------------------------------------
  61. //
  62. // Properties
  63. //
  64. //------------------------------------------------------------------
  65. /**
  66. * Number of divisions
  67. */
  68. public function get divisions():int
  69. {
  70. return _divisions;
  71. }
  72. /**
  73. * @private
  74. */
  75. public function set divisions(value:int):void
  76. {
  77. _divisions = Math.max(1, value);
  78. invalidateDisplayList();
  79. }
  80. /**
  81. * Number of subdivision
  82. */
  83. public function get subdivisions():int
  84. {
  85. return _subdivisions;
  86. }
  87. public function set subdivisions(value:int):void
  88. {
  89. _subdivisions = Math.max(1, value);
  90. invalidateDisplayList();
  91. }
  92. /**
  93. * Ratio of line height, subdivision to division
  94. */
  95. public function get subdivisionToDivisionLineHeightRatio():Number
  96. {
  97. return _subdivisionToDivisionLineHeightRatio;
  98. }
  99. /**
  100. * @private
  101. */
  102. public function set subdivisionToDivisionLineHeightRatio(value:Number):void
  103. {
  104. _subdivisionToDivisionLineHeightRatio = value;
  105. }
  106. //------------------------------------------------------------------
  107. //
  108. // Drawing
  109. //
  110. //------------------------------------------------------------------
  111. /**
  112. * Draw the ruler
  113. *
  114. * TODO: logic to actually reuse commands and data if nothing has changed
  115. * @param w width of ruler
  116. * @param h height of ruler
  117. *
  118. */
  119. override protected function draw(g:Graphics):void
  120. {
  121. //get root coordinates for this graphic element relative to the drawnDisplayObject
  122. //coordinates of this graphical element must be taken into account
  123. //coordinates to draw to relative to the drawnDisplayObject might not start at 0, 0 but at drawX, drawY
  124. //since display object might be shared
  125. var gx:Number = drawX;
  126. var gy:Number = drawY;
  127. //reset indexes for drawing commands
  128. _ci = 0;
  129. _di = 0;
  130. //height of subdivision tickline
  131. var sdth:int = height * subdivisionToDivisionLineHeightRatio;
  132. //DivisionTickDistance
  133. var dtd:Number = width/(divisions);
  134. //subdivision tick line distance
  135. var sdtd:Number = dtd/subdivisions;
  136. //x inc
  137. var tx:Number = 0;
  138. //current height of tickline to draw
  139. var th:int = height;
  140. var cntr:int = 0;
  141. var ticks:int = (divisions) * subdivisions;
  142. //the bottom line
  143. _commands[_ci++] = GraphicsPathCommand.MOVE_TO;
  144. _data[_di++] = gx;
  145. _data[_di++] = gy + height;
  146. _commands[_ci++] = GraphicsPathCommand.LINE_TO;
  147. _data[_di++] = gx + width;
  148. _data[_di++] = gy + height;
  149. //prepare drawing commands for tick lines
  150. while (cntr <= ticks) {
  151. if (cntr % subdivisions == 0) {
  152. th = 0;
  153. } else {
  154. th = sdth;
  155. }
  156. //Tick
  157. _commands[_ci++] = GraphicsPathCommand.MOVE_TO;
  158. _data[_di++] = gx + tx;
  159. _data[_di++] = gy + height;
  160. _commands[_ci++] = GraphicsPathCommand.LINE_TO;
  161. _data[_di++] = gx + tx;
  162. _data[_di++] = gy + th;
  163. tx += sdtd;
  164. cntr++;
  165. }
  166. //since we are reusing command and data vectors over multiple passses clear out unnecessary data from former pass
  167. //TODO. Actually make caching work so that if no parameters changed, chached drawing is enabled.
  168. if (_ci != _commands.length)
  169. {
  170. _commands.splice(_ci, _commands.length - _ci);
  171. _data.splice(_di, _data.length - _di);
  172. }
  173. g.drawRect(gx, gy, width, height);
  174. g.endFill();
  175. g.drawPath(_commands, _data);
  176. }
  177. }
  178. }