From 70c819393ca7706f25c96af2c5d8a054e6aa0dcd Mon Sep 17 00:00:00 2001 From: mguinhos Date: Wed, 28 Oct 2026 22:23:32 -0300 Subject: [PATCH] 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 --- include/tinyblocks/tb_renderable_block.h | 12 ++ .../include/tb_editor_internal.h | 1 + .../tinyblocks-editor/include/tb_editor_m3.h | 2 + .../include/tb_editor_modules.h | 4 + .../include/tb_editor_widgets.h | 7 + .../src/tb_editor_widgets.c | 44 +++++ libraries/tinyblocks-editor/src/tb_editor.c | 157 +++++++++++++++--- .../tinyblocks-editor/src/tb_editor_modules.c | 10 +- libraries/tinyblocks-sdl2/src/tb_sdl2.c | 15 +- src/tb_renderable_block.c | 39 +++++ 10 files changed, 268 insertions(+), 23 deletions(-) diff --git a/include/tinyblocks/tb_renderable_block.h b/include/tinyblocks/tb_renderable_block.h index 084220b..0fee5cc 100644 --- a/include/tinyblocks/tb_renderable_block.h +++ b/include/tinyblocks/tb_renderable_block.h @@ -35,6 +35,9 @@ typedef enum { TB_DRAW_CLEAR_CLIP, /* remove clip */ TB_DRAW_TEXT, /* texto (x,y=baseline-left; font_size em px) */ TB_DRAW_IMAGE, /* imagem escalada em (x,y,w,h); ver field image */ + TB_DRAW_FILL_POLY, /* poligono convexo preenchido (antialias); os pontos * + * ficam no espaco do text (tb_draw_poly_get/set), * + * param = quantos; x,y,w,h = caixa que o contem */ } TinyBlocks_DrawCmdKind; typedef struct { @@ -115,6 +118,15 @@ TB_API void tb_draw_text (TinyBlocks_RenderableBlock* rb, #define TB_FONT_ICON_FILLED 3 /* Material Symbols (preenchido) */ #define TB_FONT_COUNT 4 +/* Poligono convexo (ate TB_POLY_MAX pontos; xy = x0,y0,x1,y1,...). Formas + * inclinadas (hexagono, encaixe) ficam lisas em qualquer escala. */ +#define TB_POLY_MAX 16 +TB_API void tb_draw_fill_poly (TinyBlocks_RenderableBlock* rb, const float* xy, int n, + float r, float g, float b, float a); +/* Pontos de um TB_DRAW_FILL_POLY (quem transforma comandos usa os dois) */ +TB_API int tb_draw_poly_get (const TinyBlocks_DrawCmd* c, float* xy); +TB_API void tb_draw_poly_set (TinyBlocks_DrawCmd* c, const float* xy, int n); + TB_API void tb_draw_text_font (TinyBlocks_RenderableBlock* rb, float x, float y, const char* text, float font_size, int font, float r, float g, float b, float a); diff --git a/libraries/tinyblocks-editor/include/tb_editor_internal.h b/libraries/tinyblocks-editor/include/tb_editor_internal.h index b463452..67b4f31 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_internal.h +++ b/libraries/tinyblocks-editor/include/tb_editor_internal.h @@ -249,6 +249,7 @@ typedef struct { #define rb_text_f tb_ed_w.text_f #define rb_icon tb_ed_w.icon #define rb_shadow tb_ed_w.shadow +#define rb_ripple tb_ed_w.ripple static const Col COL_MENU TB_EW_UNUSED = C( 76,151,255); /* #4C97FF */ static const Col COL_MENU_DARK TB_EW_UNUSED = C( 61,121,204); diff --git a/libraries/tinyblocks-editor/include/tb_editor_m3.h b/libraries/tinyblocks-editor/include/tb_editor_m3.h index 65db778..2ac960f 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_m3.h +++ b/libraries/tinyblocks-editor/include/tb_editor_m3.h @@ -147,6 +147,7 @@ static inline void m3_state(TinyBlocks_RenderableBlock* rb, float x, float y, fl Col on, bool hover, bool pressed) { float a = pressed ? M3_PRESSED : hover ? M3_HOVER : 0.f; if (a > 0.f) rb_fill_rrect(rb, x, y, w, h, rad, m3_alpha(on, a)); + rb_ripple(rb, x, y, w, h, rad, on); /* gota do toque/clique */ } /* == Botoes (pilula): filled, tonal, outlined, text === */ @@ -191,6 +192,7 @@ static inline void m3_icon_button(TinyBlocks_RenderableBlock* rb, float cx, floa float s = M3_ICON_BTN; if (selected) rb_fill_circle(rb, cx, cy, s * 0.5f, m3_alpha(c, 0.16f)); if (hover) rb_fill_circle(rb, cx, cy, s * 0.5f, m3_alpha(c, M3_HOVER)); + rb_ripple(rb, cx - s * 0.5f, cy - s * 0.5f, s, s, s * 0.5f, c); rb_icon(rb, cx, cy, icon, M3_ICON, c, selected); } diff --git a/libraries/tinyblocks-editor/include/tb_editor_modules.h b/libraries/tinyblocks-editor/include/tb_editor_modules.h index 7a26829..670eff5 100644 --- a/libraries/tinyblocks-editor/include/tb_editor_modules.h +++ b/libraries/tinyblocks-editor/include/tb_editor_modules.h @@ -51,6 +51,10 @@ typedef struct { void (*text_f) (TinyBlocks_RenderableBlock*, float, float, const char*, TbEwColor, float, int); void (*icon) (TinyBlocks_RenderableBlock*, float, float, unsigned, float, TbEwColor, bool); void (*shadow) (TinyBlocks_RenderableBlock*, float, float, float, float, float, int); + void (*anim_now) (double); + void (*ripple_down) (float, float); + void (*ripple_up) (void); + void (*ripple) (TinyBlocks_RenderableBlock*, float, float, float, float, float, TbEwColor); } TbEdWidgetsApi; /* API da library privada tinyblocks-editor-image */ diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/include/tb_editor_widgets.h b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/include/tb_editor_widgets.h index 3ed45fa..cddb80a 100644 --- a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/include/tb_editor_widgets.h +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/include/tb_editor_widgets.h @@ -44,6 +44,13 @@ void tb_ew_text_f (TinyBlocks_RenderableBlock* rb, float x, float cy, const void tb_ew_icon (TinyBlocks_RenderableBlock* rb, float cx, float cy, unsigned cp, float size, TbEwColor c, bool filled); /* Sombra de elevacao do Material 3 (level 1..5) atras de (x, y, w, h) */ +/* Ripple do M3 (a gota que cresce do ponto tocado): o editor informa o + * relogio e o apertar/soltar; cada componente chama tb_ew_ripple com o + * proprio retangulo (so desenha se o toque comecou nele) */ +void tb_ew_anim_now (double ms); +void tb_ew_ripple_down(float x, float y); +void tb_ew_ripple_up (void); +void tb_ew_ripple (TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float rad, TbEwColor on); void tb_ew_shadow (TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float rad, int level); bool tb_ew_in_rect(float mx, float my, float x, float y, float w, float h); diff --git a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/src/tb_editor_widgets.c b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/src/tb_editor_widgets.c index 558d31b..035da4e 100644 --- a/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/src/tb_editor_widgets.c +++ b/libraries/tinyblocks-editor/libraries/tinyblocks-editor-widgets/src/tb_editor_widgets.c @@ -4,6 +4,7 @@ * traduz chamadas em TinyBlocks_DrawCmd. */ #include +#include #include #include "tb_editor_widgets.h" @@ -62,6 +63,49 @@ void tb_ew_icon(TinyBlocks_RenderableBlock* rb, float cx, float cy, unsigned cp, filled ? TB_FONT_ICON_FILLED : TB_FONT_ICON, c.r, c.g, c.b, c.a); } +static struct { bool on, held; float x, y; double down, up; } s_rip; +static double s_now; + +void tb_ew_anim_now(double ms) { s_now = ms; } +void tb_ew_ripple_down(float x, float y) { + s_rip.on = true; s_rip.held = true; s_rip.x = x; s_rip.y = y; s_rip.down = s_now; +} +void tb_ew_ripple_up(void) { if (s_rip.held) { s_rip.held = false; s_rip.up = s_now; } } + +/* cubic-bezier(0.05, 0.7, 0.1, 1) ~ "emphasized decelerate" do M3 */ +static float ease_out(float t) { float u = 1.f - t; return 1.f - u * u * u * u; } + +void tb_ew_ripple(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float rad, TbEwColor on) { + if (!s_rip.on || s_rip.x < x || s_rip.x >= x + w || s_rip.y < y || s_rip.y >= y + h) return; + const double GROW = 300.0, FADE = 220.0; + float g = (float)((s_now - s_rip.down) / GROW); + if (g > 1.f) g = 1.f; + float a = 0.12f; + if (!s_rip.held) { /* some depois de crescer ate o fim */ + double st = s_rip.up > s_rip.down + 150.0 ? s_rip.up : s_rip.down + 150.0; + float f = (float)((s_now - st) / FADE); + if (f >= 1.f) { s_rip.on = false; return; } + if (f > 0.f) a *= 1.f - f; + } + float dx = fmaxf(s_rip.x - x, x + w - s_rip.x), dy = fmaxf(s_rip.y - y, y + h - s_rip.y); + float r = sqrtf(dx * dx + dy * dy) * (0.2f + 0.8f * ease_out(g)); + if (rad > w * 0.5f) rad = w * 0.5f; + if (rad > h * 0.5f) rad = h * 0.5f; + /* circulo recortado no retangulo de cantos redondos, em faixas */ + float step = h > 80.f ? 2.f : 1.f; + for (float yy = y; yy < y + h; yy += step) { + float cy = yy + step * 0.5f, d = cy - s_rip.y; + if (fabsf(d) >= r) continue; + float hw = sqrtf(r * r - d * d); + float l = s_rip.x - hw, rr = s_rip.x + hw, in = 0.f; + if (cy < y + rad) { float k = y + rad - cy; in = rad - sqrtf(fmaxf(0.f, rad * rad - k * k)); } + else if (cy > y + h - rad) { float k = cy - (y + h - rad); in = rad - sqrtf(fmaxf(0.f, rad * rad - k * k)); } + if (l < x + in) l = x + in; + if (rr > x + w - in) rr = x + w - in; + if (rr > l) tb_draw_fill_rect(rb, l, yy, rr - l, step, on.r, on.g, on.b, on.a * a); + } +} + void tb_ew_shadow(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, float rad, int level) { if (level <= 0) return; /* camadas cada vez maiores e mais fracas, puxadas para baixo (luz de cima) */ diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 2ac27e5..ac9bd42 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -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; diff --git a/libraries/tinyblocks-editor/src/tb_editor_modules.c b/libraries/tinyblocks-editor/src/tb_editor_modules.c index 585ff3a..dbaab95 100644 --- a/libraries/tinyblocks-editor/src/tb_editor_modules.c +++ b/libraries/tinyblocks-editor/src/tb_editor_modules.c @@ -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, diff --git a/libraries/tinyblocks-sdl2/src/tb_sdl2.c b/libraries/tinyblocks-sdl2/src/tb_sdl2.c index 0a42b88..5b86d22 100644 --- a/libraries/tinyblocks-sdl2/src/tb_sdl2.c +++ b/libraries/tinyblocks-sdl2/src/tb_sdl2.c @@ -368,7 +368,12 @@ static void sdl2_execute(SDL_Renderer* ren, scaled = *cmd; scaled.x *= s_ui_scale; scaled.y *= s_ui_scale; scaled.w *= s_ui_scale; scaled.h *= s_ui_scale; - if (cmd->kind != TB_DRAW_TEXT) scaled.param *= s_ui_scale; /* no texto e a fonte */ + if (cmd->kind == TB_DRAW_FILL_POLY) { /* param = quantos pontos */ + float xy[2 * TB_POLY_MAX]; + int n = tb_draw_poly_get(cmd, xy); + for (int k = 0; k < 2 * n; k++) xy[k] *= s_ui_scale; + tb_draw_poly_set(&scaled, xy, n); + } else if (cmd->kind != TB_DRAW_TEXT) scaled.param *= s_ui_scale; /* no texto e a fonte */ scaled.font_size *= s_ui_scale; cmd = &scaled; } @@ -396,6 +401,14 @@ static void sdl2_execute(SDL_Renderer* ren, aa_line(ren, rgba_of(cmd), cmd->x, cmd->y, cmd->w, cmd->h, cmd->param); break; + case TB_DRAW_FILL_POLY: { + float xy[2 * TB_POLY_MAX], px[TB_POLY_MAX], py[TB_POLY_MAX]; + int n = tb_draw_poly_get(cmd, xy); + for (int k = 0; k < n; k++) { px[k] = xy[2*k]; py[k] = xy[2*k+1]; } + if (n >= 3) aa_fill_poly(ren, rgba_of(cmd), px, py, n); + break; + } + case TB_DRAW_STROKE_RECT: { /* Quatro faixas sem sobreposição, espessura para dentro */ Rgba c = rgba_of(cmd); diff --git a/src/tb_renderable_block.c b/src/tb_renderable_block.c index 0812fd2..9fcbd7a 100644 --- a/src/tb_renderable_block.c +++ b/src/tb_renderable_block.c @@ -133,6 +133,38 @@ void tb_draw_text_font(TinyBlocks_RenderableBlock* rb, if (rb && rb->count > n) rb->cmds[n].param = (float)font; } +void tb_draw_poly_set(TinyBlocks_DrawCmd* c, const float* xy, int n) { + if (n > TB_POLY_MAX) n = TB_POLY_MAX; + if (n < 0) n = 0; + memcpy(c->text, xy, (size_t)n * 2 * sizeof(float)); /* 16 pontos = 128 bytes */ + c->param = (float)n; + float x0 = n ? xy[0] : 0, y0 = n ? xy[1] : 0, x1 = x0, y1 = y0; + for (int i = 1; i < n; i++) { + if (xy[2*i] < x0) x0 = xy[2*i]; + if (xy[2*i] > x1) x1 = xy[2*i]; + if (xy[2*i+1] < y0) y0 = xy[2*i+1]; + if (xy[2*i+1] > y1) y1 = xy[2*i+1]; + } + c->x = x0; c->y = y0; c->w = x1 - x0; c->h = y1 - y0; +} + +int tb_draw_poly_get(const TinyBlocks_DrawCmd* c, float* xy) { + int n = (int)c->param; + if (n > TB_POLY_MAX) n = TB_POLY_MAX; + if (n < 0) n = 0; + memcpy(xy, c->text, (size_t)n * 2 * sizeof(float)); + return n; +} + +void tb_draw_fill_poly(TinyBlocks_RenderableBlock* rb, const float* xy, int n, + float r, float g, float b, float a) { + if (!xy || n < 3) return; + TinyBlocks_DrawCmd* c = append(rb); if (!c) return; + c->kind = TB_DRAW_FILL_POLY; + c->r=r; c->g=g; c->b=b; c->a=a; + tb_draw_poly_set(c, xy, n); +} + void tb_draw_image(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, const TinyBlocks_Image* image) { @@ -168,6 +200,13 @@ void tb_draw_merge_offset(TinyBlocks_RenderableBlock* dst, cmd.x += dx; cmd.y += dy; cmd.w += dx; cmd.h += dy; break; + case TB_DRAW_FILL_POLY: { + float xy[2 * TB_POLY_MAX]; + int n = tb_draw_poly_get(&cmd, xy); + for (int i = 0; i < n; i++) { xy[2*i] += dx; xy[2*i+1] += dy; } + tb_draw_poly_set(&cmd, xy, n); + break; + } case TB_DRAW_CLEAR: case TB_DRAW_CLEAR_CLIP: break;