2
0

Slider.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package h2d;
  2. /**
  3. A simple interactive horizontal numerical slider.
  4. **/
  5. class Slider extends h2d.Interactive {
  6. /**
  7. The slider background tile.
  8. If Tile width does not match with Slider width, it will be resized through `Tile.setSize` to match the Slider width.
  9. Defaults to the monocolor 0x808080 Tile with the size of `Slider.width x 4` and centered vertically within `Slider.height`.
  10. **/
  11. public var tile : h2d.Tile;
  12. /**
  13. Tile of the slider current position caret.
  14. Defaults to the monocolor #CCCCCC Tile with the size of `5 x Slider.height`.
  15. **/
  16. public var cursorTile : h2d.Tile;
  17. /**
  18. The minimum value the Slider can allow.
  19. **/
  20. public var minValue(default, set) : Float = 0;
  21. /**
  22. The maximum value the Slider can allow.
  23. **/
  24. public var maxValue(default, set) : Float = 1;
  25. /**
  26. Current value of the Slider.
  27. When set, will be clamped to `minValue <= value <= maxValue`.
  28. **/
  29. public var value(default, set) : Float = 0;
  30. /**
  31. Create a new Slider width specified dimensions and parent.
  32. @param width The width of the Slider interactive area.
  33. @param height The height of the Slider interactive area.
  34. @param parent An optional parent `h2d.Object` instance to which Sliders adds itself if set.
  35. **/
  36. public function new(?width:Int = 50, ?height:Int = 10, ?parent) {
  37. super(width, height, parent);
  38. tile = h2d.Tile.fromColor(0x808080, width, 4);
  39. tile.dy = (height - 4) >> 1;
  40. cursorTile = h2d.Tile.fromColor(0xCCCCCC, 5, height);
  41. }
  42. function set_minValue(v) {
  43. if( value < v ) value = v;
  44. return minValue = v;
  45. }
  46. function set_maxValue(v) {
  47. if( value > v ) value = v;
  48. return maxValue = v;
  49. }
  50. function set_value(v) {
  51. if( v < minValue ) v = minValue;
  52. if( v > maxValue ) v = maxValue;
  53. return value = v;
  54. }
  55. override function getBoundsRec( relativeTo, out, forSize ) {
  56. super.getBoundsRec(relativeTo, out, forSize);
  57. if( forSize ) addBounds(relativeTo, out, 0, 0, width, height);
  58. if( tile != null ) addBounds(relativeTo, out, tile.dx, tile.dy, tile.width, tile.height);
  59. if( cursorTile != null ) addBounds(relativeTo, out, cursorTile.dx + getDx(), cursorTile.dy, cursorTile.width, cursorTile.height);
  60. }
  61. override function draw(ctx:RenderContext) {
  62. super.draw(ctx);
  63. if( tile.width != Std.int(width) )
  64. tile.setSize(Std.int(width), tile.height);
  65. emitTile(ctx, tile);
  66. var px = getDx();
  67. cursorTile.dx += px;
  68. emitTile(ctx, cursorTile);
  69. cursorTile.dx -= px;
  70. }
  71. var handleDX = 0.0;
  72. inline function getDx() {
  73. return Math.round( (value - minValue) * (width - cursorTile.width) / (maxValue - minValue) );
  74. }
  75. inline function getValue(cursorX : Float) : Float {
  76. return ((cursorX - handleDX) / (width - cursorTile.width)) * (maxValue - minValue) + minValue;
  77. }
  78. override function handleEvent(e:hxd.Event) {
  79. super.handleEvent(e);
  80. if( e.cancel ) return;
  81. switch( e.kind ) {
  82. case EPush:
  83. var dx = getDx();
  84. handleDX = e.relX - dx;
  85. // If clicking the slider outside the handle, drag the handle
  86. // by the center of it.
  87. if (handleDX - cursorTile.dx < 0 || handleDX - cursorTile.dx > cursorTile.width) {
  88. handleDX = cursorTile.width * 0.5;
  89. }
  90. value = getValue(e.relX);
  91. onChange();
  92. var scene = scene;
  93. startCapture(function(e) {
  94. if( this.scene != scene || e.kind == ERelease ) {
  95. scene.stopCapture();
  96. return;
  97. }
  98. value = getValue(e.relX);
  99. onChange();
  100. });
  101. default:
  102. }
  103. }
  104. /**
  105. Sent when slider value is changed by user.
  106. Not sent if value is set manually from software side.
  107. **/
  108. public dynamic function onChange() {
  109. }
  110. }