diff --git a/paint/assets/plugins/import_txt.c b/paint/assets/plugins/import_txt.c deleted file mode 100644 index 75c6731d..00000000 --- a/paint/assets/plugins/import_txt.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "global.h" - -void *plugin; - -void *import_txt(char *path) { - void *b = data_get_blob(path); - - string_array_t *a = string_split(path, "\\\\"); - char *s = array_pop(a); - a = string_split(s, "/"); - s = array_pop(a); - char *filename = s; - - ui_box_show_message(filename, sys_buffer_to_string(b), true); - data_delete_blob(path); - return NULL; -} - -void on_delete() { - plugin_unregister_texture("txt", import_txt); -} - -void main() { - plugin = plugin_create(); - gc_root(plugin); - plugin_notify_on_delete(plugin, on_delete); - plugin_register_texture("txt", import_txt); -} diff --git a/paint/sources/functions.h b/paint/sources/functions.h index d8223482..05b31f81 100644 --- a/paint/sources/functions.h +++ b/paint/sources/functions.h @@ -157,6 +157,7 @@ i32 pipes_get_constant_location(char *type); void make_brush_run(node_shader_t *kong); void import_json_material_run(char *path); void import_texture_run(char *path, bool hdr_as_envmap); +void import_text_run(char *path); void import_lut_run(const char *path); void import_lut_free(); void ui_files_show(char *filters, bool is_save, bool open_multiple, void (*files_done)(char *)); diff --git a/paint/sources/globals.h b/paint/sources/globals.h index 9ba815fe..14db9492 100644 --- a/paint/sources/globals.h +++ b/paint/sources/globals.h @@ -103,6 +103,7 @@ i32 pipes_cursor_decal_texdecal; i32 pipes_cursor_decal_gbuffer0; i32 pipes_offset; any_map_t *import_texture_importers; +any_map_t *import_text_importers; #ifdef IRON_WINDOWS char *ui_files_default_path = "C:\\Users"; diff --git a/paint/sources/io/import_asset.c b/paint/sources/io/import_asset.c index dc8d1220..171f55d2 100644 --- a/paint/sources/io/import_asset.c +++ b/paint/sources/io/import_asset.c @@ -63,6 +63,9 @@ void import_asset_run(char *path, f32 drop_x, f32 drop_y, bool show_box, bool hd g_context->ddirty = 2; } } + else if (path_is_text(path)) { + import_text_run(path); + } else if (path_is_sound(path)) { import_sound_run(path); } diff --git a/paint/sources/io/import_text.c b/paint/sources/io/import_text.c new file mode 100644 index 00000000..ee7e0298 --- /dev/null +++ b/paint/sources/io/import_text.c @@ -0,0 +1,28 @@ + +#include "../global.h" + +void import_text_default_importer(char *path) { + buffer_t *b = data_get_blob(path); + if (b == NULL) { + console_error(strings_unknown_asset_format()); + return; + } + + string_array_t *ar = string_split(path, PATH_SEP); + char *name = ar->buffer[ar->length - 1]; + + ui_box_show_message(name, sys_buffer_to_string(b), true); + data_delete_blob(path); +} + +void import_text_run(char *path) { + char *ext = substring(path, string_last_index_of(path, ".") + 1, string_length(path)); + void (*importer)(char *path) = any_map_get(import_text_importers, ext); + + if (importer == NULL) { + import_text_default_importer(path); + return; + } + + importer(path); +} diff --git a/paint/sources/main.c b/paint/sources/main.c index ac06bd70..1d9c2e10 100644 --- a/paint/sources/main.c +++ b/paint/sources/main.c @@ -21,6 +21,7 @@ #include "io/import_obj.c" #include "io/import_plugin.c" #include "io/import_sound.c" +#include "io/import_text.c" #include "io/import_texture.c" #include "io/import_theme.c" @@ -281,6 +282,9 @@ void _kickstart() { import_texture_importers = any_map_create(); gc_root(import_texture_importers); + import_text_importers = any_map_create(); + gc_root(import_text_importers); + ui_files_path = ui_files_default_path; gc_root(ui_files_path); diff --git a/paint/sources/minic_api.c b/paint/sources/minic_api.c index 365efc0c..54ad1a56 100644 --- a/paint/sources/minic_api.c +++ b/paint/sources/minic_api.c @@ -65,6 +65,8 @@ void plugin_register_texture(char *format, void *fn); void plugin_unregister_texture(char *format); void plugin_register_mesh(char *format, void *fn); void plugin_unregister_mesh(char *format); +void plugin_register_text(char *format, void *fn); +void plugin_unregister_text(char *format); raw_mesh_t *plugin_make_raw_mesh(char *name, i16_array_t *posa, i16_array_t *nora, u32_array_t *inda, float scale_pos); void plugin_material_category_add(char *category_name, any_array_t *node_list); void plugin_brush_category_add(char *category_name, any_array_t *node_list); @@ -1231,6 +1233,8 @@ void minic_register_builtins() { R(plugin_unregister_texture, "v(p)"); R(plugin_register_mesh, "v(p,p)"); R(plugin_unregister_mesh, "v(p)"); + R(plugin_register_text, "v(p,p)"); + R(plugin_unregister_text, "v(p)"); R(plugin_make_raw_mesh, "p(p,p,p,p,f)"); R(plugin_material_category_add, "v(p,p)"); R(plugin_brush_category_add, "v(p,p)"); diff --git a/paint/sources/ui/ui_box.c b/paint/sources/ui/ui_box.c index 1d2b2a27..99a5a273 100644 --- a/paint/sources/ui/ui_box.c +++ b/paint/sources/ui/ui_box.c @@ -6,9 +6,10 @@ char *ui_box_title = ""; char *ui_box_text = ""; void (*ui_box_commands)(void) = NULL; void (*ui_box_modal_on_hide)(void) = NULL; -i32 ui_box_draws = 0; -bool ui_box_copyable = false; -f32 ui_box_tween_alpha = 0.0; +i32 ui_box_draws = 0; +bool ui_box_copyable = false; +f32 ui_box_tween_alpha = 0.0; +static bool ui_box_ignore_release = false; void ui_box_init() { ui_box_hwnd->redraws = 2; @@ -17,6 +18,7 @@ void ui_box_init() { ui_box_show = true; ui_box_draws = 0; ui_box_click_to_hide = true; + ui_box_ignore_release = g_ui->input_down; // Box may open on mouse down } void ui_box_render() { @@ -27,7 +29,12 @@ void ui_box_render() { if (!ui_menu_show) { bool in_use = g_ui->combo_selected_handle != NULL; bool is_escape = g_ui->is_escape_down; - if (ui_box_draws > 2 && (g_ui->input_released || is_escape) && !in_use && !g_ui->is_typing) { + bool released = g_ui->input_released; + if (released && ui_box_ignore_release) { + ui_box_ignore_release = false; + released = false; + } + if (ui_box_draws > 2 && (released || is_escape) && !in_use && !g_ui->is_typing) { i32 appw = iron_window_width(); i32 apph = iron_window_height(); i32 mw = math_floor(ui_box_modalw * UI_SCALE()); @@ -76,7 +83,13 @@ void ui_box_render() { ui_handle_t *htext = ui_handle(__ID__); htext->text = string_copy(ui_box_text); if (ui_box_copyable) { + draw_font_t *_font = g_font; + i32 _font_size = g_ui->font_size; + ui_set_font(g_ui, data_get_font("font_mono.ttf")); + g_ui->font_size = math_floor(15 * UI_SCALE()); ui_text_area(htext, UI_ALIGN_LEFT, false, "", false); + ui_set_font(g_ui, _font); + g_ui->font_size = _font_size; } else { ui_text(ui_box_text, UI_ALIGN_LEFT, 0x00000000); @@ -167,8 +180,8 @@ void ui_box_tween_out() { void ui_box_show_message(char *title, char *text, bool copyable) { ui_box_init(); - ui_box_modalw = 400; - ui_box_modalh = 180; + ui_box_modalw = copyable ? 800 : 400; + ui_box_modalh = copyable ? 600 : 180; gc_unroot(ui_box_title); ui_box_title = string_copy(title); gc_root(ui_box_title); diff --git a/paint/sources/util/util_script.c b/paint/sources/util/util_script.c index dce2edc1..2ae4fffe 100644 --- a/paint/sources/util/util_script.c +++ b/paint/sources/util/util_script.c @@ -160,9 +160,12 @@ object_t *script_get_object(char *s) { extern string_array_t *_path_texture_formats; extern string_array_t *_path_mesh_formats; +extern string_array_t *_path_text_formats; -static any_map_t *custom_texture_importers = NULL; -static any_map_t *custom_mesh_importers = NULL; +static any_map_t *custom_texture_importers = NULL; +static any_map_t *custom_mesh_importers = NULL; +static any_map_t *custom_text_importers = NULL; +static string_array_t *custom_text_formats = NULL; gpu_texture_t *plugin_import_custom_texture(char *path) { char *format = substring(path, string_last_index_of(path, ".") + 1, string_length(path)); @@ -180,6 +183,43 @@ raw_mesh_t *plugin_import_custom_mesh(char *path) { return r.p; } +void plugin_import_custom_text(char *path) { + char *format = substring(path, string_last_index_of(path, ".") + 1, string_length(path)); + void *fn = any_map_get(custom_text_importers, format); + minic_val_t args[1] = {minic_val_ptr(path)}; + minic_call_fn(fn, args, 1); +} + +void plugin_register_text(char *format, void *fn) { + any_map_set(import_text_importers, format, plugin_import_custom_text); + + if (custom_text_formats == NULL) { + custom_text_formats = string_array_create(0); + gc_root(custom_text_formats); + } + if (string_array_index_of(path_text_formats(), format) < 0) { + any_array_push((any_array_t *)_path_text_formats, format); + string_array_push(custom_text_formats, format); + } + + if (custom_text_importers == NULL) { + custom_text_importers = any_map_create(); + gc_root(custom_text_importers); + } + any_map_set(custom_text_importers, format, fn); +} + +void plugin_unregister_text(char *format) { + map_delete(import_text_importers, format); + map_delete(custom_text_importers, format); + + i32 i = custom_text_formats != NULL ? string_array_index_of(custom_text_formats, format) : -1; + if (i >= 0) { + array_splice((any_array_t *)custom_text_formats, i, 1); + array_splice((any_array_t *)_path_text_formats, string_array_index_of(_path_text_formats, format), 1); + } +} + void plugin_register_texture(char *format, void *fn) { any_map_set(import_texture_importers, format, plugin_import_custom_texture); any_array_push((any_array_t *)_path_texture_formats, format);