code wiki / _hdl_build / nx_game_touch.nx
nx_game_touch.nx source
↩ module page · 36 lines · 1433 B
1// nx_game_touch.nx -- SOVEREIGN virtual-d-pad touch mapping (the SSOT spec for mobile game input). Maps a
2// touch point (tx,ty) on a (w x h) canvas to the SAME keyCode the existing wasm games already read: 3 columns
3// -> LEFT(37)/RIGHT(39); the center column -> UP(38)/ACTION-SPACE(32)/DOWN(40) by row. So EVERY existing game
4// becomes touch-playable with ZERO engine change. The last-mile JS in game_page.tpl.html MIRRORS this mapping
5// (pure transport per the arcade architecture -- the wasm still owns input meaning). NO float. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7
8const NX_KEY_SPACE: i64 = 32
9const NX_KEY_LEFT: i64 = 37
10const NX_KEY_UP: i64 = 38
11const NX_KEY_RIGHT: i64 = 39
12const NX_KEY_DOWN: i64 = 40
13
14// which third (0,1,2) of `span` does `a` fall in, clamped.
15func tz_third(a: i64, span: i64) -> i64 {
16 if span <= 0 { return 0 }
17 var z: i64 = (a * 3) / span
18 if z < 0 { z = 0 }
19 if z > 2 { z = 2 }
20 return z
21}
22
23// map a touch (tx,ty) on a w x h canvas to a game keyCode. invalid canvas -> 0 (no key).
24func nx_touch_zone(tx: i64, ty: i64, w: i64, h: i64) -> i64 {
25 if w <= 0 { return 0 }
26 if h <= 0 { return 0 }
27 let col: i64 = tz_third(tx, w)
28 let row: i64 = tz_third(ty, h)
29 if col == 1 {
30 if row == 0 { return NX_KEY_UP }
31 if row == 2 { return NX_KEY_DOWN }
32 return NX_KEY_SPACE
33 }
34 if col == 0 { return NX_KEY_LEFT }
35 return NX_KEY_RIGHT
36}