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
24 changes: 24 additions & 0 deletions demos/Gamepads/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Gtk 4.0;
using Adw 1;

Stack stack {
visible-child: status_page;

Adw.StatusPage status_page {
title: _("Please connect a gamepad");
description: _("A controller is needed for this demo");
halfmexican marked this conversation as resolved.
Show resolved Hide resolved
icon-name: "gamepad-symbolic";

Box {
orientation: vertical;
halign: center;

LinkButton {
label: "API Reference";
uri: "https://gnome.pages.gitlab.gnome.org/libmanette/";
margin-top: 24;
}
}
}
}

48 changes: 48 additions & 0 deletions demos/Gamepads/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Gtk from "gi://Gtk";
import Manette from "gi://Manette";

const status_page = workbench.builder.get_object("status_page");

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

// Iterate over the devices and log their details
let [has_next, device] = monitor_iter.next();

while (device !== null) {
console.log("Device:", device.get_name());

status_page.title = _("Controller connected");
status_page.description = "";

// Face and Shoulder Buttons
device.connect("button-press-event", (device, event) => {
console.log(
`Device: ${device.get_name()} pressed ${event.get_hardware_code()}`,
);

if (device.has_rumble()) {
device.rumble(1000, 1500, 200);
}
});

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

[has_next, device] = monitor_iter.next();
}

7 changes: 7 additions & 0 deletions demos/Gamepads/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Gamepads",
"category": "controls",
"description": "Use Libmanette to access controllers and gamepads",
halfmexican marked this conversation as resolved.
Show resolved Hide resolved
"panels": ["code", "preview"],
"autorun": false
halfmexican marked this conversation as resolved.
Show resolved Hide resolved
}
Loading