Implement actions with the new input system

This commit is contained in:
Dusk 2023-09-22 19:23:40 +02:00
parent 82e8cfdbe1
commit e21e184c58
6 changed files with 45 additions and 41 deletions

View File

@ -1,19 +1,19 @@
const Self = @This();
//const SDL = @import("sdl2");
const input = @import("input.zig");
activate: bool = false,
holding: bool = false,
//scancode: SDL.Scancode, // SDL Keycode
input: input.Input,
callback: *const anyopaque, // MUST be a function with void return
pub fn init(scancode: i1, callback: *const anyopaque) Self {
_ = scancode;
pub fn init(tmp_input: input.Input, callback: *const anyopaque) Self {
return Self{
//.scancode = scancode,
.input = tmp_input,
.callback = callback,
};
}
pub fn call(self: Self, state: anytype) void {
@call(.auto, @as(*const fn (@TypeOf(state)) void, @ptrCast(self.callback)), .{state});
}

View File

@ -6,6 +6,7 @@ const Bag = @import("Game/Bag.zig");
const Timer = @import("Timer.zig");
const State = @import("flow.zig").State;
const Action = @import("Action.zig");
const input = @import("input.zig");
const Renderer = @import("Game/Renderer.zig");
const movement = @import("Game/movement.zig");
@ -36,13 +37,13 @@ piece: Piece,
shadow: Piece,
action_list: ActionList = .{
.right = Action.init(0, actionRight), // Right
.left = Action.init(0, actionLeft), // Left
.down = Action.init(0, actionDown), // Down
.hard = Action.init(0, actionHard), // Instant Drop
.swap = Action.init(0, actionSwap), // Swap Piece
.rot_r = Action.init(0, actionRotR), // Rotate
.rot_l = Action.init(0, actionRotL), // Rotate
.right = Action.init(input.menu_left, actionRight), // Right
.left = Action.init(input.menu_left, actionLeft), // Left
.down = Action.init(input.menu_left, actionDown), // Down
.hard = Action.init(input.menu_left, actionHard), // Instant Drop
.swap = Action.init(input.menu_left, actionSwap), // Swap Piece
.rot_r = Action.init(input.menu_left, actionRotR), // Rotate
.rot_l = Action.init(input.menu_left, actionRotL), // Rotate
},
timer_down: Timer,

View File

@ -9,6 +9,8 @@ const Renderer = @import("MainMenu/Renderer.zig");
const State = @import("flow.zig").State;
const Action = @import("Action.zig");
const input = @import("input.zig");
const Self = @This();
const ActionList = struct {
@ -20,11 +22,11 @@ const ActionList = struct {
};
action_list: ActionList = .{
.right = Action.init(0, actionTabRight), // Tab Right
.left = Action.init(0, actionTabLeft), // Tab left
.down = Action.init(0, actionSelDown), // Go down
.up = Action.init(0, actionSelUp), // Go up
.select = Action.init(0, actionSelect), // Select
.right = Action.init(input.menu_right, actionTabRight), // Tab Right
.left = Action.init(input.menu_left, actionTabLeft), // Tab left
.down = Action.init(input.menu_down, actionSelDown), // Go down
.up = Action.init(input.menu_up, actionSelUp), // Go up
.select = Action.init(input.menu_accept, actionSelect), // Select
},
tab_list: [3]MenuTab = .{
@ -39,8 +41,9 @@ tab_list: [3]MenuTab = .{
MenuTab.init("Online", .{ 105, 179, 86 }, &.{
MenuSelection.init("Play", play),
MenuSelection.init("Print", print),
MenuSelection.init("Print", print),
MenuSelection.init("Print", print),
// FIXME: Crashes upon trying to render them
//MenuSelection.init("Print", print),
//MenuSelection.init("Print", print),
}),
},
@ -101,25 +104,27 @@ pub fn tick(self: *Self) State {
//const sel = self.getSel();
//const tab = self.getTab();
//var key_state = SDL.getKeyboardState();
// const action_fields = std.meta.fields(ActionList);
// inline for (action_fields) |field| {
// const action = &@field(self.action_list, field.name);
// if (key_state.isPressed(action.*.scancode) and !action.*.holding) {
// action.*.holding = true;
// action.*.activate = true;
// }
// if (!key_state.isPressed(action.*.scancode)) {
// action.*.holding = false;
// }
const action_fields = std.meta.fields(ActionList);
inline for (action_fields) |field| {
// REVIEW: Is this necessary?
const action = &@field(self.action_list, field.name);
if (action.input.isDown() and !action.*.holding) {
action.*.holding = true;
action.*.activate = true;
}
if (!action.input.isDown()) {
action.*.holding = false;
}
// Action
if (action.*.activate) {
action.*.activate = false;
action.*.call(self);
}
}
// // Action
// if (action.*.activate) {
// action.*.activate = false;
// action.*.call(self);
// }
// }
//std.debug.print(
//\\Tab: {s}
//\\Selection: {s}

View File

@ -104,7 +104,7 @@ fn renderMenu(self: Self, x: i32, y: i32, tab: MenuTab, sel: usize, a: u8, selec
// White background
self.renderer.setColor(.{ 255, 255, 255, a });
self.renderer.fillRectangleEx(x, y, width, height, skew);
//_ = sel;
if (selected) {
// Set color if selected
self.renderer.setColor(.{ tab.color[0], tab.color[1], tab.color[2], a });

View File

@ -29,8 +29,6 @@ pub fn init() !Self {
}
pub fn render(self: *Self) void {
//_ = self;
rl.endDrawing();
rl.beginDrawing();

View File

@ -1,6 +1,6 @@
const rl = @import("raylib.zig");
const Input = struct {
pub const Input = struct {
name: []const u8,
code: i32,
default: i32,