Prévia do scratchblocks antes de exportar, copiar para o fórum e colar

- "Exportar scratchblocks" abre uma prévia do texto (rolável) com Copiar
  (já com [scratchblocks]...[/scratchblocks]), Salvar... e Fechar.
- "Colar scratchblocks": um script copiado do fórum vira uma aba nova.
- graphics: área de transferência no contrato (tb_graphics_set_clipboard /
  tb_graphics_get_clipboard); o SDL2 usa a do sistema e o backend nulo,
  uma memória interna (testável no harness).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 18:06:46 -03:00
1 parent d842890b0b
commit d9449a568e
6 files changed
+251 -11

No files matched your search

@@ -593,7 +593,7 @@ typedef struct { FlyKind kind; int cat; int bi; float y, h; } FlyItem;
typedef enum { ES_IDLE, ES_PRESS, ES_DRAG, ES_PAN } EdSt;
typedef enum { SN_NONE, SN_NEXT, SN_SUB, SN_ABOVE, SN_ARG } SnapKind;
enum { CM_DUPLICATE, CM_DELETE, CM_EDIT_DEF, CM_CLEANUP, CM_NEW_COMPOSITE,
CM_EXP_JSON, CM_EXP_XML, CM_EXP_CSV, CM_EXP_SB };
CM_EXP_JSON, CM_EXP_XML, CM_EXP_CSV, CM_EXP_SB, CM_PASTE_SB };
/* Modos da barra de caminho do menu (path_input) */
enum { PM_NONE, PM_LIB, PM_LOAD, PM_SAVE, PM_EXP_JSON, PM_EXP_XML, PM_EXP_CSV,
@@ -887,6 +887,11 @@ typedef struct Ed {
bool tab_drag; /* passou do limiar: esta sendo arrastada */
float tab_press_x, tab_press_y, tab_grab_dx;
/* Previa da exportacao scratchblocks (copiar para o forum / salvar) */
bool sbp_open;
char* sbp_text;
int sbp_scroll;
/* Abas destacadas */
TabWin twin[TABWIN_MAX];
int tabctx; /* janela de aba ativa (-1 = principal) */
+190 -10
View File
@@ -2637,6 +2637,8 @@ static void sb_export_file(Ed* ed, const char* path) {
tb_blueprint_free(bp);
}
static bool sb_import_text(Ed* ed, const char* text, const char* name);
static bool sb_import_file(Ed* ed, const char* path) {
FILE* f = fopen(path, "rb");
char* text = NULL;
@@ -2648,23 +2650,29 @@ static bool sb_import_file(Ed* ed, const char* path) {
if (text) { size_t r = fread(text, 1, (size_t)n, f); text[r] = 0; }
fclose(f);
}
TbBpTab* B = text ? calloc(1, sizeof(TbBpTab)) : NULL;
if (!B) {
if (!text) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Não consegui ler %.200s", path);
ed->hot_status_ok = false;
free(text);
return false;
}
const char* base = strrchr(path, '/');
bool ok = sb_import_text(ed, text, base ? base + 1 : path);
free(text);
return ok;
}
/* Texto em scratchblocks -> aba nova chamada `name` */
static bool sb_import_text(Ed* ed, const char* text, const char* name) {
TbBpTab* B = calloc(1, sizeof(TbBpTab));
if (!B) return false;
TbSbLabel* labels = NULL;
TbSbContext cx = sb_context(ed, &labels);
char warn[200] = "";
int n = tb_scratchblocks_import(text, B, &cx, warn, sizeof(warn));
free(labels);
free(text);
const char* base = strrchr(path, '/');
base = base ? base + 1 : path;
const char* base = name;
if (n <= 0 || !tab_new_kind(ed, base, false, TK_BLOCKS)) {
if (n <= 0) snprintf(ed->hot_status, sizeof(ed->hot_status), "Nenhum script em %.200s", path);
if (n <= 0) snprintf(ed->hot_status, sizeof(ed->hot_status), "Nenhum script em %.200s", name);
else snprintf(ed->hot_status, sizeof(ed->hot_status), "Limite de abas atingido");
ed->hot_status_ok = false;
free(B);
@@ -2681,6 +2689,165 @@ static bool sb_import_file(Ed* ed, const char* path) {
return true;
}
/* -- Previa da exportacao: o texto que vai para o forum ----------------- */
#define SBP_HEAD_H 46
#define SBP_FOOT_H 58
#define SBP_LINE_H 19
static void sbp_geom(Ed* ed, float* x, float* y, float* w, float* h) {
*w = fminf(760.f, WIN_W - 80.f);
*h = fminf(560.f, WIN_H - 80.f);
*x = (WIN_W - *w) * 0.5f;
*y = (WIN_H - *h) * 0.5f;
}
static void sbp_close(Ed* ed) {
ed->sbp_open = false;
free(ed->sbp_text);
ed->sbp_text = NULL;
}
/* Gera o texto da aba atual e abre a previa */
static void sbp_open(Ed* ed) {
if (CT.kind != TK_BLOCKS) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "Só abas de blocos viram scratchblocks");
ed->hot_status_ok = false;
return;
}
TbBlueprint* bp = ed_to_blueprint(ed);
TbSbLabel* labels = NULL;
TbSbContext cx = sb_context(ed, &labels);
char* txt = bp ? tb_scratchblocks_export(&bp->tabs[ed->cur_tab], &cx) : NULL;
free(labels);
tb_blueprint_free(bp);
if (!txt) return;
free(ed->sbp_text);
ed->sbp_text = txt;
ed->sbp_scroll = 0;
ed->sbp_open = true;
}
static int sbp_lines(const char* s) {
int n = 0;
for (const char* p = s; *p; p++) if (*p == '\n') n++;
return n;
}
static void sbp_button(TinyBlocks_RenderableBlock* rb, float x, float y, float w, const char* label,
bool primary, bool hov) {
Col bg = primary ? (hov ? COL_MENU_DARK : COL_MENU) : (hov ? COL_HOVER : COL_PANEL);
if (!primary) rb_fill_rrect(rb, x, y, w, 34, 17, COL_BORDER);
rb_fill_rrect(rb, x + (primary ? 0 : 1), y + (primary ? 0 : 1), w - (primary ? 0 : 2), 34 - (primary ? 0 : 2), 16, bg);
rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + 17, label, primary ? COL_WHITE : COL_TEXT, FS_UI);
}
/* Botoes do rodape: 0 Copiar, 1 Salvar..., 2 Fechar (direita para esquerda) */
static void sbp_button_pos(Ed* ed, int i, float* bx, float* by, float* bw) {
float x, y, w, h;
sbp_geom(ed, &x, &y, &w, &h);
static const float widths[3] = { 150, 110, 100 };
float r = x + w - 16;
for (int k = 0; k <= i; k++) { r -= widths[k]; if (k < i) r -= 10; }
*bx = r; *by = y + h - SBP_FOOT_H + 12; *bw = widths[i];
}
static void sbp_render(Ed* ed, TinyBlocks_RenderableBlock* rb) {
if (!ed->sbp_open || !ed->sbp_text) return;
float x, y, w, h;
sbp_geom(ed, &x, &y, &w, &h);
rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COLA(0, 0, 0, 120));
rb_fill_rrect(rb, x + 3, y + 4, w, h, 12, COLA(0, 0, 0, 70));
rb_fill_rrect(rb, x, y, w, h, 12, COL_PANEL);
rb_fill_rrect(rb, x, y, w, SBP_HEAD_H, 12, COL_MENU);
rb_fill_rect(rb, x, y + SBP_HEAD_H - 12, w, 12, COL_MENU);
char title[96];
snprintf(title, sizeof(title), "Prévia scratchblocks - %s", CT.name);
rb_text_c(rb, x + 18, y + SBP_HEAD_H * 0.5f, title, COL_WHITE, 17);
/* texto, numa area escura como a aba de codigo */
float cx = x + 14, cy = y + SBP_HEAD_H + 12, cw = w - 28, ch = h - SBP_HEAD_H - SBP_FOOT_H - 18;
rb_fill_rrect(rb, cx, cy, cw, ch, 8, COLA(30, 30, 30, 255));
clip_set(ed, rb, cx + 4, cy + 4, cw - 8, ch - 8);
int total = sbp_lines(ed->sbp_text);
int visible = (int)((ch - 16) / SBP_LINE_H);
if (ed->sbp_scroll > total - visible) ed->sbp_scroll = total - visible;
if (ed->sbp_scroll < 0) ed->sbp_scroll = 0;
const char* p = ed->sbp_text;
for (int l = 0; *p; l++) {
size_t n = strcspn(p, "\n");
if (l >= ed->sbp_scroll && l < ed->sbp_scroll + visible) {
char line[256];
size_t m = n < sizeof(line) - 1 ? n : sizeof(line) - 1;
memcpy(line, p, m); line[m] = 0;
int ind = 0;
while (line[ind] == ' ') ind++;
const char* body = line + ind;
float ly = cy + 12 + (l - ed->sbp_scroll + 0.5f) * SBP_LINE_H;
/* "end"/"else" apagados, o resto claro; " :: categoria" apagado */
bool kw = !strcmp(body, "end") || !strcmp(body, "else");
char main_part[256];
snprintf(main_part, sizeof(main_part), "%s", body);
char* cat = strstr(main_part, " :: ");
if (cat) *cat = 0;
float lx = cx + 14 + ind * 8.f;
rb_text_c(rb, lx, ly, main_part, kw ? COLA(150, 160, 180, 255) : COLA(230, 230, 230, 255), FS_UI);
if (cat) rb_text_c(rb, lx + tw(main_part, FS_UI) + 6, ly, body + (cat - main_part) + 1,
COLA(120, 130, 150, 255), FS_UI);
}
p += n;
if (*p == '\n') p++;
}
clip_clear(ed, rb);
/* rodape */
char info[96];
snprintf(info, sizeof(info), "%d linhas - Copiar já inclui [scratchblocks]", total);
rb_text_c(rb, x + 18, y + h - SBP_FOOT_H * 0.5f, info, COL_TEXT_DIM, FS_SM);
static const char* labels[3] = { "Copiar", "Salvar...", "Fechar" };
for (int i = 0; i < 3; i++) {
float bx, by, bw;
sbp_button_pos(ed, i, &bx, &by, &bw);
sbp_button(rb, bx, by, bw, labels[i], i == 0, in_rect(ed->mouse_x, ed->mouse_y, bx, by, bw, 34));
}
}
static void sbp_copy(Ed* ed) {
if (!ed->sbp_text) return;
size_t n = strlen(ed->sbp_text) + 40;
char* clip = malloc(n);
if (!clip) return;
snprintf(clip, n, "[scratchblocks]\n%s[/scratchblocks]\n", ed->sbp_text);
bool ok = tb_graphics_set_clipboard(clip);
free(clip);
snprintf(ed->hot_status, sizeof(ed->hot_status), ok ? "Copiado - é só colar no fórum"
: "Não consegui copiar");
ed->hot_status_ok = ok;
}
static void sbp_click(Ed* ed, float mx, float my) {
for (int i = 0; i < 3; i++) {
float bx, by, bw;
sbp_button_pos(ed, i, &bx, &by, &bw);
if (!in_rect(mx, my, bx, by, bw, 34)) continue;
if (i == 0) sbp_copy(ed);
else if (i == 1) { sbp_close(ed); start_path(ed, PM_EXP_SB, "script.txt"); }
else sbp_close(ed);
return;
}
}
/* Colar do forum: a area de transferencia vira uma aba nova */
static void sb_paste(Ed* ed) {
char* t = tb_graphics_get_clipboard();
if (!t || !t[0]) {
snprintf(ed->hot_status, sizeof(ed->hot_status), "A área de transferência está vazia");
ed->hot_status_ok = false;
} else {
sb_import_text(ed, t, "Colado");
}
free(t);
}
/* ===
* Diálogo modal "Criar um Bloco" (estilo Scratch)
* === */
@@ -4156,6 +4323,7 @@ static const char* ctx_label(int id) {
case CM_EXP_XML: return "Exportar XML";
case CM_EXP_CSV: return "Exportar CSV";
case CM_EXP_SB: return "Exportar scratchblocks";
case CM_PASTE_SB: return "Colar scratchblocks";
default: return "";
}
}
@@ -5155,7 +5323,8 @@ static void ctx_exec(Ed* ed, int id) {
case CM_EXP_JSON: start_path(ed, PM_EXP_JSON, "projeto.json"); break;
case CM_EXP_XML: start_path(ed, PM_EXP_XML, "projeto.xml"); break;
case CM_EXP_CSV: start_path(ed, PM_EXP_CSV, "projeto.csv"); break;
case CM_EXP_SB: start_path(ed, PM_EXP_SB, "script.txt"); break;
case CM_EXP_SB: sbp_open(ed); break; /* previa: copiar ou salvar */
case CM_PASTE_SB: sb_paste(ed); break;
default: break;
}
}
@@ -5166,6 +5335,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
if (ed->libman_open) { libman_click(ed, mx, my); return; }
if (ed->comp_dialog_open) { dlg_click(ed, mx, my); return; }
if (ed->fdlg.open) { if (files_click) files_click(ed, mx, my); return; }
if (ed->sbp_open) { sbp_click(ed, mx, my); return; }
if (ed->plus_menu_open) {
float px, py;
plus_menu_pos(ed, &px, &py);
@@ -5225,6 +5395,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
ed->ctx_items[ed->ctx_n++] = CM_EXP_XML;
ed->ctx_items[ed->ctx_n++] = CM_EXP_CSV;
ed->ctx_items[ed->ctx_n++] = CM_EXP_SB;
ed->ctx_items[ed->ctx_n++] = CM_PASTE_SB;
ed->ctx_menu_open = true;
ed->ctx_export = true;
ed->ctx_menu_bi = -1;
@@ -5508,6 +5679,11 @@ static void on_key(Ed* ed, int sc, bool ctrl) {
if (ed->libman_open) { libman_key(ed, sc); return; }
if (ed->comp_dialog_open) { dlg_key(ed, sc); return; }
if (ed->fdlg.open) { if (files_key) files_key(ed, sc, ctrl); else ed->fdlg.open = false; return; }
if (ed->sbp_open) {
if (sc == TB_KEY_ESCAPE) sbp_close(ed);
else if (ctrl && sc == TB_KEY_A + ('c' - 'a')) sbp_copy(ed);
return;
}
if (ed->ctx_menu_open) { if (sc == TB_KEY_ESCAPE) ed->ctx_menu_open = false; return; }
if (ed->path_input_active) {
@@ -5746,7 +5922,7 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
/* == Areas: borda sendo arrastada / area levada pela alca === */
if (ed->split_drag >= 0) { split_drag_to(ed, ed->split_drag, mx, my); lay_compute(ed); }
if (ed->area_drag >= 0) ed->area_drop = view_at(ed, mx, my);
bool modal = ed->settings_open || ed->libman_open || ed->comp_dialog_open || ed->fdlg.open;
bool modal = ed->settings_open || ed->libman_open || ed->comp_dialog_open || ed->fdlg.open || ed->sbp_open;
ed->split_hover = modal ? -1 : split_at(ed, mx, my);
/* == Aba sendo arrastada: reordena ao passar do meio da vizinha === */
@@ -5790,7 +5966,9 @@ static void ed_handle_input(Ed* ed, TinyBlocks_Block* evb, int from) {
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->fdlg.open) {
if (ed->sbp_open) {
ed->sbp_scroll -= (int)(sdy * 3);
} else if (ed->fdlg.open) {
if (files_scroll) files_scroll(ed, (float)sdy);
} else if (ed->settings_open) {
settings_scroll_by(ed, (float)sdy);
@@ -6007,6 +6185,7 @@ static bool editor_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
render_tooltip(ed, rb);
render_ctx_menu(ed, rb);
dlg_render(ed, rb);
sbp_render(ed, rb);
if (ed->fdlg.open && files_view) files_view(ed, rb);
libman_render(ed, rb); /* tela cheia: por cima de tudo */
settings_render(ed, rb);
@@ -6136,6 +6315,7 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
bubble_clear(ed);
for (int i = 0; i < ed->tab_n; i++) tab_doc_free(&ed->tabs[i]);
free(ed->fdlg.ents);
free(ed->sbp_text);
free(ed->run_blocks);
for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap);
tb_libman_shutdown();
@@ -121,6 +121,9 @@ typedef struct TinyBlocks_GraphicsBackend {
void (*window_present)(void* win, const TinyBlocks_RenderableBlock* rb);
float (*measure_text)(const char* text, float font_size); /* NULL = estimativa */
void (*frame_wait)(void); /* NULL = ~16 ms */
/* Area de transferencia (opcionais; NULL = memoria interna do graphics) */
bool (*clipboard_set)(const char* text);
char* (*clipboard_get)(void); /* malloc; NULL = vazia */
} TinyBlocks_GraphicsBackend;
/* Símbolo exportado por um backend carregado como .so */
@@ -139,6 +142,11 @@ TB_API const TinyBlocks_GraphicsBackend* tb_graphics_null_backend(void);
/* Largura de um texto na fonte do backend */
TB_API float tb_graphics_measure_text(const char* text, float font_size);
/* Area de transferencia do sistema (copiar/colar). get devolve malloc
(libere com free) ou NULL se estiver vazia. */
TB_API bool tb_graphics_set_clipboard(const char* text);
TB_API char* tb_graphics_get_clipboard(void);
/* Loop principal: roda todas as janelas abertas até a principal fechar */
TB_API void tb_graphics_run(TinyBlocks* rt);
TB_API bool tb_graphics_window_is_open(TinyBlocks_Block* window);
@@ -145,6 +145,23 @@ static float estimate_text(const char* s, float fs) {
return w * fs;
}
/* Area de transferencia: a do backend, ou esta memoria (backend nulo/testes) */
static char* s_clip = NULL;
bool tb_graphics_set_clipboard(const char* text) {
const TinyBlocks_GraphicsBackend* b = backend_ready();
if (b->clipboard_set) return b->clipboard_set(text ? text : "");
free(s_clip);
s_clip = text ? strdup(text) : NULL;
return true;
}
char* tb_graphics_get_clipboard(void) {
const TinyBlocks_GraphicsBackend* b = backend_ready();
if (b->clipboard_get) return b->clipboard_get();
return s_clip ? strdup(s_clip) : NULL;
}
float tb_graphics_measure_text(const char* text, float font_size) {
if (!text || !text[0]) return 0.f;
const TinyBlocks_GraphicsBackend* b = backend_ready();
+14
View File
@@ -684,6 +684,18 @@ static void be_window_input(void* handle, TinyBlocks_GfxInput* out) {
*out = ((SdlWin*)handle)->in;
}
static bool be_clipboard_set(const char* text) {
return be_init() && SDL_SetClipboardText(text ? text : "") == 0;
}
static char* be_clipboard_get(void) {
if (!be_init() || !SDL_HasClipboardText()) return NULL;
char* t = SDL_GetClipboardText();
char* r = t ? strdup(t) : NULL;
if (t) SDL_free(t);
return r;
}
static void be_window_present(void* handle, const TinyBlocks_RenderableBlock* rb) {
SdlWin* w = (SdlWin*)handle;
SDL_SetRenderDrawColor(w->ren, 0, 0, 0, 255);
@@ -713,6 +725,8 @@ static const TinyBlocks_GraphicsBackend s_sdl2_backend = {
.window_present = be_window_present,
.measure_text = be_measure_text,
.frame_wait = be_frame_wait,
.clipboard_set = be_clipboard_set,
.clipboard_get = be_clipboard_get,
};
const TinyBlocks_GraphicsBackend* tb_sdl2_graphics_backend(void) {
+16
View File
@@ -975,6 +975,16 @@ int main(void) {
CHECK(find("repita", R_WS, 0, &x, &y) || ws_has("repita"), "blocos importados aparecem (repita)");
if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y);
if (need("Exportar scratchblocks", R_ANY, 0, &x, &y)) click(x, y);
frame(0, 0, 0, 0, 0, 0, NULL);
/* prévia: mostra o texto antes de exportar */
CHECK(find("when flag clicked", R_ANY, 0, &x, &y) && find("Copiar", R_ANY, 0, &x, &y),
"Exportar scratchblocks abre a prévia com o texto");
if (need("Copiar", R_ANY, 0, &x, &y)) click(x, y);
char* clip = tb_graphics_get_clipboard();
CHECK(clip && strstr(clip, "[scratchblocks]\nwhen flag clicked\n") && strstr(clip, "[/scratchblocks]"),
"Copiar põe o texto com [scratchblocks] na área de transferência");
free(clip);
if (need("Salvar...", R_ANY, 0, &x, &y)) click(x, y);
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
type(dst);
key(TB_KEY_RETURN, 0, 0);
@@ -984,6 +994,12 @@ int main(void) {
if (f) { size_t n = fread(got, 1, sizeof(got) - 1, f); got[n] = 0; fclose(f); }
CHECK(!strcmp(got, script), "Exportar scratchblocks gera o mesmo texto");
if (strcmp(got, script)) printf("---- exportado:\n%s----\n", got);
/* colar do fórum: a área de transferência vira uma aba */
tb_graphics_set_clipboard("[scratchblocks]\nwhen flag clicked\nerase all\n[/scratchblocks]\n");
if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y);
if (need("Colar scratchblocks", R_ANY, 0, &x, &y)) click(x, y);
frame(0, 0, 0, 0, 0, 0, NULL);
CHECK(any_has("Importado: 1 script(s) de Colado"), "Colar scratchblocks importa da área de transferência");
remove(src); remove(dst);
}