Adiciona tinyblocks-api, tinyblocks-libman e a tela "Escolha uma Extensão"

tinyblocks-api (gestor publico de modulos/libraries):
- registry de modulos (embutidos + .so), load/unload/reload via dlopen,
  hot reload por mtime e introspeccao de tipos de bloco.
- substitui a logica de hotlib duplicada no editor.

tinyblocks-libman (gestor/buscador de libraries):
- varre library.toml (libraries/ e ~/.tinyblocks/libraries), le metadados
  de forma RECURSIVA e resolve o caminho do .so/.a.
- busca por texto e filtro por categoria; consulta o estado de carga na api.

Editor: tela cheia "Escolha uma Extensão" (UI desenhada pelo editor, como no
Scratch): botao Bibliotecas / Ctrl+L, busca, chips de categoria que quebram
linha, grade de cartoes com faixa colorida e selo "carregada"; clicar carrega
o .so via api.

Respeito a interfaces publicas entre libraries:
- libman deixa de incluir o ast.h (interno) do tinyblocks-toml e passa a usar
  uma API publica e opaca: tb_toml_open/close/str/int/int_array.
- core ganha accessors publicos: tb_rt_type_count/at, tb_rt_library_count/at,
  tb_library_name/category/block_count; api e editor usam-nos em vez de ler
  campos de struct do core diretamente.

Testes: make test-libman (novo) + step no harness que abre o libman, busca,
carrega um .so e confirma a categoria na paleta. test-editor/toml/blueprint/
plugin seguem OK.
This commit is contained in:
mguinhos committed 2026-10-20 20:22:37 -03:00
1 parent f616503caa
commit dcc20985c5
20 files changed
+1143 -112

No files matched your search

