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:
mguinhosandClaude Opus 5.5 committed 2026-10-22 17:53:07 -03:00
1 parent 22588b281e
commit 649ae14fb3
17 files changed
+2463 -183

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
/*
* TinyBlocks - tb_input.c
* Estado de entrada em coordenadas do palco (ver tb_input.h).
*/
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <time.h>
#include "tinyblocks/tb_input.h"
#include "tinyblocks/tb_stage.h"
#define HALF_W 240.f
#define HALF_H 180.f
#define RAD2DEG 57.29577951308232f
typedef struct {
float mx, my; /* palco, sem prender */
bool over, left, right;
float wheel;
unsigned char keys[TB_INPUT_MAX_KEYS];
int last_key;
int touch_n;
float tx[TB_INPUT_MAX_TOUCHES], ty[TB_INPUT_MAX_TOUCHES];
bool has_accel, has_gyro;
float accel[3], gyro[3];
double timer_t0;
bool timer_started;
} InputState;
static InputState s_in;
static double now_s(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
}
static float clampf(float v, float lo, float hi) { return v < lo ? lo : v > hi ? hi : v; }
/* -- Alimentacao --- */
void tb_input_begin_frame(void) {
s_in.wheel = 0.f;
}
void tb_input_set_mouse(float vx, float vy, bool left, bool right) {
s_in.over = tb_stage_view_to_stage(vx, vy, &s_in.mx, &s_in.my);
s_in.left = left;
s_in.right = right;
}
void tb_input_add_wheel(float dy) { s_in.wheel += dy; }
void tb_input_clear_keys(void) { memset(s_in.keys, 0, sizeof(s_in.keys)); }
void tb_input_set_key(int key, bool held) {
if (key > 0 && key < TB_INPUT_MAX_KEYS) s_in.keys[key] = held ? 1 : 0;
}
void tb_input_key_pressed(int key) {
if (key > 0 && key < TB_INPUT_MAX_KEYS) s_in.last_key = key;
}
void tb_input_set_touches(int n, const float* vx, const float* vy) {
if (n < 0) n = 0;
if (n > TB_INPUT_MAX_TOUCHES) n = TB_INPUT_MAX_TOUCHES;
s_in.touch_n = n;
for (int i = 0; i < n; i++) tb_stage_view_to_stage(vx[i], vy[i], &s_in.tx[i], &s_in.ty[i]);
}
void tb_input_set_accel(bool has, float x, float y, float z) {
s_in.has_accel = has;
s_in.accel[0] = has ? x : 0.f; s_in.accel[1] = has ? y : 0.f; s_in.accel[2] = has ? z : 0.f;
}
void tb_input_set_gyro(bool has, float x, float y, float z) {
s_in.has_gyro = has;
s_in.gyro[0] = has ? x : 0.f; s_in.gyro[1] = has ? y : 0.f; s_in.gyro[2] = has ? z : 0.f;
}
void tb_input_reset(void) {
memset(&s_in, 0, sizeof(s_in));
}
/* -- Leitura --- */
float tb_input_mouse_x(void) { return clampf(s_in.mx, -HALF_W, HALF_W); }
float tb_input_mouse_y(void) { return clampf(s_in.my, -HALF_H, HALF_H); }
bool tb_input_mouse_over_stage(void) { return s_in.over; }
bool tb_input_mouse_down(void) { return s_in.left; }
bool tb_input_mouse_right_down(void) { return s_in.right; }
float tb_input_wheel(void) { return s_in.wheel; }
bool tb_input_key_held(int key) {
return key > 0 && key < TB_INPUT_MAX_KEYS && s_in.keys[key];
}
bool tb_input_any_key_held(void) {
for (int k = 1; k < TB_INPUT_MAX_KEYS; k++) if (s_in.keys[k]) return true;
return false;
}
int tb_input_last_key(void) { return s_in.last_key; }
int tb_input_touch_count(void) { return s_in.touch_n; }
float tb_input_touch_x(int i) { return (i >= 0 && i < s_in.touch_n) ? s_in.tx[i] : 0.f; }
float tb_input_touch_y(int i) { return (i >= 0 && i < s_in.touch_n) ? s_in.ty[i] : 0.f; }
bool tb_input_has_accel(void) { return s_in.has_accel; }
float tb_input_accel(int axis) { return (axis >= 0 && axis < 3) ? s_in.accel[axis] : 0.f; }
bool tb_input_has_gyro(void) { return s_in.has_gyro; }
float tb_input_gyro(int axis) { return (axis >= 0 && axis < 3) ? s_in.gyro[axis] * RAD2DEG : 0.f; }
double tb_input_timer(void) {
if (!s_in.timer_started) { s_in.timer_t0 = now_s(); s_in.timer_started = true; }
return now_s() - s_in.timer_t0;
}
void tb_input_timer_reset(void) {
s_in.timer_t0 = now_s();
s_in.timer_started = true;
}
+12
View File
@@ -124,6 +124,18 @@ void tb_stage_set_view(float w, float h) {
if (w > 0 && h > 0) { s_stage.view_w = w; s_stage.view_h = h; }
}
bool tb_stage_view_to_stage(float vx, float vy, float* sx, float* sy) {
const Stage* s = &s_stage;
float sc = s->view_w / STAGE_W < s->view_h / STAGE_H ? s->view_w / STAGE_W : s->view_h / STAGE_H;
if (sc <= 0.f) sc = 1.f;
float ox = (s->view_w - STAGE_W*sc) * 0.5f, oy = (s->view_h - STAGE_H*sc) * 0.5f;
float x = (vx - ox) / sc - STAGE_W * 0.5f;
float y = STAGE_H * 0.5f - (vy - oy) / sc;
if (sx) *sx = x;
if (sy) *sy = y;
return x >= -STAGE_W * 0.5f && x <= STAGE_W * 0.5f && y >= -STAGE_H * 0.5f && y <= STAGE_H * 0.5f;
}
void tb_stage_reset(void) {
free(s_stage.cmds);
s_stage.cmds = NULL; s_stage.n = 0; s_stage.cap = 0;