diff --git a/libraries/tinyblocks-editor/internal/tb_editor_internal.h b/libraries/tinyblocks-editor/internal/tb_editor_internal.h index 2a41a1d..7203a03 100644 --- a/libraries/tinyblocks-editor/internal/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/internal/tb_editor_internal.h @@ -988,6 +988,27 @@ bool hit_libman_foot(Ed* ed, float mx, float my); /* Abas de codigo/imagem + tooltip de metadados */ bool tab_new_kind(Ed* ed, const char* name, bool is_composite, int kind); + +/* == Tipos de aba como classes === + * Cada kind de aba (blocos, codigo, imagem) e uma TabClass: o editor chama + * os hooks em vez de testar o kind. Hook NULL = comportamento da area de + * blocos (o padrao do editor). */ +typedef struct TabClass { + const char* name; + bool blocks; /* aceita blocos (area de scripts) */ + void (*init)(Ed* ed, Tab* T); /* aba nova */ + void (*render)(Ed* ed, TinyBlocks_RenderableBlock* rb); + void (*click)(Ed* ed, float mx, float my); /* botao esquerdo na area */ + bool (*release)(Ed* ed, float mx, float my); /* true = tratou */ + void (*motion)(Ed* ed, float mx, float my); + bool (*key)(Ed* ed, int sc, bool ctrl); /* true = tratou */ + bool (*text)(Ed* ed, const char* typed); /* true = tratou */ + bool (*scroll)(Ed* ed, float mx, float my, double dx, double dy); + bool (*icon)(TinyBlocks_RenderableBlock* rb, float x, float cy); /* icone na barra */ +} TabClass; + +const TabClass* tab_class(const Tab* T); +bool tab_has_define(const Tab* T); bool tab_has_define(const Tab* T); void tab_doc_free(Tab* T); void code_rehighlight(Tab* T); diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 9516539..9b7d6c5 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -1454,7 +1454,7 @@ bool tab_has_define(const Tab* T) { static int define_find(Ed* ed, const char* name, int* tab) { for (int t = 0; t < ed->tab_n; t++) { Tab* T = &ed->tabs[t]; - if (T->kind != TK_BLOCKS) continue; + if (!tab_class(T)->blocks) continue; for (int i = 0; i < T->top_n; i++) { EdBlock* b = &T->ws[T->top[i]]; if (b->used && strcmp(b->type_name, "Port.Define") == 0 && strcmp(b->in_text[0], name) == 0) { @@ -2198,7 +2198,7 @@ bool tab_new_kind(Ed* ed, const char* name, bool is_composite, int kind) { T->img_rgb[0] = 230; T->img_rgb[1] = 38; T->img_rgb[2] = 38; T->img_zoom = 1.f; T->img_layer_cur = -1; - if (kind == TK_IMAGE) img_ensure(ed, T); + if (tab_class(T)->init) tab_class(T)->init(ed, T); tab_select(ed, idx); return true; } @@ -2288,7 +2288,7 @@ static void tab_close(Ed* ed, int ti) { /* "Criar um Bloco": numa aba de blocos o "defina" fica na propria aba */ static void new_composite_tab(Ed* ed) { dlg_open(ed, -1); - ed->comp_dialog_in_tab = CT.kind == TK_BLOCKS && !CT.is_composite; + ed->comp_dialog_in_tab = tab_class(&CT)->blocks && !CT.is_composite; } static void open_def(Ed* ed, const char* uname) { @@ -2728,7 +2728,7 @@ static TbSbContext sb_context(Ed* ed, TbSbLabel** labels_out) { } static void sb_export_file(Ed* ed, const char* path) { - if (CT.kind != TK_BLOCKS) { + if (!tab_class(&CT)->blocks) { snprintf(ed->hot_status, sizeof(ed->hot_status), "Só abas de blocos viram scratchblocks"); ed->hot_status_ok = false; return; @@ -2820,7 +2820,7 @@ static void sbp_close(Ed* ed) { /* Gera o texto da aba atual e abre a previa */ static void sbp_open(Ed* ed) { - if (CT.kind != TK_BLOCKS) { + if (!tab_class(&CT)->blocks) { snprintf(ed->hot_status, sizeof(ed->hot_status), "Só abas de blocos viram scratchblocks"); ed->hot_status_ok = false; return; @@ -3625,15 +3625,7 @@ static void render_tab_bar(Ed* ed, TinyBlocks_RenderableBlock* rb) { if (ed->tabs[i].is_composite) { rb_fill_circle(rb, tx+14, cy, 5, s_user_colors[ed->tabs[i].color_idx]); lx = tx + 24; - } else if (ed->tabs[i].kind == TK_CODE) { - rb_fill_rrect(rb, tx+9, cy-6, 11, 13, 2, COLA(42,74,110,255)); - rb_fill_rect(rb, tx+11, cy-3, 6, 1.4f, COLA(120,190,255,255)); - rb_fill_rect(rb, tx+11, cy+0, 6, 1.4f, COLA(120,190,255,255)); - rb_fill_rect(rb, tx+11, cy+3, 4, 1.4f, COLA(120,190,255,255)); - lx = tx + 24; - } else if (ed->tabs[i].kind == TK_IMAGE) { - rb_fill_rrect(rb, tx+9, cy-6, 12, 12, 2, COLA(200,150,60,255)); - rb_fill_circle(rb, tx+13, cy-2, 2, COLA(255,235,180,255)); + } else if (tab_class(&ed->tabs[i])->icon && tab_class(&ed->tabs[i])->icon(rb, tx, cy)) { lx = tx + 24; } rb_text_c(rb, lx, cy, ed->tabs[i].name, cur ? COL_MENU : COL_TEXT, FS_UI); @@ -4226,6 +4218,75 @@ void render_tooltip(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_text_c(rb, x + 12, y + h - 8, "clique para abrir o codigo nesta linha", COL_TEXT_DIM, 9.f); } +/* == Classes de aba (ver TabClass) ===================================== */ + +static void code_tab_click(Ed* ed, float mx, float my) { ed->editing = false; code_click(ed, mx, my); } +static bool code_tab_scroll(Ed* ed, float mx, float my, double dx, double dy) { + (void)mx; (void)my; (void)dx; + CT.code_scroll -= (int)dy * 3; + if (CT.code_scroll < 0) CT.code_scroll = 0; + return true; +} +static bool code_tab_icon(TinyBlocks_RenderableBlock* rb, float x, float cy) { + rb_fill_rrect(rb, x+9, cy-6, 11, 13, 2, COLA(42,74,110,255)); + rb_fill_rect(rb, x+11, cy-3, 6, 1.4f, COLA(120,190,255,255)); + rb_fill_rect(rb, x+11, cy+0, 6, 1.4f, COLA(120,190,255,255)); + rb_fill_rect(rb, x+11, cy+3, 4, 1.4f, COLA(120,190,255,255)); + return true; +} + +static void image_tab_init(Ed* ed, Tab* T) { img_ensure(ed, T); } +static void image_tab_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { render_image_tab(ed, rb); } +static void image_tab_click(Ed* ed, float mx, float my) { image_click(ed, mx, my); } +static bool image_tab_release(Ed* ed, float mx, float my) { + if (!CT.img_drag) return false; + image_release(ed, mx, my); + return true; +} +static void image_tab_motion(Ed* ed, float mx, float my) { + if (CT.img_drag && (mx != ed->img_mx || my != ed->img_my)) image_drag(ed, mx, my); +} +/* selecao: Delete apaga, Esc fixa */ +static bool image_tab_key(Ed* ed, int sc, bool ctrl) { + (void)ctrl; + if (!CT.img_sel.on) return false; + if (sc == TB_KEY_DELETE || sc == TB_KEY_BACKSPACE) { + img_sel_reset(&CT); + CT.img_rev++; + snprintf(ed->hot_status, sizeof(ed->hot_status), "Selecao apagada"); + ed->hot_status_ok = true; + return true; + } + if (sc == TB_KEY_ESCAPE) { img_sel_commit(&CT); return true; } + return false; +} +/* roda do mouse = zoom no cursor (nao pinta) */ +static bool image_tab_scroll(Ed* ed, float mx, float my, double dx, double dy) { + (void)dx; + image_zoom(ed, mx, my, (int)(dy > 0 ? 1 : dy < 0 ? -1 : 0)); + return true; +} +static bool image_tab_icon(TinyBlocks_RenderableBlock* rb, float x, float cy) { + rb_fill_rrect(rb, x+9, cy-6, 12, 12, 2, COLA(200,150,60,255)); + rb_fill_circle(rb, x+13, cy-2, 2, COLA(255,235,180,255)); + return true; +} + +static const TabClass s_tab_classes[] = { + [TK_BLOCKS] = { "Blocos", true, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, + [TK_CODE] = { "Código", false, NULL, render_code_tab, code_tab_click, NULL, NULL, + code_key, code_text, code_tab_scroll, code_tab_icon }, + [TK_IMAGE] = { "Imagem", false, image_tab_init, image_tab_render, image_tab_click, + image_tab_release, image_tab_motion, image_tab_key, NULL, + image_tab_scroll, image_tab_icon }, +}; + +const TabClass* tab_class(const Tab* T) { + int k = T ? T->kind : TK_BLOCKS; + if (k < 0 || k >= (int)(sizeof(s_tab_classes) / sizeof(s_tab_classes[0]))) k = TK_BLOCKS; + return &s_tab_classes[k]; +} + /* == Grupos (retangulos coloridos que juntam pilhas) === */ static int group_at(Tab* T, float wx, float wy, bool head_only) { for (int g = T->group_n - 1; g >= 0; g--) { @@ -4351,8 +4412,8 @@ static void group_motion(Ed* ed, float mx, float my) { } static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { - if (CT.kind == TK_CODE) { render_code_tab(ed, rb); return; } - if (CT.kind == TK_IMAGE) { render_image_tab(ed, rb); return; } + const TabClass* tc = tab_class(&CT); + if (tc->render) { tc->render(ed, rb); return; } float ox = WS_X - CT.ws_sx, oy = WS_Y - CT.ws_sy; rb_fill_rect(rb, WS_X, WS_Y, WS_W, WS_H, COL_WS_BG); clip_set(ed, rb, WS_X, WS_Y, WS_W, WS_H); @@ -5778,8 +5839,8 @@ static void on_lmb_down(Ed* ed, float mx, float my) { /* Abas de codigo / imagem */ if (in_ws(ed, mx, my)) { - if (CT.kind == TK_CODE) { ed->editing = false; code_click(ed, mx, my); return; } - if (CT.kind == TK_IMAGE) { image_click(ed, mx, my); return; } + const TabClass* tc = tab_class(&CT); + if (tc->click) { tc->click(ed, mx, my); return; } } /* Workspace */ @@ -5823,7 +5884,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) { static void drag_to_ctx(Ed* ed, int tgt) { int dti = tgt >= 0 ? ed->twin[tgt].tab : (ed->tabctx >= 0 ? ed->ctx_main_tab : ed->cur_tab); if (dti < 0 || dti >= ed->tab_n || dti == ed->cur_tab) { apply_snap(ed); return; } - if (ed->tabs[dti].kind != TK_BLOCKS) { /* aba de imagem/codigo nao recebe blocos */ + if (!tab_class(&ed->tabs[dti])->blocks) { /* aba de imagem/codigo nao recebe blocos */ snprintf(ed->hot_status, sizeof(ed->hot_status), "A aba \"%s\" não é de blocos", ed->tabs[dti].name); ed->hot_status_ok = false; apply_snap(ed); @@ -5895,7 +5956,7 @@ static void on_lmb_up(Ed* ed, float mx, float my) { } if (ed->grp_drag >= 0) { ed->grp_drag = -1; return; } rt_cache_clear(ed); /* soltar/encaixar muda o programa que esta rodando */ - if (CT.kind == TK_IMAGE && CT.img_drag) { image_release(ed, mx, my); ed->state = ES_IDLE; return; } + if (tab_class(&CT)->release && tab_class(&CT)->release(ed, mx, my)) { ed->state = ES_IDLE; return; } if (ed->state == ES_DRAG && ed->drag_bi >= 0) { mark_drag(&CT, ed->drag_bi, false); int tgt = ws_ctx_at(ed, mx, my); @@ -5926,7 +5987,7 @@ static void on_rmb_down(Ed* ed, float mx, float my) { 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; - if (CT.kind != TK_BLOCKS) return; + if (!tab_class(&CT)->blocks) return; float wx = mx - WS_X + CT.ws_sx, wy = my - WS_Y + CT.ws_sy; int bi = hit_ws(&CT, wx, wy); int g = bi < 0 ? group_at(&CT, wx, wy, false) : -1; @@ -6027,17 +6088,7 @@ static void on_key(Ed* ed, int sc, bool ctrl) { } return; } - if (CT.kind == TK_IMAGE && CT.img_sel.on) { - if (sc == TB_KEY_DELETE || sc == TB_KEY_BACKSPACE) { - img_sel_reset(&CT); - CT.img_rev++; - snprintf(ed->hot_status, sizeof(ed->hot_status), "Selecao apagada"); - ed->hot_status_ok = true; - return; - } - if (sc == TB_KEY_ESCAPE) { img_sel_commit(&CT); return; } - } - if (CT.kind == TK_CODE && code_key(ed, sc, ctrl)) return; + if (tab_class(&CT)->key && tab_class(&CT)->key(ed, sc, ctrl)) return; if (ed->editing) { size_t cap; char* t = edit_target(ed, &cap); bool multi = is_lit_text(CT.ws[ed->edit_bi].type); @@ -6074,8 +6125,8 @@ static void on_text(Ed* ed, const char* typed) { 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); - } else if (CT.kind == TK_CODE) { - code_text(ed, typed); + } else if (tab_class(&CT)->text && tab_class(&CT)->text(ed, typed)) { + /* a aba tratou */ } else if (ed->grp_edit >= 0 && ed->grp_edit < CT.group_n) { append_text(CT.groups[ed->grp_edit].title, sizeof(CT.groups[0].title), typed); } else if (ed->editing) { @@ -6092,14 +6143,14 @@ static void update_tooltip(Ed* ed, bool alt) { /* mantem aberto enquanto o mouse estiver sobre o tooltip ... */ if (in_rect(ed->mouse_x, ed->mouse_y, ed->tip_x, ed->tip_y, ed->tip_w, ed->tip_h)) return; /* ... ou sobre o proprio bloco de origem (permite soltar o Alt e clicar) */ - if (CT.kind == TK_BLOCKS && ed->tip_bi >= 0 && ed->tip_bi < MAX_WS && CT.ws[ed->tip_bi].used) { + if (tab_class(&CT)->blocks && ed->tip_bi >= 0 && ed->tip_bi < MAX_WS && CT.ws[ed->tip_bi].used) { int hb = hit_ws(&CT, ed->mouse_x - WS_X + CT.ws_sx, ed->mouse_y - WS_Y + CT.ws_sy); if (hb == ed->tip_bi) return; } } ed->tip_open = false; if (!alt) return; - if (CT.kind != TK_BLOCKS) return; + if (!tab_class(&CT)->blocks) return; if (ed->state != ES_IDLE) return; float mx = ed->mouse_x, my = ed->mouse_y; if (!in_ws(ed, mx, my)) return; @@ -6291,8 +6342,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { CT.ws_sx = ed->pan_sx - (mx - ed->pan_mx); CT.ws_sy = ed->pan_sy - (my - ed->pan_my); } - if (CT.kind == TK_IMAGE && CT.img_drag && (mx != ed->img_mx || my != ed->img_my)) - image_drag(ed, mx, my); + if (tab_class(&CT)->motion) tab_class(&CT)->motion(ed, mx, my); /* == Scroll === */ double sdx = ev_real(evb, GFX_IN_SCROLL_DX); @@ -6306,12 +6356,8 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) { settings_scroll_by(ed, (float)sdy); } else if (ed->libman_open) { libman_scroll_by(ed, (float)sdy); - } else if (CT.kind == TK_CODE && in_ws(ed, mx, my)) { - CT.code_scroll -= (int)sdy * 3; - if (CT.code_scroll < 0) CT.code_scroll = 0; - } else if (CT.kind == TK_IMAGE && in_ws(ed, mx, my)) { - /* roda do mouse na aba de imagem = zoom no cursor (nao pinta) */ - image_zoom(ed, mx, my, (int)(sdy > 0 ? 1 : sdy < 0 ? -1 : 0)); + } 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)) { CT.ws_sx -= (float)sdx * 40.f; CT.ws_sy -= (float)sdy * 40.f;