/src_lib/tonfall/display/ParameterSlider.as

https://github.com/dallavilla/ParticleNodeSequencer
ActionScript | 130 lines | 96 code | 29 blank | 5 comment | 5 complexity | 86bef1065a0d37bdf2306856c54809af MD5 | raw file
  1. package tonfall.display
  2. {
  3. import tonfall.core.Parameter;
  4. import flash.display.Sprite;
  5. import flash.events.MouseEvent;
  6. import flash.text.TextField;
  7. import flash.text.TextFieldAutoSize;
  8. import flash.text.TextFormat;
  9. /**
  10. * Very simple static slider to control Parameter
  11. *
  12. * @author Andre Michelle
  13. */
  14. public final class ParameterSlider extends Sprite
  15. {
  16. private static const TEXT_FORMAT : TextFormat = new TextFormat( 'Verdana', 9, 0, true );
  17. private const textField : TextField = new TextField();
  18. private const thumb: Thumb = new Thumb();
  19. private var _parameter : Parameter;
  20. private var _dragging: Boolean;
  21. private var _dragOffset : Number = 0.0;
  22. public function ParameterSlider( parameter: Parameter )
  23. {
  24. _parameter = parameter;
  25. init();
  26. }
  27. private function init() : void
  28. {
  29. graphics.beginFill( 0xFFFFFF );
  30. graphics.drawRoundRect( 0.0, 0.0, 288.0, 24.0, 4.0, 4.0 );
  31. graphics.endFill();
  32. graphics.beginFill( 0x333333 );
  33. graphics.drawRect( 4.0, 4.0, 192.0, 16.0 );
  34. graphics.endFill();
  35. textField.defaultTextFormat = TEXT_FORMAT;
  36. textField.selectable = false;
  37. textField.autoSize = TextFieldAutoSize.LEFT;
  38. textField.x = 204;
  39. textField.y = 6.0;
  40. thumb.y = 4.0;
  41. addChild( textField );
  42. addChild( thumb );
  43. addEventListener( MouseEvent.MOUSE_DOWN, mouseDown );
  44. updateView();
  45. }
  46. private function mouseDown( event : MouseEvent ) : void
  47. {
  48. _dragging = true;
  49. _dragOffset = event.target == thumb ? thumb.mouseX - 4.0 : 0.0;
  50. update( mouseX );
  51. stage.addEventListener( MouseEvent.MOUSE_UP, mouseUp );
  52. stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseMove );
  53. }
  54. private function mouseMove( event : MouseEvent ) : void
  55. {
  56. if( _dragging )
  57. {
  58. update( mouseX );
  59. }
  60. }
  61. private function mouseUp( event : MouseEvent ) : void
  62. {
  63. if( _dragging )
  64. {
  65. stage.removeEventListener( MouseEvent.MOUSE_UP, mouseUp );
  66. stage.removeEventListener( MouseEvent.MOUSE_MOVE, mouseMove );
  67. _dragging = false;
  68. }
  69. }
  70. private function update( x: Number ): void
  71. {
  72. var value: Number = ( ( x - _dragOffset ) - 16.0 ) / 176.0;
  73. if( value < 0.0 )
  74. value = 0.0;
  75. else
  76. if( value > 1.0 )
  77. value = 1.0;
  78. _parameter.value = value;
  79. updateView();
  80. }
  81. private function updateView(): void
  82. {
  83. var value: Number = _parameter.value;
  84. thumb.x = 4.0 + value * 176.0;
  85. textField.text = _parameter.name + ' ' + Math.round( value * 100.0 ) + '%';
  86. }
  87. }
  88. }
  89. import flash.display.Sprite;
  90. class Thumb extends Sprite
  91. {
  92. public function Thumb()
  93. {
  94. graphics.beginFill( 0xCCCCCC );
  95. graphics.drawRect( 0.0, 0.0, 16.0, 16.0 );
  96. graphics.endFill();
  97. buttonMode = true;
  98. useHandCursor = true;
  99. cacheAsBitmap = true;
  100. }
  101. }