tab_scripts.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. let tab_scripts_hscript: ui_handle_t = ui_handle_create();
  2. let tab_scripts_text_coloring: ui_text_coloring_t = null;
  3. function tab_scripts_draw(htab: ui_handle_t) {
  4. if (ui_tab(htab, tr("Scripts"))) {
  5. ui_begin_sticky();
  6. let row: f32[] = [ -70, -70, -140 ];
  7. ui_row(row);
  8. if (ui_button(tr("Run"))) {
  9. js_eval(tab_scripts_hscript.text);
  10. }
  11. if (ui_button(tr("Edit"))) {
  12. ui_menu_draw(function() {
  13. if (ui_menu_button(tr("Clear"))) {
  14. tab_scripts_hscript.text = "";
  15. }
  16. if (ui_menu_button(tr("Import"))) {
  17. ui_files_show("js", false, false, function(path: string) {
  18. let b: buffer_t = data_get_blob(path);
  19. tab_scripts_hscript.text = sys_buffer_to_string(b);
  20. data_delete_blob(path);
  21. });
  22. }
  23. if (ui_menu_button(tr("Export"))) {
  24. ui_files_show("js", true, false, function(path: string) {
  25. let str: string = tab_scripts_hscript.text;
  26. let f: string = ui_files_filename;
  27. if (f == "") {
  28. f = tr("untitled");
  29. }
  30. path = path + path_sep + f;
  31. if (!ends_with(path, ".js")) {
  32. path += ".js";
  33. }
  34. iron_file_save_bytes(path, sys_string_to_buffer(str), 0);
  35. });
  36. }
  37. });
  38. }
  39. let ar: string[] = [ "script.js" ];
  40. let file_handle: ui_handle_t = ui_handle(__ID__);
  41. ui_combo(file_handle, ar, tr("File"), false, ui_align_t.LEFT);
  42. ui_end_sticky();
  43. let _font: draw_font_t = ui.ops.font;
  44. let _font_size: i32 = ui.font_size;
  45. let f: draw_font_t = data_get_font("font_mono.ttf");
  46. ui_set_font(ui, f);
  47. ui.font_size = math_floor(15 * UI_SCALE());
  48. ui_text_area_line_numbers = true;
  49. ui_text_area_scroll_past_end = true;
  50. ui_text_area_coloring = tab_scripts_get_text_coloring();
  51. ui_text_area(tab_scripts_hscript);
  52. ui_text_area_line_numbers = false;
  53. ui_text_area_scroll_past_end = false;
  54. ui_text_area_coloring = null;
  55. ui_set_font(ui, _font);
  56. ui.font_size = _font_size;
  57. }
  58. }
  59. function tab_scripts_get_text_coloring(): ui_text_coloring_t {
  60. if (tab_scripts_text_coloring == null) {
  61. let blob: buffer_t = data_get_blob("text_coloring.json");
  62. tab_scripts_text_coloring = json_parse(sys_buffer_to_string(blob));
  63. }
  64. return tab_scripts_text_coloring;
  65. }