Editor: zoom (Ctrl+roda, pinca), selecao de texto e partes do bloco moveis
- Area de scripts com zoom: Ctrl+roda no ponteiro, roda rola na vertical e Shift+roda na horizontal; pinca com dois dedos (toque) e pinca do touchpad (XInput 2.4 no X11, GFX_IN_ZOOM). Desenho em escala 1 escalado depois (rb_scale_from); a arvore acessivel acompanha (tb_a11y_scale_from). - SDL2: cache de fontes com 64 tamanhos e descarte do menos usado (antes vazava uma fonte por chamada depois de 8 tamanhos). - Campos de texto e editor de codigo: cursor em qualquer posicao, selecao (mouse, Shift+setas, clique duplo), Ctrl+A/C/X/V; no toque, segurar seleciona a palavra com alcas e barra Recortar/Copiar/Colar/Tudo. - "Criar um Bloco": o nome e uma parte movel (PARAM_NAME) e pode ser apagado quando um rotulo logo depois assume; hexagono booleano sem corte. - Harness: cenarios 32-34 (zoom, pinca, dialogo, selecao e clipboard). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a6f834f88e
commit
f180eaf75a
9 files changed
+1287
-209
No files matched your search
@@ -76,10 +76,10 @@ static void eval_handle_noop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
/* ===
|
||||
* Fonte TTF - cache por tamanho
|
||||
* === */
|
||||
#define FONT_CACHE_CAP 8
|
||||
#define FONT_CACHE_CAP 64 /* o zoom da area de scripts pede muitos tamanhos */
|
||||
#define FONT_SEARCH_COUNT 9
|
||||
|
||||
typedef struct { int size; TTF_Font* font; } FontEntry;
|
||||
typedef struct { int size; TTF_Font* font; unsigned used; } FontEntry;
|
||||
|
||||
static FontEntry s_font_cache[FONT_CACHE_CAP];
|
||||
static int s_font_cache_n = 0;
|
||||
@@ -114,18 +114,27 @@ static TTF_Font* bundled_font(int size) {
|
||||
}
|
||||
|
||||
static TTF_Font* get_font(int size) {
|
||||
static unsigned tick = 0;
|
||||
tick++;
|
||||
for (int i = 0; i < s_font_cache_n; i++)
|
||||
if (s_font_cache[i].size == size) return s_font_cache[i].font;
|
||||
if (s_font_cache[i].size == size) { s_font_cache[i].used = tick; return s_font_cache[i].font; }
|
||||
|
||||
TTF_Font* f = bundled_font(size);
|
||||
for (int i = 0; i < FONT_SEARCH_COUNT && !f; i++)
|
||||
f = TTF_OpenFont(s_font_paths[i], size);
|
||||
if (!f) return NULL;
|
||||
|
||||
if (f && s_font_cache_n < FONT_CACHE_CAP) {
|
||||
s_font_cache[s_font_cache_n].size = size;
|
||||
s_font_cache[s_font_cache_n].font = f;
|
||||
s_font_cache_n++;
|
||||
}
|
||||
/* cheio: fecha o usado ha mais tempo (antes vazava uma fonte por chamada) */
|
||||
int slot = s_font_cache_n;
|
||||
if (slot >= FONT_CACHE_CAP) {
|
||||
slot = 0;
|
||||
for (int i = 1; i < FONT_CACHE_CAP; i++)
|
||||
if (s_font_cache[i].used < s_font_cache[slot].used) slot = i;
|
||||
TTF_CloseFont(s_font_cache[slot].font);
|
||||
} else s_font_cache_n++;
|
||||
s_font_cache[slot].size = size;
|
||||
s_font_cache[slot].font = f;
|
||||
s_font_cache[slot].used = tick;
|
||||
return f;
|
||||
}
|
||||
|
||||
@@ -456,12 +465,104 @@ typedef struct SdlWin {
|
||||
Uint32 id;
|
||||
TinyBlocks_GfxInput in;
|
||||
int pos_fresh; /* posicao do mouse veio de um evento neste quadro */
|
||||
unsigned long xwin; /* janela X11 (0 fora do X11) */
|
||||
float pinch_acc; /* pinca do touchpad acumulada (fator; 0 = nenhuma) */
|
||||
float pinch_last; /* escala do gesto no evento anterior */
|
||||
struct SdlWin* next;
|
||||
} SdlWin;
|
||||
|
||||
static SdlWin* s_wins = NULL;
|
||||
static bool s_be_inited = false;
|
||||
|
||||
/* ===
|
||||
* Pinca do touchpad no X11: o SDL2 nao repassa os gestos do XInput 2.4, entao
|
||||
* a janela os pede (XISelectEvents) e um observador de eventos le o
|
||||
* SDL_SYSWMEVENT enquanto o cookie ainda tem os dados. libXi/libX11 vem por
|
||||
* dlopen (o SDL ja os carregou); sem eles, ou fora do X11, nada muda.
|
||||
* === */
|
||||
#if defined(__linux__) && !defined(__ANDROID__) && defined(__has_include)
|
||||
#if __has_include(<X11/extensions/XInput2.h>)
|
||||
#define TB_XI_GESTURES 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef TB_XI_GESTURES
|
||||
#include <dlfcn.h>
|
||||
#include <SDL2/SDL_syswm.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/extensions/XInput2.h>
|
||||
|
||||
static int s_xi_state = 0; /* 0 = nao tentado, 1 = ok, -1 = indisponivel */
|
||||
static int s_xi_opcode = 0;
|
||||
static int (*p_XISelectEvents)(Display*, Window, XIEventMask*, int);
|
||||
|
||||
static int SDLCALL xi_watch(void* user, SDL_Event* e) {
|
||||
(void)user;
|
||||
if (e->type != SDL_SYSWMEVENT || !e->syswm.msg || e->syswm.msg->subsystem != SDL_SYSWM_X11) return 1;
|
||||
XGenericEventCookie* ck = &e->syswm.msg->msg.x11.event.xcookie;
|
||||
if (ck->type != GenericEvent || ck->extension != s_xi_opcode || !ck->data) return 1;
|
||||
if (ck->evtype < XI_GesturePinchBegin || ck->evtype > XI_GesturePinchEnd) return 1;
|
||||
const XIGesturePinchEvent* g = (const XIGesturePinchEvent*)ck->data;
|
||||
for (SdlWin* w = s_wins; w; w = w->next) {
|
||||
if (w->xwin != (unsigned long)g->event) continue;
|
||||
if (ck->evtype == XI_GesturePinchBegin) { w->pinch_last = 1.f; break; }
|
||||
float sc = (float)g->scale;
|
||||
if (sc > 0.f && w->pinch_last > 0.f) {
|
||||
w->pinch_acc = (w->pinch_acc > 0.f ? w->pinch_acc : 1.f) * (sc / w->pinch_last);
|
||||
w->pinch_last = sc;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* dlsym -> ponteiro de funcao sem cast fora do padrao */
|
||||
static void sym_fn(void* dst, void* lib, const char* name) {
|
||||
void* p = dlsym(lib, name);
|
||||
memcpy(dst, &p, sizeof(p));
|
||||
}
|
||||
|
||||
static bool xi_init(Display* dpy) {
|
||||
if (s_xi_state) return s_xi_state > 0;
|
||||
s_xi_state = -1;
|
||||
void* xi = dlopen("libXi.so.6", RTLD_NOW | RTLD_LOCAL);
|
||||
void* x11 = dlopen("libX11.so.6", RTLD_NOW | RTLD_LOCAL);
|
||||
if (!xi || !x11) return false;
|
||||
Bool (*q_ext)(Display*, const char*, int*, int*, int*);
|
||||
Status (*q_ver)(Display*, int*, int*);
|
||||
sym_fn(&q_ext, x11, "XQueryExtension");
|
||||
sym_fn(&q_ver, xi, "XIQueryVersion");
|
||||
sym_fn(&p_XISelectEvents, xi, "XISelectEvents");
|
||||
int ev, err, major = 2, minor = 4;
|
||||
if (!q_ext || !q_ver || !p_XISelectEvents) return false;
|
||||
if (!q_ext(dpy, "XInputExtension", &s_xi_opcode, &ev, &err)) return false;
|
||||
if (q_ver(dpy, &major, &minor) != Success || major < 2 || (major == 2 && minor < 4)) return false;
|
||||
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
|
||||
SDL_AddEventWatch(xi_watch, NULL);
|
||||
s_xi_state = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Pede os gestos de pinca na janela. A mascara substitui a que o SDL pos
|
||||
(toque), entao os eventos de toque vao junto. */
|
||||
static void xi_select(SdlWin* sw) {
|
||||
SDL_SysWMinfo info;
|
||||
SDL_VERSION(&info.version);
|
||||
if (!SDL_GetWindowWMInfo(sw->win, &info) || info.subsystem != SDL_SYSWM_X11) return;
|
||||
Display* dpy = info.info.x11.display;
|
||||
if (!xi_init(dpy)) return;
|
||||
unsigned char bits[XIMaskLen(XI_GesturePinchEnd)] = {0};
|
||||
XISetMask(bits, XI_TouchBegin); XISetMask(bits, XI_TouchUpdate); XISetMask(bits, XI_TouchEnd);
|
||||
XISetMask(bits, XI_GesturePinchBegin); XISetMask(bits, XI_GesturePinchUpdate);
|
||||
XISetMask(bits, XI_GesturePinchEnd);
|
||||
XIEventMask m = { XIAllMasterDevices, (int)sizeof(bits), bits };
|
||||
p_XISelectEvents(dpy, info.info.x11.window, &m, 1);
|
||||
sw->xwin = (unsigned long)info.info.x11.window;
|
||||
}
|
||||
#else
|
||||
static void xi_select(SdlWin* sw) { (void)sw; }
|
||||
#endif
|
||||
|
||||
static int map_key(SDL_Scancode sc) {
|
||||
if (sc >= SDL_SCANCODE_A && sc <= SDL_SCANCODE_Z) return TB_KEY_A + (sc - SDL_SCANCODE_A);
|
||||
if (sc >= SDL_SCANCODE_1 && sc <= SDL_SCANCODE_9) return TB_KEY_0 + 1 + (sc - SDL_SCANCODE_1);
|
||||
@@ -578,6 +679,7 @@ static void* be_window_open(const char* title, int w, int h) {
|
||||
sw->in.win_w = w; sw->in.win_h = h;
|
||||
sw->next = s_wins;
|
||||
s_wins = sw;
|
||||
xi_select(sw);
|
||||
return sw;
|
||||
}
|
||||
|
||||
@@ -676,6 +778,10 @@ static void be_poll(void) {
|
||||
while (!s_has_pending && SDL_PollEvent(&ev)) {
|
||||
if (!be_apply(&ev)) { s_pending = ev; s_has_pending = true; }
|
||||
}
|
||||
for (SdlWin* w = s_wins; w; w = w->next) { /* pinca do touchpad (xi_watch) */
|
||||
w->in.zoom = w->pinch_acc;
|
||||
w->pinch_acc = 0.f;
|
||||
}
|
||||
|
||||
/* Dispositivo: teclas seguradas (janela com o foco do teclado), dedos na
|
||||
tela e sensores (todas as janelas) */
|
||||
|
||||
Reference in new issue
Block a user