Compare commits

...

1 Commits

Author SHA1 Message Date
4228e1797b chore(gamepads): Experimenting with gamepads and such implementations
Signed-off-by: quexeky <git@quexeky.dev>
2024-12-27 21:42:08 +11:00
10 changed files with 248 additions and 8 deletions

View File

@ -7,6 +7,8 @@
<script setup lang="ts">
import "~/composables/downloads.js";
import "~/plugins"
import "~/gamepad"
import { invoke } from "@tauri-apps/api/core";
import { AppStatus } from "~/types";

67
gamepad.ts Normal file
View File

@ -0,0 +1,67 @@
import gameControl, { XBoxButton, Button, type GCGamepad } from 'esm-gamecontroller.js';
let mainGamepad: Ref<number | undefined> = ref(undefined);
let buttonIndex = 0;
const buttons = computed((): any[] => {
let as = document.getElementsByTagName('a');
let buttons = document.getElementsByTagName('button');
return [].concat(Array.from(as)).concat(Array.from(buttons));
})
const wrap = (num: number, min: number, max: number) => ((((num - min) % (max - min)) + (max - min)) % (max - min)) + min;
setInterval(() => {
mainGamepad.value = navigator.getGamepads().filter(g => g !== null)[0]?.axes[1];
console.log(navigator.getGamepads().filter(g => g !== null)[0]?.axes)
}, 100);
watch(mainGamepad, (v) => {
console.log(v)
if (!v || v == 0) return;
buttonIndex = wrap(buttonIndex + v > 0 ? 1 : -1, 0, buttons.value.length);
console.log(`Focusing ${buttonIndex}`)
console.log(buttons.value[buttonIndex]);
buttons.value[buttonIndex].focus()
})
/*
setInterval(() => {
console.log(gamepads[0]);
console.log(gamepads[0].checkStatus())
}, 1000);
*/
/*
window.ongamepadconnected = (e) => {
console.log("ongamepadconnected", e);
}
function selectButton(index: number) {
buttonIndex = (buttonIndex + index) % buttons.value.length;
console.log(`Selecting button ${buttonIndex}`);
buttons.value[buttonIndex].focus()
}
function processGamepads() {
while (true) {
console.log("Processing gamepads");
let gamepad = navigator.getGamepads()[0];
if (!gamepad) continue;
let direction = gamepad.axes[1];
if (direction > 0.1) {
selectButton(1)
console.log("Selecting button 1")
}
else if (direction < -0.1) {
selectButton(-1)
console.log("Selecting button -1")
}
}
}
*/

View File

