main.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. type storage_t = {
  2. project?: string;
  3. file?: string;
  4. text?: string;
  5. modified?: bool;
  6. expanded?: string[];
  7. window_w?: i32;
  8. window_h?: i32;
  9. window_x?: i32;
  10. window_y?: i32;
  11. sidebar_w?: i32;
  12. };
  13. let ui: zui_t;
  14. let theme: zui_theme_t;
  15. let text_handle: zui_handle_t = zui_handle_create();
  16. let sidebar_handle: zui_handle_t = zui_handle_create();
  17. let editor_handle: zui_handle_t = zui_handle_create();
  18. let storage: storage_t = null;
  19. let resizing_sidebar: bool = false;
  20. let minimap_w: i32 = 150;
  21. let minimap_h: i32 = 0;
  22. let minimap_box_h: i32 = 0;
  23. let minimap_scrolling: bool = false;
  24. let minimap: image_t = null;
  25. let window_header_h: i32 = 0;
  26. function drop_files(path: string) {
  27. storage.project = path;
  28. sidebar_handle.redraws = 1;
  29. }
  30. function shutdown() {
  31. let storage_string: string = sys_string_to_buffer(json_stringify(storage));
  32. krom_file_save_bytes(krom_save_path() + "/config.json", storage_string, 0);
  33. }
  34. function main() {
  35. krom_set_app_name("ArmorPad");
  36. let blob_storage: buffer_t = krom_load_blob(krom_save_path() + "/config.json");
  37. if (blob_storage == null) {
  38. storage = {};
  39. storage.project = "";
  40. storage.file = "untitled";
  41. storage.text = "";
  42. storage.modified = false;
  43. storage.expanded = [];
  44. storage.window_w = 1600;
  45. storage.window_h = 900;
  46. storage.window_x = -1;
  47. storage.window_y = -1;
  48. storage.sidebar_w = 230;
  49. }
  50. else {
  51. storage = json_parse(sys_buffer_to_string(blob_storage));
  52. }
  53. text_handle.text = storage.text;
  54. let ops: kinc_sys_ops_t = {
  55. title: "ArmorPad",
  56. x: storage.window_x,
  57. y: storage.window_y,
  58. width: storage.window_w,
  59. height: storage.window_h,
  60. features: window_features_t.RESIZABLE |
  61. window_features_t.MAXIMIZABLE |
  62. window_features_t.MINIMIZABLE,
  63. mode: window_mode_t.WINDOWED,
  64. frequency: 60,
  65. vsync: true,
  66. };
  67. sys_start(ops);
  68. let font: g2_font_t = data_get_font("font_mono.ttf");
  69. g2_font_init(font);
  70. theme = {};
  71. zui_theme_default(theme);
  72. let zui_ops: zui_options_t = { scale_factor: 1.0, theme: theme, font: font.font_ };
  73. ui = zui_create(zui_ops);
  74. let blob_coloring: buffer_t = data_get_blob("text_coloring.json");
  75. let text_coloring: zui_text_coloring_t = json_parse(sys_buffer_to_string(blob_coloring));
  76. zui_text_area_coloring = text_coloring;
  77. zui_on_border_hover = on_border_hover;
  78. zui_on_text_hover = on_text_hover;
  79. zui_text_area_line_numbers = true;
  80. zui_text_area_scroll_past_end = true;
  81. sys_notify_on_frames(render);
  82. krom_set_drop_files_callback(drop_files);
  83. krom_set_application_state_callback(null, null, null, null, shutdown);
  84. }
  85. function list_folder(path: string) {
  86. let files: string[] = string_split(krom_read_directory(path, false), "\n");
  87. for (let i: i32 = 0; i < files.length; ++i) {
  88. let f: string = files[i];
  89. let abs: string = path + "/" + f;
  90. let is_file: bool = string_index_of(f, ".") >= 0;
  91. let is_expanded: bool = array_index_of(storage.expanded, abs) >= 0;
  92. // Active file
  93. if (abs == storage.file) {
  94. zui_fill(0, 1, ui._w - 1, ZUI_ELEMENT_H() - 1, theme.BUTTON_PRESSED_COL);
  95. }
  96. let prefix: string = "";
  97. if (!is_file) {
  98. prefix = is_expanded ? "- " : "+ ";
  99. }
  100. if (zui_button(prefix + f, ZUI_ALIGN_LEFT, "")) {
  101. // Open file
  102. if (is_file) {
  103. storage.file = abs;
  104. let bytes: buffer_t = krom_load_blob(storage.file);
  105. storage.text = ends_with(f, ".arm") ? json_stringify(armpack_decode(bytes)) : sys_buffer_to_string(bytes);
  106. storage.text = string_replace_all(storage.text, "\r", "");
  107. text_handle.text = storage.text;
  108. editor_handle.redraws = 1;
  109. krom_set_window_title(abs);
  110. }
  111. // Expand folder
  112. else {
  113. array_index_of(storage.expanded, abs) == -1 ? array_push(storage.expanded, abs) : array_remove(storage.expanded, abs);
  114. }
  115. }
  116. if (is_expanded) {
  117. list_folder(abs);
  118. }
  119. }
  120. }
  121. function render() {
  122. storage.window_w = sys_width();
  123. storage.window_h = sys_height();
  124. storage.window_x = krom_window_x();
  125. storage.window_y = krom_window_y();
  126. if (ui.input_dx != 0 || ui.input_dy != 0) {
  127. krom_set_mouse_cursor(0); // Arrow
  128. }
  129. zui_begin(ui);
  130. if (zui_window(sidebar_handle, 0, 0, storage.sidebar_w, sys_height(), false)) {
  131. let _BUTTON_TEXT_COL: i32 = theme.BUTTON_TEXT_COL;
  132. theme.BUTTON_TEXT_COL = theme.ACCENT_COL;
  133. if (storage.project != "") {
  134. list_folder(storage.project);
  135. }
  136. else {
  137. zui_button("Drop folder here", ZUI_ALIGN_LEFT, "");
  138. }
  139. theme.BUTTON_TEXT_COL = _BUTTON_TEXT_COL;
  140. }
  141. zui_fill(sys_width() - minimap_w, 0, minimap_w, ZUI_ELEMENT_H() + ZUI_ELEMENT_OFFSET() + 1, theme.SEPARATOR_COL);
  142. zui_fill(storage.sidebar_w, 0, 1, sys_height(), theme.SEPARATOR_COL);
  143. let editor_updated: bool = false;
  144. if (zui_window(editor_handle, storage.sidebar_w + 1, 0, sys_width() - storage.sidebar_w - minimap_w, sys_height(), false)) {
  145. editor_updated = true;
  146. let htab: zui_handle_t = zui_handle(__ID__);
  147. let file_name: string = substring(storage.file, string_last_index_of(storage.file, "/") + 1, storage.file.length);
  148. let file_names: string[] = [file_name];
  149. for (let i: i32 = 0; i < file_names.length; ++i) {
  150. let tab_name: string = file_names[i];
  151. if (storage.modified) {
  152. tab_name += "*";
  153. }
  154. if (zui_tab(htab, tab_name, false, -1)) {
  155. // File modified
  156. if (ui.is_key_pressed) {
  157. storage.modified = true;
  158. }
  159. // Save
  160. if (ui.is_ctrl_down && ui.key_code == key_code_t.S) {
  161. save_file();
  162. }
  163. // storage.text = zui_text_area(text_handle, ZUI_ALIGN_LEFT, true, "", false);
  164. zui_text_area(text_handle, ZUI_ALIGN_LEFT, true, "", false);
  165. }
  166. }
  167. window_header_h = 32;//math_floor(ui.windowHeaderH);
  168. }
  169. if (resizing_sidebar) {
  170. storage.sidebar_w += math_floor(ui.input_dx);
  171. }
  172. if (!ui.input_down) {
  173. resizing_sidebar = false;
  174. }
  175. // Minimap controls
  176. let minimap_x: i32 = sys_width() - minimap_w;
  177. let minimap_y: i32 = window_header_h + 1;
  178. let redraw: bool = false;
  179. if (ui.input_started && hit_test(ui.input_x, ui.input_y, minimap_x + 5, minimap_y, minimap_w, minimap_h)) {
  180. minimap_scrolling = true;
  181. }
  182. if (!ui.input_down) {
  183. minimap_scrolling = false;
  184. }
  185. if (minimap_scrolling) {
  186. redraw = true;
  187. }
  188. // Build project
  189. if (ui.is_ctrl_down && ui.key_code == key_code_t.B) {
  190. save_file();
  191. build_project();
  192. }
  193. zui_end(false);
  194. if (redraw) {
  195. editor_handle.redraws = 2;
  196. }
  197. if (minimap != null) {
  198. g2_begin();
  199. g2_draw_image(minimap, minimap_x, minimap_y);
  200. g2_end();
  201. }
  202. if (editor_updated) {
  203. draw_minimap();
  204. }
  205. }
  206. function save_file() {
  207. // Trim
  208. let lines: string[] = string_split(storage.text, "\n");
  209. for (let i: i32 = 0; i < lines.length; ++i) {
  210. lines[i] = trim_end(lines[i]);
  211. }
  212. storage.text = string_array_join(lines, "\n");
  213. // Spaces to tabs
  214. storage.text = string_replace_all(storage.text, " ", "\t");
  215. text_handle.text = storage.text;
  216. // Write bytes
  217. // let bytes: buffer_t = ends_with(storage.file, ".arm") ? armpack_encode(json_parse(storage.text)) : sys_string_to_buffer(storage.text);
  218. // krom_file_save_bytes(storage.file, bytes, buffer_size(bytes));
  219. storage.modified = false;
  220. }
  221. function build_file(): string {
  222. ///if krom_windows
  223. return "\\build.bat";
  224. ///else
  225. return "/build.sh";
  226. ///end
  227. }
  228. function build_project() {
  229. krom_sys_command(storage.project + build_file() + " " + storage.project, null);
  230. }
  231. function draw_minimap() {
  232. if (minimap_h != sys_height()) {
  233. minimap_h = sys_height();
  234. if (minimap != null) {
  235. image_unload(minimap);
  236. }
  237. minimap = image_create_render_target(minimap_w, minimap_h);
  238. }
  239. g2_begin(minimap);
  240. g2_clear(theme.SEPARATOR_COL);
  241. g2_set_color(0xff333333);
  242. let lines: string[] = string_split(storage.text, "\n");
  243. let minimap_full_h: i32 = lines.length * 2;
  244. let scroll_progress: f32 = -editor_handle.scroll_offset / (lines.length * ZUI_ELEMENT_H());
  245. let out_of_screen: i32 = minimap_full_h - minimap_h;
  246. if (out_of_screen < 0) {
  247. out_of_screen = 0;
  248. }
  249. let offset: i32 = math_floor((out_of_screen * scroll_progress) / 2);
  250. for (let i: i32 = 0; i < lines.length; ++i) {
  251. if (i * 2 > minimap_h || i + offset >= lines.length) {
  252. // Out of screen
  253. break;
  254. }
  255. let words: string[] = string_split(lines[i + offset], " ");
  256. let x: i32 = 0;
  257. for (let j: i32 = 0; j < words.length; ++j) {
  258. let word: string = words[j];
  259. g2_fill_rect(x, i * 2, word.length, 2);
  260. x += word.length + 1;
  261. }
  262. }
  263. // Current position
  264. let visible_area: i32 = out_of_screen > 0 ? minimap_h : minimap_full_h;
  265. g2_set_color(0x11ffffff);
  266. minimap_box_h = math_floor((sys_height() - window_header_h) / ZUI_ELEMENT_H() * 2);
  267. g2_fill_rect(0, scroll_progress * visible_area, minimap_w, minimap_box_h);
  268. g2_end();
  269. }
  270. function hit_test(mx: f32, my: f32, x: f32, y: f32, w: f32, h: f32): bool {
  271. return mx > x && mx < x + w && my > y && my < y + h;
  272. }
  273. function on_border_hover(handle: zui_handle_t, side: i32) {
  274. if (handle != sidebar_handle) {
  275. return;
  276. }
  277. if (side != 1) {
  278. return; // Right
  279. }
  280. krom_set_mouse_cursor(3); // Horizontal
  281. if (zui_current.input_started) {
  282. resizing_sidebar = true;
  283. }
  284. }
  285. function on_text_hover() {
  286. krom_set_mouse_cursor(2); // I-cursor
  287. }