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 Self = @This();
//const SDL = @import("sdl2");
const input = @import("input.zig");
activate: bool = false, activate: bool = false,
holding: bool = false, holding: bool = false,
//scancode: SDL.Scancode, // SDL Keycode input: input.Input,
callback: *const anyopaque, // MUST be a function with void return callback: *const anyopaque, // MUST be a function with void return
pub fn init(scancode: i1, callback: *const anyopaque) Self { pub fn init(tmp_input: input.Input, callback: *const anyopaque) Self {
_ = scancode;
return Self{ return Self{
//.scancode = scancode, .input = tmp_input,
.callback = callback, .callback = callback,
}; };
} }
pub fn call(self: Self, state: anytype) void { pub fn call(self: Self, state: anytype) void {
@call(.auto, @as(*const fn (@TypeOf(state)) void, @ptrCast(self.callback)), .{state}); @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 Timer = @import("Timer.zig");
const State = @import("flow.zig").State; const State = @import("flow.zig").State;
const Action = @import("Action.zig"); const Action = @import("Action.zig");
const input = @import("input.zig");
const Renderer = @import("Game/Renderer.zig"); const Renderer = @import("Game/Renderer.zig");
const movement = @import("Game/movement.zig"); const movement = @import("Game/movement.zig");
@ -36,13 +37,13 @@ piece: Piece,
shadow: Piece, shadow: Piece,
action_list: ActionList = .{ action_list: ActionList = .{
.right = Action.init(0, actionRight), // Right .right = Action.init(input.menu_left, actionRight), // Right
.left = Action.init(0, actionLeft), // Left .left = Action.init(input.menu_left, actionLeft), // Left
.down = Action.init(0, actionDown), // Down .down = Action.init(input.menu_left, actionDown), // Down
.hard = Action.init(0, actionHard), // Instant Drop .hard = Action.init(input.menu_left, actionHard), // Instant Drop
.swap = Action.init(0, actionSwap), // Swap Piece .swap = Action.init(input.menu_left, actionSwap), // Swap Piece
.rot_r = Action.init(0, actionRotR), // Rotate .rot_r = Action.init(input.menu_left, actionRotR), // Rotate
.rot_l = Action.init(0, actionRotL), // Rotate .rot_l = Action.init(input.menu_left, actionRotL), // Rotate
}, },
timer_down: Timer, timer_down: Timer,

View File

@ -9,6 +9,8 @@ const Renderer = @import("MainMenu/Renderer.zig");
const State = @import("flow.zig").State; const State = @import("flow.zig").State;
const Action = @import("Action.zig"); const Action = @import("Action.zig");
const input = @import("input.zig");
const Self = @This(); const Self = @This();
const ActionList = struct { const ActionList = struct {
@ -20,11 +22,11 @@ const ActionList = struct {
}; };
action_list: ActionList = .{ action_list: ActionList = .{
.right = Action.init(0, actionTabRight), // Tab Right .right = Action.init(input.menu_right, actionTabRight), // Tab Right
.left = Action.init(0, actionTabLeft), // Tab left .left = Action.init(input.menu_left, actionTabLeft), // Tab left
.down = Action.init(0, actionSelDown), // Go down .down = Action.init(input.menu_down, actionSelDown), // Go down
.up = Action.init(0, actionSelUp), // Go up .up = Action.init(input.menu_up, actionSelUp), // Go up
.select = Action.init(0, actionSelect), // Select .select = Action.init(input.menu_accept, actionSelect), // Select
}, },
tab_list: [3]MenuTab = .{ tab_list: [3]MenuTab = .{
@ -39,8 +41,9 @@ tab_list: [3]MenuTab = .{
MenuTab.init("Online", .{ 105, 179, 86 }, &.{ MenuTab.init("Online", .{ 105, 179, 86 }, &.{
MenuSelection.init("Play", play), 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),
//MenuSelection.init("Print", print),
}), }),
}, },
@ -101,25 +104,27 @@ pub fn tick(self: *Self) State {
//const sel = self.getSel(); //const sel = self.getSel();
//const tab = self.getTab(); //const tab = self.getTab();
//var key_state = SDL.getKeyboardState();
// const action_fields = std.meta.fields(ActionList); const action_fields = std.meta.fields(ActionList);
// inline for (action_fields) |field| { inline for (action_fields) |field| {
// const action = &@field(self.action_list, field.name); // REVIEW: Is this necessary?
// if (key_state.isPressed(action.*.scancode) and !action.*.holding) { const action = &@field(self.action_list, field.name);
// action.*.holding = true;
// action.*.activate = true; if (action.input.isDown() and !action.*.holding) {
// } action.*.holding = true;
// if (!key_state.isPressed(action.*.scancode)) { action.*.activate = true;
// action.*.holding = false; }
// } 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( //std.debug.print(
//\\Tab: {s} //\\Tab: {s}
//\\Selection: {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 // White background
self.renderer.setColor(.{ 255, 255, 255, a }); self.renderer.setColor(.{ 255, 255, 255, a });
self.renderer.fillRectangleEx(x, y, width, height, skew); self.renderer.fillRectangleEx(x, y, width, height, skew);
//_ = sel;
if (selected) { if (selected) {
// Set color if selected // Set color if selected
self.renderer.setColor(.{ tab.color[0], tab.color[1], tab.color[2], a }); 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 { pub fn render(self: *Self) void {
//_ = self;
rl.endDrawing(); rl.endDrawing();
rl.beginDrawing(); rl.beginDrawing();

View File

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