@ -14,15 +14,19 @@
"dependencies": {
"@headlessui/vue": "^1.7.23",
"@heroicons/vue": "^2.1.5",
"@maulingmonkey/gamepad": "^0.0.5",
"@nuxtjs/tailwindcss": "^6.12.2",
"@tauri-apps/api": ">=2.0.0",
"@tauri-apps/plugin-deep-link": "~2",
"@tauri-apps/plugin-dialog": "^2.0.1",
"@tauri-apps/plugin-os": "~2",
"@tauri-apps/plugin-shell": ">=2.0.0",
"esm-gamecontroller.js": "^1.0.4",
"gamepad-events": "^0.7.0",
"markdown-it": "^14.1.0",
"nuxt": "^3.13.0",
"scss": "^0.2.4",
"tauri-plugin-gamepad-api": "^0.0.5",
"vue": "latest",
"vue-router": "latest",
"vuedraggable": "^4.1.0"

View File

@ -1,15 +1,13 @@
<template>
<div class="bg-zinc-950 p-4 min-h-full space-y-4">
<div class="h-16 overflow-hidden relative rounded-xl flex flex-row border border-zinc-900">
<div
class="bg-zinc-900 z-10 w-32 flex flex-col gap-x-2 text-blue-400 font-display items-left justify-center pl-2">
<div class="bg-zinc-900 z-10 w-32 flex flex-col gap-x-2 text-blue-400 font-display items-left justify-center pl-2">
<span class="font-semibold">{{ formatKilobytes(stats.speed) }}</span>
<span v-if="stats.time > 0" class="text-sm">{{ formatTime(stats.time) }} left</span>
</div>
<div class="absolute inset-0 h-full flex flex-row items-end justify-end">
<div v-for="bar in speedHistory" :style="{ height: `${bar / speedMax * 100}%` }"
class="w-[8px] bg-blue-600/40" />
<div v-for="bar in speedHistory" :style="{ height: `${bar / speedMax * 100}%` }" class="w-[8px] bg-blue-600/40" />
</div>
</div>
<draggable v-model="queue.queue" @end="onEnd">
@ -40,8 +38,7 @@
</div>
</div>
<button @click="() => cancelGame(element.id)" class="group">
<XMarkIcon class="transition size-8 flex-none text-zinc-600 group-hover:text-zinc-300"
aria-hidden="true" />
<XMarkIcon class="transition size-8 flex-none text-zinc-600 group-hover:text-zinc-300" aria-hidden="true" />
</button>
</div>
</li>
@ -57,6 +54,7 @@
<script setup lang="ts">
import { XMarkIcon } from "@heroicons/vue/20/solid";
import { invoke } from "@tauri-apps/api/core";
import gameControl, { type GCGamepad } from "esm-gamecontroller.js";
import type { Game, GameStatus } from "~/types";
const windowWidth = ref(window.innerWidth);

1
plugins.ts Normal file
View File

@ -0,0 +1 @@
import 'tauri-plugin-gamepad-api'

139
src-tauri/Cargo.lock generated
View File

@ -991,6 +991,7 @@ dependencies = [
"tauri-build",
"tauri-plugin-deep-link",
"tauri-plugin-dialog",
"tauri-plugin-gamepad",
"tauri-plugin-os",
"tauri-plugin-shell",
"tauri-plugin-single-instance",
@ -1069,6 +1070,26 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3d8a32ae18130a3c84dd492d4215c3d913c3b07c6b63c2eb3eb7ff1101ab7bf"
[[package]]
name = "enum-iterator"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c280b9e6b3ae19e152d8e31cf47f18389781e119d4013a2a2bb0180e5facc635"
dependencies = [
"enum-iterator-derive",
]
[[package]]
name = "enum-iterator-derive"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ab991c1362ac86c61ab6f556cff143daa22e5a15e4e189df818b2fd19fe65b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
]
[[package]]
name = "enumflags2"
version = "0.7.10"
@ -1475,6 +1496,40 @@ dependencies = [
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "gilrs"
version = "0.11.0"
source = "git+https://github.com/eugenehp/gilrs.git#cbcff6a4cd722a132bf2ca3c55fa9147775c9fe9"
dependencies = [
"fnv",
"gilrs-core",
"log",
"serde",
"uuid",
"vec_map",
]
[[package]]
name = "gilrs-core"
version = "0.6.0"
source = "git+https://github.com/eugenehp/gilrs.git#cbcff6a4cd722a132bf2ca3c55fa9147775c9fe9"
dependencies = [
"core-foundation 0.10.0",
"inotify",
"io-kit-sys",
"js-sys",
"libc",
"libudev-sys",
"log",
"nix 0.29.0",
"serde",
"uuid",
"vec_map",
"wasm-bindgen",
"web-sys",
"windows",
]
[[package]]
name = "gimli"
version = "0.31.1"
@ -2040,6 +2095,26 @@ dependencies = [
"cfb",
]
[[package]]
name = "inotify"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3"
dependencies = [
"bitflags 2.6.0",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]]
name = "instant"
version = "0.1.13"
@ -2049,6 +2124,16 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "io-kit-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b"
dependencies = [
"core-foundation-sys",
"mach2",
]
[[package]]
name = "ipnet"
version = "2.10.1"
@ -2253,6 +2338,16 @@ dependencies = [
"libc",
]
[[package]]
name = "libudev-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
dependencies = [
"libc",
"pkg-config",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@ -2324,6 +2419,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]]
name = "mach2"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709"
dependencies = [
"libc",
]
[[package]]
name = "malloc_buf"
version = "0.0.6"
@ -2486,6 +2590,18 @@ dependencies = [
"memoffset",
]
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.6.0",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "nodrop"
version = "0.1.14"
@ -4426,6 +4542,21 @@ dependencies = [
"uuid",
]
[[package]]
name = "tauri-plugin-gamepad"
version = "0.0.5"
source = "git+https://github.com/quexeky/tauri-plugin-gamepad.git#92cafc0aae66a3de01c252ae2ed599b329b0c686"
dependencies = [
"enum-iterator",
"gilrs",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"thiserror 2.0.9",
"uuid",
]
[[package]]
name = "tauri-plugin-os"
version = "2.2.0"
@ -5077,6 +5208,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version-compare"
version = "0.2.0"
@ -5912,7 +6049,7 @@ dependencies = [
"futures-sink",
"futures-util",
"hex",
"nix",
"nix 0.27.1",
"ordered-stream",
"rand 0.8.5",
"serde",

View File

@ -43,6 +43,7 @@ md5 = "0.7.0"
chrono = "0.4.38"
tauri-plugin-os = "2"
boxcar = "0.2.7"
tauri-plugin-gamepad = {git = "https://github.com/quexeky/tauri-plugin-gamepad.git", version = "0.0.5"}
[dependencies.tauri]
version = "2.1.1"

View File

@ -14,6 +14,7 @@
"core:window:allow-close",
"deep-link:default",
"dialog:default",
"os:default"
"os:default",
"gamepad:default"
]
}

