TabScript.hx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package arm.ui;
  2. import haxe.io.Bytes;
  3. import zui.Zui;
  4. import zui.Ext;
  5. import zui.Id;
  6. import kha.Blob;
  7. import iron.data.Data;
  8. import arm.sys.Path;
  9. import arm.io.ImportAsset;
  10. import arm.Enums;
  11. class TabScript {
  12. public static var hscript = Id.handle();
  13. @:access(zui.Zui)
  14. public static function draw() {
  15. var ui = UISidebar.inst.ui;
  16. var statush = Config.raw.layout[LayoutStatusH];
  17. if (ui.tab(UIStatus.inst.statustab, tr("Script")) && statush > UIStatus.defaultStatusH * ui.SCALE()) {
  18. ui.beginSticky();
  19. ui.row([1 / 14, 1 / 14, 1 / 14, 1 / 14]);
  20. if (ui.button(tr("Run"))) {
  21. try {
  22. js.Lib.eval(hscript.text);
  23. }
  24. catch(e: Dynamic) {
  25. Console.log(e);
  26. }
  27. }
  28. if (ui.button(tr("Clear"))) {
  29. hscript.text = "";
  30. }
  31. if (ui.button(tr("Import"))) {
  32. UIFiles.show("js", false, false, function(path: String) {
  33. Data.getBlob(path, function(b: Blob) {
  34. hscript.text = b.toString();
  35. Data.deleteBlob(path);
  36. });
  37. });
  38. }
  39. if (ui.button(tr("Export"))) {
  40. var str = hscript.text;
  41. UIFiles.show("js", true, false, function(path: String) {
  42. var f = UIFiles.filename;
  43. if (f == "") f = tr("untitled");
  44. path = path + Path.sep + f;
  45. if (!path.endsWith(".js")) path += ".js";
  46. Krom.fileSaveBytes(path, Bytes.ofString(str).getData());
  47. });
  48. }
  49. ui.endSticky();
  50. var _font = ui.ops.font;
  51. var _fontSize = ui.fontSize;
  52. Data.getFont("font_mono.ttf", function(f: kha.Font) { ui.ops.font = f; }); // Sync
  53. ui.fontSize = 15;
  54. Ext.textArea(ui, hscript);
  55. ui.ops.font = _font;
  56. ui.fontSize = _fontSize;
  57. }
  58. }
  59. }