juegos

Juegos de terminal de Pancho: Catan (TUI, GUI, web, servidor, PicoCalc), ajedrez, calculadora y minijuegos
git clone https://git.lu3dhn.xyz/juegos.git
Log | Files | Refs

input.h (2825B)


      1 /* input.h - teclas abstractas, eventos y zonas clickeables, comunes a todos los
      2  * frontends (TUI, GUI SDL, PicoCalc). */
      3 #ifndef COMUN_INPUT_H
      4 #define COMUN_INPUT_H
      5 #include <stdint.h>
      6 #include <stdbool.h>
      7 
      8 /* Teclas especiales; el resto son caracteres (Latin-1). */
      9 enum Key {
     10     K_UP = 0x100, K_DOWN, K_LEFT, K_RIGHT, K_OK, K_BACK, K_TAB, K_PLUS, K_MINUS, K_BKSP,
     11     K_DEL, K_HOME, K_END, K_PGUP, K_PGDN,
     12     K_F1 = 0x120,                 /* K_F1 + n - 1 = Fn (hasta F10) */
     13     K_RAW = 0x200                 /* K_RAW | codigo: tecla de la PicoCalc sin traducir */
     14 };
     15 
     16 /* APP_KEYS (depuracion sin teclado): '~' Enter, '<' Esc, '>' Tab, '^' 'v' '{' '}' flechas,
     17  * '.' pausa de 300 ms, '!' salir; '\' + caracter lo manda tal cual, '\1'..'\9' '\0' F1..F10,
     18  * '\d' Supr, '\h' Inicio, '\e' Fin, '\u' RePag, '\w' AvPag. Devuelve la tecla, KS_PAUSE, KS_QUIT o 0 si se termino. */
     19 enum { KS_PAUSE = -2, KS_QUIT = -3 };
     20 static inline int key_script(const char **p)
     21 {
     22     int c = (unsigned char)**p;
     23     if (!c) return 0;
     24     (*p)++;
     25     switch (c) {
     26     case '~': return K_OK;
     27     case '<': return K_BACK;
     28     case '>': return K_TAB;
     29     case '^': return K_UP;
     30     case 'v': return K_DOWN;
     31     case '{': return K_LEFT;
     32     case '}': return K_RIGHT;
     33     case '.': return KS_PAUSE;
     34     case '!': return KS_QUIT;
     35     case '\\':
     36         c = (unsigned char)**p;
     37         if (!c) return '\\';
     38         (*p)++;
     39         if (c >= '1' && c <= '9') return K_F1 + c - '1';
     40         if (c == '0') return K_F1 + 9;
     41         if (c == 'd') return K_DEL;
     42         if (c == 'h') return K_HOME;
     43         if (c == 'e') return K_END;
     44         if (c == 'u') return K_PGUP;
     45         if (c == 'w') return K_PGDN;
     46         return c;
     47     }
     48     return c;
     49 }
     50 
     51 /* Eventos de los frontends de PC. */
     52 enum { EV_NONE, EV_KEY, EV_CLICK, EV_RESIZE, EV_QUIT, EV_WHEEL };
     53 typedef struct { int type, key, x, y; } InEvent;     /* EV_WHEEL: key +1 arriba / -1 abajo, en (x, y) */
     54 
     55 /* Zonas clickeables que registra el frontend al dibujar. kind lo define cada
     56  * juego (0..HIT_APP-1); HIT_APP son las de la app (formularios, dialogos). */
     57 enum { HIT_APP = 7 };
     58 typedef struct { int16_t x, y, w, h; uint8_t kind; int16_t id; } Hit;
     59 typedef struct { Hit h[96]; int n; } HitList;
     60 
     61 static inline void hit_add(HitList *l, int x, int y, int w, int h, int kind, int id)
     62 {
     63     if (l && l->n < 96) { Hit *t = &l->h[l->n++]; t->x = (int16_t)x; t->y = (int16_t)y; t->w = (int16_t)w; t->h = (int16_t)h; t->kind = (uint8_t)kind; t->id = (int16_t)id; }
     64 }
     65 
     66 static inline const Hit *hit_find(const HitList *l, int x, int y)
     67 {
     68     for (int i = l->n - 1; i >= 0; i--) {
     69         const Hit *t = &l->h[i];
     70         if (x >= t->x && x < t->x + t->w && y >= t->y && y < t->y + t->h) return t;
     71     }
     72     return 0;
     73 }
     74 
     75 /* Layouts de pantalla en pixeles. */
     76 enum { LAYOUT_COMPACT, LAYOUT_WIDE };
     77 
     78 #endif