2026-03-09 13:17:15 +01:00
|
|
|
|
2026-06-04 11:00:05 +02:00
|
|
|
#include "../global.h"
|
2026-03-09 13:17:15 +01:00
|
|
|
|
2026-04-05 16:09:00 +02:00
|
|
|
ui_text_coloring_t *tab_scripts_text_coloring = NULL;
|
2026-06-03 22:43:57 +02:00
|
|
|
i32 tab_scripts_selected = 0;
|
2026-04-05 16:09:00 +02:00
|
|
|
|
2026-04-15 10:12:01 +02:00
|
|
|
void tab_scripts_prepare() {
|
|
|
|
|
if (g_project->script_datas == NULL) {
|
|
|
|
|
g_project->script_datas = string_array_create(0);
|
2026-06-03 22:43:57 +02:00
|
|
|
g_project->script_names = string_array_create(0);
|
2026-04-15 10:12:01 +02:00
|
|
|
}
|
|
|
|
|
if (g_project->script_datas->length == 0) {
|
2026-06-03 22:43:57 +02:00
|
|
|
string_array_push(g_project->script_datas, "void main() {\n \n}\n");
|
|
|
|
|
string_array_push(g_project->script_names, "main.c");
|
2026-04-15 10:12:01 +02:00
|
|
|
}
|
2026-06-04 18:39:23 +02:00
|
|
|
// A script may have been removed
|
|
|
|
|
if (tab_scripts_selected >= g_project->script_datas->length) {
|
|
|
|
|
tab_scripts_selected = g_project->script_datas->length - 1;
|
|
|
|
|
}
|
2026-04-15 10:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *tab_scripts_get() {
|
|
|
|
|
tab_scripts_prepare();
|
2026-06-03 22:43:57 +02:00
|
|
|
return g_project->script_datas->buffer[tab_scripts_selected];
|
2026-04-15 10:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tab_scripts_set(char *s) {
|
|
|
|
|
tab_scripts_prepare();
|
2026-06-03 22:43:57 +02:00
|
|
|
g_project->script_datas->buffer[tab_scripts_selected] = string_copy(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tab_scripts_create(char *name) {
|
|
|
|
|
tab_scripts_prepare();
|
|
|
|
|
i32 i = string_array_index_of(g_project->script_names, name);
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
string_array_push(g_project->script_names, string_copy(name));
|
|
|
|
|
string_array_push(g_project->script_datas, "");
|
|
|
|
|
tab_scripts_selected = g_project->script_datas->length - 1;
|
|
|
|
|
}
|
2026-04-15 10:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 23:02:08 +01:00
|
|
|
void tab_scripts_draw_export(char *path) {
|
2026-04-15 10:12:01 +02:00
|
|
|
char *str = tab_scripts_get();
|
2026-03-09 23:02:08 +01:00
|
|
|
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) {
|
2026-04-21 10:52:27 +02:00
|
|
|
buffer_t *b = data_get_blob(path);
|
2026-04-15 10:12:01 +02:00
|
|
|
tab_scripts_set(sys_buffer_to_string(b));
|
2026-03-09 23:02:08 +01:00
|
|
|
data_delete_blob(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tab_scripts_draw_edit() {
|
2026-06-03 22:43:57 +02:00
|
|
|
tab_scripts_prepare();
|
|
|
|
|
|
2026-03-15 11:37:45 +01:00
|
|
|
if (ui_menu_button(tr("Clear"), "", ICON_ERASE)) {
|
2026-04-15 10:12:01 +02:00
|
|
|
tab_scripts_set("");
|
2026-03-09 23:02:08 +01:00
|
|
|
}
|
2026-06-03 22:43:57 +02:00
|
|
|
ui->enabled = !string_equals(g_project->script_names->buffer[tab_scripts_selected], "main.c");
|
|
|
|
|
if (ui_menu_button(tr("Delete"), "", ICON_DELETE)) {
|
|
|
|
|
array_splice((any_array_t *)g_project->script_datas, tab_scripts_selected, 1);
|
|
|
|
|
array_splice((any_array_t *)g_project->script_names, tab_scripts_selected, 1);
|
|
|
|
|
tab_scripts_selected = 0;
|
|
|
|
|
}
|
|
|
|
|
ui->enabled = true;
|
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-24 23:01:29 +01:00
|
|
|
if (ui_menu_sub_button(ui_handle(__ID__), tr("Templates"))) {
|
|
|
|
|
ui_menu_sub_begin(2);
|
|
|
|
|
if (ui_menu_button("hello.c", "", ICON_DRAFT)) {
|
2026-06-03 22:43:57 +02:00
|
|
|
tab_scripts_set("\
|
2026-03-24 23:01:29 +01:00
|
|
|
void main() {\n\
|
|
|
|
|
printf(\"Hello, world!\\n\");\n\
|
|
|
|
|
}\n\
|
2026-06-03 22:43:57 +02:00
|
|
|
");
|
2026-03-24 23:01:29 +01:00
|
|
|
}
|
|
|
|
|
if (ui_menu_button("rotate.c", "", ICON_DRAFT)) {
|
2026-06-03 22:43:57 +02:00
|
|
|
tab_scripts_set("\
|
2026-03-24 23:01:29 +01:00
|
|
|
void on_update() {\n\
|
|
|
|
|
mesh_object_t *o = context_main_object();\n\
|
|
|
|
|
transform_rotate(o->base->transform, vec4_z_axis(), 0.005);\n\
|
|
|
|
|
context_t *c = script_get_context();\n\
|
|
|
|
|
c->ddirty = 2;\n\
|
|
|
|
|
}\n\
|
|
|
|
|
void main() {\n\
|
|
|
|
|
script_notify_on_update(on_update);\n\
|
|
|
|
|
}\n\
|
2026-06-03 22:43:57 +02:00
|
|
|
");
|
2026-03-24 23:01:29 +01:00
|
|
|
}
|
|
|
|
|
ui_menu_sub_end();
|
|
|
|
|
}
|
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,
|
|
|
|
|
},
|
2026-05-07 13:24:49 +02:00
|
|
|
3);
|
|
|
|
|
|
2026-06-03 22:43:57 +02:00
|
|
|
// #ifndef NDEBUG
|
|
|
|
|
// if (g_config->experimental) {
|
|
|
|
|
// f32_array_push(row, -90);
|
|
|
|
|
// }
|
|
|
|
|
// #endif
|
2026-05-05 17:16:01 +02:00
|
|
|
|
2026-03-06 12:56:38 +01:00
|
|
|
ui_row(row);
|
|
|
|
|
|
2026-03-15 11:37:45 +01:00
|
|
|
if (ui_icon_button(tr("Run"), ICON_PLAY, UI_ALIGN_CENTER)) {
|
2026-03-24 23:01:29 +01:00
|
|
|
minic_ctx_t *ctx = minic_eval(tab_scripts_hscript->text);
|
|
|
|
|
// minic_ctx_free(ctx);
|
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
|
|
|
}
|
|
|
|
|
|
2026-06-03 22:43:57 +02:00
|
|
|
tab_scripts_prepare();
|
2026-03-06 12:56:38 +01:00
|
|
|
ui_handle_t *file_handle = ui_handle(__ID__);
|
2026-06-03 22:43:57 +02:00
|
|
|
file_handle->i = tab_scripts_selected;
|
|
|
|
|
tab_scripts_selected = ui_combo(file_handle, g_project->script_names, tr("File"), false, UI_ALIGN_LEFT, true);
|
2026-03-06 12:56:38 +01:00
|
|
|
|
2026-06-03 22:43:57 +02:00
|
|
|
// #ifndef NDEBUG
|
|
|
|
|
// if (g_config->experimental && ui_icon_button("Run Tests", ICON_PLAY, UI_ALIGN_CENTER)) {
|
|
|
|
|
// minic_tests();
|
|
|
|
|
// }
|
|
|
|
|
// #endif
|
2026-03-23 23:20:02 +01:00
|
|
|
|
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
|
|
|
|
2026-04-15 10:12:01 +02:00
|
|
|
tab_scripts_prepare();
|
2026-03-24 20:26:53 +01:00
|
|
|
|
2026-06-03 22:43:57 +02:00
|
|
|
tab_scripts_hscript->text = g_project->script_datas->buffer[tab_scripts_selected];
|
2026-03-06 12:56:38 +01:00
|
|
|
ui_text_area(tab_scripts_hscript, UI_ALIGN_LEFT, true, "", false);
|
2026-06-03 22:43:57 +02:00
|
|
|
g_project->script_datas->buffer[tab_scripts_selected] = tab_scripts_hscript->text;
|
2026-03-24 20:26:53 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|