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

+270
View File
@@ -0,0 +1,270 @@
/*
* TinyBlocks - tb_input.c (library tinyblocks-input)
* Categoria "Sensores": Input.MouseX / MouseY / Wheel / KeyPressed /
* LastKey / TouchCount / TouchX / TouchY / Accel / Gyro / Timer /
* ResetTimer e os booleanos Input.MouseDown / MouseRight / MouseOver /
* Touching / HasAccel / HasGyro.
*
* Nao conhece janela nem editor: le o estado do core (tinyblocks/tb_input.h)
* em coordenadas do palco. Os nomes de tecla usam os TB_KEY_* do
* tinyblocks-graphics (so o cabecalho).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <math.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_input.h>
#include <tb_graphics.h>
#include "tb_input_lib.h"
/* -- Nomes de tecla ------------------------------------------------------- */
typedef struct { const char* name; int key; } KeyName;
/* O primeiro nome de cada tecla e o usado em "ultima tecla" */
static const KeyName s_keys[] = {
{"espaço", TB_KEY_SPACE}, {"espaco", TB_KEY_SPACE}, {"space", TB_KEY_SPACE},
{"seta para cima", TB_KEY_UP}, {"cima", TB_KEY_UP}, {"up", TB_KEY_UP},
{"seta para baixo", TB_KEY_DOWN}, {"baixo", TB_KEY_DOWN}, {"down", TB_KEY_DOWN},
{"seta para esquerda", TB_KEY_LEFT}, {"esquerda", TB_KEY_LEFT}, {"left", TB_KEY_LEFT},
{"seta para direita", TB_KEY_RIGHT}, {"direita", TB_KEY_RIGHT}, {"right", TB_KEY_RIGHT},
{"enter", TB_KEY_RETURN}, {"return", TB_KEY_RETURN},
{"esc", TB_KEY_ESCAPE}, {"escape", TB_KEY_ESCAPE},
{"apagar", TB_KEY_BACKSPACE}, {"backspace", TB_KEY_BACKSPACE},
{"delete", TB_KEY_DELETE}, {"del", TB_KEY_DELETE},
{"tab", TB_KEY_TAB},
{"home", TB_KEY_HOME}, {"end", TB_KEY_END},
};
TB_API int tb_input_key_from_name(const char* name) {
if (!name) return 0;
while (isspace((unsigned char)*name)) name++;
size_t n = strlen(name);
while (n && isspace((unsigned char)name[n - 1])) n--;
if (n == 0) return 0;
char buf[32];
if (n >= sizeof(buf)) return 0;
memcpy(buf, name, n);
buf[n] = 0;
if (!strcasecmp(buf, "qualquer") || !strcasecmp(buf, "any")) return -1;
if (n == 1) {
char c = (char)tolower((unsigned char)buf[0]);
if (c >= 'a' && c <= 'z') return TB_KEY_A + (c - 'a');
if (c >= '0' && c <= '9') return TB_KEY_0 + (c - '0');
}
if ((buf[0] == 'f' || buf[0] == 'F') && n <= 3) {
int k = atoi(buf + 1);
if (k >= 1 && k <= 12) return TB_KEY_F1 + (k - 1);
}
for (size_t i = 0; i < sizeof(s_keys) / sizeof(s_keys[0]); i++)
if (!strcasecmp(buf, s_keys[i].name)) return s_keys[i].key;
return 0;
}
TB_API const char* tb_input_key_name(int key) {
static char one[4][8];
static int slot = 0;
if (key >= TB_KEY_A && key < TB_KEY_A + 26) {
char* b = one[slot++ & 3];
snprintf(b, 8, "%c", 'a' + (key - TB_KEY_A));
return b;
}
if (key >= TB_KEY_0 && key < TB_KEY_0 + 10) {
char* b = one[slot++ & 3];
snprintf(b, 8, "%c", '0' + (key - TB_KEY_0));
return b;
}
if (key >= TB_KEY_F1 && key <= TB_KEY_F12) {
char* b = one[slot++ & 3];
snprintf(b, 8, "f%d", key - TB_KEY_F1 + 1);
return b;
}
for (size_t i = 0; i < sizeof(s_keys) / sizeof(s_keys[0]); i++)
if (s_keys[i].key == key) return s_keys[i].name;
return "";
}
/* -- Entradas/saidas ------------------------------------------------------ */
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
static char num[32];
if (v.kind == TB_VALUE_TEXT && v.as.text) return v.as.text;
if (v.kind == TB_VALUE_INTEGER) { snprintf(num, sizeof(num), "%lld", (long long)v.as.integer); return num; }
return "";
}
static long long in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
if (v.kind == TB_VALUE_INTEGER) return v.as.integer;
if (v.kind == TB_VALUE_REAL) return (long long)llround(v.as.real);
if (v.kind == TB_VALUE_TEXT && v.as.text) return strtoll(v.as.text, NULL, 10);
return 0;
}
/* Eixo "x"/"y"/"z" (ou 1/2/3) -> 0..2; -1 se invalido */
static int in_axis(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
const char* s = in_text(self, rt, i);
while (isspace((unsigned char)*s)) s++;
switch (tolower((unsigned char)*s)) {
case 'x': case '1': return 0;
case 'y': case '2': return 1;
case 'z': case '3': return 2;
default: return -1;
}
}
/* Numero: inteiro quando exato (como o Scratch mostra "120", nao "120.0") */
static void out_num(TinyBlocks_Block* self, double x) {
if (x == floor(x) && fabs(x) < 1e15) tb_block_set_output(self, 0, tb_value_integer((int64_t)x));
else tb_block_set_output(self, 0, tb_value_real(x));
self->dirty = false;
}
static void out_bool(TinyBlocks_Block* self, bool b) {
tb_block_set_output(self, 0, tb_value_integer(b ? 1 : 0));
self->dirty = false;
}
/* -- Blocos --------------------------------------------------------------- */
static void eval_mouse_x(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_num(self, roundf(tb_input_mouse_x())); }
static void eval_mouse_y(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_num(self, roundf(tb_input_mouse_y())); }
static void eval_mouse_down(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_mouse_down()); }
static void eval_mouse_right(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_mouse_right_down()); }
static void eval_mouse_over(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_mouse_over_stage()); }
static void eval_wheel(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_num(self, tb_input_wheel()); }
static void eval_key_pressed(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* nm = in_text(self, rt, 0);
int k = tb_input_key_from_name(nm);
if (k == 0 && nm[0]) tinyblocks_set_error(rt, "tecla desconhecida: '%s'", nm);
out_bool(self, k < 0 ? tb_input_any_key_held() : tb_input_key_held(k));
}
static void eval_last_key(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
tb_block_set_output(self, 0, tb_value_text(tb_input_key_name(tb_input_last_key())));
self->dirty = false;
}
static void eval_touch_count(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_num(self, tb_input_touch_count()); }
static void eval_touching(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_touch_count() > 0); }
static void eval_touch_x(TinyBlocks_Block* self, TinyBlocks* rt) {
out_num(self, roundf(tb_input_touch_x((int)in_int(self, rt, 0) - 1)));
}
static void eval_touch_y(TinyBlocks_Block* self, TinyBlocks* rt) {
out_num(self, roundf(tb_input_touch_y((int)in_int(self, rt, 0) - 1)));
}
static void eval_has_accel(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_has_accel()); }
static void eval_has_gyro(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_bool(self, tb_input_has_gyro()); }
/* sensores: 2 casas, para nao piscar com o ruido */
static void eval_accel(TinyBlocks_Block* self, TinyBlocks* rt) {
int a = in_axis(self, rt, 0);
out_num(self, a < 0 ? 0.0 : round(tb_input_accel(a) * 100.0) / 100.0);
}
static void eval_gyro(TinyBlocks_Block* self, TinyBlocks* rt) {
int a = in_axis(self, rt, 0);
out_num(self, a < 0 ? 0.0 : round(tb_input_gyro(a) * 100.0) / 100.0);
}
static void eval_timer(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
out_num(self, round(tb_input_timer() * 1000.0) / 1000.0);
}
static void eval_reset_timer(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
tb_input_timer_reset();
self->dirty = false;
}
/* -- Registro ------------------------------------------------------------- */
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
return t && tb_library_add(lib, &t->base);
}
static bool input_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Sensores",
{ 92/255.f, 177/255.f, 214/255.f, 1.f},
{ 46/255.f, 142/255.f, 184/255.f, 1.f},
};
tb_library_set_category(lib, &cat);
TB_BLOCK_META("Input.MouseX", "eval_mouse_x", "Posicao x do mouse no palco (-240 a 240)");
TB_BLOCK_META("Input.MouseY", "eval_mouse_y", "Posicao y do mouse no palco (-180 a 180)");
TB_BLOCK_META("Input.MouseDown", "eval_mouse_down", "Verdadeiro enquanto o botao do mouse estiver apertado");
TB_BLOCK_META("Input.MouseRight", "eval_mouse_right", "Verdadeiro enquanto o botao direito estiver apertado");
TB_BLOCK_META("Input.MouseOver", "eval_mouse_over", "Verdadeiro se o mouse estiver sobre o palco");
TB_BLOCK_META("Input.Wheel", "eval_wheel", "Quanto a roda do mouse girou neste quadro");
TB_BLOCK_META("Input.KeyPressed", "eval_key_pressed", "Verdadeiro enquanto a tecla estiver apertada");
TB_BLOCK_META("Input.LastKey", "eval_last_key", "Nome da ultima tecla apertada");
TB_BLOCK_META("Input.TouchCount", "eval_touch_count", "Quantos dedos estao na tela");
TB_BLOCK_META("Input.Touching", "eval_touching", "Verdadeiro se algum dedo estiver na tela");
TB_BLOCK_META("Input.TouchX", "eval_touch_x", "Posicao x do toque N no palco");
TB_BLOCK_META("Input.TouchY", "eval_touch_y", "Posicao y do toque N no palco");
TB_BLOCK_META("Input.HasAccel", "eval_has_accel", "Verdadeiro se o aparelho tiver acelerometro");
TB_BLOCK_META("Input.Accel", "eval_accel", "Aceleracao no eixo x, y ou z (m/s², inclui a gravidade)");
TB_BLOCK_META("Input.HasGyro", "eval_has_gyro", "Verdadeiro se o aparelho tiver giroscopio");
TB_BLOCK_META("Input.Gyro", "eval_gyro", "Velocidade de giro no eixo x, y ou z (graus por segundo)");
TB_BLOCK_META("Input.Timer", "eval_timer", "Segundos desde a bandeira verde (ou do ultimo zerar)");
TB_BLOCK_META("Input.ResetTimer", "eval_reset_timer", "Zera o cronometro");
static const TinyBlocks_SlotDesc key_in[] = {{"tecla", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc n_in[] = {{"n", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc axis_in[] = {{"eixo", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc num_out[] = {{"numero", TB_VALUE_NONE}};
static const TinyBlocks_SlotDesc txt_out[] = {{"texto", TB_VALUE_TEXT}};
/* saidas terminadas em '?' = blocos booleanos (hexagonais no editor) */
static const TinyBlocks_SlotDesc down_out[] = {{"pressionado?", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc over_out[] = {{"sobre o palco?", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc key_out[] = {{"pressionada?", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc touch_out[] = {{"tocando?", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc has_out[] = {{"tem?", TB_VALUE_INTEGER}};
return reg(rt, lib, "Input.MouseX", NULL, 0, num_out, 1, eval_mouse_x)
&& reg(rt, lib, "Input.MouseY", NULL, 0, num_out, 1, eval_mouse_y)
&& reg(rt, lib, "Input.MouseDown", NULL, 0, down_out, 1, eval_mouse_down)
&& reg(rt, lib, "Input.MouseRight", NULL, 0, down_out, 1, eval_mouse_right)
&& reg(rt, lib, "Input.MouseOver", NULL, 0, over_out, 1, eval_mouse_over)
&& reg(rt, lib, "Input.Wheel", NULL, 0, num_out, 1, eval_wheel)
&& reg(rt, lib, "Input.KeyPressed", key_in, 1, key_out, 1, eval_key_pressed)
&& reg(rt, lib, "Input.LastKey", NULL, 0, txt_out, 1, eval_last_key)
&& reg(rt, lib, "Input.Touching", NULL, 0, touch_out, 1, eval_touching)
&& reg(rt, lib, "Input.TouchCount", NULL, 0, num_out, 1, eval_touch_count)
&& reg(rt, lib, "Input.TouchX", n_in, 1, num_out, 1, eval_touch_x)
&& reg(rt, lib, "Input.TouchY", n_in, 1, num_out, 1, eval_touch_y)
&& reg(rt, lib, "Input.HasAccel", NULL, 0, has_out, 1, eval_has_accel)
&& reg(rt, lib, "Input.Accel", axis_in, 1, num_out, 1, eval_accel)
&& reg(rt, lib, "Input.HasGyro", NULL, 0, has_out, 1, eval_has_gyro)
&& reg(rt, lib, "Input.Gyro", axis_in, 1, num_out, 1, eval_gyro)
&& reg(rt, lib, "Input.Timer", NULL, 0, num_out, 1, eval_timer)
&& reg(rt, lib, "Input.ResetTimer", NULL, 0, NULL, 0, eval_reset_timer);
}
static void input_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
}
TINYBLOCKS_DEFINE_LIBRARY(input,
"tinyblocks-input",
"1.0.0",
input_init,
input_deinit)
const TinyBlocks_LibraryDescriptor* tb_input_get_library(void) {
return &_tb_lib_desc_input;
}