SampleApp.hx 3.2 KB

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