+285 -101
View File
@@ -39,6 +39,8 @@
#include <tinyblocks/tb_stage.h>
#include <tb_variables.h>
#include <tb_blueprint.h>
#include <tb_api.h>
#include <tb_libman.h>
#include "tb_stage_widget.h"
#include "../include/tb_editor.h"
@@ -91,8 +93,10 @@ TB_API void tb_editor_preload(const char* path) {
#define SAVE_BTN_W 70
#define EXP_BTN_X 296
#define EXP_BTN_W 84
#define LIB_BTN_X 392
#define LIB_BTN_W 168
#define LIBM_BTN_X 388
#define LIBM_BTN_W 104
#define LIB_BTN_X 500
#define LIB_BTN_W 200
/* == Geometria dos blocos === */
#define FS 12 /* texto dos blocos */
@@ -129,7 +133,6 @@ TB_API void tb_editor_preload(const char* path) {
#define MAX_RUN_BLOCKS 200000
#define STAGE_BUF 16384
#define LIT_TEXT_CAP 512
#define MAX_HOT_LIBS 16
#define MAX_HOT_TYPES 64
#define MAX_USER_BLOCKS 16
#define MAX_UBLK_SLOTS 8
@@ -302,13 +305,6 @@ typedef struct {
TinyBlocks_Value out[MAX_UBLK_SLOTS];
} CallFrame;
typedef struct {
char path[512];
void* dl;
time_t mtime;
TinyBlocks_LibraryBlock* lib;
} HotLib;
typedef struct { char name[32]; char prefix[32]; Col col, dark; float fly_y;
const TinyBlocks_LibraryBlock* lib; } Cat;
@@ -408,8 +404,6 @@ typedef struct {
bool clip_on;
float clip[4];
HotLib hot_libs[MAX_HOT_LIBS];
int hot_lib_n;
int hot_check_frame;
char hot_status[256];
bool hot_status_ok;
@@ -419,6 +413,17 @@ typedef struct {
int path_input_len;
int path_mode; /* PM_* - o que fazer no Enter */
/* Gestor de libraries (tinyblocks-libman); a UI e desenhada aqui */
bool libman_open;
char libman_query[128];
int libman_query_len;
bool libman_search_focus;
int libman_filter; /* -1 = Tudo; senao indice em libman_cats */
char libman_cats[MAX_CATS][48];
int libman_cat_n;
int libman_scroll;
int libman_hover;
int win_w, win_h;
} Ed;
@@ -437,6 +442,14 @@ static bool dlg_click(Ed* ed, float mx, float my);
static void dlg_key(Ed* ed, int sc);
static void dlg_text(Ed* ed, const char* typed);
/* Gestor de libraries (tinyblocks-libman) - UI desenhada pelo editor */
static void libman_show(Ed* ed);
static void libman_click(Ed* ed, float mx, float my);
static void libman_key(Ed* ed, int sc);
static void libman_text(Ed* ed, const char* typed);
static void libman_scroll_by(Ed* ed, float dy);
static void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb);
/* ===
* Helpers de desenho - emitem TinyBlocks_DrawCmd em um RenderableBlock
* === */
@@ -1644,16 +1657,15 @@ static bool type_hidden(Ed* ed, const TinyBlocks_TypeBlock* t) {
static void cats_rebuild(Ed* ed) {
ed->cat_n = 0;
/* 1) Categoria declarada por cada library, na ordem de registro */
for (uint32_t i = 0; i < ed->rt->library_count && ed->cat_n < MAX_CATS; i++) {
TinyBlocks_LibraryBlock* lib = ed->rt->libraries[i];
if (!lib->category || !lib->category->name) continue;
for (uint32_t i = 0; i < tb_rt_library_count(ed->rt) && ed->cat_n < MAX_CATS; i++) {
TinyBlocks_LibraryBlock* lib = tb_rt_library_at(ed->rt, i);
const TinyBlocks_Category* cat = tb_library_category(lib);
if (!cat || !cat->name) continue;
Cat* c = &ed->cats[ed->cat_n++];
memset(c, 0, sizeof(*c));
snprintf(c->name, sizeof(c->name), "%s", lib->category->name);
c->col = (Col){lib->category->color[0], lib->category->color[1],
lib->category->color[2], lib->category->color[3]};
c->dark = (Col){lib->category->dark[0], lib->category->dark[1],
lib->category->dark[2], lib->category->dark[3]};
snprintf(c->name, sizeof(c->name), "%s", cat->name);
c->col = (Col){cat->color[0], cat->color[1], cat->color[2], cat->color[3]};
c->dark = (Col){cat->dark[0], cat->dark[1], cat->dark[2], cat->dark[3]};
c->lib = lib;
}
/* 2) "Meus Blocos" pertence ao editor */
@@ -1666,8 +1678,8 @@ static void cats_rebuild(Ed* ed) {
ed->myblocks_cat = ed->cat_n - 1;
}
/* 3) Uma categoria por prefixo para libraries/tipos sem categoria */
for (uint32_t i = 0; i < ed->rt->type_count && ed->cat_n < MAX_CATS; i++) {
TinyBlocks_TypeBlock* t = ed->rt->types[i];
for (uint32_t i = 0; i < tb_rt_type_count(ed->rt) && ed->cat_n < MAX_CATS; i++) {
TinyBlocks_TypeBlock* t = tb_rt_type_at(ed->rt, i);
if (type_hidden(ed, t) || cat_of_type(ed, t) >= 0) continue;
Cat* c = &ed->cats[ed->cat_n++];
memset(c, 0, sizeof(*c));
@@ -1705,8 +1717,8 @@ static void palette_rebuild(Ed* ed) {
ed->fly_items[ed->fly_n++] = (FlyItem){FI_BUTTON, c, -1, y, 42};
y += 42;
}
for (uint32_t i = 0; i < ed->rt->type_count; i++) {
TinyBlocks_TypeBlock* t = ed->rt->types[i];
for (uint32_t i = 0; i < tb_rt_type_count(ed->rt); i++) {
TinyBlocks_TypeBlock* t = tb_rt_type_at(ed->rt, i);
if (type_hidden(ed, t) || cat_of_type(ed, t) != c) continue;
fly_add_block(ed, c, t, NULL, &y);
}
@@ -2462,93 +2474,29 @@ static void dlg_text(Ed* ed, const char* typed) {
/* ===
* Hot reload de libraries .so
* === */
static time_t file_mtime(const char* path) {
struct stat st;
return (stat(path, &st) == 0) ? st.st_mtime : 0;
}
typedef const TinyBlocks_LibraryDescriptor* (*GetLibFn)(void);
static bool hotlib_load(Ed* ed, const char* path) {
if (ed->hot_lib_n >= MAX_HOT_LIBS) {
snprintf(ed->hot_status, sizeof(ed->hot_status),
"Hot reload: limite de %d libs atingido", MAX_HOT_LIBS);
int idx = tb_api_load(path);
if (idx < 0) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "%s", tb_api_last_error());
ed->hot_status_ok = false;
return false;
}
void* dl = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (!dl) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "dlopen: %s", dlerror());
ed->hot_status_ok = false;
return false;
}
GetLibFn get_lib = (GetLibFn)(uintptr_t)dlsym(dl, TINYBLOCKS_LIBRARY_SYMBOL);
if (!get_lib) {
const char* base = strrchr(path, '/');
base = base ? base+1 : path;
if (strncmp(base, "libtinyblocks-", 14) == 0)
snprintf(ed->hot_status, sizeof(ed->hot_status),
"'%s' é dependência = carregue a library (ex: python.so, mcp.so)", base);
else
snprintf(ed->hot_status, sizeof(ed->hot_status),
"'%s' não exporta tinyblocks_get_library = não é uma library válida", base);
ed->hot_status_ok = false;
dlclose(dl);
return false;
}
TinyBlocks_LibraryBlock* lib = tb_library_register(ed->rt, get_lib());
if (!lib) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "tb_library_register falhou para %s", path);
ed->hot_status_ok = false;
dlclose(dl);
return false;
}
HotLib* h = &ed->hot_libs[ed->hot_lib_n++];
strncpy(h->path, path, sizeof(h->path)-1);
h->dl = dl;
h->mtime = file_mtime(path);
h->lib = lib;
retype_all(ed); /* blocos que estavam sem tipo podem ganhar um agora */
snprintf(ed->hot_status, sizeof(ed->hot_status), "Carregado: %s", path);
ed->hot_status_ok = true;
return true;
}
static void hotlib_reload(Ed* ed, int idx) {
HotLib* h = &ed->hot_libs[idx];
/* Nenhuma instância ou ponteiro de tipo pode sobreviver ao unload */
invalidate_types(ed);
tb_library_unload(ed->rt, h->lib);
h->lib = NULL;
dlclose(h->dl);
h->dl = NULL;
h->mtime = file_mtime(h->path);
void* dl = dlopen(h->path, RTLD_NOW | RTLD_LOCAL);
GetLibFn get_lib = dl ? (GetLibFn)(uintptr_t)dlsym(dl, TINYBLOCKS_LIBRARY_SYMBOL) : NULL;
TinyBlocks_LibraryBlock* lib = get_lib ? tb_library_register(ed->rt, get_lib()) : NULL;
if (!lib) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Hot reload falhou: %.230s",
dl ? h->path : dlerror());
ed->hot_status_ok = false;
if (dl) dlclose(dl);
retype_all(ed);
return;
}
h->dl = dl;
h->lib = lib;
retype_all(ed);
snprintf(ed->hot_status, sizeof(ed->hot_status), "Recarregado: %.240s", h->path);
ed->hot_status_ok = true;
}
/* Hot reload: recarrega os .so cujo mtime mudou (delegado a tinyblocks-api). */
static void hotlib_check(Ed* ed) {
for (int i = 0; i < ed->hot_lib_n; i++) {
HotLib* h = &ed->hot_libs[i];
if (!h->dl || !h->path[0]) continue;
time_t mt = file_mtime(h->path);
if (mt && mt != h->mtime) hotlib_reload(ed, i);
}
if (++ed->hot_check_frame < 60) return;
ed->hot_check_frame = 0;
if (!tb_api_any_changed()) return;
invalidate_types(ed); /* nenhum ponteiro de tipo pode sobreviver ao unload */
tb_api_poll_reload();
retype_all(ed);
snprintf(ed->hot_status, sizeof(ed->hot_status), "Hot reload: libraries recarregadas");
ed->hot_status_ok = true;
}
/* ===
@@ -2557,6 +2505,7 @@ static void hotlib_check(Ed* ed) {
static bool hit_open_btn(float mx, float my);
static bool hit_save_btn(float mx, float my);
static bool hit_export_btn(float mx, float my);
static bool hit_libman_btn(float mx, float my);
static void menu_button(TinyBlocks_RenderableBlock* rb, float x, float w, const char* label, bool active) {
rb_fill_rrect(rb, x, 7, w, MENU_H - 14, 6, active ? COL_MENU_DARK : COLA(255, 255, 255, 34));
@@ -2572,6 +2521,7 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
menu_button(rb, OPEN_BTN_X, OPEN_BTN_W, "Abrir", (active && ed->path_mode == PM_LOAD) || hit_open_btn(mx, my));
menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || hit_save_btn(mx, my));
menu_button(rb, EXP_BTN_X, EXP_BTN_W, "Exportar", (ed->ctx_menu_open && ed->ctx_export) || hit_export_btn(mx, my));
menu_button(rb, LIBM_BTN_X, LIBM_BTN_W, "Bibliotecas", ed->libman_open || hit_libman_btn(mx, my));
if (ed->lib_hover || (active && ed->path_mode == PM_LIB))
rb_fill_rect(rb, LIB_BTN_X, 0, LIB_BTN_W, MENU_H, COL_MENU_DARK);
@@ -2891,6 +2841,225 @@ static void render_ctx_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
}
}
/* ===
* Gestor de libraries (tinyblocks-libman) - UI do editor
* === */
#define LM_HEAD_H 64
#define LM_MARGIN 40
#define LM_CARD_H 190
#define LM_CARD_GAP 24
#define LM_CHIP_H 34
#define LM_CHIP_GAP 10
#define LM_SEARCH_W 340
#define LM_CARD_W ((WIN_W - 2*LM_MARGIN - LM_CARD_GAP) / 2)
#define LM_MAX_CHIPS (MAX_CATS + 1)
static void libman_close(Ed* ed) {
ed->libman_open = false;
ed->libman_search_focus = false;
}
static void libman_show(Ed* ed) {
ed->libman_open = true;
ed->libman_query[0] = 0;
ed->libman_query_len = 0;
ed->libman_search_focus = true;
ed->libman_filter = -1;
ed->libman_scroll = 0;
ed->libman_hover = -1;
ed->libman_cat_n = tb_libman_categories(ed->libman_cats, MAX_CATS);
ed->path_input_active = false;
ed->ctx_menu_open = false;
}
static const char* libman_filter_name(Ed* ed) {
return (ed->libman_filter >= 0 && ed->libman_filter < ed->libman_cat_n)
? ed->libman_cats[ed->libman_filter] : "Tudo";
}
static int libman_chip_labels(Ed* ed, const char** out) {
out[0] = "Tudo";
int n = 1;
for (int i = 0; i < ed->libman_cat_n && n < LM_MAX_CHIPS; i++) out[n++] = ed->libman_cats[i];
return n;
}
/* Posiciona as chips (layout que quebra linha). Retorna a altura total
* (relativa ao topo, a partir de LM_HEAD_H). */
static float libman_chip_layout(Ed* ed, float rects[][4]) {
const char* labels[LM_MAX_CHIPS];
int n = libman_chip_labels(ed, labels);
float x0 = LM_MARGIN + LM_SEARCH_W + 24;
float x = x0, y = LM_HEAD_H + 14, rowmax = y;
for (int i = 0; i < n; i++) {
float w = tw(labels[i], FS_UI) + 28;
if (x + w > WIN_W - LM_MARGIN) { x = x0; y += LM_CHIP_H + LM_CHIP_GAP; }
if (rects) { rects[i][0] = x; rects[i][1] = y; rects[i][2] = w; rects[i][3] = LM_CHIP_H; }
x += w + LM_CHIP_GAP;
rowmax = y;
}
return rowmax + LM_CHIP_H + 14 - LM_HEAD_H;
}
static float libman_grid_y(Ed* ed) {
return LM_HEAD_H + libman_chip_layout(ed, NULL) + 8;
}
static void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (!ed->libman_open) return;
int hits[TBLM_MAX_ENTRIES];
int nh = tb_libman_search(ed->libman_query, libman_filter_name(ed), hits, TBLM_MAX_ENTRIES);
float gridY = libman_grid_y(ed);
/* Fundo */
rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COL_UI_BG);
/* Cabeçalho */
rb_fill_rect(rb, 0, 0, WIN_W, LM_HEAD_H, COL_MENU);
float bx = LM_MARGIN, by = LM_HEAD_H*0.5f;
rb_fill_circle(rb, bx, by, 16, COLA(255,255,255,40));
rb_line(rb, bx+4, by-7, bx-5, by, 2.f, COL_WHITE);
rb_line(rb, bx-5, by, bx+4, by+7, 2.f, COL_WHITE);
rb_text_c(rb, bx + 28, by, "Voltar", COL_WHITE, FS_UI);
{ const char* t = "Escolha uma Extensão";
rb_text_c(rb, (WIN_W - tw(t, 20))*0.5f, by, t, COL_WHITE, 20); }
/* Barra de filtros */
float fh = libman_chip_layout(ed, NULL);
rb_fill_rect(rb, 0, LM_HEAD_H, WIN_W, fh, COL_PANEL);
float sx = LM_MARGIN, sy = LM_HEAD_H + 14;
rb_fill_rrect(rb, sx, sy, LM_SEARCH_W, 44, 22, COL_UI_BG);
if (ed->libman_search_focus) rb_stroke_rect(rb, sx-1, sy-1, LM_SEARCH_W+2, 46, 2.f, COL_MENU);
{
const char* t = ed->libman_query[0] ? ed->libman_query : "Buscar";
rb_text_c(rb, sx + 16, sy + 22, t,
ed->libman_query[0] ? COL_TEXT : COL_TEXT_DIM, FS_UI);
if (ed->libman_search_focus) {
float cw = tw(ed->libman_query, FS_UI);
rb_fill_rect(rb, sx + 16 + cw, sy + 12, 1.5f, 20, COL_TEXT);
}
}
{
const char* labels[LM_MAX_CHIPS];
int n = libman_chip_labels(ed, labels);
float rects[LM_MAX_CHIPS][4];
libman_chip_layout(ed, rects);
for (int i = 0; i < n; i++) {
bool sel = (i == 0) ? (ed->libman_filter < 0) : (ed->libman_filter == i-1);
rb_fill_rrect(rb, rects[i][0], rects[i][1], rects[i][2], rects[i][3], 17,
sel ? COL_MENU : COL_UI_BG);
rb_text_c(rb, rects[i][0] + 14, rects[i][1] + LM_CHIP_H*0.5f, labels[i],
sel ? COL_WHITE : COL_TEXT, FS_UI);
}
}
/* Grade de cartoes (com recorte e rolagem) */
rb_fill_rect(rb, 0, gridY, WIN_W, WIN_H - gridY, COL_UI_BG);
clip_set(ed, rb, 0, gridY, WIN_W, WIN_H - gridY);
for (int k = 0; k < nh; k++) {
int col = k % 2, row = k / 2;
float x = LM_MARGIN + col*(LM_CARD_W + LM_CARD_GAP);
float y = gridY + 8 + row*(LM_CARD_H + LM_CARD_GAP) - ed->libman_scroll;
if (y + LM_CARD_H < gridY) continue;
if (y > WIN_H) break;
const TbLibmanEntry* e = tb_libman_entry(hits[k]);
if (!e) continue;
Col c = { e->color[0]/255.f, e->color[1]/255.f, e->color[2]/255.f, 1.f };
rb_fill_rrect(rb, x, y, LM_CARD_W, LM_CARD_H, 8, COL_PANEL);
rb_fill_rrect(rb, x, y, LM_CARD_W, 56, 8, c);
rb_fill_rect(rb, x, y + 40, LM_CARD_W, 16, c);
rb_text_c(rb, x + 16, y + 28, e->category, COL_WHITE, FS_UI);
rb_text_c(rb, x + 16, y + 84, e->name, COL_TEXT, 18);
rb_text_c(rb, x + 16, y + 114, e->description, COL_TEXT_DIM, FS_UI);
rb_text_c(rb, x + 16, y + LM_CARD_H - 22, e->shared, COL_TEXT_DIM, FS_SM);
if (tb_libman_loaded(hits[k])) {
rb_fill_circle(rb, x + LM_CARD_W - 24, y + 28, 8, COL_FLAG);
rb_text_c(rb, x + LM_CARD_W - 110, y + LM_CARD_H - 22, "carregada", COL_FLAG, FS_SM);
}
if (ed->libman_hover == k) rb_stroke_rect(rb, x-1, y-1, LM_CARD_W+2, LM_CARD_H+2, 2.f, COL_MENU);
}
clip_clear(ed, rb);
if (nh == 0)
rb_text_c(rb, LM_MARGIN, gridY + 40, "Nenhuma library encontrada.", COL_TEXT_DIM, FS_UI);
}
static void libman_click(Ed* ed, float mx, float my) {
if (my < LM_HEAD_H) {
if (mx < 170) libman_close(ed);
return;
}
float gridY = libman_grid_y(ed);
if (my < gridY) {
if (in_rect(mx, my, LM_MARGIN, LM_HEAD_H + 14, LM_SEARCH_W, 44)) {
ed->libman_search_focus = true;
return;
}
const char* labels[LM_MAX_CHIPS];
int n = libman_chip_labels(ed, labels);
float rects[LM_MAX_CHIPS][4];
libman_chip_layout(ed, rects);
for (int i = 0; i < n; i++)
if (in_rect(mx, my, rects[i][0], rects[i][1], rects[i][2], rects[i][3])) {
ed->libman_filter = i - 1;
ed->libman_scroll = 0;
ed->libman_search_focus = false;
return;
}
ed->libman_search_focus = false;
return;
}
ed->libman_search_focus = false;
if (mx < LM_MARGIN || mx >= WIN_W - LM_MARGIN) return;
int hits[TBLM_MAX_ENTRIES];
int nh = tb_libman_search(ed->libman_query, libman_filter_name(ed), hits, TBLM_MAX_ENTRIES);
for (int k = 0; k < nh; k++) {
int col = k % 2, row = k / 2;
float x = LM_MARGIN + col*(LM_CARD_W + LM_CARD_GAP);
float y = gridY + 8 + row*(LM_CARD_H + LM_CARD_GAP) - ed->libman_scroll;
if (y + LM_CARD_H < gridY || y > WIN_H) continue;
if (in_rect(mx, my, x, y, LM_CARD_W, LM_CARD_H)) {
const TbLibmanEntry* e = tb_libman_entry(hits[k]);
if (e && e->shared[0]) hotlib_load(ed, e->shared);
else { snprintf(ed->hot_status, sizeof(ed->hot_status),
"%s: caminho do .so indisponivel", e ? e->name : "?");
ed->hot_status_ok = false; }
libman_close(ed);
return;
}
}
}
static void libman_key(Ed* ed, int sc) {
if (sc == TB_KEY_ESCAPE) { libman_close(ed); return; }
if (sc == TB_KEY_BACKSPACE && ed->libman_search_focus) {
if (ed->libman_query[0]) {
utf8_pop(ed->libman_query);
ed->libman_query_len = (int)strlen(ed->libman_query);
}
}
}
static void libman_text(Ed* ed, const char* typed) {
if (!ed->libman_search_focus) return;
append_text(ed->libman_query, sizeof(ed->libman_query), typed);
ed->libman_query_len = (int)strlen(ed->libman_query);
}
static void libman_scroll_by(Ed* ed, float dy) {
ed->libman_scroll -= (int)(dy * 48.f);
int hits[TBLM_MAX_ENTRIES];
int nh = tb_libman_search(ed->libman_query, libman_filter_name(ed), hits, TBLM_MAX_ENTRIES);
int rows = (nh + 1) / 2;
float gridY = libman_grid_y(ed);
int content = rows * (LM_CARD_H + LM_CARD_GAP) + 16;
int visible = (int)(WIN_H - gridY);
int maxs = content - visible;
if (maxs < 0) maxs = 0;
if (ed->libman_scroll > maxs) ed->libman_scroll = maxs;
if (ed->libman_scroll < 0) ed->libman_scroll = 0;
}
/* ===
* Arrastar e encaixar
* === */
@@ -3038,6 +3207,7 @@ static bool hit_lib_btn(float mx, float my) { return in_rect(mx, my, LIB_BT
static bool hit_open_btn(float mx, float my) { return in_rect(mx, my, OPEN_BTN_X, 0, OPEN_BTN_W, MENU_H); }
static bool hit_save_btn(float mx, float my) { return in_rect(mx, my, SAVE_BTN_X, 0, SAVE_BTN_W, MENU_H); }
static bool hit_export_btn(float mx, float my) { return in_rect(mx, my, EXP_BTN_X, 0, EXP_BTN_W, MENU_H); }
static bool hit_libman_btn(float mx, float my) { return in_rect(mx, my, LIBM_BTN_X, 0, LIBM_BTN_W, MENU_H); }
static void start_path(Ed* ed, int mode, const char* def) {
ed->path_input_active = true;
@@ -3131,6 +3301,7 @@ static void ctx_exec(Ed* ed, int id) {
}
static void on_lmb_down(Ed* ed, float mx, float my) {
if (ed->libman_open) { libman_click(ed, mx, my); return; }
if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; }
if (ed->ctx_menu_open) {
int it = ctx_hit(ed, mx, my);
@@ -3147,6 +3318,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
if (hit_lib_btn(mx, my)) start_path(ed, PM_LIB, "");
else if (hit_open_btn(mx, my)) start_path(ed, PM_LOAD, "projeto.json");
else if (hit_save_btn(mx, my)) start_path(ed, PM_SAVE, "projeto.json");
else if (hit_libman_btn(mx, my)) libman_show(ed);
else if (hit_export_btn(mx, my)) {
ed->ctx_n = 0;
ed->ctx_items[ed->ctx_n++] = CM_EXP_JSON;
@@ -3281,6 +3453,7 @@ static void on_rmb_down(Ed* ed, float mx, float my) {
static void on_key(Ed* ed, int sc, bool ctrl) {
bool enter = sc == TB_KEY_RETURN;
if (ed->libman_open) { libman_key(ed, sc); return; }
if (ed->comp_dialog_open) { dlg_key(ed, sc); return; }
if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; }
@@ -3314,6 +3487,10 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
else if (sc == TB_KEY_BACKSPACE) utf8_pop(t);
return;
}
if (ctrl && sc == (TB_KEY_A + ('l' - 'a'))) {
libman_show(ed);
return;
}
if (ctrl && sc == (TB_KEY_A + ('o' - 'a'))) {
start_path(ed, PM_LIB, "");
return;
@@ -3330,6 +3507,7 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
}
static void on_text(Ed* ed, const char* typed) {
if (ed->libman_open) { libman_text(ed, typed); return; }
if (ed->comp_dialog_open) { dlg_text(ed, typed); return; }
if (ed->path_input_active) {
append_text(ed->path_input_buf, sizeof(ed->path_input_buf), typed);
@@ -3391,7 +3569,9 @@ static void editor_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
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 (mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
if (ed->libman_open) {
libman_scroll_by(ed, (float)sdy);
} else if (mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
CT.ws_sx -= (float)sdx * 40.f;
CT.ws_sy -= (float)sdy * 40.f;
} else if (mx < PALETTE_W && my >= PAL_Y) {
@@ -3454,6 +3634,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
render_ctx_menu(ed, rb);
dlg_render(ed, rb);
libman_render(ed, rb); /* tela cheia: por cima de tudo */
*dst = rb;
return true;
@@ -3526,6 +3707,8 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
if (t_stage) ed->stage = tb_block_create(rt, t_stage);
palette_rebuild(ed);
tb_libman_init(rt); /* liga tinyblocks-api e adota as libs embutidas */
tb_libman_scan_defaults(); /* procura library.toml em libraries/ e ~/.tinyblocks */
for (int i = 0; i < s_preload_n; i++) hotlib_load(ed, s_preload[i]);
s_preload_n = 0;
seed_example(ed);
@@ -3541,6 +3724,7 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
* aqui só soltamos o que é do editor. */
free(ed->run_blocks);
for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap);
tb_libman_shutdown();
if (g_active_ed == ed) g_active_ed = NULL;
free(ed);
*(Ed**)self->userdata = NULL;