TabScript.hx 2.1 KB

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