View File

@ -236,6 +236,7 @@ pub fn run() {
])
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_gamepad::init())
.setup(|app| {
let handle = app.handle().clone();
let state = setup(handle);

View File

@ -773,6 +773,11 @@
semver "^7.3.5"
tar "^6.1.11"
"@maulingmonkey/gamepad@^0.0.5":
version "0.0.5"
resolved "https://registry.yarnpkg.com/@maulingmonkey/gamepad/-/gamepad-0.0.5.tgz#50220895cc310f66f0678e07fa1e17959332ac20"
integrity sha512-dokZlauyTVAW2UEFC8T/L+1Oc7oK+vgakjfa4JZQPxkSoCTf7e3l0zV3E/Gys3Ps43Mmc8mnX+7d1ICTyD5pcg==
"@netlify/functions@^2.8.0":
version "2.8.2"
resolved "https://registry.yarnpkg.com/@netlify/functions/-/functions-2.8.2.tgz#653395b901a74a6189e913a089f9cb90083ca6ce"
@ -1307,6 +1312,11 @@
dependencies:
"@tanstack/virtual-core" "3.10.8"
"@tauri-apps/api@2.1.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@tauri-apps/api/-/api-2.1.1.tgz#77d4ddb683d31072de4e6a47c8613d9db011652b"
integrity sha512-fzUfFFKo4lknXGJq8qrCidkUcKcH2UHhfaaCNt4GzgzGaW2iS26uFOg4tS3H4P8D6ZEeUxtiD5z0nwFF0UN30A==
"@tauri-apps/api@>=2.0.0", "@tauri-apps/api@^2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@tauri-apps/api/-/api-2.0.1.tgz#dc49d899fb873b96ee1d46a171384625ba5ad404"
@ -2776,6 +2786,11 @@ escape-string-regexp@^5.0.0:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz#4683126b500b61762f2dbebace1806e8be31b1c8"
integrity sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==
esm-gamecontroller.js@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/esm-gamecontroller.js/-/esm-gamecontroller.js-1.0.4.tgz#215998947c796ead90133b4cb2cef7852729d985"
integrity sha512-787c6eP+DwFJqUsAudnb0zt7ipG+pGYjVT1vyIecsVpRW/GJw5YGV5FQT5asuMQ2k1JCnTY3MTVEUftKkyYoNw==
estree-walker@2.0.2, estree-walker@^2.0.1, estree-walker@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
@ -2952,6 +2967,11 @@ function-bind@^1.1.2:
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.2.tgz#2c02d864d97f3ea6c8830c464cbd11ab6eab7a1c"
integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==
gamepad-events@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/gamepad-events/-/gamepad-events-0.7.0.tgz#63566540dd930f7d5997335a401118b390399494"
integrity sha512-930McsulrWFlGqzlC5plW4WQhqL+3PZ9PAaICqT7OtQLSQ5BzF+wJ32LPzjpZYaoCH7NzvEgtzadqZs8rzNkJw==
gauge@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/gauge/-/gauge-3.0.2.tgz#03bf4441c044383908bcfa0656ad91803259b395"
@ -5582,6 +5602,14 @@ tar@^6.1.11, tar@^6.2.0:
mkdirp "^1.0.3"
yallist "^4.0.0"
tauri-plugin-gamepad-api@^0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/tauri-plugin-gamepad-api/-/tauri-plugin-gamepad-api-0.0.5.tgz#39f9137421b632956f1d617a239b08814ce2c19f"
integrity sha512-WO0xwXmPr4PMe9HzljKUbTkGv7J6ur2Df/OuEPsxBeSA5QPffAJR3N32M4pM4yW72D9Vt19XplPnHHDVATVMSw==
dependencies:
"@tauri-apps/api" "2.1.1"
tslib "^2.1.0"
terser@^5.17.4:
version "5.34.1"
resolved "https://registry.yarnpkg.com/terser/-/terser-5.34.1.tgz#af40386bdbe54af0d063e0670afd55c3105abeb6"