Divide as seções da paleta em libraries de categoria
Cada seção (Eventos, Controle, Operadores, Variáveis, Aparência, Caneta) virou uma library própria, com seus blocos e eval. Foi adicionado um metadado opcional de categoria (TinyBlocks_Category) para a UI agrupar a paleta a partir das libraries registradas, sem hardcode no editor. - núcleo: tb_library_set_category() e campo category em LibraryBlock - editor: cats_rebuild/cat_of usam a library dona de cada TypeBlock; "Meus Blocos" (User./Port.) continua no editor - variáveis: armazenamento movido para tinyblocks-variables, com API de enumeração (tb_variables_count/name/value/reset_blocks) - palco: Editor.Stage continua no editor; a API de desenho (tb_stage_*) é usada pela library tinyblocks-pen - host/tests/Makefile: registram e linkam as novas libraries
This commit is contained in:
1 parent
278d3f4473
commit
1cc0ff9d02
20 files changed
+1063
-597
No files matched your search
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* TinyBlocks - tb_variables.c
|
||||
* Categoria "Variáveis": mude / adicione / valor de <variável>.
|
||||
* A library é dona do armazenamento de variáveis e o expõe para monitores.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_variables.h"
|
||||
|
||||
#define MAX_VARS 64
|
||||
|
||||
typedef struct { char name[32]; TinyBlocks_Value val; } Var;
|
||||
|
||||
static Var s_vars[MAX_VARS];
|
||||
static int s_n = 0;
|
||||
|
||||
static int var_find(const char* n, bool create) {
|
||||
for (int i = 0; i < s_n; i++)
|
||||
if (strcmp(s_vars[i].name, n) == 0) return i;
|
||||
if (!create || s_n >= MAX_VARS) return -1;
|
||||
int i = s_n++;
|
||||
strncpy(s_vars[i].name, n, 31); s_vars[i].name[31] = 0;
|
||||
s_vars[i].val = tb_value_integer(0);
|
||||
return i;
|
||||
}
|
||||
|
||||
static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i,
|
||||
char* out, size_t cap) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
/* value_to_str reduzido */
|
||||
switch (v.kind) {
|
||||
case TB_VALUE_INTEGER: snprintf(out, cap, "%lld", (long long)v.as.integer); break;
|
||||
case TB_VALUE_REAL: snprintf(out, cap, "%g", v.as.real); break;
|
||||
case TB_VALUE_TEXT: snprintf(out, cap, "%s", v.as.text ? v.as.text : ""); break;
|
||||
default: out[0] = 0; break;
|
||||
}
|
||||
}
|
||||
|
||||
static void eval_var_get(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
||||
int i = var_find(name, false);
|
||||
tb_block_set_output(self, 0, i >= 0 ? tb_value_clone(s_vars[i].val)
|
||||
: tb_value_integer(0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_var_set(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
||||
int i = var_find(name, true);
|
||||
if (i >= 0) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 1);
|
||||
tb_value_free(&s_vars[i].val);
|
||||
s_vars[i].val = tb_value_clone(v);
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
||||
int i = var_find(name, true);
|
||||
if (i >= 0) {
|
||||
TinyBlocks_Value by = tb_block_get_input(self, rt, 1);
|
||||
TinyBlocks_Value* cur = &s_vars[i].val;
|
||||
double a = 0, d = 0;
|
||||
if (cur->kind == TB_VALUE_INTEGER) a = (double)cur->as.integer;
|
||||
else if (cur->kind == TB_VALUE_REAL) a = cur->as.real;
|
||||
else if (cur->kind == TB_VALUE_TEXT && cur->as.text) a = strtod(cur->as.text, NULL);
|
||||
if (by.kind == TB_VALUE_INTEGER) d = (double)by.as.integer;
|
||||
else if (by.kind == TB_VALUE_REAL) d = by.as.real;
|
||||
else if (by.kind == TB_VALUE_TEXT && by.as.text) d = strtod(by.as.text, NULL);
|
||||
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
|
||||
? tb_value_integer((long long)llround(a + d))
|
||||
: tb_value_real(a + d);
|
||||
tb_value_free(cur);
|
||||
*cur = nv;
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* == API de enumeração === */
|
||||
int tb_variables_count(void) { return s_n; }
|
||||
|
||||
const char* tb_variables_name(int i) {
|
||||
return (i >= 0 && i < s_n) ? s_vars[i].name : "";
|
||||
}
|
||||
|
||||
const TinyBlocks_Value* tb_variables_value(int i) {
|
||||
return (i >= 0 && i < s_n) ? &s_vars[i].val : NULL;
|
||||
}
|
||||
|
||||
void tb_variables_reset_blocks(void) {
|
||||
for (int v = 0; v < s_n; v++)
|
||||
if (s_vars[v].val.kind == TB_VALUE_BLOCK) {
|
||||
tb_value_free(&s_vars[v].val);
|
||||
s_vars[v].val = tb_value_integer(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* == Registro === */
|
||||
static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Variáveis",
|
||||
{255/255.f, 140/255.f, 26/255.f, 1.f},
|
||||
{219/255.f, 110/255.f, 0/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc var_in[] = {{"variável", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc var_set[] = {{"variável", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||
|
||||
TinyBlocks_TypeBlock* t;
|
||||
t = tb_type_block_create(rt, "Variables.Set", var_set, 2, NULL, 0, eval_var_set);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
t = tb_type_block_create(rt, "Variables.Change", var_set, 2, NULL, 0, eval_var_change);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
t = tb_type_block_create(rt, "Variables.Get", var_in, 1, any_out, 1, eval_var_get);
|
||||
if (!t || !tb_library_add(lib, &t->base)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void variables_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
for (int i = 0; i < s_n; i++) tb_value_free(&s_vars[i].val);
|
||||
s_n = 0;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(variables,
|
||||
"tinyblocks-variables",
|
||||
"1.0.0",
|
||||
variables_init,
|
||||
variables_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_variables_get_library(void) {
|
||||
return &_tb_lib_desc_variables;
|
||||
}
|
||||
Reference in new issue
Block a user