Adiciona tinyblocks-base64 e tinyblocks-curves (listas de pontos)
This commit is contained in:
1 parent
a14c7f9937
commit
b1b15a4c49
12 files changed
+815
-9
No files matched your search
@@ -0,0 +1,29 @@
|
||||
# Makefile da library base64 (categoria "Base64")
|
||||
# Constroi build/libtinyblocks-base64.a e build/libtinyblocks-base64.so
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
|
||||
|
||||
OBJ := $(BUILD)/tb_base64.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-base64.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-base64.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_base64.c include/tb_base64.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* TinyBlocks - tb_base64.h
|
||||
* Library tinyblocks-base64: codifica/decodifica Base64 (categoria "Base64").
|
||||
* Independente do editor.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_BASE64_H
|
||||
#define TINYBLOCKS_BASE64_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_base64_get_library(void);
|
||||
|
||||
/* -- API C: buffers alocados com malloc (libere com free) --- */
|
||||
/* Codifica n bytes em texto Base64 terminado em '\0'. */
|
||||
TB_API char* tb_base64_encode(const void* data, size_t n);
|
||||
/* Decodifica texto Base64; escreve o tamanho em *out_len (se != NULL). */
|
||||
TB_API unsigned char* tb_base64_decode(const char* text, size_t* out_len);
|
||||
/* Atalhos para strings (o decode devolve texto terminado em '\0'). */
|
||||
TB_API char* tb_base64_encode_str(const char* s);
|
||||
TB_API char* tb_base64_decode_str(const char* s, size_t* out_len);
|
||||
TB_API const char* tb_base64_last_error(void);
|
||||
|
||||
#endif /* TINYBLOCKS_BASE64_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library base64. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-base64"
|
||||
version = "1.0.0"
|
||||
category = "Base64"
|
||||
description = "Codifica e decodifica Base64"
|
||||
color = [0, 150, 136, 255]
|
||||
dark = [0, 121, 107, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_base64.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-base64.so"
|
||||
static = "build/libtinyblocks-base64.a"
|
||||
depends = []
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* TinyBlocks - tb_base64.c
|
||||
* Categoria "Base64": Base64.Encode / Base64.Decode.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_base64.h"
|
||||
|
||||
static const char B64_ALPHA[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static char s_err[160] = "";
|
||||
|
||||
const char* tb_base64_last_error(void) { return s_err; }
|
||||
|
||||
char* tb_base64_encode(const void* data, size_t n) {
|
||||
s_err[0] = 0;
|
||||
const unsigned char* p = (const unsigned char*)data;
|
||||
size_t out = ((n + 2) / 3) * 4;
|
||||
char* buf = (char*)malloc(out + 1);
|
||||
if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
|
||||
size_t o = 0;
|
||||
for (size_t i = 0; i < n; i += 3) {
|
||||
unsigned v = (unsigned)p[i] << 16;
|
||||
if (i + 1 < n) v |= (unsigned)p[i + 1] << 8;
|
||||
if (i + 2 < n) v |= (unsigned)p[i + 2];
|
||||
buf[o++] = B64_ALPHA[(v >> 18) & 63];
|
||||
buf[o++] = B64_ALPHA[(v >> 12) & 63];
|
||||
buf[o++] = (i + 1 < n) ? B64_ALPHA[(v >> 6) & 63] : '=';
|
||||
buf[o++] = (i + 2 < n) ? B64_ALPHA[v & 63] : '=';
|
||||
}
|
||||
buf[o] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int b64_val(int c) {
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
||||
if (c >= '0' && c <= '9') return c - '0' + 52;
|
||||
if (c == '+') return 62;
|
||||
if (c == '/') return 63;
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char* tb_base64_decode(const char* text, size_t* out_len) {
|
||||
s_err[0] = 0;
|
||||
if (out_len) *out_len = 0;
|
||||
if (!text) { snprintf(s_err, sizeof(s_err), "entrada nula"); return NULL; }
|
||||
size_t n = strlen(text);
|
||||
unsigned char* buf = (unsigned char*)malloc(n / 4 * 3 + 4);
|
||||
if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
|
||||
size_t o = 0;
|
||||
int quad[4], qn = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char)text[i];
|
||||
if (c == '\n' || c == '\r' || c == ' ' || c == '\t') continue;
|
||||
if (c == '=') { quad[qn++] = -2; }
|
||||
else {
|
||||
int v = b64_val(c);
|
||||
if (v < 0) { free(buf); snprintf(s_err, sizeof(s_err), "caractere invalido '%c'", c); return NULL; }
|
||||
quad[qn++] = v;
|
||||
}
|
||||
if (qn == 4) {
|
||||
int q0 = quad[0] < 0 ? 0 : quad[0];
|
||||
int q1 = quad[1] < 0 ? 0 : quad[1];
|
||||
int q2 = quad[2] < 0 ? 0 : quad[2];
|
||||
int q3 = quad[3] < 0 ? 0 : quad[3];
|
||||
if (quad[0] >= 0) buf[o++] = (unsigned char)((q0 << 2) | ((q1 & 0x30) >> 4));
|
||||
if (quad[2] >= 0) buf[o++] = (unsigned char)(((q1 & 0x0F) << 4) | ((q2 & 0x3C) >> 2));
|
||||
if (quad[3] >= 0) buf[o++] = (unsigned char)(((q2 & 0x03) << 6) | (q3 & 0x3F));
|
||||
qn = 0;
|
||||
}
|
||||
}
|
||||
buf[o] = 0;
|
||||
if (out_len) *out_len = o;
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* tb_base64_encode_str(const char* s) {
|
||||
return tb_base64_encode(s ? s : "", s ? strlen(s) : 0);
|
||||
}
|
||||
|
||||
char* tb_base64_decode_str(const char* s, size_t* out_len) {
|
||||
return (char*)tb_base64_decode(s, out_len);
|
||||
}
|
||||
|
||||
/* -- Blocos -------------------------------------------------------------- */
|
||||
|
||||
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
||||
}
|
||||
|
||||
static void eval_encode(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char* s = tb_base64_encode_str(in_text(self, rt, 0));
|
||||
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
||||
free(s);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_decode(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char* s = tb_base64_decode_str(in_text(self, rt, 0), NULL);
|
||||
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
||||
free(s);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------ */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool base64_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Base64",
|
||||
{ 0/255.f, 150/255.f, 136/255.f, 1.f},
|
||||
{ 0/255.f, 121/255.f, 107/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc in_text_s[] = {{"texto", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc out_text_s[] = {{"texto", TB_VALUE_TEXT}};
|
||||
|
||||
return reg(rt, lib, "Base64.Encode", in_text_s, 1, out_text_s, 1, eval_encode)
|
||||
&& reg(rt, lib, "Base64.Decode", in_text_s, 1, out_text_s, 1, eval_decode);
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(base64, "tinyblocks-base64", "1.0.0", base64_init, NULL)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_base64_get_library(void) {
|
||||
return &_tb_lib_desc_base64;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
# Makefile da library curves (categoria "Curvas")
|
||||
# Constroi build/libtinyblocks-curves.a e build/libtinyblocks-curves.so
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
|
||||
|
||||
OBJ := $(BUILD)/tb_curves.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-curves.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-curves.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_curves.c include/tb_curves.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^ -lm
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* TinyBlocks - tb_curves.h
|
||||
* Library tinyblocks-curves: curvas (reta, bezier, quadratica, arco, circulo).
|
||||
* Cada curva devolve uma LISTA DE PONTOS como array JSON: [[x,y],[x,y],...].
|
||||
* Independente do editor.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_CURVES_H
|
||||
#define TINYBLOCKS_CURVES_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_curves_get_library(void);
|
||||
|
||||
/* -- API C: cada funcao retorna um JSON array novo (libere com tb_json_release).
|
||||
* "steps" e o numero de segmentos (>=1); a lista sai com steps+1 pontos. */
|
||||
TB_API TinyBlocks_JsonValue* tb_curves_line(double x0, double y0,
|
||||
double x1, double y1, int steps);
|
||||
TB_API TinyBlocks_JsonValue* tb_curves_bezier(double x0, double y0,
|
||||
double cx1, double cy1,
|
||||
double cx2, double cy2,
|
||||
double x1, double y1, int steps);
|
||||
TB_API TinyBlocks_JsonValue* tb_curves_quadratic(double x0, double y0,
|
||||
double cx, double cy,
|
||||
double x1, double y1, int steps);
|
||||
TB_API TinyBlocks_JsonValue* tb_curves_arc(double cx, double cy, double r,
|
||||
double a0_deg, double a1_deg, int steps);
|
||||
TB_API TinyBlocks_JsonValue* tb_curves_circle(double cx, double cy, double r,
|
||||
int steps);
|
||||
|
||||
/* -- Leitura de uma lista de pontos --- */
|
||||
TB_API int tb_curves_count(const TinyBlocks_JsonValue* pts);
|
||||
TB_API bool tb_curves_point(const TinyBlocks_JsonValue* pts, int i,
|
||||
double* x, double* y);
|
||||
|
||||
#endif /* TINYBLOCKS_CURVES_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library curves. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-curves"
|
||||
version = "1.0.0"
|
||||
category = "Curvas"
|
||||
description = "Curvas: reta, bezier, quadratica, arco e circulo (lista de pontos)"
|
||||
color = [233, 30, 99, 255]
|
||||
dark = [194, 24, 91, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_curves.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-curves.so"
|
||||
static = "build/libtinyblocks-curves.a"
|
||||
depends = []
|
||||
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* TinyBlocks - tb_curves.c
|
||||
* Categoria "Curvas": Curvas.Line / Bezier / Quadratic / Arc / Circle / Count /
|
||||
* PointX / PointY. As curvas retornam uma lista de pontos [[x,y],...] (JSON).
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
#include "tb_curves.h"
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
#define CURVES_MAX_STEPS 100000
|
||||
|
||||
static int clamp_steps(int steps) {
|
||||
if (steps < 1) return 1;
|
||||
if (steps > CURVES_MAX_STEPS) return CURVES_MAX_STEPS;
|
||||
return steps;
|
||||
}
|
||||
|
||||
static void push_number(TinyBlocks_JsonValue* arr, double v) {
|
||||
TinyBlocks_JsonValue* n = tb_json_number(v);
|
||||
if (n) { tb_json_array_push(arr, n); tb_json_release(n); }
|
||||
}
|
||||
|
||||
/* Adiciona o ponto [x,y] ao array. */
|
||||
static void push_point(TinyBlocks_JsonValue* arr, double x, double y) {
|
||||
TinyBlocks_JsonValue* pt = tb_json_array();
|
||||
if (!pt) return;
|
||||
push_number(pt, x);
|
||||
push_number(pt, y);
|
||||
tb_json_array_push(arr, pt);
|
||||
tb_json_release(pt);
|
||||
}
|
||||
|
||||
/* -- Curvas -------------------------------------------------------------- */
|
||||
|
||||
TinyBlocks_JsonValue* tb_curves_line(double x0, double y0, double x1, double y1,
|
||||
int steps) {
|
||||
steps = clamp_steps(steps);
|
||||
TinyBlocks_JsonValue* a = tb_json_array();
|
||||
if (!a) return NULL;
|
||||
for (int i = 0; i <= steps; i++) {
|
||||
double t = (double)i / steps;
|
||||
push_point(a, x0 + (x1 - x0) * t, y0 + (y1 - y0) * t);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_curves_bezier(double x0, double y0,
|
||||
double cx1, double cy1,
|
||||
double cx2, double cy2,
|
||||
double x1, double y1, int steps) {
|
||||
steps = clamp_steps(steps);
|
||||
TinyBlocks_JsonValue* a = tb_json_array();
|
||||
if (!a) return NULL;
|
||||
for (int i = 0; i <= steps; i++) {
|
||||
double t = (double)i / steps, u = 1.0 - t;
|
||||
double b0 = u*u*u, b1 = 3*u*u*t, b2 = 3*u*t*t, b3 = t*t*t;
|
||||
push_point(a,
|
||||
b0*x0 + b1*cx1 + b2*cx2 + b3*x1,
|
||||
b0*y0 + b1*cy1 + b2*cy2 + b3*y1);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_curves_quadratic(double x0, double y0,
|
||||
double cx, double cy,
|
||||
double x1, double y1, int steps) {
|
||||
steps = clamp_steps(steps);
|
||||
TinyBlocks_JsonValue* a = tb_json_array();
|
||||
if (!a) return NULL;
|
||||
for (int i = 0; i <= steps; i++) {
|
||||
double t = (double)i / steps, u = 1.0 - t;
|
||||
push_point(a,
|
||||
u*u*x0 + 2*u*t*cx + t*t*x1,
|
||||
u*u*y0 + 2*u*t*cy + t*t*y1);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_curves_arc(double cx, double cy, double r,
|
||||
double a0_deg, double a1_deg, int steps) {
|
||||
steps = clamp_steps(steps);
|
||||
TinyBlocks_JsonValue* a = tb_json_array();
|
||||
if (!a) return NULL;
|
||||
double a0 = a0_deg * M_PI / 180.0, a1 = a1_deg * M_PI / 180.0;
|
||||
for (int i = 0; i <= steps; i++) {
|
||||
double t = (double)i / steps;
|
||||
double ang = a0 + (a1 - a0) * t;
|
||||
push_point(a, cx + r*cos(ang), cy + r*sin(ang));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
TinyBlocks_JsonValue* tb_curves_circle(double cx, double cy, double r, int steps) {
|
||||
return tb_curves_arc(cx, cy, r, 0.0, 360.0, steps);
|
||||
}
|
||||
|
||||
/* -- Leitura ------------------------------------------------------------- */
|
||||
|
||||
int tb_curves_count(const TinyBlocks_JsonValue* pts) {
|
||||
if (!pts || tb_json_kind(pts) != TB_JSON_ARRAY) return 0;
|
||||
return (int)tb_json_array_length(pts);
|
||||
}
|
||||
|
||||
bool tb_curves_point(const TinyBlocks_JsonValue* pts, int i, double* x, double* y) {
|
||||
if (!pts || tb_json_kind(pts) != TB_JSON_ARRAY) return false;
|
||||
if (i < 0 || (uint32_t)i >= tb_json_array_length(pts)) return false;
|
||||
TinyBlocks_JsonValue* p = tb_json_array_get(pts, (uint32_t)i);
|
||||
if (!p) return false;
|
||||
bool ok = 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);
|
||||
if (x) *x = tb_json_as_number(px, 0.0);
|
||||
if (y) *y = tb_json_as_number(py, 0.0);
|
||||
tb_json_release(px); tb_json_release(py);
|
||||
}
|
||||
tb_json_release(p);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* -- Helpers de entrada -------------------------------------------------- */
|
||||
|
||||
static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_REAL) return v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text);
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_INTEGER) return v.as.integer;
|
||||
if (v.kind == TB_VALUE_REAL) return (int64_t)v.as.real;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* in_pts(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return (v.kind == TB_VALUE_JSON) ? v.as.json : NULL;
|
||||
}
|
||||
|
||||
static void out_pts(TinyBlocks_Block* self, TinyBlocks_JsonValue* pts) {
|
||||
tb_block_set_output(self, 0, tb_value_json(pts));
|
||||
tb_json_release(pts);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Blocos -------------------------------------------------------------- */
|
||||
|
||||
static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_pts(self, tb_curves_line(in_real(self,rt,0), in_real(self,rt,1),
|
||||
in_real(self,rt,2), in_real(self,rt,3),
|
||||
(int)in_int(self,rt,4)));
|
||||
}
|
||||
static void eval_bezier(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_pts(self, tb_curves_bezier(in_real(self,rt,0), in_real(self,rt,1),
|
||||
in_real(self,rt,2), in_real(self,rt,3),
|
||||
in_real(self,rt,4), in_real(self,rt,5),
|
||||
in_real(self,rt,6), in_real(self,rt,7),
|
||||
(int)in_int(self,rt,8)));
|
||||
}
|
||||
static void eval_quadratic(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_pts(self, tb_curves_quadratic(in_real(self,rt,0), in_real(self,rt,1),
|
||||
in_real(self,rt,2), in_real(self,rt,3),
|
||||
in_real(self,rt,4), in_real(self,rt,5),
|
||||
(int)in_int(self,rt,6)));
|
||||
}
|
||||
static void eval_arc(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_pts(self, tb_curves_arc(in_real(self,rt,0), in_real(self,rt,1),
|
||||
in_real(self,rt,2), in_real(self,rt,3),
|
||||
in_real(self,rt,4), (int)in_int(self,rt,5)));
|
||||
}
|
||||
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
out_pts(self, tb_curves_circle(in_real(self,rt,0), in_real(self,rt,1),
|
||||
in_real(self,rt,2), (int)in_int(self,rt,3)));
|
||||
}
|
||||
static void eval_count(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_curves_count(in_pts(self,rt,0))));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_pointx(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
double x = 0, y = 0;
|
||||
tb_curves_point(in_pts(self,rt,0), (int)in_int(self,rt,1), &x, &y);
|
||||
tb_block_set_output(self, 0, tb_value_real(x));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_pointy(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
double x = 0, y = 0;
|
||||
tb_curves_point(in_pts(self,rt,0), (int)in_int(self,rt,1), &x, &y);
|
||||
tb_block_set_output(self, 0, tb_value_real(y));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------ */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool curves_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Curvas",
|
||||
{ 233/255.f, 30/255.f, 99/255.f, 1.f},
|
||||
{ 194/255.f, 24/255.f, 91/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc pts_out[] = {{"pontos", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc num_out[] = {{"valor", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc int_out[] = {{"n", TB_VALUE_INTEGER}};
|
||||
|
||||
TinyBlocks_SlotDesc line_in[5], bez_in[9], quad_in[7], arc_in[6], circ_in[4];
|
||||
TinyBlocks_SlotDesc cnt_in[1], pt_in[2];
|
||||
const char* ln[5] = {"x0","y0","x1","y1","passos"};
|
||||
const char* bn[9] = {"x0","y0","cx1","cy1","cx2","cy2","x1","y1","passos"};
|
||||
const char* qn[7] = {"x0","y0","cx","cy","x1","y1","passos"};
|
||||
const char* an[6] = {"cx","cy","raio","ang0","ang1","passos"};
|
||||
const char* cn[4] = {"cx","cy","raio","passos"};
|
||||
for (int i = 0; i < 5; i++) line_in[i] = (TinyBlocks_SlotDesc){ln[i], TB_VALUE_NONE};
|
||||
for (int i = 0; i < 9; i++) bez_in[i] = (TinyBlocks_SlotDesc){bn[i], TB_VALUE_NONE};
|
||||
for (int i = 0; i < 7; i++) quad_in[i] = (TinyBlocks_SlotDesc){qn[i], TB_VALUE_NONE};
|
||||
for (int i = 0; i < 6; i++) arc_in[i] = (TinyBlocks_SlotDesc){an[i], TB_VALUE_NONE};
|
||||
for (int i = 0; i < 4; i++) circ_in[i] = (TinyBlocks_SlotDesc){cn[i], TB_VALUE_NONE};
|
||||
cnt_in[0] = (TinyBlocks_SlotDesc){"pontos", TB_VALUE_JSON};
|
||||
pt_in[0] = (TinyBlocks_SlotDesc){"pontos", TB_VALUE_JSON};
|
||||
pt_in[1] = (TinyBlocks_SlotDesc){"indice", TB_VALUE_INTEGER};
|
||||
|
||||
return reg(rt, lib, "Curves.Line", line_in, 5, pts_out, 1, eval_line)
|
||||
&& reg(rt, lib, "Curves.Bezier", bez_in, 9, pts_out, 1, eval_bezier)
|
||||
&& reg(rt, lib, "Curves.Quadratic", quad_in, 7, pts_out, 1, eval_quadratic)
|
||||
&& reg(rt, lib, "Curves.Arc", arc_in, 6, pts_out, 1, eval_arc)
|
||||
&& reg(rt, lib, "Curves.Circle", circ_in, 4, pts_out, 1, eval_circle)
|
||||
&& reg(rt, lib, "Curves.Count", cnt_in, 1, int_out, 1, eval_count)
|
||||
&& reg(rt, lib, "Curves.PointX", pt_in, 2, num_out, 1, eval_pointx)
|
||||
&& reg(rt, lib, "Curves.PointY", pt_in, 2, num_out, 1, eval_pointy);
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(curves, "tinyblocks-curves", "1.0.0", curves_init, NULL)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_curves_get_library(void) {
|
||||
return &_tb_lib_desc_curves;
|
||||
}
|
||||
@@ -1221,17 +1221,34 @@ static int hit_slot(Tab* T, int bi, float x, float y) {
|
||||
/* ===
|
||||
* Renderização de blocos
|
||||
* === */
|
||||
/* Campo de nome de variavel (slot 0 de Variables.Get/Set/Change):
|
||||
* desenhado laranja (cor da categoria), como no Scratch. */
|
||||
static bool slot_is_var_name(const TinyBlocks_TypeBlock* t, int s) {
|
||||
if (!t || !t->type_name || s != 0) return false;
|
||||
const char* n = t->type_name;
|
||||
return strcmp(n, "Variables.Get") == 0 ||
|
||||
strcmp(n, "Variables.Set") == 0 ||
|
||||
strcmp(n, "Variables.Change") == 0;
|
||||
}
|
||||
|
||||
static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s,
|
||||
float x, float y, Col dark, bool editing) {
|
||||
float sx = x + b->slot_x[s], sy = y + b->slot_y[s];
|
||||
float sw = b->slot_w[s], sh = b->slot_h[s];
|
||||
if (slot_is_bool(b->type, s)) { draw_hex(rb, sx, sy, sw, sh, col_mul(dark, 0.9f)); return; }
|
||||
|
||||
bool varname = slot_is_var_name(b->type, s);
|
||||
/* Laranja num tom mais escuro que o bloco, para o campo aparecer
|
||||
* (como o "vinco" do dropdown de variavel do Scratch). */
|
||||
Col border = varname ? col_mul(COL_VAR, 0.60f) : COLA(0,0,0,40);
|
||||
Col fill = varname ? col_mul(COL_VAR, 0.82f) : COL_WHITE;
|
||||
Col txt = varname ? COL_WHITE : COL_TEXT;
|
||||
|
||||
const char* src = slot_src(b, s);
|
||||
int lines = text_lines(src);
|
||||
float rad = lines > 1 ? 4.f : sh*0.5f;
|
||||
rb_fill_rrect(rb, sx, sy, sw, sh, rad, editing ? COL_MENU : COLA(0,0,0,40));
|
||||
rb_fill_rrect(rb, sx+1, sy+1, sw-2, sh-2, rad-1, COL_WHITE);
|
||||
rb_fill_rrect(rb, sx, sy, sw, sh, rad, editing ? COL_MENU : border);
|
||||
rb_fill_rrect(rb, sx+1, sy+1, sw-2, sh-2, rad-1, fill);
|
||||
|
||||
char line[64];
|
||||
float ly = sy + (sh - lines*SLOT_LINE_H)*0.5f + SLOT_LINE_H*0.5f;
|
||||
@@ -1240,11 +1257,11 @@ static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s,
|
||||
text_line(src, l, line, sizeof(line));
|
||||
float lw = tw(line, FS);
|
||||
float lx = lines > 1 ? sx + 7 : sx + (sw - lw)*0.5f;
|
||||
rb_text_c(rb, lx, ly, line, COL_TEXT, FS);
|
||||
rb_text_c(rb, lx, ly, line, txt, FS);
|
||||
end_x = lx + lw;
|
||||
ly += SLOT_LINE_H;
|
||||
}
|
||||
if (editing) rb_fill_rect(rb, end_x + 1, ly - SLOT_LINE_H - 7, 1.5f, 14, COL_TEXT);
|
||||
if (editing) rb_fill_rect(rb, end_x + 1, ly - SLOT_LINE_H - 7, 1.5f, 14, txt);
|
||||
}
|
||||
|
||||
static void render_chain(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int head, float ox, float oy);
|
||||
|
||||
Reference in new issue
Block a user