CheckBox.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package h2d;
  2. /**
  3. A simple Interactive checkbox button with a label.
  4. Useful for fast construction of development UI, but lacks on configurability side.
  5. **/
  6. class CheckBox extends h2d.Flow {
  7. /**
  8. When disabled, the user would not be able to change the checkbox state by interacting with it.
  9. It is still possible to change the `selected` state manually through the code even if checkbox is disabled.
  10. **/
  11. public var enable(default,set) : Bool = true;
  12. /**
  13. Current toggle state of the checkbox.
  14. Note that changing the state from the code will cause `CheckBox.onChange` to trigger.
  15. **/
  16. public var selected(default,set) : Bool = false;
  17. /**
  18. Optional text label that will be shown to the right of the checkbox.
  19. **/
  20. public var text(default,set) : String = "";
  21. var tf : h2d.Text;
  22. var select : h2d.Bitmap;
  23. /**
  24. Create a new CheckBox instance.
  25. @param parent An optional parent `h2d.Object` instance to which CheckBox adds itself if set.
  26. **/
  27. public function new(?parent) {
  28. super(parent);
  29. padding = 0;
  30. verticalAlign = Middle;
  31. horizontalSpacing = 5;
  32. borderHeight = borderWidth = 1;
  33. var width = 13;
  34. var box = new h2d.Flow(this);
  35. var t = h2d.Tile.fromColor(0x404040, width, width);
  36. new h2d.Bitmap(t, box);
  37. var borderWidth = borderLeft + borderRight;
  38. var borderHeight = borderTop + borderBottom;
  39. var t = h2d.Tile.fromColor(0, width - borderWidth * 2, width - borderHeight * 2);
  40. var bg = new h2d.Bitmap(t, box);
  41. bg.x = borderWidth;
  42. bg.y = borderHeight;
  43. box.getProperties(bg).isAbsolute = true;
  44. var t = h2d.Tile.fromColor(0x404040, width - (borderWidth + 1) * 2, width - (borderHeight + 1) * 2);
  45. select = new h2d.Bitmap(t, box);
  46. select.x = borderWidth + 1;
  47. select.y = borderHeight + 1;
  48. box.getProperties(select).isAbsolute = true;
  49. tf = new h2d.Text(hxd.res.DefaultFont.get(), this);
  50. getProperties(tf).offsetY = -2;
  51. //
  52. enableInteractive = true;
  53. interactive.cursor = Button;
  54. interactive.onClick = function(e) {
  55. if( enable )
  56. selected = !selected;
  57. }
  58. enable = true;
  59. selected = false;
  60. needReflow = true;
  61. }
  62. function set_text(str : String) {
  63. if(tf != null)
  64. tf.text = str;
  65. return text = str;
  66. }
  67. function set_enable(b) {
  68. alpha = b ? 1 : 0.6;
  69. return enable = b;
  70. }
  71. function set_selected(s) {
  72. needReflow = true;
  73. selected = s;
  74. select.visible = s;
  75. onChange();
  76. return selected;
  77. }
  78. /**
  79. Sent when the `CheckBox.selected` state is changed.
  80. Can be triggered both by user interaction (when checkbox is enabled) and from the software side by changing `selected` directly.
  81. **/
  82. public dynamic function onChange() {
  83. }
  84. }