diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 9b7d6c5..36d8abb 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -5679,13 +5679,70 @@ static void ctx_exec(Ed* ed, int id) { } } +/* == Dialogos modais como classes === + * Cada dialogo (configuracoes, bibliotecas, explorador, previa do + * scratchblocks, "Criar um Bloco") e uma ModalClass. A entrada vai para o + * aberto mais alto; o desenho vai de baixo para cima. */ +typedef struct ModalClass { + const char* name; + bool (*is_open)(Ed* ed); + void (*render)(Ed* ed, TinyBlocks_RenderableBlock* rb); + void (*click)(Ed* ed, float mx, float my); + void (*key)(Ed* ed, int sc, bool ctrl); + void (*text)(Ed* ed, const char* typed); + void (*scroll)(Ed* ed, float dy); +} ModalClass; + +static bool settings_is_open(Ed* ed) { return ed->settings_open; } +static void settings_scroll_m(Ed* ed, float dy) { settings_scroll_by(ed, dy); } + +static bool libman_is_open(Ed* ed) { return ed->libman_open; } +static void libman_key_m(Ed* ed, int sc, bool ctrl) { (void)ctrl; libman_key(ed, sc); } +static void libman_scroll_m(Ed* ed, float dy) { libman_scroll_by(ed, dy); } + +/* o explorador vem do modulo do editor (pode faltar) */ +static bool files_is_open(Ed* ed) { return ed->fdlg.open; } +static void files_render_m(Ed* ed, TinyBlocks_RenderableBlock* rb) { if (files_view) files_view(ed, rb); } +static void files_click_m(Ed* ed, float mx, float my) { if (files_click) files_click(ed, mx, my); } +static void files_key_m(Ed* ed, int sc, bool ctrl) { if (files_key) files_key(ed, sc, ctrl); else ed->fdlg.open = false; } +static void files_text_m(Ed* ed, const char* t) { if (files_text) files_text(ed, t); } +static void files_scroll_m(Ed* ed, float dy) { if (files_scroll) files_scroll(ed, dy); } + +static bool sbp_is_open(Ed* ed) { return ed->sbp_open; } +static void sbp_key(Ed* ed, int sc, bool ctrl) { + if (sc == TB_KEY_ESCAPE) sbp_close(ed); + else if (ctrl && sc == TB_KEY_A + ('c' - 'a')) sbp_copy(ed); +} +static void sbp_scroll(Ed* ed, float dy) { ed->sbp_scroll -= (int)(dy * 3); } + +static bool dlg_is_open(Ed* ed) { return ed->comp_dialog_open; } +static void dlg_key_m(Ed* ed, int sc, bool ctrl) { (void)ctrl; dlg_key(ed, sc); } +static void dlg_click_m(Ed* ed, float mx, float my) { dlg_click(ed, mx, my); } + +static const ModalClass s_modals[] = { /* do mais alto para o mais baixo */ + { "configurações", settings_is_open, settings_render, settings_click, settings_key, settings_text, settings_scroll_m }, + { "bibliotecas", libman_is_open, libman_render, libman_click, libman_key_m, libman_text, libman_scroll_m }, + { "explorador", files_is_open, files_render_m, files_click_m, files_key_m, files_text_m, files_scroll_m }, + { "scratchblocks", sbp_is_open, sbp_render, sbp_click, sbp_key, NULL, sbp_scroll }, + { "criar bloco", dlg_is_open, dlg_render, dlg_click_m, dlg_key_m, dlg_text, NULL }, +}; +#define MODAL_N ((int)(sizeof(s_modals) / sizeof(s_modals[0]))) + +/* Dialogo aberto mais alto (recebe a entrada), ou NULL */ +static const ModalClass* modal_top(Ed* ed) { + for (int i = 0; i < MODAL_N; i++) if (s_modals[i].is_open(ed)) return &s_modals[i]; + return NULL; +} + +static void modals_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { + for (int i = MODAL_N - 1; i >= 0; i--) + if (s_modals[i].is_open(ed) && s_modals[i].render) s_modals[i].render(ed, rb); +} + static void on_lmb_down(Ed* ed, float mx, float my) { if (toasts_click(ed, mx, my)) return; - if (ed->settings_open) { settings_click(ed, mx, my); return; } - if (ed->libman_open) { libman_click(ed, mx, my); return; } - if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; } - if (ed->fdlg.open) { if (files_click) files_click(ed, mx, my); return; } - if (ed->sbp_open) { sbp_click(ed, mx, my); return; } + const ModalClass* mc = modal_top(ed); + if (mc) { if (mc->click) mc->click(ed, mx, my); return; } if (ed->plus_menu_open) { float px, py; plus_menu_pos(ed, &px, &py); @@ -5922,7 +5979,7 @@ static void drag_to_ctx(Ed* ed, int tgt) { } static void on_lmb_up(Ed* ed, float mx, float my) { - if (ed->comp_dialog_open || ed->fdlg.open) return; + if (modal_top(ed)) return; if (ed->split_drag >= 0) { ed->split_drag = -1; lay_save(ed); return; } if (ed->tab_press >= 0) { bool was = ed->tab_drag; @@ -5983,7 +6040,7 @@ static void on_lmb_up(Ed* ed, float mx, float my) { } static void on_rmb_down(Ed* ed, float mx, float my) { - if (ed->fdlg.open) return; + if (modal_top(ed)) return; if (ed->ctx_menu_open) { ed->ctx_menu_open = false; ed->ctx_export = false; return; } if (ed->state != ES_IDLE) return; if (!in_ws(ed, mx, my)) return; @@ -6054,15 +6111,8 @@ TB_ED_API void path_commit(Ed* ed, int mode, const char* p) { static void on_key(Ed* ed, int sc, bool ctrl) { bool enter = sc == TB_KEY_RETURN; rt_cache_clear(ed); - if (ed->settings_open) { settings_key(ed, sc, ctrl); return; } - if (ed->libman_open) { libman_key(ed, sc); return; } - if (ed->comp_dialog_open) { dlg_key(ed, sc); return; } - if (ed->fdlg.open) { if (files_key) files_key(ed, sc, ctrl); else ed->fdlg.open = false; return; } - if (ed->sbp_open) { - if (sc == TB_KEY_ESCAPE) sbp_close(ed); - else if (ctrl && sc == TB_KEY_A + ('c' - 'a')) sbp_copy(ed); - return; - } + const ModalClass* mc = modal_top(ed); + if (mc) { if (mc->key) mc->key(ed, sc, ctrl); return; } if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; } if (ed->grp_edit >= 0 && ed->grp_edit < CT.group_n && !ed->path_input_active) { char* t = CT.groups[ed->grp_edit].title; @@ -6118,10 +6168,8 @@ static void on_key(Ed* ed, int sc, bool ctrl) { static void on_text(Ed* ed, const char* typed) { rt_cache_clear(ed); - if (ed->settings_open) { settings_text(ed, typed); return; } - if (ed->libman_open) { libman_text(ed, typed); return; } - if (ed->comp_dialog_open) { dlg_text(ed, typed); return; } - if (ed->fdlg.open) { if (files_text) files_text(ed, typed); return; } + const ModalClass* mc = modal_top(ed); + if (mc) { if (mc->text) mc->text(ed, typed); return; } if (ed->path_input_active) { append_text(ed->path_input_buf, sizeof(ed->path_input_buf), typed); ed->path_input_len = (int)strlen(ed->path_input_buf); @@ -6303,7 +6351,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { /* == Areas: borda sendo arrastada / area levada pela alca === */ if (ed->split_drag >= 0) { split_drag_to(ed, ed->split_drag, mx, my); lay_compute(ed); } if (ed->area_drag >= 0) ed->area_drop = view_at(ed, mx, my); - bool modal = ed->settings_open || ed->libman_open || ed->comp_dialog_open || ed->fdlg.open || ed->sbp_open; + bool modal = modal_top(ed) != NULL; ed->split_hover = modal ? -1 : split_at(ed, mx, my); /* == Aba sendo arrastada: reordena ao passar do meio da vizinha === */ @@ -6348,14 +6396,9 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { double sdx = ev_real(evb, GFX_IN_SCROLL_DX); double sdy = ev_real(evb, GFX_IN_SCROLL_DY); if (sdx != 0.0 || sdy != 0.0) { - if (ed->sbp_open) { - ed->sbp_scroll -= (int)(sdy * 3); - } else if (ed->fdlg.open) { - if (files_scroll) files_scroll(ed, (float)sdy); - } else if (ed->settings_open) { - settings_scroll_by(ed, (float)sdy); - } else if (ed->libman_open) { - libman_scroll_by(ed, (float)sdy); + const ModalClass* mc = modal_top(ed); + if (mc) { + if (mc->scroll) mc->scroll(ed, (float)sdy); } else if (in_ws(ed, mx, my) && tab_class(&CT)->scroll) { tab_class(&CT)->scroll(ed, mx, my, sdx, sdy); } else if (in_ws(ed, mx, my)) { @@ -6562,11 +6605,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, render_plus_menu(ed, rb); render_tooltip(ed, rb); render_ctx_menu(ed, rb); - dlg_render(ed, rb); - sbp_render(ed, rb); - if (ed->fdlg.open && files_view) files_view(ed, rb); - libman_render(ed, rb); /* tela cheia: por cima de tudo */ - settings_render(ed, rb); + modals_render(ed, rb); /* de baixo para cima; configuracoes por cima de tudo */ render_toasts(ed, rb); /* avisos flutuam por cima de tudo */ areas_restore(ed, saved);