Editor: ripple do M3, formas lisas e encaixes como no Scratch

- Ripple (gota que cresce do toque/clique) em todo componente com camada
  de estado; recortado no formato do componente (tb_ew_ripple).
- TB_DRAW_FILL_POLY: poligono convexo com antialias (pontos no espaco do
  text). Hexagono e encaixe deixam de ser faixas de 1 px (sumiam em linhas
  com zoom e na densidade do celular).
- Encaixe macho com cantos arredondados; encaixe femea recortado no topo
  dos comandos e dos blocos C (e nos bracos de baixo/"senao").
- Destaque ao soltar um reporter num campo segue a forma do campo; sombra
  de encaixe da pilha com a forma de bloco.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-28 22:23:32 -03:00
1 parent 0cb2019704
commit 70c819393c
10 files changed
+268 -23

No files matched your search

+137 -20
View File
@@ -100,22 +100,85 @@ TB_ED_API void clip_restore(Ed* ed, TinyBlocks_RenderableBlock* rb) {
/* Hexágono (repórter booleano) desenhado em faixas de 1px */
static void draw_hex(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, Col c) {
float half = h*0.5f;
int rows = (int)(h + 0.5f);
for (int r = 0; r < rows; r++) {
float in = fabsf(r + 0.5f - half);
if (in > w*0.5f) in = w*0.5f;
rb_fill_rect(rb, x+in, y+r, w-2*in, 1.f, c);
}
float in = fminf(h * 0.5f, w * 0.5f);
const float xy[12] = { x, y + h * 0.5f, x + in, y, x + w - in, y, x + w, y + h * 0.5f,
x + w - in, y + h, x + in, y + h };
tb_draw_fill_poly(rb, xy, 6, c.r, c.g, c.b, c.a);
}
/* Encaixe trapezoidal sob um comando; y - borda inferior do bloco */
/* Canto (px,py) entre a ponta anterior (ax,ay) e a seguinte (bx,by),
arredondado com raio r: curva quadratica em `n` pontos */
static int round_corner(float* out, float ax, float ay, float px, float py, float bx, float by, float r, int n) {
float ux = ax - px, uy = ay - py, vx = bx - px, vy = by - py;
float lu = sqrtf(ux * ux + uy * uy), lv = sqrtf(vx * vx + vy * vy);
if (lu < 1e-3f || lv < 1e-3f) { out[0] = px; out[1] = py; return 1; }
float t = fminf(r, fminf(lu, lv) * 0.5f);
float x0 = px + ux / lu * t, y0 = py + uy / lu * t, x1 = px + vx / lv * t, y1 = py + vy / lv * t;
for (int i = 0; i < n; i++) {
float k = (float)i / (float)(n - 1), m = 1.f - k;
out[2*i] = m * m * x0 + 2 * m * k * px + k * k * x1;
out[2*i+1] = m * m * y0 + 2 * m * k * py + k * k * y1;
}
return n;
}
/* Trapezio do encaixe com os cantos de baixo arredondados */
static void bump_poly(TinyBlocks_RenderableBlock* rb, float l, float r, float top, float bot, float in,
float rad, Col c) {
float xy[2 * TB_POLY_MAX];
int n = 0;
xy[n++] = l; xy[n++] = top;
n += 2 * round_corner(xy + n, l, top, l + in, bot, r - in, bot, rad, 6);
n += 2 * round_corner(xy + n, l + in, bot, r - in, bot, r, top, rad, 6);
xy[n++] = r; xy[n++] = top;
tb_draw_fill_poly(rb, xy, n / 2, c.r, c.g, c.b, c.a);
}
/* Retangulo de cantos redondos com o encaixe (femea) recortado no topo:
o trapezio [nl, nr] desce `depth` com lados inclinados de `in`. Feito de
pecas convexas (esquerda, direita e o meio sob o encaixe), que se
sobrepoem um pouco para nao deixar costura. */
static void fill_notched_ov(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float rad,
float nl, float nr, float depth, float in, Col c, float ov) {
float xy[2 * TB_POLY_MAX];
int n = 0;
/* esquerda: canto de cima, lado do encaixe, canto de baixo */
n += 2 * round_corner(xy + n, x, y + h, x, y, nl, y, rad, 5);
xy[n++] = nl; xy[n++] = y;
xy[n++] = nl + in; xy[n++] = y + depth;
xy[n++] = nl + in; xy[n++] = y + h;
n += 2 * round_corner(xy + n, nl + in, y + h, x, y + h, x, y, rad, 5);
tb_draw_fill_poly(rb, xy, n / 2, c.r, c.g, c.b, c.a);
/* direita (espelhada) */
n = 0;
xy[n++] = nr - in; xy[n++] = y + depth;
xy[n++] = nr; xy[n++] = y;
n += 2 * round_corner(xy + n, nr, y, x + w, y, x + w, y + h, rad, 5);
n += 2 * round_corner(xy + n, x + w, y, x + w, y + h, nr - in, y + h, rad, 5);
xy[n++] = nr - in; xy[n++] = y + h;
tb_draw_fill_poly(rb, xy, n / 2, c.r, c.g, c.b, c.a);
/* meio, abaixo do encaixe */
rb_fill_rect(rb, nl + in - ov, y + depth, (nr - in) - (nl + in) + 2 * ov, h - depth, c);
}
static void fill_notched(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float rad,
float nl, float nr, float depth, float in, Col c) {
fill_notched_ov(rb, x, y, w, h, rad, nl, nr, depth, in, c, 1.f);
}
/* Contorno (d) e miolo (c) de um corpo com encaixe em nx (borda esquerda
do encaixe macho que entra nele) */
static void body_notched(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float nx,
Col c, Col d) {
fill_notched(rb, x, y, w, h, 4, nx, nx + NOTCH_W, NOTCH_D, NOTCH_D, d);
fill_notched(rb, x + 1, y + 1, w - 2, h - 2, 3, nx - 1, nx + NOTCH_W + 1, NOTCH_D, NOTCH_D, c);
}
/* Encaixe trapezoidal sob um comando; y - borda inferior do bloco.
Contorno (d) e miolo (c) como poligonos: lisos em qualquer escala. */
static void draw_bump(TinyBlocks_RenderableBlock* rb, float x, float y, Col c, Col d) {
float bx = x + NOTCH_X;
for (int r = 0; r <= NOTCH_D; r++)
rb_fill_rect(rb, bx+r-1, y-1+r, NOTCH_W-2*r+2, 1.f, d);
for (int r = 0; r < NOTCH_D; r++)
rb_fill_rect(rb, bx+r, y-1+r, NOTCH_W-2*r, 1.f, c);
bump_poly(rb, bx - 1, bx + NOTCH_W + 1, y - 1, y + NOTCH_D, NOTCH_D, 1.6f, d);
bump_poly(rb, bx, bx + NOTCH_W, y - 1.5f, y + NOTCH_D - 1, NOTCH_D, 1.2f, c);
}
/* ===
@@ -1231,8 +1294,22 @@ static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi,
{ x, y + b->sub_y[1] + b->sub_h[1], w, MID_H }, /* "finalmente" */
};
int n = 2 + shape_mouths(sh);
for (int i = 0; i < n; i++) rb_fill_rrect(rb, r[i][0], r[i][1], r[i][2], r[i][3], 4, d);
for (int i = 0; i < n; i++) rb_fill_rrect(rb, r[i][0]+1, r[i][1]+1, r[i][2]-2, r[i][3]-2, 3, c);
r[1][1] += NOTCH_D + 2; r[1][3] -= NOTCH_D + 2; /* o braco esquerdo nao tampa o encaixe */
/* braco de cima: encaixe do bloco anterior; bracos de baixo/"senao":
encaixe do ultimo bloco da boca */
for (int i = 0; i < n; i++) {
if (i == 1) continue;
body_notched(rb, r[i][0], r[i][1], r[i][2], r[i][3],
i == 0 ? x + NOTCH_X : x + ARM_W + NOTCH_X, c, d);
}
rb_fill_rrect(rb, r[1][0], r[1][1], r[1][2], r[1][3], 4, d);
rb_fill_rrect(rb, r[1][0] + 1, r[1][1] - 2, r[1][2] - 2, r[1][3], 3, c);
for (int i = 0; i < n; i++) { /* o miolo dos bracos se junta ao esquerdo */
if (i == 1) continue;
fill_notched(rb, r[i][0] + 1, r[i][1] + 1, r[i][2] - 2, r[i][3] - 2, 3,
(i == 0 ? x + NOTCH_X : x + ARM_W + NOTCH_X) - 1,
(i == 0 ? x + NOTCH_X : x + ARM_W + NOTCH_X) + NOTCH_W + 1, NOTCH_D, NOTCH_D, c);
}
break;
}
case SH_REPORTER: {
@@ -1245,9 +1322,8 @@ static void render_block(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int bi,
draw_hex(rb, x, y, w, h, d);
draw_hex(rb, x+1, y+1, w-2, h-2, c);
break;
default:
rb_fill_rrect(rb, x, y, w, h, 4, d);
rb_fill_rrect(rb, x+1, y+1, w-2, h-2, 3, c);
default: /* comando: encaixe femea no topo */
body_notched(rb, x, y, w, h, x + NOTCH_X, c, d);
break;
}
@@ -2651,6 +2727,13 @@ static void rb_scale_from(TinyBlocks_RenderableBlock* rb, uint32_t first, float
for (uint32_t i = first; i < rb->count; i++) {
TinyBlocks_DrawCmd* c = &rb->cmds[i];
if (c->kind == TB_DRAW_CLEAR || c->kind == TB_DRAW_CLEAR_CLIP || c->kind == TB_DRAW_SET_CLIP) continue;
if (c->kind == TB_DRAW_FILL_POLY) {
float xy[2 * TB_POLY_MAX];
int n = tb_draw_poly_get(c, xy);
for (int k = 0; k < n; k++) { xy[2*k] = ox + (xy[2*k] - ox) * s; xy[2*k+1] = oy + (xy[2*k+1] - oy) * s; }
tb_draw_poly_set(c, xy, n);
continue;
}
c->x = ox + (c->x - ox) * s; c->y = oy + (c->y - oy) * s;
if (c->kind == TB_DRAW_STROKE_LINE) { /* w,h sao o segundo ponto */
c->w = ox + (c->w - ox) * s; c->h = oy + (c->h - oy) * s;
@@ -4918,10 +5001,26 @@ static void render_snap(Ed* ed, TinyBlocks_RenderableBlock* rb, float ox, float
EdBlock* d = &T->ws[ed->drag_bi];
EdBlock* t = &T->ws[ed->snap_bi];
if (ed->snap_kind == SN_ARG) {
/* brilho branco em volta do campo, na forma dele (pilula, hexagono
ou encaixe de codigo), e o campo de novo por cima */
int s = ed->snap_idx;
float sx = t->ax + ox + t->slot_x[s], sy = t->ay + oy + t->slot_y[s];
rb_stroke_rect(rb, sx-3, sy-3, t->slot_w[s]+6, t->slot_h[s]+6, 1.f, COLA(0,0,0,70));
rb_stroke_rect(rb, sx-2, sy-2, t->slot_w[s]+4, t->slot_h[s]+4, 2.f, COL_WHITE);
float sw = t->slot_w[s], sh = t->slot_h[s];
Col c, d; block_cols(ed, t, &c, &d);
if (slot_is_bool(t->type, s)) {
draw_hex(rb, sx - 3, sy - 3, sw + 6, sh + 6, COLA(0, 0, 0, 60));
draw_hex(rb, sx - 2, sy - 2, sw + 4, sh + 4, COL_WHITE);
draw_slot(ed, rb, t, s, t->ax + ox, t->ay + oy, d, false);
} else if (slot_is_code(t->type, s)) {
rb_fill_rrect(rb, sx - 3, sy - 3, sw + 6, sh + 6, 7, COLA(0, 0, 0, 60));
rb_fill_rrect(rb, sx - 2, sy - 2, sw + 4, sh + 4, 6, COL_WHITE);
draw_socket(rb, t, s, t->ax + ox, t->ay + oy, c, d);
} else {
float r = text_lines(slot_src(t, s)) > 1 ? 4.f : sh * 0.5f;
rb_fill_rrect(rb, sx - 3, sy - 3, sw + 6, sh + 6, r + 3, COLA(0, 0, 0, 60));
rb_fill_rrect(rb, sx - 2, sy - 2, sw + 4, sh + 4, r + 2, COL_WHITE);
draw_slot(ed, rb, t, s, t->ax + ox, t->ay + oy, d, false);
}
return;
}
float gh = chain_h(T, ed->drag_bi), gx = t->ax, gy;
@@ -4932,7 +5031,15 @@ static void render_snap(Ed* ed, TinyBlocks_RenderableBlock* rb, float ox, float
gy = t->ay + t->slot_y[ed->snap_idx] + STK_PAD;
}
else gy = t->ay - gh;
rb_fill_rrect(rb, gx + ox, gy + oy, d->w, gh, 4, COLA(0,0,0,45));
/* sombra na forma de bloco: encaixe femea em cima, macho embaixo
(translucida: pecas sem sobreposicao) */
Col sc = COLA(0, 0, 0, 45);
float px = gx + ox, py = gy + oy, nx = px + NOTCH_X;
int last = ed->drag_bi;
while (T->ws[last].next >= 0) last = T->ws[last].next;
if (shape_of(d->type) == SH_HAT) rb_fill_rrect(rb, px, py, d->w, gh, 4, sc); /* chapeu: sem encaixe */
else fill_notched_ov(rb, px, py, d->w, gh, 4, nx, nx + NOTCH_W, NOTCH_D, NOTCH_D, sc, 0.f);
if (!is_cap(T->ws[last].type)) bump_poly(rb, nx, nx + NOTCH_W, py + gh, py + gh + NOTCH_D, NOTCH_D, 1.2f, sc);
}
/* ==========================================================================
@@ -8571,6 +8678,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
}
if (from < 0) {
ed->frame_no++;
tb_ed_w.anim_now((double)now_ms());
int ww = (int)ev_int(evb, GFX_IN_WIN_W);
int wh = (int)ev_int(evb, GFX_IN_WIN_H);
if (ww > 0) ed->win_w = ww;
@@ -8682,6 +8790,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
ed->press_t = now_ms();
ed->capture_view = from;
ed->te_eat_up = false;
tb_ed_w.ripple_down(mx, my);
if (te_pointer_down(ed, mx, my, ed->press_touch)) {
ed->te_eat_up = true; /* alca ou barra da selecao */
} else if (ed->press_touch && modal_top(ed)) {
@@ -8700,6 +8809,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
}
if (ev_int(evb, GFX_IN_MOUSE_R_DOWN)) on_rmb_down(ed, mx, my);
te_pointer_update(ed, mx, my, ev_int(evb, GFX_IN_MOUSE_L_HELD) != 0);
if (ev_int(evb, GFX_IN_MOUSE_L_UP)) tb_ed_w.ripple_up();
if (ev_int(evb, GFX_IN_MOUSE_L_UP) && ed->te_eat_up) { /* soltar a alca / a palavra segurada */
ed->te_eat_up = false;
ed->te_handle_drag = -1;
@@ -8787,6 +8897,13 @@ static void rb_translate(TinyBlocks_RenderableBlock* rb, float dx, float dy) {
case TB_DRAW_STROKE_LINE: /* w,h sao o segundo ponto */
c->x += dx; c->y += dy; c->w += dx; c->h += dy;
break;
case TB_DRAW_FILL_POLY: {
float xy[2 * TB_POLY_MAX];
int n = tb_draw_poly_get(c, xy);
for (int k = 0; k < n; k++) { xy[2*k] += dx; xy[2*k+1] += dy; }
tb_draw_poly_set(c, xy, n);
break;
}
default:
c->x += dx; c->y += dy;
break;
@@ -127,9 +127,13 @@ static bool resolve_widgets(void) {
sym_fn(&w.text_f, "tinyblocks-editor-widgets", "tb_ew_text_f");
sym_fn(&w.icon, "tinyblocks-editor-widgets", "tb_ew_icon");
sym_fn(&w.shadow, "tinyblocks-editor-widgets", "tb_ew_shadow");
sym_fn(&w.anim_now, "tinyblocks-editor-widgets", "tb_ew_anim_now");
sym_fn(&w.ripple_down, "tinyblocks-editor-widgets", "tb_ew_ripple_down");
sym_fn(&w.ripple_up, "tinyblocks-editor-widgets", "tb_ew_ripple_up");
sym_fn(&w.ripple, "tinyblocks-editor-widgets", "tb_ew_ripple");
bool ok = w.col_mul && w.fill_rect && w.fill_rrect && w.fill_circle && w.stroke_rect &&
w.stroke_circle && w.line && w.text_c && w.text_width && w.in_rect &&
w.text_f && w.icon && w.shadow;
w.text_f && w.icon && w.shadow && w.anim_now && w.ripple_down && w.ripple_up && w.ripple;
if (ok) tb_ed_w = w;
return ok;
}
@@ -179,7 +183,9 @@ bool tb_ed_modules_load(Ed* ed) {
.fill_circle = tb_ew_fill_circle, .stroke_rect = tb_ew_stroke_rect,
.stroke_circle = tb_ew_stroke_circle, .line = tb_ew_line, .text_c = tb_ew_text_c,
.text_width = tb_ew_text_width, .in_rect = tb_ew_in_rect,
.text_f = tb_ew_text_f, .icon = tb_ew_icon, .shadow = tb_ew_shadow };
.text_f = tb_ew_text_f, .icon = tb_ew_icon, .shadow = tb_ew_shadow,
.anim_now = tb_ew_anim_now, .ripple_down = tb_ew_ripple_down, .ripple_up = tb_ew_ripple_up,
.ripple = tb_ew_ripple };
tb_ed_img = (TbEdImageApi){
.view = tb_editor_image_view, .click = tb_editor_image_click, .drag = tb_editor_image_drag,
.release = tb_editor_image_release, .load_file = tb_editor_image_load_file,