diff --git a/libraries/tinyblocks-editor/internal/tb_editor_internal.h b/libraries/tinyblocks-editor/internal/tb_editor_internal.h index 7351864..8b6a78f 100644 --- a/libraries/tinyblocks-editor/internal/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/internal/tb_editor_internal.h @@ -299,6 +299,7 @@ static const BlockSpec s_specs[] = { {"Stage.Line", "desenhe linha de x %1 y %2 até x %3 y %4", "-100|0|100|0"}, {"Stage.Rect", "desenhe retângulo em x %1 y %2 largura %3 altura %4", "0|0|120|80"}, {"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3", "0|0|50"}, + {"Stage.Draw", "desenhe %1 em x %2 y %3", "|0|0"}, {"Stage.ImageByName", "desenhe imagem nome %1 em x %2 y %3", "|0|0"}, {"Stage.ImageByNumber", "desenhe imagem número %1 em x %2 y %3", "1|0|0"}, {"Stage.Text", "escreva %1 no palco em x %2 y %3", "Olá!|-40|0"}, @@ -654,6 +655,7 @@ typedef struct Ed { int bubble_bi; char bubble_text[128]; + char bubble_type[48]; /* tipo do valor: "texto", "imagem", ... */ TinyBlocks_JsonValue* bubble_json; /* lista de pontos (retida) ou NULL */ TinyBlocks_Image* bubble_img; /* imagem (retida) ou NULL */ int last_click_bi; diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index b9733d1..23740fc 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -1144,6 +1144,7 @@ void bubble_clear(Ed* ed) { if (ed->bubble_img) { tb_image_release(ed->bubble_img); ed->bubble_img = NULL; } ed->bubble_bi = -1; ed->bubble_text[0] = 0; + ed->bubble_type[0] = 0; } /* Um array JSON é "lista de pontos" quando todos os itens são [x, y]? */ @@ -1177,10 +1178,40 @@ static bool json_point_at(const TinyBlocks_JsonValue* arr, int i, double* x, dou return ok; } +/* Nome do tipo do valor, para o balão de resultado */ +static const char* value_type_name(const TinyBlocks_Value* v) { + switch (v->kind) { + case TB_VALUE_INTEGER: return "número inteiro"; + case TB_VALUE_REAL: return "número real"; + case TB_VALUE_TEXT: return "texto"; + case TB_VALUE_BLOCK: return "bloco"; + case TB_VALUE_VECTOR: return "desenho vetorial"; + case TB_VALUE_IMAGE: return "imagem"; + case TB_VALUE_JSON: + if (!v->as.json) return "JSON"; + if (json_is_points(v->as.json)) return "lista de pontos"; + switch (tb_json_kind(v->as.json)) { + case TB_JSON_ARRAY: return "lista"; + case TB_JSON_OBJECT: return "objeto JSON"; + default: return "JSON"; + } + default: return "nada"; + } +} + /* Mostra o resultado de um repórter no balão (estilo Scratch) */ static void bubble_set(Ed* ed, int root, TinyBlocks_Value* v) { bubble_clear(ed); ed->bubble_bi = root; + scopy(ed->bubble_type, sizeof(ed->bubble_type), value_type_name(v)); + /* bloco booleano (hexagonal): verdadeiro/falso, como no Scratch */ + if (root >= 0 && shape_of(blk_type(ed, &CT.ws[root])) == SH_BOOL && + (v->kind == TB_VALUE_INTEGER || v->kind == TB_VALUE_REAL)) { + bool t = v->kind == TB_VALUE_INTEGER ? v->as.integer != 0 : v->as.real != 0.0; + scopy(ed->bubble_type, sizeof(ed->bubble_type), "booleano"); + scopy(ed->bubble_text, sizeof(ed->bubble_text), t ? "verdadeiro" : "falso"); + return; + } if (v->kind == TB_VALUE_JSON && v->as.json && json_is_points(v->as.json)) { tb_json_retain(v->as.json); ed->bubble_json = v->as.json; @@ -2240,6 +2271,12 @@ static bool hit_export_btn(float mx, float my); static bool hit_libman_btn(float mx, float my); static bool hit_settings_btn(Ed* ed, float mx, float my); +/* Tipo do valor no topo dos baloes graficos (imagem, lista de pontos) */ +static void bubble_type_label(Ed* ed, TinyBlocks_RenderableBlock* rb, float bx, float by) { + if (!ed->bubble_type[0]) return; + rb_text_c(rb, bx + 10, by + 12, ed->bubble_type, COL_TEXT_DIM, 10.f); +} + 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)); rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, MENU_H * 0.5f, label, COL_WHITE, FS_UI); @@ -2276,8 +2313,21 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) { float cw = tw(ed->path_input_buf, FS_UI); rb_fill_rect(rb, bx + 11 + cw, MENU_H*0.5f - 8, 1.5f, 16, COL_TEXT); } else if (ed->hot_status[0]) { - float w = tw(ed->hot_status, FS_SM); - rb_text_c(rb, CFG_BTN_X - 12 - w, MENU_H*0.5f, ed->hot_status, + /* cabe entre "Carregar library=" e "Config"; se nao couber, mostra + o fim da mensagem (o fim de um caminho e o que importa) */ + float avail = CFG_BTN_X - 12 - (LIB_BTN_X + LIB_BTN_W + 10); + const char* s = ed->hot_status; + char buf[300]; + if (avail > 20 && tw(s, FS_SM) > avail) { + while (*s && tw(s, FS_SM) + tw("...", FS_SM) > avail) { + s++; + while ((*s & 0xC0) == 0x80) s++; + } + snprintf(buf, sizeof(buf), "...%s", s); + s = buf; + } + float w = tw(s, FS_SM); + rb_text_c(rb, CFG_BTN_X - 12 - w, MENU_H*0.5f, s, ed->hot_status_ok ? COL_WHITE : COL_ERR_MENU, FS_SM); } } @@ -2912,27 +2962,29 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { EdBlock* b = &CT.ws[ed->bubble_bi]; if (ed->bubble_img) { /* Balão com prévia da imagem (escalada, mantendo proporção) */ - const float BW = 184, BH = 160, PAD = 10; + const float BW = 184, BH = 174, PAD = 10, HEAD = 16; float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6); rb_fill_rrect(rb, bx, by, BW, BH, 10, COL_BORDER); rb_fill_rrect(rb, bx+1, by+1, BW-2, BH-2, 9, COL_WHITE); rb_bubble_tail(rb, bx + 22, by + BH - 1, COL_WHITE); + bubble_type_label(ed, rb, bx, by); int iw = tb_image_width(ed->bubble_img); int ih = tb_image_height(ed->bubble_img); if (iw > 0 && ih > 0) { - float aw = BW - 2*PAD, ah = BH - 2*PAD - 12; + float aw = BW - 2*PAD, ah = BH - 2*PAD - 12 - HEAD; float sc = aw/iw < ah/ih ? aw/iw : ah/ih; float dw = iw*sc, dh = ih*sc; - tb_draw_image(rb, bx + (BW-dw)*0.5f, by + (BH-12-dh)*0.5f, dw, dh, ed->bubble_img); + tb_draw_image(rb, bx + (BW-dw)*0.5f, by + HEAD + (BH-HEAD-12-dh)*0.5f, dw, dh, ed->bubble_img); } rb_text_c(rb, bx + 10, by + BH - 4, ed->bubble_text, COL_TEXT_DIM, 11.f); } else if (ed->bubble_json) { /* Balão com prévia gráfica de uma lista de pontos (curva) */ - const float BW = 180, BH = 140, PAD = 16; + const float BW = 180, BH = 154, PAD = 16, HEAD = 14; float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6); rb_fill_rrect(rb, bx, by, BW, BH, 10, COL_BORDER); rb_fill_rrect(rb, bx+1, by+1, BW-2, BH-2, 9, COL_WHITE); rb_bubble_tail(rb, bx + 22, by + BH - 1, COL_WHITE); + bubble_type_label(ed, rb, bx, by); int n = (int)tb_json_array_length(ed->bubble_json); double minx = 1e30, maxx = -1e30, miny = 1e30, maxy = -1e30; @@ -2947,10 +2999,10 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { double spanx = maxx - minx, spany = maxy - miny; if (spanx < 1e-9) { spanx = 1; minx -= 0.5; } if (spany < 1e-9) { spany = 1; miny -= 0.5; } - float aw = BW - 2*PAD, ah = BH - 2*PAD - 12; + float aw = BW - 2*PAD, ah = BH - 2*PAD - 12 - HEAD; float sc = (float)((aw/spanx < ah/spany) ? aw/spanx : ah/spany); float gx = bx + PAD + (aw - (float)spanx*sc)*0.5f; - float gy = by + PAD + (ah - (float)spany*sc)*0.5f; + float gy = by + HEAD + PAD + (ah - (float)spany*sc)*0.5f; Col cc = COLA(233, 30, 99, 255); float lx = 0, ly = 0, fx = 0, fy = 0, ex = 0, ey = 0; for (int i = 0; i < n; i++) { @@ -2967,12 +3019,14 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) { rb_fill_circle(rb, ex, ey, 3.2f, COLA(229,57,53,255)); /* fim */ rb_text_c(rb, bx + 10, by + BH - 4, ed->bubble_text, COL_TEXT_DIM, 11.f); } else { - float w = tw(ed->bubble_text, FS) + 20; - float bx = b->ax + ox + 8, by = b->ay + oy - 32; - rb_fill_rrect(rb, bx, by, w, 26, 8, COL_BORDER); - rb_fill_rrect(rb, bx+1, by+1, w-2, 24, 7, COL_WHITE); - rb_bubble_tail(rb, bx + 16, by + 25, COL_WHITE); - rb_text_c(rb, bx + 10, by + 13, ed->bubble_text, COL_TEXT, FS); + /* tipo (pequeno, em cima) + valor */ + float w = fmaxf(tw(ed->bubble_text, FS), tw(ed->bubble_type, 10.f)) + 20; + float bx = b->ax + ox + 8, by = b->ay + oy - 44; + rb_fill_rrect(rb, bx, by, w, 38, 8, COL_BORDER); + rb_fill_rrect(rb, bx+1, by+1, w-2, 36, 7, COL_WHITE); + rb_bubble_tail(rb, bx + 16, by + 37, COL_WHITE); + rb_text_c(rb, bx + 10, by + 11, ed->bubble_type, COL_TEXT_DIM, 10.f); + rb_text_c(rb, bx + 10, by + 26, ed->bubble_text, COL_TEXT, FS); } } clip_clear(ed, rb); diff --git a/libraries/tinyblocks-pen/src/tb_pen.c b/libraries/tinyblocks-pen/src/tb_pen.c index 9981e31..5f9fc1e 100644 --- a/libraries/tinyblocks-pen/src/tb_pen.c +++ b/libraries/tinyblocks-pen/src/tb_pen.c @@ -1,13 +1,16 @@ /* * TinyBlocks - tb_pen.c * Categoria "Caneta": apague / mude fundo / cor / espessura / desenhe linhas, - * retângulos, círculos e texto. Delega o desenho à API do palco do editor. + * retângulos, círculos, texto e "desenhe [imagem/pontos]" com a saida de + * outros blocos. Delega o desenho à API do palco do editor. */ #include #include #include #include #include +#include +#include #include "tb_pen.h" static float num(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { @@ -69,6 +72,54 @@ static void eval_image_number(TinyBlocks_Block* self, TinyBlocks* rt) { self->dirty = false; } +/* Ponto [x, y] numero i da lista; false se o item nao for um ponto */ +static bool point_at(const TinyBlocks_JsonValue* arr, uint32_t i, float* x, float* y) { + TinyBlocks_JsonValue* p = tb_json_array_get(arr, i); + bool ok = p && tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2; + if (ok) { + TinyBlocks_JsonValue* px = tb_json_array_get(p, 0); + TinyBlocks_JsonValue* py = tb_json_array_get(p, 1); + *x = (float)tb_json_as_number(px, 0.0); + *y = (float)tb_json_as_number(py, 0.0); + if (px) tb_json_release(px); + if (py) tb_json_release(py); + } + if (p) tb_json_release(p); + return ok; +} + +/* "desenhe [valor] em x y": aceita a saida de outros blocos. + imagem -> desenha centrada em (x,y); lista de pontos [[x,y],...] -> + traco com a caneta atual, deslocado por (x,y); texto -> nome de uma + imagem guardada. */ +static void eval_draw(TinyBlocks_Block* self, TinyBlocks* rt) { + TinyBlocks_Value v = tb_block_get_input(self, rt, 0); + float x = num(self, rt, 1), y = num(self, rt, 2); + if (v.kind == TB_VALUE_IMAGE && v.as.image) { + tb_stage_image(v.as.image, x, y); + } else if (v.kind == TB_VALUE_JSON && v.as.json && tb_json_kind(v.as.json) == TB_JSON_ARRAY) { + uint32_t n = tb_json_array_length(v.as.json); + float lx = 0, ly = 0; + bool have = false; + for (uint32_t i = 0; i < n; i++) { + float px, py; + if (!point_at(v.as.json, i, &px, &py)) { + tinyblocks_set_error(rt, "desenhe: a lista precisa ter pontos [x, y]"); + break; + } + if (have) tb_stage_line(lx + x, ly + y, px + x, py + y); + else if (n == 1) tb_stage_circle(px + x, py + y, 1.f); /* ponto sozinho */ + lx = px; ly = py; have = true; + } + } else if (v.kind == TB_VALUE_TEXT && v.as.text && v.as.text[0]) { + if (!tb_stage_image_named(v.as.text, x, y)) + tinyblocks_set_error(rt, "imagem '%s' nao encontrada", v.as.text); + } else if (v.kind != TB_VALUE_NONE) { + tinyblocks_set_error(rt, "desenhe: encaixe uma imagem ou uma lista de pontos"); + } + self->dirty = false; +} + static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name, const TinyBlocks_SlotDesc* in, uint32_t ni, void (*fn)(TinyBlocks_Block*, TinyBlocks*)) { @@ -101,6 +152,8 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { "Desenha um circulo"); TB_BLOCK_META("Stage.Text", "eval_text", "Escreve um texto no palco"); + TB_BLOCK_META("Stage.Draw", "eval_draw", + "Desenha a imagem ou a lista de pontos que vier de outro bloco"); TB_BLOCK_META("Stage.ImageByName", "eval_image_name", "Desenha no palco a imagem com esse nome"); TB_BLOCK_META("Stage.ImageByNumber", "eval_image_number", @@ -115,6 +168,7 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { static const TinyBlocks_SlotDesc circ[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}, {"raio", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc text[] = {{"texto", TB_VALUE_TEXT}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc img_name[] = {{"nome", TB_VALUE_TEXT}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}}; + static const TinyBlocks_SlotDesc draw[] = {{"imagem", TB_VALUE_NONE}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc img_num[] = {{"numero", TB_VALUE_INTEGER}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}}; return reg(rt, lib, "Stage.Clear", NULL, 0, eval_clear) @@ -125,6 +179,7 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { && reg(rt, lib, "Stage.Rect", rect, 4, eval_rect) && reg(rt, lib, "Stage.Circle", circ, 3, eval_circle) && reg(rt, lib, "Stage.Text", text, 3, eval_text) + && reg(rt, lib, "Stage.Draw", draw, 3, eval_draw) && reg(rt, lib, "Stage.ImageByName", img_name, 3, eval_image_name) ; (void)img_num; } diff --git a/tests/editor_harness.c b/tests/editor_harness.c index e5dc739..5d77df9 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -465,6 +465,45 @@ int main(void) { rmdir(fdir); } + /* 13c. "desenhe [lista de pontos]" (Caneta) + tipo do valor no balao */ + { + const char* dj = "/tmp/tb_harness_draw.json"; + FILE* df = fopen(dj, "wb"); + if (df) { + fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0," + "\"tabs\":[{\"name\":\"Desenho\",\"composite\":false,\"comp_id\":-1,\"color\":0," + "\"blocks\":[" + "{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1}," + "{\"i\":1,\"type\":\"Stage.Draw\",\"x\":40,\"y\":100,\"parent\":0,\"prel\":0," + "\"args\":[2,-1,-1],\"in_text\":[\"\",\"0\",\"0\"]}," + "{\"i\":2,\"type\":\"Curves.Line\",\"x\":0,\"y\":0,\"parent\":1,\"prel\":3," + "\"in_text\":[\"-100\",\"0\",\"100\",\"50\",\"10\"]}," + "{\"i\":3,\"type\":\"Curves.Line\",\"x\":40,\"y\":220," + "\"in_text\":[\"0\",\"0\",\"100\",\"0\",\"20\"]}" + "],\"top\":[0,3]}],\"user_blocks\":[],\"variables\":[]}", df); + fclose(df); + } + if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y); + for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0); + type(dj); + key(TB_KEY_RETURN, 0, 0); + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(find("Desenho", R_ANY, 0, &x, &y), "projeto com 'desenhe' aberto"); + int before = 0, after = 0; + for (uint32_t i = 0; i < ncmd; i++) + if (cmds[i].kind == TB_DRAW_STROKE_LINE && cmds[i].x >= 920) before++; + flag(); + frame(0, 0, 0, 0, 0, 0, NULL); + for (uint32_t i = 0; i < ncmd; i++) + if (cmds[i].kind == TB_DRAW_STROKE_LINE && cmds[i].x >= 920) after++; + CHECK(after >= before + 10, "desenhe (reta com 10 passos) traca 10 segmentos no palco"); + /* repórter solto (o encaixado roda a pilha inteira, como no Scratch) */ + if (need("reta de x", R_WS, 1, &x, &y)) { click(x, y); click(x, y); } + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(find("lista de pontos", R_WS, 0, &x, &y), "balao mostra o tipo 'lista de pontos'"); + remove(dj); + } + /* 14. Exportar pelo menu de contexto do botao Exportar */ if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y); CHECK(find("Exportar JSON", R_FLY, 0, &x, &y) && find("Exportar XML", R_FLY, 0, &x, &y)