SampleApp.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. class SampleApp extends hxd.App {
  2. var fui : h2d.Flow;
  3. override function init() {
  4. fui = new h2d.Flow(s2d);
  5. fui.layout = Vertical;
  6. fui.verticalSpacing = 5;
  7. fui.padding = 10;
  8. }
  9. function getFont() {
  10. return hxd.res.DefaultFont.get();
  11. }
  12. function addButton( label : String, onClick : Void -> Void ) {
  13. var f = new h2d.Flow(fui);
  14. f.padding = 5;
  15. f.paddingBottom = 7;
  16. f.backgroundTile = h2d.Tile.fromColor(0x404040);
  17. var tf = new h2d.Text(getFont(), f);
  18. tf.text = label;
  19. f.enableInteractive = true;
  20. f.interactive.cursor = Button;
  21. f.interactive.onClick = function(_) onClick();
  22. f.interactive.onOver = function(_) f.backgroundTile = h2d.Tile.fromColor(0x606060);
  23. f.interactive.onOut = function(_) f.backgroundTile = h2d.Tile.fromColor(0x404040);
  24. return f;
  25. }
  26. function addSlider( label : String, get : Void -> Float, set : Float -> Void, min : Float = 0., max : Float = 1. ) {
  27. var f = new h2d.Flow(fui);
  28. f.horizontalSpacing = 5;
  29. var tf = new h2d.Text(getFont(), f);
  30. tf.text = label;
  31. tf.maxWidth = 70;
  32. tf.textAlign = Right;
  33. var sli = new h2d.Slider(100, 10, f);
  34. sli.minValue = min;
  35. sli.maxValue = max;
  36. sli.value = get();
  37. var tf = new h2d.TextInput(getFont(), f);
  38. tf.text = "" + hxd.Math.fmt(sli.value);
  39. sli.onChange = function() {
  40. set(sli.value);
  41. tf.text = "" + hxd.Math.fmt(sli.value);
  42. f.needReflow = true;
  43. };
  44. tf.onChange = function() {
  45. var v = Std.parseFloat(tf.text);
  46. if( Math.isNaN(v) ) return;
  47. sli.value = v;
  48. set(v);
  49. };
  50. return sli;
  51. }
  52. function addCheck( label : String, get : Void -> Bool, set : Bool -> Void ) {
  53. var f = new h2d.Flow(fui);
  54. f.horizontalSpacing = 5;
  55. var tf = new h2d.Text(getFont(), f);
  56. tf.text = label;
  57. tf.maxWidth = 70;
  58. tf.textAlign = Right;
  59. var size = 10;
  60. var b = new h2d.Graphics(f);
  61. function redraw() {
  62. b.clear();
  63. b.beginFill(0x808080);
  64. b.drawRect(0, 0, size, size);
  65. b.beginFill(0);
  66. b.drawRect(1, 1, size-2, size-2);
  67. if( get() ) {
  68. b.beginFill(0xC0C0C0);
  69. b.drawRect(2, 2, size-4, size-4);
  70. }
  71. }
  72. var i = new h2d.Interactive(size, size, b);
  73. i.onClick = function(_) {
  74. set(!get());
  75. redraw();
  76. };
  77. redraw();
  78. return i;
  79. }
  80. function addChoice( text, choices, callb : Int -> Void, value = 0 ) {
  81. var font = getFont();
  82. var i = new h2d.Interactive(110, font.lineHeight, fui);
  83. i.backgroundColor = 0xFF808080;
  84. fui.getProperties(i).paddingLeft = 20;
  85. var t = new h2d.Text(font, i);
  86. t.maxWidth = i.width;
  87. t.text = text+":"+choices[value];
  88. t.textAlign = Center;
  89. i.onClick = function(_) {
  90. value++;
  91. value %= choices.length;
  92. callb(value);
  93. t.text = text + ":" + choices[value];
  94. };
  95. i.onOver = function(_) {
  96. t.textColor = 0xFFFFFF;
  97. };
  98. i.onOut = function(_) {
  99. t.textColor = 0xEEEEEE;
  100. };
  101. i.onOut(null);
  102. return i;
  103. }
  104. function addText(text="") {
  105. var tf = new h2d.Text(getFont(), fui);
  106. tf.text = text;
  107. return tf;
  108. }
  109. }