Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Gamepad" demo #88

Merged
merged 13 commits into from
Oct 9, 2024
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ format:

test:
# list folders that have changed and run workbench-cli ci on them
git diff --dirstat=files,0 origin/main src | sed 's/^[ 0-9.]\+% //g' | uniq | xargs -d '\n' flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci
# $$ is for the Makefile only - use $ otherwise
git diff --name-only origin/main src | awk -F/ '{print $$1 FS $$2}' | uniq | xargs -d '\n' flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci

all:
flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci src/*
Expand Down
2 changes: 2 additions & 0 deletions src/Gamepad/icons/gamepad-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions src/Gamepad/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Gtk 4.0;
using Adw 1;

Adw.StatusPage {
title: _("Gamepad");
description: _("Respond to gamepads and controllers input");
icon-name: "gamepad-symbolic";

Box {
orientation: vertical;
halign: center;
spacing: 24;

Stack stack {
Label {
label: _("Press Run");

styles [
"title-2"
]
}

StackPage {
name: "connect";

child: Label {
label: _("Please connect a controller");

styles [
"title-2"
]
};
}

StackPage {
name: "watch";

child: Box {
orientation: vertical;
spacing: 6;

Label {
label: _("Press buttons on the controller and watch the Console");

styles [
"title-2"
]
}

Button button_rumble {
label: _("Rumble");
halign: center;
}
};
}
}

LinkButton {
label: _("API Reference");
uri: "https://gnome.pages.gitlab.gnome.org/libmanette/";
}
}
}
73 changes: 73 additions & 0 deletions src/Gamepad/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Manette from "gi://Manette";

const stack = workbench.builder.get_object("stack");
const button_rumble = workbench.builder.get_object("button_rumble");

const devices = new Set();

stack.visible_child_name = "connect";
button_rumble.connect("clicked", () => {
for (const device of devices) {
if (device.has_rumble()) {
device.rumble(1000, 1500, 200);
}
}
});

function onDevice(device) {
console.log("Device connected:", device.get_name());

// Face and Shoulder Buttons
device.connect("button-press-event", (device, event) => {
const [success, button] = event.get_button();
console.log(
`${device.get_name()}: press ${success ? button : event.get_hardware_code()}`,
);
});

// Face and Shoulder Buttons
device.connect("button-release-event", (device, event) => {
const [success, button] = event.get_button();
console.log(
`${device.get_name()}: release ${success ? button : event.get_hardware_code()}`,
);
});

// D-pads
device.connect("hat-axis-event", (device, event) => {
const [, hat_axis, hat_value] = event.get_hat();
console.log(`${device.get_name()}: moved axis ${hat_axis} to ${hat_value}`);
});

// Analog Axis - Triggers and Joysticks
device.connect("absolute-axis-event", (device, event) => {
const [, axis, value] = event.get_absolute();
if (Math.abs(value) > 0.2)
console.log(`${device.get_name()}: moved axis ${axis} to ${value}`);
});

devices.add(device);
stack.visible_child_name = "watch";
}

function onDeviceDisconnected(device) {
console.log("Device Disconnected:", device.get_name());

devices.delete(device);
stack.visible_child_name = devices.size < 1 ? "connect" : "watch";
}

const monitor = new Manette.Monitor();
const monitor_iter = monitor.iterate();

let has_next;
let device;
do {
[has_next, device] = monitor_iter.next();
if (device !== null) onDevice(device);
} while (has_next);

monitor.connect("device-connected", (_self, device) => onDevice(device));
monitor.connect("device-disconnected", (_self, device) =>
onDeviceDisconnected(device),
);
8 changes: 8 additions & 0 deletions src/Gamepad/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Gamepad",
"category": "controls",
"description": "Respond to gamepads and controllers input",
"panels": ["code", "preview"],
"autorun": true,
"flatpak-finish-args": ["--device=input"]
}
Loading