diff --git a/.gitignore b/.gitignore index 9560f56..cbf10b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /build/ /ziran.local.toml +/work/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 63efb8e..ccb50fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Unreleased] +- Swipe steering exposes accepted-step and tap events separately from retained direction. + - A floating touch stick steers in any direction and shows a high-contrast anchor, finger and heading while held. - Games can share title, pause, settings and movement-control navigation across native and portable hosts. - A reusable swipe/direction-pad chooser supports continuous touch steering and changing controls during a paused game. diff --git a/README.md b/README.md index 49ce3e1..5845065 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ module provides the existing raylib game API as a separate adapter. Kryon keeps its UI geometry, drawing primitives, widgets, and raylib UI host. `game2d/navigation` owns the portable title/play/pause/settings/control-choice -state machine. `game2d/controls` supplies `GameControlsChoice` and continuous, -re-anchored `GameSwipeDirection` steering. The chooser uses Kryon's standard +state machine. `game2d/controls` supplies `GameControlsChoice` and re-anchored +`GameSwipeDirection` steering. The chooser uses Kryon's standard DirectionPad and buttons. Callers supply translated labels, persist their chosen mode, and retain their own game rules, title artwork and account actions. Neither module depends on a publisher, account service, game catalog or raylib. @@ -34,3 +34,33 @@ host in the application; a Ziran package does not install a renderer by itself. For local development, add an ignored `ziran.local.toml` with overrides for `Game2D`, `Kryon`, and `ziran` in the application. Commit `ziran.lock` for reproducible builds. + +`GameSwipeDirection(*state, down, x, y, minimum = 6.0) -> s32` keeps its +retained-direction return: `0` left, `1` up, `2` right, `3` down, `-1` idle. +Sample once per input frame with a `GameSwipe` state. Initial down anchors +without moving. Each displacement reaching the threshold on either axis +sets `state.step = true` and reanchors, including another crossing in the +same direction. The dominant axis wins; ties choose horizontal. A nonpositive +minimum uses 6. Below threshold, the return retains direction but `step` is +false; it must not trigger another discrete move. + +`state.moved` records any accepted crossing during the press. On release, +`state.tapped` is true only if the press never crossed the threshold; direction +returns to -1 and `active`, `step`, and `moved` clear. Both `step` and `tapped` +are per-sample events. For cancellation or leaving play, discard the gesture +with `state = GameSwipe.{}` instead of feeding a release, then sample the new +press normally. Navigation does not own input state. Read direction from the +function return, or from `state.direction` after sampling. + +For Wurm adoption, map `ifx state.step then direction else -1` to its existing one-event +turn input; never compare with the previous direction to detect a turn. Its +simulation and timed turns remain game-owned. Shovey's tap-to-step, +tap-to-fire, held-repeat timing and board-coordinate mapping remain game-owned; +consume `step` only for accepted swipe movement and route taps separately. +Kryon retains DirectionPad, SwipeArea and pointer capture. Choano uses +`GameStickSteer` for continuous headings and needs no swipe API migration. + +Run `env -u DISPLAY -u WAYLAND_DISPLAY sh tests/swipe_result_test.sh` for the +navigation/control/floating-stick suite through native C and portable `.zx` +from source and saved IR, plus an external public-import consumer. `make -j1 test` +includes this suite and the existing geometry and raylib ABI local tests. diff --git a/src/controls.zi b/src/controls.zi index 769524e..7b9f194 100644 --- a/src/controls.zi +++ b/src/controls.zi @@ -1,4 +1,4 @@ -// Shared control selection and continuous touch steering for game apps. +// Shared control selection and touch steering for game apps. // All copy is caller-owned so consumers can supply their own translations. #import "kryon/session" #import "kryon/geometry" @@ -13,13 +13,32 @@ #import "kryon/text_align" #import "kryon/direction_pad" Math :: #import "std/math" -GameSwipe :: struct { active: bool; x: float32; y: float32; direction: s32 } -// Re-anchor after each accepted turn, as a finger follows successive corners. +GameSwipe :: struct { + active: bool + x: float32 + y: float32 + direction: s32 + // Per sample: an accepted crossing, even if direction did not change. + step: bool + // Gesture-wide: at least one crossing has been accepted. + moved: bool + // Per sample: release of a press that never crossed the threshold. + tapped: bool +} +// Returns the retained direction (0 left, 1 up, 2 right, 3 down; -1 idle). +// Read state.step for discrete movement; each crossing re-anchors the gesture. +// Sample once per input frame. Reset to GameSwipe.{} on cancellation/navigation +// rather than sending a release: a cancelled press must not produce a tap. GameSwipeDirection :: (state: *GameSwipe, down: bool, x: float32, y: float32, minimum: float32 = 6.0) -> s32 { - if !down { state.active = false; state.direction = -1; return -1 } + state.step = false; state.tapped = false + if !down { + state.tapped = state.active && !state.moved + state.active = false; state.moved = false; state.direction = -1 + return -1 + } if !state.active { - state.active = true; state.x = x; state.y = y + state.active = true; state.moved = false; state.x = x; state.y = y state.direction = -1 return -1 } @@ -30,6 +49,7 @@ GameSwipeDirection :: (state: *GameSwipe, down: bool, x: float32, y: float32, if minimum <= 0.0 { minimum = 6.0 } if across < minimum && along < minimum { return state.direction } state.x = x; state.y = y + state.step = true; state.moved = true state.direction = ifx across >= along then (ifx dx < 0.0 then 0 else 2) else (ifx dy < 0.0 then 1 else 3) return state.direction } diff --git a/tests/navigation_test.zi b/tests/navigation_test.zi index 5d089d1..6959ebb 100644 --- a/tests/navigation_test.zi +++ b/tests/navigation_test.zi @@ -43,11 +43,38 @@ main :: () -> s32 { unused GameNavigationBack(*state) if !Expect(state.page == GAME_TITLE && !GameNavigationBack(*state), "settings returns to title; title does not trap Back") { return 1 } swipe: GameSwipe - if !Expect(GameSwipeDirection(*swipe, true, 20.0, 20.0) == -1, "press does not invent a direction") { return 1 } - if !Expect(GameSwipeDirection(*swipe, true, 40.0, 20.0) == 2, "swipe right") { return 1 } - if !Expect(GameSwipeDirection(*swipe, true, 40.0, 22.0) == 2, "held direction ignores jitter") { return 1 } - if !Expect(GameSwipeDirection(*swipe, true, 40.0, 2.0) == 1, "re-anchor allows a corner") { return 1 } - if !Expect(GameSwipeDirection(*swipe, false, 40.0, 2.0) == -1, "release stops movement") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 20.0, 20.0) == -1 && swipe.active && !swipe.step && !swipe.moved && !swipe.tapped, "press does not invent movement") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 25.0, 25.0) == -1 && !swipe.step && !swipe.moved, "both axes below threshold ignore jitter") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 26.0, 20.0) == 2 && swipe.step && swipe.moved && !swipe.tapped, "exact threshold accepts first right step") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 32.0, 20.0) == 2 && swipe.step && swipe.x == 32.0, "same direction crossing after reanchor is another event") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 32.0, 22.0) == 2 && !swipe.step && swipe.moved, "held direction ignores jitter without repeating a step") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 32.0, 22.0) == 2 && !swipe.step, "stationary held frame has no event") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 32.0, 14.0) == 1 && swipe.step, "reanchor allows an upward corner") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 26.0, 14.0) == 0 && swipe.step, "left corner") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 26.0, 20.0) == 3 && swipe.step, "down corner") { return 1 } + if !Expect(GameSwipeDirection(*swipe, false, 26.0, 20.0) == -1 && !swipe.active && !swipe.step && !swipe.moved && !swipe.tapped, "moved release stops movement without tapping") { return 1 } + unused GameSwipeDirection(*swipe, true, 50.0, 50.0) + unused GameSwipeDirection(*swipe, true, 55.0, 54.0) + if !Expect(GameSwipeDirection(*swipe, false, 55.0, 54.0) == -1 && swipe.tapped && !swipe.step, "subthreshold release is a tap without movement") { return 1 } + unused GameSwipeDirection(*swipe, false, 55.0, 54.0) + if !Expect(!swipe.tapped && !swipe.step, "release events last one sample") { return 1 } + unused GameSwipeDirection(*swipe, true, 10.0, 10.0) + swipe = GameSwipe.{} + unused GameSwipeDirection(*swipe, false, 10.0, 10.0) + if !Expect(!swipe.tapped && !swipe.step && !swipe.active, "cancel discards a possible tap") { return 1 } + unused GameSwipeDirection(*swipe, true, 0.0, 0.0) + unused GameSwipeDirection(*swipe, true, 6.0, 0.0) + unused GameNavigationPlay(*state) + GameNavigationPause(*state) + if !Expect(state.page == GAME_PAUSED, "navigation leaves play before input reset") { return 1 } + swipe = GameSwipe.{} + GameNavigationResume(*state) + if !Expect(GameSwipeDirection(*swipe, true, 100.0, 100.0) == -1 && !swipe.step && !swipe.moved, "navigation reset starts a fresh anchor without a stale step") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 106.0, 106.0, 0.0) == 2 && swipe.step, "default threshold fallback and horizontal tie break") { return 1 } + unused GameSwipeDirection(*swipe, false, 106.0, 106.0) + unused GameSwipeDirection(*swipe, true, 0.0, 0.0, 12.0) + if !Expect(GameSwipeDirection(*swipe, true, 11.0, 0.0, 12.0) == -1 && !swipe.step, "caller threshold suppresses smaller movements") { return 1 } + if !Expect(GameSwipeDirection(*swipe, true, 12.0, 0.0, 12.0) == 2 && swipe.step, "caller threshold accepts exact crossing") { return 1 } stick: GameStick if !Expect(!GameStickIndicatorPlace(stick, 0.0, 0.0).visible, "no indicator without a press") { return 1 } if !Expect(!GameStickSteer(*stick, true, 100.0, 100.0).steering, "a press alone does not steer") { return 1 } diff --git a/tests/swipe_result_test.sh b/tests/swipe_result_test.sh new file mode 100644 index 0000000..9352cc2 --- /dev/null +++ b/tests/swipe_result_test.sh @@ -0,0 +1,78 @@ +#!/bin/sh +# Shared swipe events, navigation and floating-stick behavior, without a UI host. +set -eu +unset DISPLAY WAYLAND_DISPLAY +repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$repo" +export ZIRAN_TEST_ALLOW_LOCAL_GIT=1 +ziran=${ZIRAN:-ziran} +lock_flags= +if test ! -f ziran.local.toml; then lock_flags=--locked; fi +mkdir -p work +work="$repo/work/swipe-result" +mkdir -p "$work" +# Prevent two invocations from sharing generated outputs. +mkdir "$work/.running" +trap 'rmdir "$work/.running"' EXIT HUP INT TERM + +# The portable runner prints main's result; assert it as well as process status. +run_portable() { + "$ziran" run --project "$1" > "$work/portable.log" + tail -n 10 "$work/portable.log" + test "$(tail -n 1 "$work/portable.log")" = 0 +} + +"$ziran" build --project --target=c --exe --entry navigation_test:main \ + -o "$work/native" tests/navigation_test.zi +"$work/native/navigation_test" +# The stick paint test commits Box paint; raster stubs stand in for a host. +set -- --bind kryon/raster_shape:RasterRoundedRectangle=navigation_test:RasterRoundedRectangle \ + --bind kryon/raster_shape:RasterRoundedRectangleOutline=navigation_test:RasterRoundedRectangleOutline +"$ziran" bundle --project "$@" --entry navigation_test:main \ + -o "$work/navigation-test.zx" tests/navigation_test.zi +run_portable "$work/navigation-test.zx" +"$ziran" ir --project --entry navigation_test:main -o "$work/ir" tests/navigation_test.zi +"$ziran" bundle --project "$@" --entry navigation_test:main \ + -o "$work/navigation-ir.zx" "$work/ir/navigation_test.zir" +run_portable "$work/navigation-ir.zx" + +# Exercise the maintained suite from an external project's public imports. +# This is a consumer fixture, not a dependency checkout or copied library. +compiler=$("$ziran" pkg path ziran $lock_flags) +kryon=$("$ziran" pkg path kryon $lock_flags) +python3 - "$repo" "$work/consumer" "$compiler" "$kryon" <<'PY' +import json +from pathlib import Path +import sys +repo, consumer, compiler, kryon = map(Path, sys.argv[1:]) +(consumer / 'src').mkdir(parents=True, exist_ok=True) +source = (repo / 'tests/navigation_test.zi').read_text() +for module in ('controls', 'navigation'): + source = source.replace(f'#import "{module}"', f'#import "Game2D/{module}"') +(consumer / 'src/navigation_test.zi').write_text(source) +(consumer / 'ziran.toml').write_text('''[package] +name = "swipe_consumer" +module_roots = ["src"] +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "master" +[dependencies.Game2D] +git = "https://github.com/kryonlabs/game2d.git" +ref = "master" +[dependencies.kryon] +git = "https://github.com/kryonlabs/kryon.git" +ref = "master" +''') +(consumer / 'ziran.local.toml').write_text('[overrides]\n' + ''.join( + f'{name} = {json.dumps(str(path))}\n' for name, path in + (('Game2D', repo), ('kryon', kryon), ('ziran', compiler)))) +PY +cd "$work/consumer" +"$ziran" lock --offline +"$ziran" build --project --offline --target=c --exe --entry navigation_test:main \ + -o "$work/consumer-native" src/navigation_test.zi +"$work/consumer-native/navigation_test" +"$ziran" bundle --project --offline "$@" --entry navigation_test:main \ + -o "$work/consumer.zx" src/navigation_test.zi +run_portable "$work/consumer.zx" +echo "Game2D local swipe events, navigation, floating-stick and public-import consumer tests passed" diff --git a/tests/test.sh b/tests/test.sh index bb59771..39b861d 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -10,7 +10,8 @@ ziran=${ZIRAN:-ziran} lock_flags= if test ! -f ziran.local.toml; then lock_flags=--locked; fi compiler=$("$ziran" pkg path ziran $lock_flags) -work=$(mktemp -d) +mkdir -p work +work=$(mktemp -d "$repo/work/test.XXXXXX") trap 'rm -rf "$work"' EXIT HUP INT TERM "$ziran" check --project src/Game2D/module.zi @@ -39,13 +40,5 @@ if test -f "$raylib_header/raylib.h"; then test "$(build/generated-abi)" = "$(build/raylib-abi)" fi -"$ziran" build --project --target=c --exe --entry navigation_test:main -o build/navigation-test tests/navigation_test.zi -build/navigation-test/navigation_test -# The stick paint test commits Box paint; its raster stubs stand in for a host. -shape=kryon/raster_shape -"$ziran" bundle --project \ - --bind "$shape:RasterRoundedRectangle=navigation_test:RasterRoundedRectangle" \ - --bind "$shape:RasterRoundedRectangleOutline=navigation_test:RasterRoundedRectangleOutline" \ - --entry navigation_test:main -o build/navigation-test.zx tests/navigation_test.zi -"$ziran" run --project build/navigation-test.zx -echo "Game2D source, native, portable, and raylib ABI checks passed" +sh tests/swipe_result_test.sh +echo "Game2D local source, native, portable, and raylib ABI tests passed"