Spaces:
Sleeping
Sleeping
Download tests/test_virtual_controls_touch.py from Cnass/rpgmakerv2: direct link, hf CLI and curl.
- Browser
- Download file 5.59 kB
-
https://huggingface.co/spaces/Cnass/rpgmakerv2/resolve/main/tests/test_virtual_controls_touch.py
- Command line
-
hf download hf://spaces/Cnass/rpgmakerv2/tests/test_virtual_controls_touch.py
-
curl -L -o test_virtual_controls_touch.py https://huggingface.co/spaces/Cnass/rpgmakerv2/resolve/main/tests/test_virtual_controls_touch.py
5.59 kB
| """ | |
| RIA_VirtualControls: touches on the pad and its edit toolbar must never reach | |
| the game's own touch handling (RPG Maker's TouchInput listens for | |
| touchstart/mousedown on document; some games add earlier, capture-phase | |
| listeners). Otherwise resizing a control or tapping −/+ also "taps the | |
| screen" and advances the dialogue. | |
| Runs the real plugin in headless Chrome: python3 tests/test_virtual_controls_touch.py | |
| """ | |
| import json | |
| import re | |
| import shutil | |
| import subprocess | |
| import tempfile | |
| from pathlib import Path | |
| from _t import ROOT, run | |
| PLUGIN = ROOT / "android-template/app/src/main/assets/www/js/plugins/RIA_VirtualControls.js" | |
| HARNESS = """<!doctype html><html><body><div id="out">pending</div> | |
| <script> | |
| window.Input = { _currentState: {} }; | |
| const seen = { game: [], earlyPlugin: [] }; | |
| // A game plugin that listens before everyone else on document (capture). | |
| ['touchstart', 'touchmove', 'touchend', 'mousedown', 'mouseup'].forEach((t) => | |
| document.addEventListener(t, () => seen.earlyPlugin.push(t), true)); | |
| // RPG Maker's TouchInput: bubble-phase listeners on document. | |
| // MV's TouchInput also listens for pointerdown (a second, non-primary finger | |
| // there means "cancel" -> menu on the map). | |
| ['touchstart', 'touchmove', 'touchend', 'mousedown', 'mouseup', 'pointerdown', 'pointermove', 'pointerup'].forEach((t) => | |
| document.addEventListener(t, (e) => { seen.game.push(t); })); | |
| </script> | |
| <script src="RIA_VirtualControls.js"></script> | |
| <script> | |
| // The plugin boots on DOMContentLoaded; this listener is added after its own. | |
| document.addEventListener('DOMContentLoaded', () => { | |
| function touch(type, el) { el.dispatchEvent(new TouchEvent(type, { bubbles: true, cancelable: true })); } | |
| function mouse(type, el) { el.dispatchEvent(new MouseEvent(type, { bubbles: true, cancelable: true })); } | |
| const results = {}; | |
| const btnA = document.querySelector('.ria-btn-a'); | |
| const joy = document.getElementById('ria-joy-zone'); | |
| const canvasLike = document.getElementById('out'); | |
| // Normal mode: the A button still works through pointer events... | |
| btnA.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, pointerId: 1 })); | |
| results.aPressed = Input._currentState.ok === true; | |
| btnA.dispatchEvent(new PointerEvent('pointerup', { bubbles: true, pointerId: 1 })); | |
| // A second finger on B while the first holds the joystick: non-primary pointer. | |
| const btnB = document.querySelector('.ria-btn-b'); | |
| joy.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, pointerId: 1, isPrimary: true, pointerType: 'touch' })); | |
| joy.dispatchEvent(new PointerEvent('pointermove', { bubbles: true, pointerId: 1, isPrimary: true, pointerType: 'touch' })); | |
| btnB.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, pointerId: 1, isPrimary: false, pointerType: 'touch' })); | |
| results.bPressed = Input._currentState.escape === true; | |
| btnB.dispatchEvent(new PointerEvent('pointerup', { bubbles: true, pointerId: 1, isPrimary: false, pointerType: 'touch' })); | |
| joy.dispatchEvent(new PointerEvent('pointerup', { bubbles: true, pointerId: 1, isPrimary: true, pointerType: 'touch' })); | |
| // ...but its touch/mouse events do not reach the game. | |
| touch('touchstart', btnA); touch('touchend', btnA); mouse('mousedown', btnA); mouse('mouseup', btnA); | |
| results.normal = { game: seen.game.splice(0), early: seen.earlyPlugin.splice(0) }; | |
| // Edit mode: two-finger resize on the joystick and taps on the toolbar. | |
| RIA_VirtualControls.setEditMode(true); | |
| touch('touchstart', joy); touch('touchmove', joy); touch('touchend', joy); | |
| const bigger = document.querySelector('#ria-ctl-edit-bar [aria-label="Büyüt"]'); | |
| touch('touchstart', bigger); touch('touchend', bigger); mouse('mousedown', bigger); mouse('mouseup', bigger); | |
| let clicked = false; | |
| bigger.addEventListener('click', () => { clicked = true; }); | |
| bigger.click(); | |
| results.toolbarClick = clicked; | |
| results.edit = { game: seen.game.splice(0), early: seen.earlyPlugin.splice(0) }; | |
| RIA_VirtualControls.setEditMode(false, { save: false }); | |
| // The rest of the screen still reaches the game. | |
| touch('touchstart', canvasLike); mouse('mousedown', canvasLike); | |
| canvasLike.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, pointerId: 4 })); | |
| results.elsewhere = { game: seen.game.splice(0), early: seen.earlyPlugin.splice(0) }; | |
| document.getElementById('out').textContent = JSON.stringify(results); | |
| }); | |
| </script></body></html>""" | |
| def _run_harness() -> dict: | |
| tmp = Path(tempfile.mkdtemp()) | |
| shutil.copy(PLUGIN, tmp / PLUGIN.name) | |
| (tmp / "index.html").write_text(HARNESS, encoding="utf-8") | |
| dom = subprocess.run( | |
| ["timeout", "60", "google-chrome", "--headless=new", "--no-sandbox", "--disable-gpu", | |
| "--allow-file-access-from-files", "--dump-dom", (tmp / "index.html").as_uri()], | |
| capture_output=True, text=True, check=False, | |
| ).stdout | |
| found = re.search(r'id="out">([^<]*)<', dom) | |
| assert found and found.group(1) != "pending", dom[-2000:] | |
| return json.loads(found.group(1).replace(""", '"')) | |
| def test_controls_keep_touches_from_the_game(): | |
| if not shutil.which("google-chrome"): | |
| print("ATLANDI: google-chrome yok") | |
| return | |
| r = _run_harness() | |
| assert r["aPressed"], r | |
| assert r["bPressed"], r | |
| assert r["normal"] == {"game": [], "early": []}, r["normal"] | |
| assert r["edit"] == {"game": [], "early": []}, r["edit"] | |
| assert r["toolbarClick"], r | |
| assert r["elsewhere"]["game"] == ["touchstart", "mousedown", "pointerdown"], r["elsewhere"] | |
| assert r["elsewhere"]["early"] == ["touchstart", "mousedown"], r["elsewhere"] | |
| if __name__ == "__main__": | |
| run(globals()) | |