Editor: botao Exportar abre menu de contexto (JSON/XML/CSV)
- Substitui os tres botoes de exportacao por um unico "Exportar" que abre um menu de contexto ancorado no botao (reusa o menu de contexto do workspace via os itens CM_EXP_JSON/XML/CSV e o flag ctx_export). - Layout do menu: Abrir / Salvar / Exportar / Carregar library. - Harness: abre um projeto .json escrito a mao (aba distinta, blocos e variavel) e exporta XML pelo menu de contexto do botao.
This commit is contained in:
1 parent
1199d2d7c7
commit
f37884ccdf
2 files changed
+67
-27
No files matched your search
@@ -89,15 +89,9 @@ TB_API void tb_editor_preload(const char* path) {
|
||||
#define OPEN_BTN_W 64
|
||||
#define SAVE_BTN_X 218
|
||||
#define SAVE_BTN_W 70
|
||||
#define EXP_LBL_X 296
|
||||
#define EXP_LBL_W 60
|
||||
#define EXP_JSON_X 360
|
||||
#define EXP_JSON_W 54
|
||||
#define EXP_XML_X 418
|
||||
#define EXP_XML_W 44
|
||||
#define EXP_CSV_X 466
|
||||
#define EXP_CSV_W 44
|
||||
#define LIB_BTN_X 522
|
||||
#define EXP_BTN_X 296
|
||||
#define EXP_BTN_W 84
|
||||
#define LIB_BTN_X 392
|
||||
#define LIB_BTN_W 168
|
||||
|
||||
/* == Geometria dos blocos === */
|
||||
@@ -323,7 +317,8 @@ 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 };
|
||||
enum { CM_DUPLICATE, CM_DELETE, CM_EDIT_DEF, CM_CLEANUP, CM_NEW_COMPOSITE,
|
||||
CM_EXP_JSON, CM_EXP_XML, CM_EXP_CSV };
|
||||
|
||||
/* 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 };
|
||||
@@ -366,6 +361,7 @@ typedef struct {
|
||||
float comp_dialog_name_x, comp_dialog_name_w;
|
||||
|
||||
bool ctx_menu_open;
|
||||
bool ctx_export; /* menu de contexto aberto e o de exportacao */
|
||||
int ctx_menu_bi;
|
||||
int ctx_menu_x, ctx_menu_y;
|
||||
int ctx_menu_hover;
|
||||
@@ -2560,7 +2556,7 @@ static void hotlib_check(Ed* ed) {
|
||||
* === */
|
||||
static bool hit_open_btn(float mx, float my);
|
||||
static bool hit_save_btn(float mx, float my);
|
||||
static bool hit_exp_btn(float mx, float my, int fmt);
|
||||
static bool hit_export_btn(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));
|
||||
@@ -2575,10 +2571,7 @@ static void render_menu(Ed* ed, TinyBlocks_RenderableBlock* rb) {
|
||||
|
||||
menu_button(rb, OPEN_BTN_X, OPEN_BTN_W, "Abrir", (active && ed->path_mode == PM_LOAD) || hit_open_btn(mx, my));
|
||||
menu_button(rb, SAVE_BTN_X, SAVE_BTN_W, "Salvar", (active && ed->path_mode == PM_SAVE) || hit_save_btn(mx, my));
|
||||
rb_text_c(rb, EXP_LBL_X, MENU_H*0.5f, "Exportar:", COL_WHITE, FS_UI);
|
||||
menu_button(rb, EXP_JSON_X, EXP_JSON_W, "JSON", (active && ed->path_mode == PM_EXP_JSON) || hit_exp_btn(mx, my, EX_JSON));
|
||||
menu_button(rb, EXP_XML_X, EXP_XML_W, "XML", (active && ed->path_mode == PM_EXP_XML) || hit_exp_btn(mx, my, EX_XML));
|
||||
menu_button(rb, EXP_CSV_X, EXP_CSV_W, "CSV", (active && ed->path_mode == PM_EXP_CSV) || hit_exp_btn(mx, my, EX_CSV));
|
||||
menu_button(rb, EXP_BTN_X, EXP_BTN_W, "Exportar", (ed->ctx_menu_open && ed->ctx_export) || hit_export_btn(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);
|
||||
@@ -2864,6 +2857,9 @@ static const char* ctx_label(int id) {
|
||||
case CM_EDIT_DEF: return "Editar definição";
|
||||
case CM_CLEANUP: return "Organizar blocos";
|
||||
case CM_NEW_COMPOSITE: return "Criar um bloco";
|
||||
case CM_EXP_JSON: return "Exportar JSON";
|
||||
case CM_EXP_XML: return "Exportar XML";
|
||||
case CM_EXP_CSV: return "Exportar CSV";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
@@ -3041,13 +3037,7 @@ static bool hit_detach(Ed* ed, float mx, float my) {
|
||||
static bool hit_lib_btn(float mx, float my) { return in_rect(mx, my, LIB_BTN_X, 0, LIB_BTN_W, MENU_H); }
|
||||
static bool hit_open_btn(float mx, float my) { return in_rect(mx, my, OPEN_BTN_X, 0, OPEN_BTN_W, MENU_H); }
|
||||
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_exp_btn(float mx, float my, int fmt) {
|
||||
switch (fmt) {
|
||||
case EX_JSON: return in_rect(mx, my, EXP_JSON_X, 0, EXP_JSON_W, MENU_H);
|
||||
case EX_XML: return in_rect(mx, my, EXP_XML_X, 0, EXP_XML_W, MENU_H);
|
||||
default: return in_rect(mx, my, EXP_CSV_X, 0, EXP_CSV_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 void start_path(Ed* ed, int mode, const char* def) {
|
||||
ed->path_input_active = true;
|
||||
@@ -3133,6 +3123,9 @@ static void ctx_exec(Ed* ed, int id) {
|
||||
case CM_EDIT_DEF: if (T->ws[bi].type) open_def(ed, T->ws[bi].type->type_name + 5); break;
|
||||
case CM_CLEANUP: cleanup_ws(ed); break;
|
||||
case CM_NEW_COMPOSITE: new_composite_tab(ed); break;
|
||||
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;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -3142,6 +3135,7 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
|
||||
if (ed->ctx_menu_open) {
|
||||
int it = ctx_hit(ed, mx, my);
|
||||
ed->ctx_menu_open = false;
|
||||
ed->ctx_export = false;
|
||||
if (it >= 0 && it < ed->ctx_n) ctx_exec(ed, ed->ctx_items[it]);
|
||||
return;
|
||||
}
|
||||
@@ -3153,10 +3147,18 @@ static void on_lmb_down(Ed* ed, float mx, float my) {
|
||||
if (hit_lib_btn(mx, my)) start_path(ed, PM_LIB, "");
|
||||
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_exp_btn(mx, my, EX_JSON)) start_path(ed, PM_EXP_JSON, "projeto.json");
|
||||
else if (hit_exp_btn(mx, my, EX_XML)) start_path(ed, PM_EXP_XML, "projeto.xml");
|
||||
else if (hit_exp_btn(mx, my, EX_CSV)) start_path(ed, PM_EXP_CSV, "projeto.csv");
|
||||
else ed->path_input_active = false;
|
||||
else if (hit_export_btn(mx, my)) {
|
||||
ed->ctx_n = 0;
|
||||
ed->ctx_items[ed->ctx_n++] = CM_EXP_JSON;
|
||||
ed->ctx_items[ed->ctx_n++] = CM_EXP_XML;
|
||||
ed->ctx_items[ed->ctx_n++] = CM_EXP_CSV;
|
||||
ed->ctx_menu_open = true;
|
||||
ed->ctx_export = true;
|
||||
ed->ctx_menu_bi = -1;
|
||||
ed->ctx_menu_x = (int)EXP_BTN_X;
|
||||
ed->ctx_menu_y = (int)MENU_H;
|
||||
ed->ctx_menu_hover = -1;
|
||||
} else ed->path_input_active = false;
|
||||
return;
|
||||
}
|
||||
ed->path_input_active = false;
|
||||
@@ -3256,7 +3258,7 @@ static void on_lmb_up(Ed* ed, float mx, float my) {
|
||||
}
|
||||
|
||||
static void on_rmb_down(Ed* ed, float mx, float my) {
|
||||
if (ed->ctx_menu_open) { ed->ctx_menu_open = false; return; }
|
||||
if (ed->ctx_menu_open) { ed->ctx_menu_open = false; ed->ctx_export = false; return; }
|
||||
if (ed->state != ES_IDLE) return;
|
||||
if (my < WS_Y || mx < WS_X || mx >= WIN_W - STAGE_W) return;
|
||||
int bi = hit_ws(&CT, mx - WS_X + CT.ws_sx, my - WS_Y + CT.ws_sy);
|
||||
@@ -3271,6 +3273,7 @@ static void on_rmb_down(Ed* ed, float mx, float my) {
|
||||
ed->ctx_items[ed->ctx_n++] = CM_NEW_COMPOSITE;
|
||||
}
|
||||
ed->ctx_menu_open = true;
|
||||
ed->ctx_export = false;
|
||||
ed->ctx_menu_bi = bi;
|
||||
ed->ctx_menu_x = (int)mx; ed->ctx_menu_y = (int)my;
|
||||
ed->ctx_menu_hover = -1;
|
||||
|
||||
@@ -308,6 +308,43 @@ int main(void) {
|
||||
"estado restaurado apos abrir (chapéu de exemplo de volta)");
|
||||
remove(proj);
|
||||
|
||||
/* 13. Abrir um projeto escrito a mao (aba distinta) */
|
||||
const char* openjson = "/tmp/tb_harness_open.json";
|
||||
FILE* pf = fopen(openjson, "wb");
|
||||
if (pf) {
|
||||
fputs("{\"format\":\"tinyblocks-project\",\"version\":1,\"cur_tab\":0,"
|
||||
"\"tabs\":[{\"name\":\"Aberto!\",\"composite\":false,\"comp_id\":-1,\"color\":0,"
|
||||
"\"blocks\":["
|
||||
"{\"i\":0,\"type\":\"Events.WhenRun\",\"x\":40,\"y\":40,\"next\":1},"
|
||||
"{\"i\":1,\"type\":\"IO.Print\",\"x\":40,\"y\":100,\"in_text\":[\"Oi do arquivo\",\"\"]},"
|
||||
"{\"i\":2,\"type\":\"Stage.Circle\",\"x\":40,\"y\":180,\"in_text\":[\"0\",\"0\",\"80\"]}"
|
||||
"],\"top\":[0,2]}],"
|
||||
"\"user_blocks\":[],\"variables\":[{\"name\":\"x\",\"kind\":0,\"num\":7,\"text\":\"\"}]}", pf);
|
||||
fclose(pf);
|
||||
}
|
||||
if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y);
|
||||
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
|
||||
type(openjson);
|
||||
key(TB_KEY_RETURN, 0, 0);
|
||||
frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
CHECK(find("Aberto!", R_ANY, 0, &x, &y), "abrir projeto de arquivo externo (aba Aberto!)");
|
||||
CHECK(find("Oi do arquivo", R_WS, 0, &x, &y), "bloco do arquivo externo aparece no workspace");
|
||||
remove(openjson);
|
||||
|
||||
/* 14. Exportar pelo menu de contexto do botao Exportar */
|
||||
if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y);
|
||||
CHECK(find("Exportar JSON", R_FLY, 0, &x, &y) && find("Exportar XML", R_FLY, 0, &x, &y)
|
||||
&& find("Exportar CSV", R_FLY, 0, &x, &y), "botao Exportar abre menu com JSON/XML/CSV");
|
||||
const char* expf = "/tmp/tb_harness_export.xml";
|
||||
remove(expf);
|
||||
if (need("Exportar XML", R_FLY, 0, &x, &y)) click(x, y);
|
||||
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
|
||||
type(expf);
|
||||
key(TB_KEY_RETURN, 0, 0);
|
||||
frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
CHECK(any_has("Exportado xml"), "exportar XML pelo menu escreve o arquivo");
|
||||
remove(expf);
|
||||
|
||||
printf("comandos de desenho no último frame: %u\n", ncmd);
|
||||
tinyblocks_deinit(&rt);
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
|
||||
Reference in new issue
Block a user