editor: Sensores, avisos flutuantes, áreas e abas destacáveis (estilo Blender/navegador)
Sensores (tinyblocks-input, categoria Sensores) - Mouse (posição no palco, botões, rolagem), teclas seguradas e última tecla, toques, acelerômetro, giroscópio e cronômetro. - Estado no core (tinyblocks/tb_input.h), alimentado pelo editor em coordenadas do palco (tb_stage_view_to_stage); o graphics passa teclas seguradas, toques e sensores (saídas JSON do Graphics.Input) e o backend SDL2 os lê (SDL_GetKeyboardState, toque, SDL_Sensor). - test-input + cenário no harness. Avisos flutuantes (estilo EditorToaster do Godot) - Mensagens de status viram avisos no canto da área de scripts, com barra de tempo, pausa com o mouse em cima, "x" para fechar e contador para repetidas. O menu fica só com os botões. Áreas (estilo Blender) - Layout em árvore de divisões (paleta, scripts, palco, saída): bordas arrastáveis, alça para trocar views de lugar, destacar qualquer view numa janela própria (cena Editor.Area) e voltar pelo menu ou fechando a janela; blocos podem ser arrastados entre janelas. - Layout salvo em "editor.layout" nas configurações. - Coluna de categorias se compacta para caber. Abas (estilo navegador) - Arrastar reordena; arrastar para fora destaca a aba numa janela; soltar na barra principal, o menu ou fechar a janela devolvem. - Blocos passam da paleta/outra aba para a janela de uma aba (só abas de blocos recebem blocos). Correções - SDL2: SDL_QUIT (SIGTERM/Ctrl+C) fecha o editor; posição do clique do evento vale mais que o estado global; posição da janela e mouse global no contrato (a posição local do SDL atrasa depois de mover a janela). - Aba de código não mostra mais "</code>" na última linha. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
22588b281e
commit
649ae14fb3
17 files changed
+2463
-183
No files matched your search
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* input_test.c - testa o estado de entrada do core (tb_input) e os blocos
|
||||
* de Sensores da tinyblocks-input: mouse em coordenadas do palco, teclas
|
||||
* por nome, toques, sensores e cronometro.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_input.h>
|
||||
#include <tinyblocks/tb_stage.h>
|
||||
#include <tb_graphics.h>
|
||||
#include <tb_input_lib.h>
|
||||
|
||||
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 TinyBlocks* rt;
|
||||
static TinyBlocks_Block* last;
|
||||
|
||||
static TinyBlocks_Value* run(const char* type, const char* arg) {
|
||||
if (last) { tb_block_destroy(last, rt); last = NULL; }
|
||||
TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, type);
|
||||
if (!t) { printf(" (tipo %s nao existe)\n", type); return NULL; }
|
||||
last = tb_block_create(rt, t);
|
||||
if (arg) {
|
||||
TinyBlocks_Value v = t->input_descs[0].expected_kind == TB_VALUE_INTEGER
|
||||
? tb_value_integer(atoll(arg)) : tb_value_text(arg);
|
||||
tb_block_set_default(last, 0, v);
|
||||
tb_value_free(&v);
|
||||
}
|
||||
tb_block_eval(last, rt);
|
||||
return last->output_count ? &last->outputs[0] : NULL;
|
||||
}
|
||||
|
||||
static long long num(TinyBlocks_Value* v) {
|
||||
if (!v) return -99999;
|
||||
if (v->kind == TB_VALUE_INTEGER) return v->as.integer;
|
||||
if (v->kind == TB_VALUE_REAL) return llround(v->as.real);
|
||||
return -99999;
|
||||
}
|
||||
static double real(TinyBlocks_Value* v) {
|
||||
if (!v) return -99999;
|
||||
return v->kind == TB_VALUE_REAL ? v->as.real : v->kind == TB_VALUE_INTEGER ? (double)v->as.integer : -99999;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
tinyblocks_init(&rt, NULL);
|
||||
TinyBlocks_LibraryBlock* lib = tb_library_register(rt, tb_input_get_library());
|
||||
CHECK(lib && !strcmp(tb_library_category(lib)->name, "Sensores"), "categoria 'Sensores'");
|
||||
|
||||
/* vista 480x360 = palco 1:1; (240,180) da vista = centro do palco */
|
||||
tb_stage_set_view(480, 360);
|
||||
tb_input_reset();
|
||||
tb_input_begin_frame();
|
||||
tb_input_set_mouse(240, 180, false, false);
|
||||
CHECK(num(run("Input.MouseX", NULL)) == 0 && num(run("Input.MouseY", NULL)) == 0,
|
||||
"centro da vista = (0, 0) do palco");
|
||||
tb_input_set_mouse(480, 0, true, false);
|
||||
CHECK(num(run("Input.MouseX", NULL)) == 240 && num(run("Input.MouseY", NULL)) == 180,
|
||||
"canto superior direito = (240, 180), y para cima");
|
||||
CHECK(num(run("Input.MouseDown", NULL)) == 1, "mouse pressionado?");
|
||||
tb_input_set_mouse(900, 500, false, false);
|
||||
CHECK(num(run("Input.MouseX", NULL)) == 240 && num(run("Input.MouseOver", NULL)) == 0,
|
||||
"fora do palco: posição presa na borda e 'sobre o palco?' falso");
|
||||
|
||||
/* vista maior com faixas (letterbox): 960x540 -> escala 1.5, faixas laterais */
|
||||
tb_stage_set_view(960, 540);
|
||||
tb_input_set_mouse(480, 270, false, false);
|
||||
CHECK(num(run("Input.MouseX", NULL)) == 0 && num(run("Input.MouseY", NULL)) == 0,
|
||||
"letterbox: centro continua (0, 0)");
|
||||
tb_input_set_mouse(120 + 720, 270, false, false);
|
||||
CHECK(num(run("Input.MouseX", NULL)) == 240, "letterbox: borda direita do palco = 240");
|
||||
tb_stage_set_view(480, 360);
|
||||
|
||||
/* teclado */
|
||||
CHECK(tb_input_key_from_name("espaço") == TB_KEY_SPACE && tb_input_key_from_name("Space") == TB_KEY_SPACE &&
|
||||
tb_input_key_from_name("seta para cima") == TB_KEY_UP && tb_input_key_from_name("a") == TB_KEY_A &&
|
||||
tb_input_key_from_name("7") == TB_KEY_0 + 7 && tb_input_key_from_name("qualquer") == -1 &&
|
||||
tb_input_key_from_name("f5") == TB_KEY_F5, "nomes de tecla (PT/EN, letras, números, F)");
|
||||
tb_input_clear_keys();
|
||||
tb_input_set_key(TB_KEY_SPACE, true);
|
||||
CHECK(num(run("Input.KeyPressed", "espaço")) == 1, "tecla espaço pressionada?");
|
||||
CHECK(num(run("Input.KeyPressed", "a")) == 0, "tecla a não pressionada");
|
||||
CHECK(num(run("Input.KeyPressed", "qualquer")) == 1, "qualquer tecla");
|
||||
tb_input_clear_keys();
|
||||
CHECK(num(run("Input.KeyPressed", "qualquer")) == 0, "soltou todas");
|
||||
tb_input_key_pressed(TB_KEY_UP);
|
||||
TinyBlocks_Value* lk = run("Input.LastKey", NULL);
|
||||
CHECK(lk && lk->kind == TB_VALUE_TEXT && !strcmp(lk->as.text, "seta para cima"), "última tecla em português");
|
||||
|
||||
/* toque */
|
||||
float tx[2] = {240, 0}, ty[2] = {180, 0};
|
||||
tb_input_set_touches(2, tx, ty);
|
||||
CHECK(num(run("Input.TouchCount", NULL)) == 2 && num(run("Input.Touching", NULL)) == 1, "dois dedos na tela");
|
||||
CHECK(num(run("Input.TouchX", "2")) == -240 && num(run("Input.TouchY", "2")) == 180, "toque 2 no canto (-240, 180)");
|
||||
tb_input_set_touches(0, NULL, NULL);
|
||||
CHECK(num(run("Input.Touching", NULL)) == 0, "sem toque");
|
||||
|
||||
/* sensores */
|
||||
CHECK(num(run("Input.HasAccel", NULL)) == 0 && num(run("Input.HasGyro", NULL)) == 0, "sem sensores por padrão");
|
||||
tb_input_set_accel(true, 0.5f, -9.81f, 0.f);
|
||||
tb_input_set_gyro(true, 0.f, 0.f, 3.14159265f);
|
||||
CHECK(num(run("Input.HasAccel", NULL)) == 1 && fabs(real(run("Input.Accel", "y")) + 9.81) < 1e-6,
|
||||
"acelerômetro y = -9.81");
|
||||
CHECK(fabs(real(run("Input.Gyro", "z")) - 180.0) < 0.01, "giroscópio em graus/s (pi rad/s = 180)");
|
||||
|
||||
/* cronômetro */
|
||||
run("Input.ResetTimer", NULL);
|
||||
double t = real(run("Input.Timer", NULL));
|
||||
CHECK(t >= 0 && t < 0.5, "zere o cronômetro");
|
||||
|
||||
/* booleanos declarados com '?' */
|
||||
TinyBlocks_TypeBlock* tk = tb_rt_find_type(rt, "Input.KeyPressed");
|
||||
CHECK(tk && tk->output_descs[0].name[strlen(tk->output_descs[0].name) - 1] == '?',
|
||||
"tecla pressionada? é booleano ('?')");
|
||||
|
||||
if (last) tb_block_destroy(last, rt);
|
||||
tinyblocks_deinit(&rt);
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
else printf("\nTUDO OK\n");
|
||||
return fails != 0;
|
||||
}
|
||||
Reference in new issue
Block a user