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
@@ -398,6 +398,7 @@ typedef struct SdlWin {
|
||||
SDL_Renderer* ren;
|
||||
Uint32 id;
|
||||
TinyBlocks_GfxInput in;
|
||||
int pos_fresh; /* posicao do mouse veio de um evento neste quadro */
|
||||
struct SdlWin* next;
|
||||
} SdlWin;
|
||||
|
||||
@@ -439,6 +440,24 @@ static SdlWin* win_by_id(Uint32 id) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Sensores do dispositivo (celular/tablet; no desktop costuma nao haver) */
|
||||
static SDL_Sensor* s_accel = NULL;
|
||||
static SDL_Sensor* s_gyro = NULL;
|
||||
|
||||
static void sensors_open(void) {
|
||||
if (SDL_InitSubSystem(SDL_INIT_SENSOR) != 0) return;
|
||||
for (int i = 0; i < SDL_NumSensors(); i++) {
|
||||
SDL_SensorType t = SDL_SensorGetDeviceType(i);
|
||||
if (t == SDL_SENSOR_ACCEL && !s_accel) s_accel = SDL_SensorOpen(i);
|
||||
if (t == SDL_SENSOR_GYRO && !s_gyro) s_gyro = SDL_SensorOpen(i);
|
||||
}
|
||||
}
|
||||
|
||||
static void sensors_close(void) {
|
||||
if (s_accel) { SDL_SensorClose(s_accel); s_accel = NULL; }
|
||||
if (s_gyro) { SDL_SensorClose(s_gyro); s_gyro = NULL; }
|
||||
}
|
||||
|
||||
static bool be_init(void) {
|
||||
if (s_be_inited) return true;
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
||||
@@ -452,6 +471,7 @@ static bool be_init(void) {
|
||||
}
|
||||
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
|
||||
SDL_StartTextInput();
|
||||
sensors_open();
|
||||
s_be_inited = true;
|
||||
return true;
|
||||
}
|
||||
@@ -468,6 +488,7 @@ static void be_window_close(void* handle) {
|
||||
static void be_shutdown(void) {
|
||||
if (!s_be_inited) return;
|
||||
while (s_wins) be_window_close(s_wins);
|
||||
sensors_close();
|
||||
fonts_close_all();
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
@@ -512,6 +533,9 @@ static bool s_has_pending = false;
|
||||
static bool be_apply(SDL_Event* ev) {
|
||||
SdlWin* w = NULL;
|
||||
switch (ev->type) {
|
||||
case SDL_QUIT: /* SIGTERM/Ctrl+C ou o SO pediu para sair */
|
||||
for (w = s_wins; w; w = w->next) w->in.close = 1;
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if ((w = win_by_id(ev->window.windowID)) && ev->window.event == SDL_WINDOWEVENT_CLOSE)
|
||||
w->in.close = 1;
|
||||
@@ -520,6 +544,7 @@ static bool be_apply(SDL_Event* ev) {
|
||||
if ((w = win_by_id(ev->motion.windowID))) {
|
||||
w->in.mouse_x = (float)ev->motion.x;
|
||||
w->in.mouse_y = (float)ev->motion.y;
|
||||
w->pos_fresh = 1;
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
@@ -528,6 +553,7 @@ static bool be_apply(SDL_Event* ev) {
|
||||
bool down = ev->type == SDL_MOUSEBUTTONDOWN;
|
||||
w->in.mouse_x = (float)ev->button.x;
|
||||
w->in.mouse_y = (float)ev->button.y;
|
||||
w->pos_fresh = 1;
|
||||
/* clique repetido no mesmo quadro: espera o proximo */
|
||||
if (ev->button.button == SDL_BUTTON_LEFT && (down ? w->in.l_down : w->in.l_up)) return false;
|
||||
if (ev->button.button == SDL_BUTTON_LEFT) { if (down) w->in.l_down = 1; else w->in.l_up = 1; }
|
||||
@@ -580,6 +606,7 @@ static void be_poll(void) {
|
||||
in->text[0] = 0;
|
||||
in->drop_file[0] = 0;
|
||||
in->close = 0;
|
||||
w->pos_fresh = 0;
|
||||
}
|
||||
|
||||
if (s_has_pending) {
|
||||
@@ -591,6 +618,36 @@ static void be_poll(void) {
|
||||
if (!be_apply(&ev)) { s_pending = ev; s_has_pending = true; }
|
||||
}
|
||||
|
||||
/* Dispositivo: teclas seguradas (janela com o foco do teclado), dedos na
|
||||
tela e sensores (todas as janelas) */
|
||||
SDL_Window* kfocus = SDL_GetKeyboardFocus();
|
||||
int nkeys = 0;
|
||||
const Uint8* ks = SDL_GetKeyboardState(&nkeys);
|
||||
unsigned char held[128];
|
||||
memset(held, 0, sizeof(held));
|
||||
for (int sc = 0; ks && sc < nkeys; sc++) {
|
||||
if (!ks[sc]) continue;
|
||||
int k = map_key((SDL_Scancode)sc);
|
||||
if (k > 0 && k < 128) held[k] = 1;
|
||||
}
|
||||
float tx[GFX_MAX_TOUCHES], ty[GFX_MAX_TOUCHES]; /* 0..1 na janela */
|
||||
int tn = 0;
|
||||
for (int d = 0; d < SDL_GetNumTouchDevices(); d++) {
|
||||
SDL_TouchID tid = SDL_GetTouchDevice(d);
|
||||
for (int f = 0; f < SDL_GetNumTouchFingers(tid) && tn < GFX_MAX_TOUCHES; f++) {
|
||||
SDL_Finger* fg = SDL_GetTouchFinger(tid, f);
|
||||
if (fg) { tx[tn] = fg->x; ty[tn] = fg->y; tn++; }
|
||||
}
|
||||
}
|
||||
float acc[3] = {0, 0, 0}, gyr[3] = {0, 0, 0};
|
||||
bool has_acc = s_accel && SDL_SensorGetData(s_accel, acc, 3) == 0;
|
||||
bool has_gyr = s_gyro && SDL_SensorGetData(s_gyro, gyr, 3) == 0;
|
||||
|
||||
/* Mouse na tela: consulta direta, confiavel mesmo logo depois de o
|
||||
gerenciador de janelas mover a janela (a posicao local do SDL atrasa) */
|
||||
int gmx = 0, gmy = 0;
|
||||
SDL_GetGlobalMouseState(&gmx, &gmy);
|
||||
|
||||
/* Estado contínuo: botões segurados só valem na janela com o foco do mouse */
|
||||
SDL_Window* focus = SDL_GetMouseFocus();
|
||||
int mx, my;
|
||||
@@ -598,11 +655,28 @@ static void be_poll(void) {
|
||||
int mods = map_mod((Uint16)SDL_GetModState());
|
||||
for (SdlWin* w = s_wins; w; w = w->next) {
|
||||
bool f = w->win == focus;
|
||||
if (f) { w->in.mouse_x = (float)mx; w->in.mouse_y = (float)my; }
|
||||
/* a posicao do evento vale mais: o estado global fica velho quando a
|
||||
janela acabou de ganhar o foco com aquele mesmo clique */
|
||||
if (f && !w->pos_fresh) { w->in.mouse_x = (float)mx; w->in.mouse_y = (float)my; }
|
||||
w->in.l_held = f && (btns & SDL_BUTTON_LMASK) ? 1 : 0;
|
||||
w->in.r_held = f && (btns & SDL_BUTTON_RMASK) ? 1 : 0;
|
||||
w->in.mod_state = mods;
|
||||
SDL_GetWindowSize(w->win, &w->in.win_w, &w->in.win_h);
|
||||
SDL_GetWindowPosition(w->win, &w->in.win_x, &w->in.win_y);
|
||||
w->in.gmouse_x = (float)gmx;
|
||||
w->in.gmouse_y = (float)gmy;
|
||||
w->in.mouse_in = f ? 1 : 0;
|
||||
if (w->win == kfocus) memcpy(w->in.keys_held, held, sizeof(held));
|
||||
else memset(w->in.keys_held, 0, sizeof(w->in.keys_held));
|
||||
w->in.touch_n = (w->win == kfocus || w->win == focus) ? tn : 0;
|
||||
for (int i = 0; i < w->in.touch_n; i++) {
|
||||
w->in.touch_x[i] = tx[i] * (float)w->in.win_w;
|
||||
w->in.touch_y[i] = ty[i] * (float)w->in.win_h;
|
||||
}
|
||||
w->in.has_accel = has_acc;
|
||||
w->in.has_gyro = has_gyr;
|
||||
memcpy(w->in.accel, acc, sizeof(acc));
|
||||
memcpy(w->in.gyro, gyr, sizeof(gyr));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user