Adiciona tinyblocks-environ e tela de Configuracoes (salvar/exportar CSV)

tinyblocks-environ (categoria "Ambiente"):
- blocos Environ.Get/Set/Has/Unset (store chave->valor; Get cai no getenv).
- API C enumeravel (count/key/value_at/get/has/set/unset) e
  tb_environ_export_csv(path) com escape de CSV; tb_environ_reset.
- library.toml com metadados; Makefile proprio (.a/.so).

Editor:
- biblioteca de settings no store da environ, com valores iniciais
  (autor, projeto, tema, escala, autosave).
- tela "Configurações" (botao Config no menu): lista as chaves, edita o
  valor da linha (Enter confirma, Esc cancela), "+ Nova" cria chave,
  Delete remove, campo de caminho + botoes "Salvar" e "Exportar CSV".
- Ctrl/scroll e foco de texto integrados ao mesmo fluxo do libman.

Build/testes:
- environ entra em CAT_LIBS (compila no editor, no harness e no libman).
- make test-environ (store, fallback getenv, escape CSV, blocos).
- harness: passo 18 cobre abrir Configuracoes, editar valor, salvar CSV
  e verificar o conteudo; Esc fecha.
This commit is contained in:
mguinhos committed 2026-10-20 20:36:14 -03:00
1 parent 3120d2d797
commit b29908e65b
9 files changed
+714 -7

No files matched your search

