Skip to content

Commit

Permalink
Controller deadzone.
Browse files Browse the repository at this point in the history
This was becoming annoying.
  • Loading branch information
Senryoku committed Jul 18, 2024
1 parent 94f2fc5 commit 0d0a52f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
17 changes: 11 additions & 6 deletions src/debug_ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,31 @@ pub fn draw(self: *@This(), d: *Deecy) !void {
}

inline for (0..4) |i| {
const number = std.fmt.comptimePrint("{d}", .{i + 1});
var connected: bool = d.dc.maple.ports[i].main != null;
if (zgui.checkbox("Connected##" ++ std.fmt.comptimePrint("{d}", .{i + 1}), .{ .v = &connected })) {
if (zgui.checkbox("Connected##" ++ number, .{ .v = &connected })) {
if (d.dc.maple.ports[i].main != null) {
d.dc.maple.ports[i].main = null;
} else {
d.dc.maple.ports[i].main = .{ .Controller = .{} };
}
}
const name = if (d.controllers[i]) |jid|
(if (zglfw.Joystick.get(jid)) |joystick|
if (d.controllers[i]) |*j| {
zgui.sameLine(.{});
_ = zgui.sliderFloat("Deadzone##" ++ number, .{ .v = &j.deadzone, .min = 0.1, .max = 1.0, .flags = .{} });
}
const name = if (d.controllers[i]) |j|
(if (zglfw.Joystick.get(j.id)) |joystick|
(if (joystick.asGamepad()) |gamepad| gamepad.getName() else "None")
else
"None")
else
"None";
if (zgui.beginCombo("Controller #" ++ std.fmt.comptimePrint("{d}", .{i + 1}), .{ .preview_value = name })) {
if (zgui.beginCombo("Controller #" ++ number, .{ .preview_value = name })) {
for (available_controllers.items, 0..) |item, index| {
const idx = @as(u32, @intCast(index));
if (zgui.selectable(item.name, .{ .selected = d.controllers[i] == available_controllers.items[idx].id }))
d.controllers[i] = available_controllers.items[idx].id;
if (zgui.selectable(item.name, .{ .selected = d.controllers[i] != null and d.controllers[i].?.id == available_controllers.items[idx].id }))
d.controllers[i] = .{ .id = available_controllers.items[idx].id.? };
}
zgui.endCombo();
}
Expand Down
20 changes: 15 additions & 5 deletions src/deecy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub const Deecy = struct {
enable_jit: bool = true,
breakpoints: std.ArrayList(u32),

controllers: [4]?zglfw.Joystick.Id = .{ null, null, null, null },
controllers: [4]?struct { id: zglfw.Joystick.Id, deadzone: f32 = 0.1 } = .{null} ** 4,

debug_ui: DebugUI = undefined,

Expand Down Expand Up @@ -115,7 +115,7 @@ pub const Deecy = struct {
const jid: zglfw.Joystick.Id = @intCast(idx);
if (zglfw.Joystick.get(jid)) |joystick| {
if (joystick.asGamepad()) |_| {
self.controllers[curr_pad] = jid;
self.controllers[curr_pad] = .{ .id = jid };
curr_pad += 1;
if (curr_pad >= 4)
break;
Expand Down Expand Up @@ -190,7 +190,7 @@ pub const Deecy = struct {

if (!any_keyboard_key_pressed) {
if (self.controllers[controller_idx]) |host_controller| {
if (zglfw.Joystick.get(host_controller)) |joystick| {
if (zglfw.Joystick.get(host_controller.id)) |joystick| {
if (joystick.asGamepad()) |gamepad| {
const gamepad_state = gamepad.getState();
const gamepad_binds: [9]struct { zglfw.Gamepad.Button, DreamcastModule.Maple.ControllerButtons } = .{
Expand All @@ -214,8 +214,18 @@ pub const Deecy = struct {
}
c.axis[0] = @as(u8, @intFromFloat((gamepad_state.axes[@intFromEnum(zglfw.Gamepad.Axis.right_trigger)] * 0.5 + 0.5) * 255));
c.axis[1] = @as(u8, @intFromFloat((gamepad_state.axes[@intFromEnum(zglfw.Gamepad.Axis.left_trigger)] * 0.5 + 0.5) * 255));
c.axis[2] = @as(u8, @intFromFloat((gamepad_state.axes[@intFromEnum(zglfw.Gamepad.Axis.left_x)] * 0.5 + 0.5) * 255));
c.axis[3] = @as(u8, @intFromFloat((gamepad_state.axes[@intFromEnum(zglfw.Gamepad.Axis.left_y)] * 0.5 + 0.5) * 255));

var x_axis = gamepad_state.axes[@intFromEnum(zglfw.Gamepad.Axis.left_x)];
var y_axis = gamepad_state.axes[@intFromEnum(zglfw.Gamepad.Axis.left_y)];
if (@abs(x_axis) < host_controller.deadzone)
x_axis = 0.0;
if (@abs(y_axis) < host_controller.deadzone)
y_axis = 0.0;
// TODO: Remap with deadzone?
x_axis = x_axis * 0.5 + 0.5;
y_axis = y_axis * 0.5 + 0.5;
c.axis[2] = @as(u8, @intFromFloat(x_axis * 255));
c.axis[3] = @as(u8, @intFromFloat(y_axis * 255));
}
} else {
// Not valid anymore? Disconnected?
Expand Down

0 comments on commit 0d0a52f

Please sign in to comment.