diff --git a/include/tinyblocks/tb_type_block.h b/include/tinyblocks/tb_type_block.h index f9752f2..48f6691 100644 --- a/include/tinyblocks/tb_type_block.h +++ b/include/tinyblocks/tb_type_block.h @@ -91,6 +91,11 @@ struct TinyBlocks_TypeBlock { int pure; double (*pure_num)(const double* x); uint32_t pure_arity; + + /* Texto de um valor que referencia uma instancia deste tipo (um bloco- + handle viajando como TB_VALUE_BLOCK): semantica do snprintf. NULL = + "[bloco Tipo.Nome]". Ex.: "tensor [3, 32, 32]" no monitor e no balao. */ + size_t (*value_text)(TinyBlocks_Block* self, char* out, size_t cap); }; /* Semanticas puras que um executor sabe compilar. Quem declara garante que o @@ -124,6 +129,9 @@ TB_API void tb_type_block_set_event(TinyBlocks_TypeBlock* tb, const char* event) TB_API void tb_type_block_set_label(TinyBlocks_TypeBlock* tb, const char* label, const char* defaults); /* Declara a semantica pura do bloco (TB_PURE_*); num/arity so para NUM */ TB_API void tb_type_block_set_pure(TinyBlocks_TypeBlock* tb, int pure, double (*num)(const double* x), uint32_t arity); +/* Texto das instancias quando viajam como valor (ver value_text) */ +TB_API void tb_type_block_set_value_text(TinyBlocks_TypeBlock* tb, + size_t (*fn)(TinyBlocks_Block* self, char* out, size_t cap)); TB_API void tb_type_block_set_input_flags (TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags); TB_API void tb_type_block_set_output_flags(TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags); TB_API uint32_t tb_type_block_input_flags (const TinyBlocks_TypeBlock* tb, uint32_t i); diff --git a/libraries/tinyblocks-editor/include/tb_editor_internal.h b/libraries/tinyblocks-editor/include/tb_editor_internal.h index d39d42f..19a5334 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/include/tb_editor_internal.h @@ -187,7 +187,6 @@ enum { IT_SEL = 0, IT_BRUSH, IT_LINE, IT_RECT, IT_ELLIPSE, #define MAX_GROUPS 32 #define GRP_HEAD_H 28 /* faixa do titulo (arrastar = mover tudo) */ #define GRP_GRIP 16 /* canto de baixo a direita: redimensionar */ -#define MAX_FLY 192 #define MAX_CATS 24 #define MAX_CALL_DEPTH 32 #define MAX_RUN_BLOCKS 200000 @@ -791,8 +790,8 @@ typedef struct Ed { int cat_n; int myblocks_cat; /* índice da categoria "Meus Blocos" */ Tab fly; /* blocos-modelo do flyout */ - FlyItem fly_items[MAX_FLY]; - int fly_n; + FlyItem* fly_items; /* itens da paleta (vetor que cresce) */ + int fly_n, fly_cap; float fly_scroll, fly_total_h; Tab tabs[MAX_TABS]; diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 022714e..1abdd53 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -2052,15 +2052,26 @@ static void cats_rebuild(Ed* ed) { } } +/* Mais um item na paleta (o vetor cresce: libraries e variaveis nao tem teto) */ +static bool fly_push(Ed* ed, FlyItem it) { + if (ed->fly_n == ed->fly_cap) { + int nc = ed->fly_cap ? ed->fly_cap * 2 : 256; + FlyItem* p = realloc(ed->fly_items, (size_t)nc * sizeof(*p)); + if (!p) return false; + ed->fly_items = p; ed->fly_cap = nc; + } + ed->fly_items[ed->fly_n++] = it; + return true; +} + static void fly_add_block(Ed* ed, int cat, TinyBlocks_TypeBlock* t, const char* first, float* y) { - if (ed->fly_n >= MAX_FLY) return; int bi = blk_new(&ed->fly, t); if (bi < 0) return; if (first) strncpy(ed->fly.ws[bi].in_text[0], first, 63); measure(&ed->fly, bi); place(&ed->fly, bi, FLY_PAD, *y); float h = ed->fly.ws[bi].h; - ed->fly_items[ed->fly_n++] = (FlyItem){FI_BLOCK, cat, bi, *y, h + NOTCH_D}; + fly_push(ed, (FlyItem){FI_BLOCK, cat, bi, *y, h + NOTCH_D}); *y += h + 14; } @@ -2079,12 +2090,12 @@ void palette_rebuild(Ed* ed) { float y = 8; TinyBlocks_TypeBlock* var_get = tb_type_block_find(ed->rt, "Variables.Get"); int var_cat = cat_of(ed, "Variables.Get"); - for (int c = 0; c < ed->cat_n && ed->fly_n < MAX_FLY; c++) { + for (int c = 0; c < ed->cat_n; c++) { ed->cats[c].fly_y = y; - ed->fly_items[ed->fly_n++] = (FlyItem){FI_HEADER, c, -1, y, 34}; + fly_push(ed, (FlyItem){FI_HEADER, c, -1, y, 34}); y += 34; - if (c == ed->myblocks_cat && ed->fly_n < MAX_FLY) { - ed->fly_items[ed->fly_n++] = (FlyItem){FI_BUTTON, c, -1, y, 42}; + if (c == ed->myblocks_cat) { + fly_push(ed, (FlyItem){FI_BUTTON, c, -1, y, 42}); y += 42; } for (uint32_t i = 0; i < tb_rt_type_count(ed->rt); i++) { @@ -7705,6 +7716,7 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) { bubble_clear(ed); for (int i = 0; i < ed->tab_n; i++) tab_doc_free(&ed->tabs[i]); tab_doc_free(&ed->fly); /* pool da paleta */ + free(ed->fly_items); free(ed->fdlg.ents); free(ed->sbp_text); free(ed->run_blocks); diff --git a/src/tb_type_block.c b/src/tb_type_block.c index 8583bd9..b213e5d 100644 --- a/src/tb_type_block.c +++ b/src/tb_type_block.c @@ -49,6 +49,7 @@ static TinyBlocks_Block* type_clone(const TinyBlocks_Block* self, TinyBlocks* rt t->lazy_inputs = src->lazy_inputs; t->label = src->label; t->label_defaults = src->label_defaults; + t->value_text = src->value_text; for (uint32_t i = 0; i < src->input_desc_count; i++) tb_type_block_set_input_flags(t, i, tb_type_block_input_flags(src, i)); for (uint32_t i = 0; i < src->output_desc_count; i++) @@ -82,6 +83,12 @@ void tb_type_block_set_pure(TinyBlocks_TypeBlock* tb, int pure, double (*num)(co tb->pure_arity = arity; } +void tb_type_block_set_value_text(TinyBlocks_TypeBlock* tb, + size_t (*fn)(TinyBlocks_Block* self, char* out, size_t cap)) +{ + if (tb) tb->value_text = fn; +} + void tb_type_block_set_input_flags(TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags) { if (tb && tb->input_flags && i < tb->input_desc_count) tb->input_flags[i] = flags; } void tb_type_block_set_output_flags(TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags) diff --git a/src/tb_value.c b/src/tb_value.c index f101f1e..11197b6 100644 --- a/src/tb_value.c +++ b/src/tb_value.c @@ -153,6 +153,8 @@ static const TinyBlocks_ValueClass s_text = { static size_t block_text(TinyBlocks_Value v, char* out, size_t cap) { + if (v.as.block && v.as.block->type && v.as.block->type->value_text) + return v.as.block->type->value_text(v.as.block, out, cap); const char* n = v.as.block && v.as.block->type ? v.as.block->type->type_name : "?"; return (size_t)snprintf(out, cap, "[bloco %s]", n); } diff --git a/tests/value_test.c b/tests/value_test.c index c8b2a00..b611a07 100644 --- a/tests/value_test.c +++ b/tests/value_test.c @@ -16,6 +16,11 @@ static bool text_is(TinyBlocks_Value v, const char* s) { return strcmp(buf, s) == 0; } +static size_t handle_text(TinyBlocks_Block* self, char* out, size_t cap) { + (void)self; + return (size_t)snprintf(out, cap, "tensor [3, 32, 32]"); +} + int main(void) { /* toda kind tem classe; desconhecida cai em "nada" */ bool all = true; @@ -65,6 +70,16 @@ int main(void) { CHECK(tb_value_equals(ve, ve2) && !tb_value_equals(ve, vo), "resultados iguais pela tag e pelo conteúdo"); tb_value_free(&vo); tb_value_free(&ve); tb_value_free(&ve2); tb_value_free(&jl); tb_value_free(&t); + + /* Bloco-handle: o tipo diz como o valor vira texto (value_text) */ + TinyBlocks* rt = NULL; + tinyblocks_init(&rt, NULL); + TinyBlocks_TypeBlock* th = tb_type_block_create(rt, "Teste._Handle", NULL, 0, NULL, 0, NULL); + TinyBlocks_Block* hb = tb_block_create(rt, th); + CHECK(text_is(tb_value_block(hb), "[bloco Teste._Handle]"), "bloco sem value_text: \"[bloco Tipo]\""); + tb_type_block_set_value_text(th, handle_text); + CHECK(text_is(tb_value_block(hb), "tensor [3, 32, 32]"), "bloco com value_text: o texto do tipo"); + tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0;