+301 -2
View File
@@ -41,6 +41,7 @@
#include <tb_blueprint.h>
#include <tb_api.h>
#include <tb_libman.h>
#include <tb_environ.h>
#include "tb_stage_widget.h"
#include "../include/tb_editor.h"
@@ -98,6 +99,10 @@ TB_API void tb_editor_preload(const char* path) {
#define LIB_BTN_X 500
#define LIB_BTN_W 200
/* Botao "Config" (settings) ancorado a direita do menu */
#define CFG_BTN_W 78
#define CFG_BTN_X (WIN_W - CFG_BTN_W - 10)
/* Botao de buscar/adicionar library no fim da coluna de categorias */
#define LIBMAN_BTN_H 56
@@ -429,6 +434,18 @@ typedef struct {
int libman_scroll;
int libman_hover;
/* Configuracoes (settings) - store da tinyblocks-environ */
bool settings_open;
int settings_sel; /* linha selecionada (-1 = nenhuma) */
bool settings_path_focus; /* campo de caminho do CSV em foco */
bool settings_value_edit; /* editando o valor da linha sel. */
bool settings_new_key; /* digitando o nome de uma nova chave */
char settings_path[256];
char settings_edit[256]; /* buffer do valor em edicao */
char settings_key[64]; /* buffer da nova chave */
int settings_scroll;
int settings_hover;
int win_w, win_h;
} Ed;
@@ -456,6 +473,14 @@ static void libman_scroll_by(Ed* ed, float dy);
static void libman_render(Ed* ed, TinyBlocks_RenderableBlock* rb);
static bool hit_libman_foot(Ed* ed, float mx, float my);
/* Configuracoes (settings) - UI desenhada pelo editor */
static void settings_show(Ed* ed);
static void settings_click(Ed* ed, float mx, float my);
static void settings_key(Ed* ed, int sc, bool ctrl);
static void settings_text(Ed* ed, const char* typed);
static void settings_scroll_by(Ed* ed, float dy);
static void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb);
/* ===
* Helpers de desenho - emitem TinyBlocks_DrawCmd em um RenderableBlock
* === */
@@ -2512,6 +2537,7 @@ static bool hit_open_btn(float mx, float my);
static bool hit_save_btn(float mx, float my);
static bool hit_export_btn(float mx, float my);
static bool hit_libman_btn(float mx, float my);
static bool hit_settings_btn(Ed* ed, float mx, float my);
static void menu_button(TinyBlocks_RenderableBlock* rb, float x, float w, const char* label, bool active) {
rb_fill_rrect(rb, x, 7, w, MENU_H - 14, 6, active ? COL_MENU_DARK : COLA(255, 255, 255, 34));
@@ -2528,6 +2554,7 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || hit_save_btn(mx, my));
menu_button(rb, EXP_BTN_X, EXP_BTN_W, "Exportar", (ed->ctx_menu_open && ed->ctx_export) || hit_export_btn(mx, my));
menu_button(rb, LIBM_BTN_X, LIBM_BTN_W, "Bibliotecas", ed->libman_open || hit_libman_btn(mx, my));
menu_button(rb, CFG_BTN_X, CFG_BTN_W, "Config", ed->settings_open || hit_settings_btn(ed, mx, my));
if (ed->lib_hover || (active && ed->path_mode == PM_LIB))
rb_fill_rect(rb, LIB_BTN_X, 0, LIB_BTN_W, MENU_H, COL_MENU_DARK);
@@ -2549,7 +2576,7 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
rb_fill_rect(rb, bx + 11 + cw, MENU_H*0.5f - 8, 1.5f, 16, COL_TEXT);
} else if (ed->hot_status[0]) {
float w = tw(ed->hot_status, FS_SM);
rb_text_c(rb, WIN_W - 16 - w, MENU_H*0.5f, ed->hot_status,
rb_text_c(rb, CFG_BTN_X - 12 - w, MENU_H*0.5f, ed->hot_status,
ed->hot_status_ok ? COL_WHITE : COL_ERR_MENU, FS_SM);
}
}
@@ -3079,6 +3106,259 @@ static void libman_scroll_by(Ed* ed, float dy) {
if (ed->libman_scroll < 0) ed->libman_scroll = 0;
}
/* ===
* Configuracoes (settings) - store da tinyblocks-environ, salvo/exportado em CSV
* === */
#define ST_HEAD_H 64
#define ST_TOOL_Y ST_HEAD_H
#define ST_TOOL_H 64
#define ST_MARGIN 40
#define ST_ROW_H 40
#define ST_LIST_Y (ST_TOOL_Y + ST_TOOL_H + 6)
#define ST_BTN_H 40
static void settings_show(Ed* ed) {
ed->settings_open = true;
ed->settings_sel = -1;
ed->settings_path_focus = false;
ed->settings_value_edit = false;
ed->settings_new_key = false;
ed->settings_scroll = 0;
ed->settings_hover = -1;
if (!ed->settings_path[0]) strcpy(ed->settings_path, "tinyblocks-settings.csv");
ed->libman_open = false;
ed->path_input_active = false;
ed->ctx_menu_open = false;
}
static void settings_close(Ed* ed) {
ed->settings_open = false;
ed->settings_value_edit = false;
ed->settings_new_key = false;
ed->settings_path_focus = false;
}
static float settings_btn_w(int i) { return i == 1 ? 150.f : 96.f; }
static void settings_btn_rect(Ed* ed, int i, float* x, float* y, float* w, float* h) {
float gap = 12.f, cur = WIN_W - ST_MARGIN;
float pos[3];
for (int k = 2; k >= 0; k--) { cur -= settings_btn_w(k); pos[k] = cur; cur -= gap; }
*x = pos[i]; *y = ST_TOOL_Y + 12.f; *w = settings_btn_w(i); *h = ST_BTN_H;
}
static void settings_path_rect(Ed* ed, float* x, float* y, float* w, float* h) {
float bx; settings_btn_rect(ed, 0, &bx, y, w, h);
*x = ST_MARGIN; *w = bx - 20.f - ST_MARGIN;
}
static void st_button(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h,
const char* label, bool active) {
rb_fill_rrect(rb, x, y, w, h, 6, active ? COL_MENU_DARK : COL_MENU);
rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + h * 0.5f, label, COL_WHITE, FS_UI);
}
static void settings_export(Ed* ed, const char* path) {
bool ok = tb_environ_export_csv(path);
if (ok) snprintf(ed->hot_status, sizeof(ed->hot_status),
"Configuracoes salvas (CSV): %.200s", path);
else snprintf(ed->hot_status, sizeof(ed->hot_status),
"Falha ao salvar: %.200s", tb_environ_last_error());
ed->hot_status_ok = ok;
}
/* Rect da linha i (com o deslocamento da linha "nova chave", se ativa). */
static void settings_row_rect(Ed* ed, int i, float* x, float* y, float* w, float* h) {
int off = ed->settings_new_key ? 1 : 0;
*x = ST_MARGIN; *y = ST_LIST_Y + (i + off) * ST_ROW_H - ed->settings_scroll;
*w = WIN_W - 2.f * ST_MARGIN; *h = ST_ROW_H;
}
static void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (!ed->settings_open) return;
int n = tb_environ_count();
rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COL_UI_BG);
/* Cabecalho */
rb_fill_rect(rb, 0, 0, WIN_W, ST_HEAD_H, COL_MENU);
float bx = ST_MARGIN, by = ST_HEAD_H * 0.5f;
rb_fill_circle(rb, bx, by, 16, COLA(255,255,255,40));
rb_line(rb, bx+4, by-7, bx-5, by, 2.f, COL_WHITE);
rb_line(rb, bx-5, by, bx+4, by+7, 2.f, COL_WHITE);
rb_text_c(rb, bx + 28, by, "Voltar", COL_WHITE, FS_UI);
{ const char* t = "Configurações";
rb_text_c(rb, (WIN_W - tw(t, 20)) * 0.5f, by, t, COL_WHITE, 20); }
/* Barra de ferramentas */
rb_fill_rect(rb, 0, ST_TOOL_Y, WIN_W, ST_TOOL_H, COL_PANEL);
{
float px, py, pw, ph; settings_path_rect(ed, &px, &py, &pw, &ph);
rb_fill_rrect(rb, px, py, pw, ph, 6, COL_WHITE);
if (ed->settings_path_focus) rb_stroke_rect(rb, px-1, py-1, pw+2, ph+2, 2.f, COL_MENU);
rb_text_c(rb, px + 12, py + ph*0.5f, ed->settings_path, COL_TEXT, FS_UI);
if (ed->settings_path_focus) {
float cw = tw(ed->settings_path, FS_UI);
rb_fill_rect(rb, px + 13 + cw, py + ph*0.5f - 9, 1.5f, 18, COL_TEXT);
}
}
for (int i = 0; i < 3; i++) {
float x, y, w, h; settings_btn_rect(ed, i, &x, &y, &w, &h);
bool hov = in_rect(ed->mouse_x, ed->mouse_y, x, y, w, h);
st_button(rb, x, y, w, h,
i == 0 ? "Salvar" : i == 1 ? "Exportar CSV" : "+ Nova", hov);
}
/* Lista */
rb_fill_rect(rb, 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y, COL_UI_BG);
clip_set(ed, rb, 0, ST_LIST_Y, WIN_W, WIN_H - ST_LIST_Y);
if (ed->settings_new_key) {
float y = ST_LIST_Y - ed->settings_scroll;
rb_fill_rrect(rb, ST_MARGIN, y + 2, WIN_W - 2*ST_MARGIN, ST_ROW_H - 4, 6, COL_PANEL);
rb_text_c(rb, ST_MARGIN + 16, y + ST_ROW_H*0.5f, "+", COL_MENU, FS_UI);
const char* t = ed->settings_key[0] ? ed->settings_key : "nome da chave (Enter confirma, Esc cancela)";
rb_text_c(rb, ST_MARGIN + 40, y + ST_ROW_H*0.5f, t,
ed->settings_key[0] ? COL_TEXT : COL_TEXT_DIM, FS_UI);
if (ed->settings_key[0]) {
float cw = tw(ed->settings_key, FS_UI);
rb_fill_rect(rb, ST_MARGIN + 41 + cw, y + ST_ROW_H*0.5f - 9, 1.5f, 18, COL_TEXT);
}
}
for (int i = 0; i < n; i++) {
float x, y, w, h; settings_row_rect(ed, i, &x, &y, &w, &h);
if (y + h < ST_LIST_Y || y > WIN_H) continue;
bool sel = (i == ed->settings_sel);
rb_fill_rrect(rb, x, y + 2, w, h - 4, 6, sel ? COL_PANEL : COL_FLY);
if (sel && !ed->settings_value_edit) rb_stroke_rect(rb, x, y+1, w, h-2, 2.f, COL_MENU);
rb_text_c(rb, x + 16, y + h*0.5f, tb_environ_key(i), COL_TEXT, FS_UI);
float vx = x + 16 + tw("", FS_UI) + 260.f;
if (vx > x + w - 260.f) vx = x + w - 260.f;
if (sel && ed->settings_value_edit) {
rb_fill_rrect(rb, vx - 6, y + 6, w - (vx - x) - 4, h - 12, 4, COL_WHITE);
rb_text_c(rb, vx, y + h*0.5f, ed->settings_edit, COL_TEXT, FS_UI);
float cw = tw(ed->settings_edit, FS_UI);
rb_fill_rect(rb, vx + 1 + cw, y + h*0.5f - 9, 1.5f, 18, COL_TEXT);
} else {
rb_text_c(rb, vx, y + h*0.5f, tb_environ_value_at(i), COL_TEXT_DIM, FS_UI);
}
}
clip_clear(ed, rb);
if (n == 0 && !ed->settings_new_key)
rb_text_c(rb, ST_MARGIN, ST_LIST_Y + 24,
"Nenhuma configuracao. Use \"+ Nova\".", COL_TEXT_DIM, FS_UI);
if (ed->hot_status[0])
rb_text_c(rb, ST_MARGIN, WIN_H - 22, ed->hot_status,
ed->hot_status_ok ? COL_TEXT_DIM : COL_ERR_MENU, FS_SM);
}
static void settings_click(Ed* ed, float mx, float my) {
if (my < ST_HEAD_H) { if (mx < 170) settings_close(ed); return; }
if (my < ST_TOOL_Y + ST_TOOL_H) {
float px, py, pw, ph; settings_path_rect(ed, &px, &py, &pw, &ph);
if (in_rect(mx, my, px, py, pw, ph)) {
ed->settings_path_focus = true; ed->settings_value_edit = false; ed->settings_new_key = false;
return;
}
for (int i = 0; i < 3; i++) {
float x, y, w, h; settings_btn_rect(ed, i, &x, &y, &w, &h);
if (!in_rect(mx, my, x, y, w, h)) continue;
ed->settings_path_focus = false;
if (i == 0) settings_export(ed, "tinyblocks-settings.csv");
else if (i == 1) settings_export(ed, ed->settings_path);
else { ed->settings_new_key = true; ed->settings_key[0] = 0;
ed->settings_value_edit = false; ed->settings_sel = -1;
ed->settings_scroll = 0; }
return;
}
ed->settings_path_focus = false;
return;
}
if (ed->settings_new_key && in_rect(mx, my, ST_MARGIN, ST_LIST_Y - ed->settings_scroll,
WIN_W - 2*ST_MARGIN, ST_ROW_H)) return;
int n = tb_environ_count();
for (int i = 0; i < n; i++) {
float x, y, w, h; settings_row_rect(ed, i, &x, &y, &w, &h);
if (!in_rect(mx, my, x, y, w, h)) continue;
ed->settings_sel = i;
ed->settings_new_key = false;
ed->settings_path_focus = false;
ed->settings_value_edit = true;
const char* v = tb_environ_value_at(i);
strncpy(ed->settings_edit, v ? v : "", sizeof(ed->settings_edit) - 1);
ed->settings_edit[sizeof(ed->settings_edit) - 1] = 0;
return;
}
ed->settings_sel = -1;
ed->settings_value_edit = false;
}
static void settings_key(Ed* ed, int sc, bool ctrl) {
(void)ctrl;
bool enter = sc == TB_KEY_RETURN;
if (sc == TB_KEY_ESCAPE) {
if (ed->settings_new_key) { ed->settings_new_key = false; ed->settings_key[0] = 0; }
else if (ed->settings_value_edit) ed->settings_value_edit = false;
else settings_close(ed);
return;
}
if (ed->settings_new_key) {
if (enter && ed->settings_key[0]) {
tb_environ_set(ed->settings_key, "");
ed->settings_new_key = false;
ed->settings_sel = tb_environ_count() - 1;
ed->settings_value_edit = true;
ed->settings_edit[0] = 0;
ed->settings_scroll = 0;
} else if (sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->settings_key);
}
return;
}
if (ed->settings_value_edit && ed->settings_sel >= 0) {
if (enter) {
const char* k = tb_environ_key(ed->settings_sel);
if (k) tb_environ_set(k, ed->settings_edit);
ed->settings_value_edit = false;
} else if (sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->settings_edit);
}
return;
}
if (ed->settings_path_focus && sc == TB_KEY_BACKSPACE) {
utf8_pop(ed->settings_path);
return;
}
if (sc == TB_KEY_DELETE && ed->settings_sel >= 0) {
const char* k = tb_environ_key(ed->settings_sel);
if (k) tb_environ_unset(k);
ed->settings_sel = -1;
}
}
static void settings_text(Ed* ed, const char* typed) {
if (ed->settings_new_key) append_text(ed->settings_key, sizeof(ed->settings_key), typed);
else if (ed->settings_value_edit) append_text(ed->settings_edit, sizeof(ed->settings_edit), typed);
else if (ed->settings_path_focus) append_text(ed->settings_path, sizeof(ed->settings_path), typed);
}
static void settings_scroll_by(Ed* ed, float dy) {
ed->settings_scroll -= (int)(dy * 48.f);
int off = ed->settings_new_key ? 1 : 0;
int content = (tb_environ_count() + off) * ST_ROW_H + 24;
int visible = (int)(WIN_H - ST_LIST_Y);
int maxs = content - visible;
if (maxs < 0) maxs = 0;
if (ed->settings_scroll > maxs) ed->settings_scroll = maxs;
if (ed->settings_scroll < 0) ed->settings_scroll = 0;
}
/* ===
* Arrastar e encaixar
* === */
@@ -3227,6 +3507,7 @@ static bool hit_open_btn(float mx, float my) { return in_rect(mx, my, OPEN_B
static bool hit_save_btn(float mx, float my) { return in_rect(mx, my, SAVE_BTN_X, 0, SAVE_BTN_W, MENU_H); }
static bool hit_export_btn(float mx, float my) { return in_rect(mx, my, EXP_BTN_X, 0, EXP_BTN_W, MENU_H); }
static bool hit_libman_btn(float mx, float my) { return in_rect(mx, my, LIBM_BTN_X, 0, LIBM_BTN_W, MENU_H); }
static bool hit_settings_btn(Ed* ed, float mx, float my) { return in_rect(mx, my, CFG_BTN_X, 0, CFG_BTN_W, MENU_H); }
static void start_path(Ed* ed, int mode, const char* def) {
ed->path_input_active = true;
@@ -3320,6 +3601,7 @@ static void ctx_exec(Ed* ed, int id) {
}
static void on_lmb_down(Ed* ed, float mx, float my) {
if (ed->settings_open) { settings_click(ed, mx, my); return; }
if (ed->libman_open) { libman_click(ed, mx, my); return; }
if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; }
if (ed->ctx_menu_open) {
@@ -3338,6 +3620,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
else if (hit_open_btn(mx, my)) start_path(ed, PM_LOAD, "projeto.json");
else if (hit_save_btn(mx, my)) start_path(ed, PM_SAVE, "projeto.json");
else if (hit_libman_btn(mx, my)) libman_show(ed);
else if (hit_settings_btn(ed, mx, my)) settings_show(ed);
else if (hit_export_btn(mx, my)) {
ed->ctx_n = 0;
ed->ctx_items[ed->ctx_n++] = CM_EXP_JSON;
@@ -3473,6 +3756,7 @@ static void on_rmb_down(Ed* ed, float mx, float my) {
static void on_key(Ed* ed, int sc, bool ctrl) {
bool enter = sc == TB_KEY_RETURN;
if (ed->settings_open) { settings_key(ed, sc, ctrl); return; }
if (ed->libman_open) { libman_key(ed, sc); return; }
if (ed->comp_dialog_open) { dlg_key(ed, sc); return; }
if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; }
@@ -3527,6 +3811,7 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
}
static void on_text(Ed* ed, const char* typed) {
if (ed->settings_open) { settings_text(ed, typed); return; }
if (ed->libman_open) { libman_text(ed, typed); return; }
if (ed->comp_dialog_open) { dlg_text(ed, typed); return; }
if (ed->path_input_active) {
@@ -3589,7 +3874,9 @@ static void editor_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
double sdx = ev_real(evb, GFX_IN_SCROLL_DX);
double sdy = ev_real(evb, GFX_IN_SCROLL_DY);
if (sdx != 0.0 || sdy != 0.0) {
if (ed->libman_open) {
if (ed->settings_open) {
settings_scroll_by(ed, (float)sdy);
} else if (ed->libman_open) {
libman_scroll_by(ed, (float)sdy);
} else if (mx >= WS_X && mx < WIN_W - STAGE_W && my >= WS_Y) {
CT.ws_sx -= (float)sdx * 40.f;
@@ -3655,6 +3942,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
render_ctx_menu(ed, rb);
dlg_render(ed, rb);
libman_render(ed, rb); /* tela cheia: por cima de tudo */
settings_render(ed, rb);
*dst = rb;
return true;
@@ -3709,6 +3997,8 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
ed->bubble_bi = -1;
ed->ctx_menu_bi = -1;
ed->path_mode = PM_NONE;
ed->settings_sel = -1;
strcpy(ed->settings_path, "tinyblocks-settings.csv");
ed->tab_n = 1;
ed->cur_tab = 0;
@@ -3732,6 +4022,15 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
for (int i = 0; i < s_preload_n; i++) hotlib_load(ed, s_preload[i]);
s_preload_n = 0;
seed_example(ed);
/* Configuracoes iniciais (store da tinyblocks-environ) */
if (tb_environ_count() == 0) {
tb_environ_set("autor", "");
tb_environ_set("projeto", "sem_titulo");
tb_environ_set("tema", "claro");
tb_environ_set("escala", "1.0");
tb_environ_set("autosave", "0");
}
return true;
}