Caneta "desenhe [ ] em x y" e tipo do valor no balão de prévia
- Stage.Draw aceita a saída de outro bloco: imagem (desenha centrada),
lista de pontos [[x,y],...] (traço com a caneta atual, deslocado por
x/y) ou texto (nome de uma imagem guardada).
- O balão de resultado mostra o tipo do valor ("lista de pontos",
"imagem", "texto", "número inteiro"...); blocos booleanos mostram
"verdadeiro"/"falso".
- Status longo no menu mostra o fim da mensagem e não cobre mais o
botão "Carregar library=".
- Harness: desenhe (reta) no palco e tipo no balão.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
33705b2903
commit
4df6eb1bbf
4 files changed
+165
-15
No files matched your search
@@ -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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_stage.h>
|
||||
#include <tinyblocks/tb_image.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user