/* * customappearance.c — Demonstração de on_draw (aparência personalizada de bloco). * * Blocos: * Viz.ColorSwatch — inputs: r, g, b, a (0..1) → mostra quadrado colorido * Viz.Gauge — inputs: value (0..1), r, g, b → barra de progresso * Viz.Star — inputs: r, g, b, a → estrela de 5 pontas (contorno) * Viz.BarChart — inputs: a, b, c, d (reais) → mini gráfico de barras * * make customappearance → build/customappearance.so */ #define _POSIX_C_SOURCE 200809L #include #include #include #include /* ── Helpers ─────────────────────────────────────────────────────────────── */ static float gf(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) { TinyBlocks_Value v = tb_block_get_input(self, rt, idx); if (v.kind == TB_VALUE_REAL) return (float)v.as.real; if (v.kind == TB_VALUE_INTEGER) return (float)v.as.integer; return 0.0f; } static float out_f(TinyBlocks_Block* self, uint32_t idx) { if (!self || idx >= self->output_count) return 0.0f; TinyBlocks_Value* v = &self->outputs[idx]; if (v->kind == TB_VALUE_REAL) return (float)v->as.real; if (v->kind == TB_VALUE_INTEGER) return (float)v->as.integer; return 0.0f; } /* ══════════════════════════════════════════════════════════════════════════ * Viz.ColorSwatch * ══════════════════════════════════════════════════════════════════════════ */ static void eval_color_swatch(TinyBlocks_Block* self, TinyBlocks* rt) { for (uint32_t i = 0; i < 4 && i < self->output_count; i++) tb_block_set_output(self, i, tb_value_real((double)gf(self, rt, i))); self->dirty = false; } static bool draw_color_swatch(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) { (void)rt; TinyBlocks_RenderableBlock* rb = tb_renderable_acquire(); if (!rb) return false; float r = out_f(self, 0), g = out_f(self, 1); float b = out_f(self, 2), a = out_f(self, 3); /* Área de conteúdo: coordenadas locais ao bloco (0,0 = topo-esq) */ float bw = 168.f, bh = 60.f; float pad = 6.f; float w = bw - 2*pad, h = bh - 2*pad; /* Xadrez de fundo */ float cell = 8.f; for (float cy = 0; cy < h; cy += cell) { for (float cx = 0; cx < w; cx += cell) { float dark = ((int)(cx/cell)+(int)(cy/cell)) % 2 == 0 ? 0.6f : 0.9f; float cw2 = (cx+cell > w) ? w-cx : cell; float ch2 = (cy+cell > h) ? h-cy : cell; tb_draw_fill_rect(rb, pad+cx, pad+cy, cw2, ch2, dark, dark, dark, 1.f); } } /* Swatch com alpha real */ tb_draw_fill_rect(rb, pad, pad, w, h, r, g, b, a); /* Borda */ tb_draw_stroke_rect(rb, pad, pad, w, h, 1.f, 0.9f, 0.9f, 0.9f, 0.6f); *dst = rb; return true; } /* ══════════════════════════════════════════════════════════════════════════ * Viz.Gauge * ══════════════════════════════════════════════════════════════════════════ */ static void eval_gauge(TinyBlocks_Block* self, TinyBlocks* rt) { for (uint32_t i = 0; i < 4 && i < self->output_count; i++) tb_block_set_output(self, i, tb_value_real((double)gf(self, rt, i))); self->dirty = false; } static bool draw_gauge(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) { (void)rt; TinyBlocks_RenderableBlock* rb = tb_renderable_acquire(); if (!rb) return false; float value = out_f(self, 0); float r = out_f(self, 1), g = out_f(self, 2), b = out_f(self, 3); if (value < 0.f) value = 0.f; if (value > 1.f) value = 1.f; float bw = 168.f, bh = 60.f; float pad = 8.f; float barh = 14.f; float bary = (bh - barh) * 0.5f; float barw = bw - 2*pad; /* Fundo escuro */ tb_draw_fill_rect(rb, pad, bary, barw, barh, 0.15f, 0.15f, 0.2f, 1.f); /* Preenchimento proporcional */ if (value > 0.001f) tb_draw_fill_rect(rb, pad, bary, barw * value, barh, r, g, b, 1.f); /* Borda */ tb_draw_stroke_rect(rb, pad, bary, barw, barh, 1.f, 0.6f, 0.6f, 0.7f, 0.8f); *dst = rb; return true; } /* ══════════════════════════════════════════════════════════════════════════ * Viz.Star * ══════════════════════════════════════════════════════════════════════════ */ static void eval_star(TinyBlocks_Block* self, TinyBlocks* rt) { for (uint32_t i = 0; i < 4 && i < self->output_count; i++) tb_block_set_output(self, i, tb_value_real((double)gf(self, rt, i))); self->dirty = false; } static bool draw_star(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) { (void)rt; TinyBlocks_RenderableBlock* rb = tb_renderable_acquire(); if (!rb) return false; float r = out_f(self, 0), g = out_f(self, 1); float b = out_f(self, 2), a = out_f(self, 3); float bw = 168.f, bh = 60.f; float cx = bw * 0.5f, cy = bh * 0.5f; float ro = (bw < bh ? bw : bh) * 0.42f; float ri = ro * 0.42f; #ifndef M_PI #define M_PI 3.14159265358979323846 #endif /* Desenha 5 linhas da estrela (conecta pontos alternados) */ float px[10], py[10]; for (int i = 0; i < 10; i++) { float angle = (float)i * (float)M_PI / 5.f - (float)M_PI / 2.f; float rad = (i % 2 == 0) ? ro : ri; px[i] = cx + rad * cosf(angle); py[i] = cy + rad * sinf(angle); } /* Contorno */ for (int i = 0; i < 10; i++) { int j = (i + 1) % 10; tb_draw_stroke_line(rb, px[i], py[i], px[j], py[j], 1.5f, r, g, b, a); } /* Diagonais internas (estrela de 5 pontas típica) */ for (int i = 0; i < 5; i++) { int j = (i + 2) % 10 * 2 % 10; tb_draw_stroke_line(rb, px[i*2], py[i*2], px[j], py[j], 1.f, r*0.6f, g*0.6f, b*0.6f, a); } *dst = rb; return true; } /* ══════════════════════════════════════════════════════════════════════════ * Viz.BarChart * ══════════════════════════════════════════════════════════════════════════ */ static void eval_barchart(TinyBlocks_Block* self, TinyBlocks* rt) { for (uint32_t i = 0; i < 4 && i < self->output_count; i++) tb_block_set_output(self, i, tb_value_real((double)gf(self, rt, i))); self->dirty = false; } static bool draw_barchart(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) { (void)rt; TinyBlocks_RenderableBlock* rb = tb_renderable_acquire(); if (!rb) return false; float vals[4]; for (int i = 0; i < 4; i++) vals[i] = out_f(self, i); float maxv = 0.001f; for (int i = 0; i < 4; i++) if (vals[i] > maxv) maxv = vals[i]; static const float colors[4][3] = { {0.36f, 0.70f, 1.00f}, {0.40f, 0.86f, 0.56f}, {1.00f, 0.75f, 0.30f}, {0.86f, 0.40f, 0.50f}, }; float bw = 168.f, bh = 60.f; float pad = 6.f; float gap = 4.f; float tw = bw - 2*pad; float th = bh - 2*pad - 4.f; float barw = (tw - gap*3.f) / 4.f; /* Linha de base */ tb_draw_fill_rect(rb, pad, pad+th, tw, 1.f, 0.3f, 0.3f, 0.35f, 1.f); for (int i = 0; i < 4; i++) { float norm = vals[i] / maxv; float bh2 = norm * th; float bx = pad + i * (barw + gap); float by = pad + th - bh2; if (bh2 < 1.f) bh2 = 1.f; tb_draw_fill_rect(rb, bx, by, barw, bh2, colors[i][0], colors[i][1], colors[i][2], 0.9f); } *dst = rb; return true; } /* ══════════════════════════════════════════════════════════════════════════ * Descritores de slots * ══════════════════════════════════════════════════════════════════════════ */ static const TinyBlocks_SlotDesc rgba_in[] = { {"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}, {"a", TB_VALUE_REAL} }; static const TinyBlocks_SlotDesc rgba_out[] = { {"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}, {"a", TB_VALUE_REAL} }; static const TinyBlocks_SlotDesc gauge_in[] = { {"value", TB_VALUE_REAL}, {"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL} }; static const TinyBlocks_SlotDesc gauge_out[] = { {"value", TB_VALUE_REAL}, {"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL} }; static const TinyBlocks_SlotDesc chart_in[] = { {"a", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}, {"c", TB_VALUE_REAL}, {"d", TB_VALUE_REAL} }; static const TinyBlocks_SlotDesc chart_out[] = { {"a", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}, {"c", TB_VALUE_REAL}, {"d", TB_VALUE_REAL} }; /* ══════════════════════════════════════════════════════════════════════════ * Registro * ══════════════════════════════════════════════════════════════════════════ */ static bool ca_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { TinyBlocks_TypeBlock* t; #define REG_VIZ(name, ins, ni, outs, no, eval_fn, draw_fn) \ t = tb_type_block_create(rt, name, ins, ni, outs, no, eval_fn); \ if (t) { t->on_draw = draw_fn; tb_library_add(lib, &t->base); } REG_VIZ("Viz.ColorSwatch", rgba_in, 4, rgba_out, 4, eval_color_swatch, draw_color_swatch); REG_VIZ("Viz.Gauge", gauge_in, 4, gauge_out, 4, eval_gauge, draw_gauge); REG_VIZ("Viz.Star", rgba_in, 4, rgba_out, 4, eval_star, draw_star); REG_VIZ("Viz.BarChart", chart_in, 4, chart_out, 4, eval_barchart, draw_barchart); #undef REG_VIZ return true; } static void ca_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { (void)rt; (void)lib; } TINYBLOCKS_DEFINE_LIBRARY(customappearance, "customappearance", "1.0.0", ca_init, ca_deinit)