Files
armorpaint/paint/sources/tab_scripts.c
T

112 lines
3.1 KiB
C
Raw Normal View History

2026-03-09 13:17:15 +01:00
#include "global.h"
2026-03-09 23:02:08 +01:00
void tab_scripts_draw_export(char *path) {
char *str = tab_scripts_hscript->text;
char *f = ui_files_filename;
if (string_equals(f, "")) {
2026-03-15 11:37:45 +01:00
f = string_copy(tr("untitled"));
2026-03-09 23:02:08 +01:00
}
2026-03-10 21:00:00 +01:00
path = string("%s%s%s", path, PATH_SEP, f);
2026-03-23 23:20:02 +01:00
if (!ends_with(path, ".c")) {
path = string("%s.c", path);
2026-03-09 23:02:08 +01:00
}
iron_file_save_bytes(path, sys_string_to_buffer(str), 0);
}
void tab_scripts_draw_import(char *path) {
buffer_t *b = data_get_blob(path);
tab_scripts_hscript->text = string_copy(sys_buffer_to_string(b));
data_delete_blob(path);
}
void tab_scripts_draw_edit() {
2026-03-15 11:37:45 +01:00
if (ui_menu_button(tr("Clear"), "", ICON_ERASE)) {
2026-03-09 23:02:08 +01:00
tab_scripts_hscript->text = "";
}
2026-03-15 11:37:45 +01:00
if (ui_menu_button(tr("Import"), "", ICON_IMPORT)) {
2026-03-23 23:20:02 +01:00
ui_files_show("c", false, false, &tab_scripts_draw_import);
2026-03-09 23:02:08 +01:00
}
2026-03-15 11:37:45 +01:00
if (ui_menu_button(tr("Export"), "", ICON_EXPORT)) {
2026-03-23 23:20:02 +01:00
ui_files_show("c", true, false, &tab_scripts_draw_export);
2026-03-09 23:02:08 +01:00
}
}
2026-03-06 12:56:38 +01:00
void tab_scripts_draw(ui_handle_t *htab) {
2026-03-15 11:37:45 +01:00
if (ui_tab(htab, tr("Scripts"), false, -1, false)) {
2026-03-06 12:56:38 +01:00
ui_begin_sticky();
f32_array_t *row = f32_array_create_from_raw(
(f32[]){
-70,
-70,
-140,
},
3);
ui_row(row);
2026-03-15 11:37:45 +01:00
if (ui_icon_button(tr("Run"), ICON_PLAY, UI_ALIGN_CENTER)) {
2026-03-23 23:20:02 +01:00
minic_ctx_free(minic_eval(tab_scripts_hscript->text));
2026-03-06 12:56:38 +01:00
}
2026-03-15 11:37:45 +01:00
if (ui_icon_button(tr("Edit"), ICON_EDIT, UI_ALIGN_CENTER)) {
2026-03-09 23:02:08 +01:00
ui_menu_draw(&tab_scripts_draw_edit, -1, -1);
2026-03-06 12:56:38 +01:00
}
string_t_array_t *ar = any_array_create_from_raw(
2026-03-08 08:39:29 +01:00
(void *[]){
2026-03-23 23:20:02 +01:00
"script.c",
2026-03-06 12:56:38 +01:00
},
1);
ui_handle_t *file_handle = ui_handle(__ID__);
2026-03-15 11:37:45 +01:00
ui_combo(file_handle, ar, tr("File"), false, UI_ALIGN_LEFT, true);
2026-03-06 12:56:38 +01:00
2026-03-23 23:20:02 +01:00
#ifdef is_debug
if (ui_icon_button("Run Tests", ICON_PLAY, UI_ALIGN_CENTER)) {
minic_tests();
}
#endif
2026-03-06 12:56:38 +01:00
ui_end_sticky();
draw_font_t *_font = ui->ops->font;
i32 _font_size = ui->font_size;
draw_font_t *f = data_get_font("font_mono.ttf");
ui_set_font(ui, f);
ui->font_size = math_floor(15 * UI_SCALE());
ui_text_area_line_numbers = true;
ui_text_area_scroll_past_end = true;
gc_unroot(ui_text_area_coloring);
ui_text_area_coloring = tab_scripts_get_text_coloring();
gc_root(ui_text_area_coloring);
2026-03-24 20:26:53 +01:00
if (project_raw->script_datas == NULL) {
project_raw->script_datas = string_array_create(0);
}
if (project_raw->script_datas->length == 0) {
string_array_push(project_raw->script_datas, "");
}
tab_scripts_hscript->text = project_raw->script_datas->buffer[0];
2026-03-06 12:56:38 +01:00
ui_text_area(tab_scripts_hscript, UI_ALIGN_LEFT, true, "", false);
2026-03-24 20:26:53 +01:00
project_raw->script_datas->buffer[0] = tab_scripts_hscript->text;
2026-03-06 12:56:38 +01:00
ui_text_area_line_numbers = false;
ui_text_area_scroll_past_end = false;
gc_unroot(ui_text_area_coloring);
2026-03-07 21:12:59 +01:00
ui_text_area_coloring = NULL;
2026-03-06 12:56:38 +01:00
ui_set_font(ui, _font);
ui->font_size = _font_size;
}
}
ui_text_coloring_t *tab_scripts_get_text_coloring() {
2026-03-07 21:12:59 +01:00
if (tab_scripts_text_coloring == NULL) {
2026-03-06 12:56:38 +01:00
buffer_t *blob = data_get_blob("text_coloring.json");
gc_unroot(tab_scripts_text_coloring);
tab_scripts_text_coloring = json_parse(sys_buffer_to_string(blob));
gc_root(tab_scripts_text_coloring);
}
return tab_scripts_text_coloring;
}