TabScript.hx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package arm.ui;
  2. import haxe.io.Bytes;
  3. import kha.Blob;
  4. import zui.Zui;
  5. import zui.Ext;
  6. import zui.Id;
  7. import iron.data.Data;
  8. import arm.sys.Path;
  9. import arm.Enums;
  10. class TabScript {
  11. public static var hscript = Id.handle();
  12. static var textColoring: TTextColoring = null;
  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. if (Config.raw.touch_ui) {
  20. ui.row([1 / 4, 1 / 4, 1 / 4, 1 / 4]);
  21. }
  22. else {
  23. ui.row([1 / 14, 1 / 14, 1 / 14, 1 / 14]);
  24. }
  25. if (ui.button(tr("Run"))) {
  26. try {
  27. js.Lib.eval(hscript.text);
  28. }
  29. catch(e: Dynamic) {
  30. Console.log(e);
  31. }
  32. }
  33. if (ui.button(tr("Clear"))) {
  34. hscript.text = "";
  35. }
  36. if (ui.button(tr("Import"))) {
  37. UIFiles.show("js", false, false, function(path: String) {
  38. Data.getBlob(path, function(b: Blob) {
  39. hscript.text = b.toString();
  40. Data.deleteBlob(path);
  41. });
  42. });
  43. }
  44. if (ui.button(tr("Export"))) {
  45. var str = hscript.text;
  46. UIFiles.show("js", true, false, function(path: String) {
  47. var f = UIFiles.filename;
  48. if (f == "") f = tr("untitled");
  49. path = path + Path.sep + f;
  50. if (!path.endsWith(".js")) path += ".js";
  51. Krom.fileSaveBytes(path, Bytes.ofString(str).getData());
  52. });
  53. }
  54. ui.endSticky();
  55. var _font = ui.ops.font;
  56. var _fontSize = ui.fontSize;
  57. Data.getFont("font_mono.ttf", function(f: kha.Font) { ui.ops.font = f; }); // Sync
  58. ui.fontSize = 15;
  59. Ext.textAreaLineNumbers = true;
  60. Ext.textAreaScrollPastEnd = true;
  61. Ext.textAreaColoring = getTextColoring();
  62. Ext.textArea(ui, hscript);
  63. Ext.textAreaLineNumbers = false;
  64. Ext.textAreaScrollPastEnd = false;
  65. Ext.textAreaColoring = null;
  66. ui.ops.font = _font;
  67. ui.fontSize = _fontSize;
  68. }
  69. }
  70. static function getTextColoring(): TTextColoring {
  71. if (textColoring == null) {
  72. Data.getBlob("text_coloring.json", function(blob: Blob) {
  73. textColoring = haxe.Json.parse(blob.toString());
  74. });
  75. }
  76. return textColoring;
  77. }
  78. }