From b29908e65b34c9851b532fa1487e59b613c36f0d Mon Sep 17 00:00:00 2001 From: mguinhos Date: Tue, 20 Oct 2026 20:36:14 -0300 Subject: [PATCH] Adiciona tinyblocks-environ e tela de Configuracoes (salvar/exportar CSV) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Makefile | 22 +- libraries/tinyblocks-editor/Makefile | 2 + libraries/tinyblocks-editor/src/tb_editor.c | 303 +++++++++++++++++- libraries/tinyblocks-environ/Makefile | 29 ++ .../tinyblocks-environ/include/tb_environ.h | 32 ++ libraries/tinyblocks-environ/library.toml | 15 + libraries/tinyblocks-environ/src/tb_environ.c | 197 ++++++++++++ tests/editor_harness.c | 24 ++ tests/environ_test.c | 97 ++++++ 9 files changed, 714 insertions(+), 7 deletions(-) create mode 100644 libraries/tinyblocks-environ/Makefile create mode 100644 libraries/tinyblocks-environ/include/tb_environ.h create mode 100644 libraries/tinyblocks-environ/library.toml create mode 100644 libraries/tinyblocks-environ/src/tb_environ.c create mode 100644 tests/environ_test.c diff --git a/Makefile b/Makefile index 1428f78..6fd3948 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ VR_DIR := $(LIBDIR)/tinyblocks-variables AP_DIR := $(LIBDIR)/tinyblocks-appearance PN_DIR := $(LIBDIR)/tinyblocks-pen FS_DIR := $(LIBDIR)/tinyblocks-fs +EN_DIR := $(LIBDIR)/tinyblocks-environ TOML_DIR := $(LIBDIR)/tinyblocks-toml JSON_DIR := $(LIBDIR)/tinyblocks-json XML_DIR := $(LIBDIR)/tinyblocks-xml @@ -57,7 +58,7 @@ SRCS := src/tinyblocks.c \ OBJS := $(patsubst src/%.c,$(BUILD)/%.o,$(SRCS)) OBJS_DBG := $(patsubst src/%.c,$(BUILD)/%-dbg.o,$(SRCS)) -CAT_LIBS := events control operators variables appearance pen fs +CAT_LIBS := events control operators variables appearance pen fs environ CAT_A := $(foreach l,$(CAT_LIBS),$(LIBDIR)/tinyblocks-$(l)/build/libtinyblocks-$(l).a) CAT_LDFLAGS := $(foreach l,$(CAT_LIBS),-ltinyblocks-$(l)) CAT_INCS := $(foreach l,$(CAT_LIBS),-I$(LIBDIR)/tinyblocks-$(l)/include) @@ -79,9 +80,10 @@ LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \ test-toml test-plugin test-blueprint \ category-libs category-sos libs \ lib-events lib-control lib-operators lib-variables lib-appearance lib-pen lib-fs \ + lib-environ \ lib-editor lib-graphics lib-sdl2 lib-python lib-mcp lib-toml \ lib-json lib-xml lib-csv lib-blueprint lib-api lib-libman \ - test-libman + test-libman test-environ all: static example ## Compila a biblioteca estática e o exemplo hello (padrão) @@ -175,7 +177,7 @@ lib-mcp: $(MAKE) -C $(MC_DIR) libs: static lib-events lib-control lib-operators lib-variables \ - lib-appearance lib-pen lib-fs lib-graphics lib-sdl2 lib-toml \ + lib-appearance lib-pen lib-fs lib-environ lib-graphics lib-sdl2 lib-toml \ lib-json lib-xml lib-csv lib-blueprint lib-api lib-libman lib-editor @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + api + libman + editor" @@ -212,7 +214,7 @@ EDITOR_HDRS := $(EDITOR_SRC)/tb_stage_widget.h $(ED_DIR)/include/tb_editor.h $(G $(BUILD)/editor: examples/editor/main.c static libs $(CC) $(CFLAGS_REL) \ -I$(ED_DIR)/include -I$(GFX_INC) $(CAT_INCS) \ - -I$(FS_DIR)/include -I$(TOML_DIR)/include \ + -I$(FS_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include \ -I$(JSON_DIR)/include -I$(XML_DIR)/include -I$(CSV_DIR)/include -I$(BP_DIR)/include \ -I$(API_DIR)/include -I$(LIBM_DIR)/include \ $< -o $@ \ @@ -287,7 +289,7 @@ $(BUILD)/editor_harness: tests/editor_harness.c $(SRCS) \ @mkdir -p $(BUILD) $(CC) $(CFLAGS_DBG) \ -I$(ED_DIR)/include -I$(EDITOR_SRC) -I$(GFX_INC) $(CAT_INCS) -I$(BP_DIR)/include \ - -I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(TOML_DIR)/include \ + -I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include \ $(SRCS) \ $(EDITOR_SRC)/tb_editor.c $(EDITOR_SRC)/tb_stage_widget.c \ $(GX_DIR)/src/tb_graphics.c \ @@ -338,6 +340,16 @@ $(BUILD)/libman_test: tests/libman_test.c \ test-libman: $(BUILD)/libman_test ## Roda o teste do libman/api (ASan/UBSan) ./$(BUILD)/libman_test +# -- Teste da library environ (store + blocos + CSV) --- +$(BUILD)/environ_test: tests/environ_test.c $(EN_DIR)/src/tb_environ.c static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(EN_DIR)/include $(EN_DIR)/src/tb_environ.c \ + tests/environ_test.c -o $@ \ + -Wl,--start-group $(LIB_A) -Wl,--end-group + +test-environ: $(BUILD)/environ_test ## Roda o teste da environ (ASan/UBSan) + ./$(BUILD)/environ_test + # -- Teste de plugin .so standalone --- FS_SO := $(FS_DIR)/build/libtinyblocks-fs.so diff --git a/libraries/tinyblocks-editor/Makefile b/libraries/tinyblocks-editor/Makefile index de99017..c637c9d 100644 --- a/libraries/tinyblocks-editor/Makefile +++ b/libraries/tinyblocks-editor/Makefile @@ -11,6 +11,7 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ -I../tinyblocks-graphics/include \ -I../tinyblocks-variables/include \ -I../tinyblocks-blueprint/include \ + -I../tinyblocks-environ/include \ -I../tinyblocks-api/include \ -I../tinyblocks-libman/include @@ -27,6 +28,7 @@ $(BUILD)/tb_editor.o: src/tb_editor.c include/tb_editor.h src/tb_stage_widget.h ../tinyblocks-graphics/include/tb_graphics.h \ ../tinyblocks-variables/include/tb_variables.h \ ../tinyblocks-blueprint/include/tb_blueprint.h \ + ../tinyblocks-environ/include/tb_environ.h \ ../tinyblocks-api/include/tb_api.h \ ../tinyblocks-libman/include/tb_libman.h @mkdir -p $(BUILD) diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 80f8ba3..365d79b 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -41,6 +41,7 @@ #include #include #include +#include #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; } diff --git a/libraries/tinyblocks-environ/Makefile b/libraries/tinyblocks-environ/Makefile new file mode 100644 index 0000000..133aed5 --- /dev/null +++ b/libraries/tinyblocks-environ/Makefile @@ -0,0 +1,29 @@ +# Makefile da library environ (categoria "Ambiente") +# Constroi build/libtinyblocks-environ.a e build/libtinyblocks-environ.so +CC := gcc +BUILD := build + +CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ + -D_POSIX_C_SOURCE=200809L -I../../include -Iinclude + +OBJ := $(BUILD)/tb_environ.o +LIB_A := $(BUILD)/libtinyblocks-environ.a +LIB_SO := $(BUILD)/libtinyblocks-environ.so + +.PHONY: all static shared clean +all: static shared +static: $(LIB_A) +shared: $(LIB_SO) + +$(OBJ): src/tb_environ.c include/tb_environ.h + @mkdir -p $(BUILD) + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +$(LIB_A): $(OBJ) + ar rcs $@ $^ + +$(LIB_SO): $(OBJ) + $(CC) -shared -o $@ $^ + +clean: + $(RM) -r $(BUILD) diff --git a/libraries/tinyblocks-environ/include/tb_environ.h b/libraries/tinyblocks-environ/include/tb_environ.h new file mode 100644 index 0000000..1049ad8 --- /dev/null +++ b/libraries/tinyblocks-environ/include/tb_environ.h @@ -0,0 +1,32 @@ +/* + * TinyBlocks - tb_environ.h + * Library tinyblocks-environ: variaveis de ambiente / configuracao + * (chave -> valor). Independente do editor. Registra a categoria "Ambiente". + * + * O store pode ser usado tambem como "settings" pelo editor: enumeravel e + * exportavel para CSV. + */ +#ifndef TINYBLOCKS_ENVIRON_H +#define TINYBLOCKS_ENVIRON_H + +#include + +/* -- Descriptor (para registrar os blocos no runtime) --- */ +TB_API const TinyBlocks_LibraryDescriptor* tb_environ_get_library(void); + +/* -- API C: store chave/valor (ordenado por insercao) --- */ +TB_API void tb_environ_reset(void); /* limpa tudo */ +TB_API int tb_environ_count(void); +TB_API const char* tb_environ_key(int i); /* NULL se fora do range */ +TB_API const char* tb_environ_value_at(int i); /* NULL se fora do range */ + +TB_API const char* tb_environ_get(const char* key); /* "" se ausente */ +TB_API bool tb_environ_has(const char* key); +TB_API void tb_environ_set(const char* key, const char* value); +TB_API void tb_environ_unset(const char* key); + +/* Exporta/enumera como CSV "chave,valor". Retorna false em erro de IO. */ +TB_API bool tb_environ_export_csv(const char* path); +TB_API const char* tb_environ_last_error(void); + +#endif /* TINYBLOCKS_ENVIRON_H */ diff --git a/libraries/tinyblocks-environ/library.toml b/libraries/tinyblocks-environ/library.toml new file mode 100644 index 0000000..3b4e1b5 --- /dev/null +++ b/libraries/tinyblocks-environ/library.toml @@ -0,0 +1,15 @@ +# Metadados da library environ. Lido pela UI/build sem recompilar o .so. +[library] +name = "tinyblocks-environ" +version = "1.0.0" +category = "Ambiente" +description = "Variaveis de ambiente e configuracao (chave, valor)" +color = [92, 107, 192, 255] +dark = [57, 73, 171, 255] + +[build] +sources = ["src/tb_environ.c"] +includes = ["include"] +shared = "build/libtinyblocks-environ.so" +static = "build/libtinyblocks-environ.a" +depends = [] diff --git a/libraries/tinyblocks-environ/src/tb_environ.c b/libraries/tinyblocks-environ/src/tb_environ.c new file mode 100644 index 0000000..bc22308 --- /dev/null +++ b/libraries/tinyblocks-environ/src/tb_environ.c @@ -0,0 +1,197 @@ +/* + * TinyBlocks - tb_environ.c + * Categoria "Ambiente": le/define variaveis de ambiente (chave -> valor). + * + * Mantem um store em memoria, ordenado por insercao, que serve de base para a + * tela de Configuracoes do editor (enumerar e salvar/exportar como CSV). + * `Environ.Get` consulta primeiro o store e, se nao houver, o ambiente real + * do processo (getenv). + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include + +#include +#include "tb_environ.h" + +#define ENV_MAX 256 + +typedef struct { char* key; char* value; } EnvPair; + +static EnvPair s_env[ENV_MAX]; +static int s_env_n = 0; +static char s_err[256] = ""; + +/* -- Store --------------------------------------------------------------- */ + +static char* dup_str(const char* s) { + if (!s) s = ""; + size_t n = strlen(s) + 1; + char* p = (char*)malloc(n); + if (p) memcpy(p, s, n); + return p; +} + +static int env_find(const char* key) { + if (!key) return -1; + for (int i = 0; i < s_env_n; i++) + if (strcmp(s_env[i].key, key) == 0) return i; + return -1; +} + +void tb_environ_reset(void) { + for (int i = 0; i < s_env_n; i++) { free(s_env[i].key); free(s_env[i].value); } + s_env_n = 0; +} + +int tb_environ_count(void) { return s_env_n; } + +const char* tb_environ_key(int i) { + return (i >= 0 && i < s_env_n) ? s_env[i].key : NULL; +} + +const char* tb_environ_value_at(int i) { + return (i >= 0 && i < s_env_n) ? s_env[i].value : NULL; +} + +bool tb_environ_has(const char* key) { + return env_find(key) >= 0; +} + +const char* tb_environ_get(const char* key) { + int i = env_find(key); + if (i >= 0) return s_env[i].value; + const char* v = key ? getenv(key) : NULL; + return v ? v : ""; +} + +void tb_environ_set(const char* key, const char* value) { + if (!key || !key[0]) return; + if (!value) value = ""; + int i = env_find(key); + if (i >= 0) { + free(s_env[i].value); + s_env[i].value = dup_str(value); + } else if (s_env_n < ENV_MAX) { + s_env[s_env_n].key = dup_str(key); + s_env[s_env_n].value = dup_str(value); + s_env_n++; + } + setenv(key, value, 1); /* mantem o ambiente do processo coerente */ +} + +void tb_environ_unset(const char* key) { + int i = env_find(key); + if (i < 0) return; + free(s_env[i].key); + free(s_env[i].value); + for (int j = i; j < s_env_n - 1; j++) s_env[j] = s_env[j + 1]; + s_env_n--; + if (key) unsetenv(key); +} + +const char* tb_environ_last_error(void) { return s_err; } + +/* Escreve um campo CSV: aspas quando contem virgula, aspas ou quebra. */ +static void csv_field(FILE* f, const char* s) { + if (!s) s = ""; + bool quote = strpbrk(s, ",\"\n\r") != NULL; + if (!quote) { fputs(s, f); return; } + fputc('"', f); + for (const char* p = s; *p; p++) { + if (*p == '"') fputc('"', f); + fputc(*p, f); + } + fputc('"', f); +} + +bool tb_environ_export_csv(const char* path) { + s_err[0] = 0; + if (!path || !path[0]) { snprintf(s_err, sizeof(s_err), "caminho vazio"); return false; } + FILE* f = fopen(path, "wb"); + if (!f) { snprintf(s_err, sizeof(s_err), "nao foi possivel abrir %s", path); return false; } + fputs("chave,valor\n", f); + for (int i = 0; i < s_env_n; i++) { + csv_field(f, s_env[i].key); + fputc(',', f); + csv_field(f, s_env[i].value); + fputc('\n', f); + } + fclose(f); + return true; +} + +/* -- 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_get(TinyBlocks_Block* self, TinyBlocks* rt) { + tb_block_set_output(self, 0, tb_value_text(tb_environ_get(in_text(self, rt, 0)))); + self->dirty = false; +} + +static void eval_set(TinyBlocks_Block* self, TinyBlocks* rt) { + tb_environ_set(in_text(self, rt, 0), in_text(self, rt, 1)); + self->dirty = false; +} + +static void eval_has(TinyBlocks_Block* self, TinyBlocks* rt) { + tb_block_set_output(self, 0, tb_value_integer(tb_environ_has(in_text(self, rt, 0)) ? 1 : 0)); + self->dirty = false; +} + +static void eval_unset(TinyBlocks_Block* self, TinyBlocks* rt) { + tb_environ_unset(in_text(self, rt, 0)); + 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 environ_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { + static const TinyBlocks_Category cat = { + "Ambiente", + { 92/255.f, 107/255.f, 192/255.f, 1.f}, + { 57/255.f, 73/255.f, 171/255.f, 1.f}, + }; + tb_library_set_category(lib, &cat); + + static const TinyBlocks_SlotDesc get_in[] = {{"nome", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc set_in[] = {{"nome", TB_VALUE_TEXT}, {"valor", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc has_in[] = {{"nome", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc unset_in[] = {{"nome", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc val_out[] = {{"valor", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc bool_out[] = {{"existe",TB_VALUE_INTEGER}}; + + return reg(rt, lib, "Environ.Get", get_in, 1, val_out, 1, eval_get) + && reg(rt, lib, "Environ.Set", set_in, 2, NULL, 0, eval_set) + && reg(rt, lib, "Environ.Has", has_in, 1, bool_out, 1, eval_has) + && reg(rt, lib, "Environ.Unset", unset_in, 1, NULL, 0, eval_unset); +} + +static void environ_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { + (void)rt; (void)lib; + /* Mantem o store: o editor pode continuar lendo as settings. */ +} + +TINYBLOCKS_DEFINE_LIBRARY(environ, + "tinyblocks-environ", + "1.0.0", + environ_init, + environ_deinit) + +const TinyBlocks_LibraryDescriptor* tb_environ_get_library(void) { + return &_tb_lib_desc_environ; +} diff --git a/tests/editor_harness.c b/tests/editor_harness.c index fd62760..8e68586 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -385,6 +385,30 @@ int main(void) { key(TB_KEY_ESCAPE, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); + /* 18. Tela de Configuracoes (settings da tinyblocks-environ) */ + remove("tinyblocks-settings.csv"); + click(1231, 22); /* botao Config (WIN_W=1280) */ + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(find("Configurações", R_ANY, 0, &x, &y), "botao Config abre a tela de configuracoes"); + CHECK(find("autor", R_ANY, 0, &x, &y), "listas as settings do store (autor)"); + click(200, 156); /* seleciona a linha 'autor' */ + type("maria"); + key(TB_KEY_RETURN, 0, 0); + frame(0, 0, 0, 0, 0, 0, NULL); + click(922, 96); /* botao Salvar (CSV) */ + frame(0, 0, 0, 0, 0, 0, NULL); + { + FILE* sf = fopen("tinyblocks-settings.csv", "rb"); + char sbuf[512] = ""; + if (sf) { size_t sn = fread(sbuf, 1, sizeof(sbuf)-1, sf); sbuf[sn] = 0; fclose(sf); } + CHECK(strstr(sbuf, "chave,valor") != NULL, "settings salvas em CSV (cabecalho)"); + CHECK(strstr(sbuf, "autor,maria") != NULL, "valor editado gravado no CSV"); + remove("tinyblocks-settings.csv"); + } + key(TB_KEY_ESCAPE, 0, 0); /* fecha a tela */ + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(!find("Configurações", R_ANY, 0, &x, &y), "Esc fecha as configuracoes"); + printf("comandos de desenho no último frame: %u\n", ncmd); tinyblocks_deinit(&rt); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); diff --git a/tests/environ_test.c b/tests/environ_test.c new file mode 100644 index 0000000..349a0ad --- /dev/null +++ b/tests/environ_test.c @@ -0,0 +1,97 @@ +/* + * environ_test.c - testa tinyblocks-environ: + * store chave/valor, getenv de fallback, exportacao CSV e os blocos + * Environ.Get / Environ.Set / Environ.Has / Environ.Unset. + */ +#include +#include +#include +#include +#include + +static int fails = 0; +#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0) + +static void set_def(TinyBlocks_Block* b, uint32_t i, const char* v) { + TinyBlocks_Value vv = tb_value_text(v); + tb_block_set_default(b, i, vv); + tb_value_free(&vv); +} + +int main(void) { + TinyBlocks* rt; + tinyblocks_init(&rt, NULL); + tb_library_register(rt, tb_environ_get_library()); + + /* -- Tipos registrados --- */ + CHECK(tb_rt_find_type(rt, "Environ.Get") != NULL, "bloco Environ.Get registrado"); + CHECK(tb_rt_find_type(rt, "Environ.Set") != NULL, "bloco Environ.Set registrado"); + CHECK(tb_rt_find_type(rt, "Environ.Has") != NULL, "bloco Environ.Has registrado"); + CHECK(tb_rt_find_type(rt, "Environ.Unset") != NULL, "bloco Environ.Unset registrado"); + + /* -- Store chave/valor --- */ + tb_environ_reset(); + CHECK(tb_environ_count() == 0, "store vazio apos reset"); + + tb_environ_set("a", "1"); + tb_environ_set("b", "dois"); + tb_environ_set("a", "3"); /* sobrescreve */ + CHECK(tb_environ_count() == 2, "set cria e sobrescreve sem duplicar"); + CHECK(strcmp(tb_environ_get("a"), "3") == 0, "get le valor sobrescrito"); + CHECK(strcmp(tb_environ_get("b"), "dois") == 0, "get le segundo valor"); + CHECK(strcmp(tb_environ_key(1), "b") == 0, "ordem de insercao preservada"); + CHECK(tb_environ_has("b") && !tb_environ_has("z"), "has distingue presente/ausente"); + + /* Fallback para o ambiente real do processo */ + const char* home = getenv("HOME"); + if (home) CHECK(strcmp(tb_environ_get("HOME"), home) == 0, "get faz fallback para getenv"); + + tb_environ_unset("b"); + CHECK(tb_environ_count() == 1, "unset remove a chave"); + + /* -- Exportacao CSV (com campo que precisa de aspas) --- */ + tb_environ_set("com,quota", "ele disse \"oi\""); + const char* path = "build/environ_test.csv"; + CHECK(tb_environ_export_csv(path), "export_csv grava o arquivo"); + + FILE* f = fopen(path, "rb"); + char buf[512] = ""; + if (f) { size_t n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = 0; fclose(f); } + CHECK(strstr(buf, "chave,valor") != NULL, "CSV tem cabecalho chave,valor"); + CHECK(strstr(buf, "\"com,quota\"") != NULL, "campo com virgula vem entre aspas"); + CHECK(strstr(buf, "\"com,quota\",\"ele disse \"\"oi\"\"\"") != NULL, "CSV escapa aspas corretamente"); + remove(path); + + /* -- Blocos --- */ + TinyBlocks_TypeBlock* tset = tb_rt_find_type(rt, "Environ.Set"); + TinyBlocks_Block* bset = tb_block_create(rt, tset); + set_def(bset, 0, "nome_do_bloco"); + set_def(bset, 1, "TinyBlocks"); + tb_block_eval(bset, rt); + + TinyBlocks_TypeBlock* tget = tb_rt_find_type(rt, "Environ.Get"); + TinyBlocks_Block* bget = tb_block_create(rt, tget); + set_def(bget, 0, "nome_do_bloco"); + tb_block_eval(bget, rt); + CHECK(bget->outputs[0].kind == TB_VALUE_TEXT && + strcmp(bget->outputs[0].as.text, "TinyBlocks") == 0, + "Environ.Set -> Environ.Get transporta o valor"); + + TinyBlocks_TypeBlock* thas = tb_rt_find_type(rt, "Environ.Has"); + TinyBlocks_Block* bhas = tb_block_create(rt, thas); + set_def(bhas, 0, "nome_do_bloco"); + tb_block_eval(bhas, rt); + CHECK(bhas->outputs[0].as.integer == 1, "Environ.Has retorna 1 para chave existente"); + + TinyBlocks_TypeBlock* tunset = tb_rt_find_type(rt, "Environ.Unset"); + TinyBlocks_Block* bunset = tb_block_create(rt, tunset); + set_def(bunset, 0, "nome_do_bloco"); + tb_block_eval(bunset, rt); + CHECK(!tb_environ_has("nome_do_bloco"), "Environ.Unset remove a chave"); + + tinyblocks_deinit(&rt); + + if (fails) printf("\n%d FALHA(S)\n", fails); + else printf("\nTUDO OK\n"); + return fails != 0; +}