From e21e184c585c1d7ca1da83839f96c1fa2943815d Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 22 Sep 2023 19:23:40 +0200 Subject: [PATCH] Implement actions with the new input system --- src/Action.zig | 12 ++++----- src/Game.zig | 15 +++++------ src/MainMenu.zig | 53 +++++++++++++++++++++------------------ src/MainMenu/Renderer.zig | 2 +- src/Renderer.zig | 2 -- src/input.zig | 2 +- 6 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/Action.zig b/src/Action.zig index 798542e..ddfc67f 100644 --- a/src/Action.zig +++ b/src/Action.zig @@ -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}); } diff --git a/src/Game.zig b/src/Game.zig index 5c78195..919d6f3 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -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, diff --git a/src/MainMenu.zig b/src/MainMenu.zig index a34eb21..4a79e7b 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -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} diff --git a/src/MainMenu/Renderer.zig b/src/MainMenu/Renderer.zig index ab9a84c..6f5dddf 100644 --- a/src/MainMenu/Renderer.zig +++ b/src/MainMenu/Renderer.zig @@ -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 }); diff --git a/src/Renderer.zig b/src/Renderer.zig index 02cbeb6..a3fea65 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -29,8 +29,6 @@ pub fn init() !Self { } pub fn render(self: *Self) void { - //_ = self; - rl.endDrawing(); rl.beginDrawing(); diff --git a/src/input.zig b/src/input.zig index 7e3e58f..972a252 100644 --- a/src/input.zig +++ b/src/input.zig @@ -1,6 +1,6 @@ const rl = @import("raylib.zig"); -const Input = struct { +pub const Input = struct { name: []const u8, code: i32, default: i32,