From 6c77226652f252be9ff8d11825e99e4ffcf85b40 Mon Sep 17 00:00:00 2001 From: strangedusk Date: Thu, 1 Dec 2022 12:01:55 +0100 Subject: [PATCH] Use new struct for actions --- src/MainMenu.zig | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/MainMenu.zig b/src/MainMenu.zig index d58ace2..21c177c 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -11,12 +11,20 @@ const Action = @import("Action.zig"); const Self = @This(); -action_list: [5]Action = .{ - Action.init(SDL.Scancode.right, actionTabRight), // Tab Right - Action.init(SDL.Scancode.left, actionTabLeft), // Tab left - Action.init(SDL.Scancode.down, actionSelDown), // Go down - Action.init(SDL.Scancode.up, actionSelUp), // Go up - Action.init(SDL.Scancode.@"return", actionSelect), // Select +const ActionList = struct { + right: Action, + left: Action, + down: Action, + up: Action, + select: Action, +}; + +action_list: ActionList = .{ + .right = Action.init(SDL.Scancode.right, actionTabRight), // Tab Right + .left = Action.init(SDL.Scancode.left, actionTabLeft), // Tab left + .down = Action.init(SDL.Scancode.down, actionSelDown), // Go down + .up = Action.init(SDL.Scancode.up, actionSelUp), // Go up + .select = Action.init(SDL.Scancode.@"return", actionSelect), // Select }, tab_list: [3]MenuTab = .{ @@ -95,7 +103,9 @@ pub fn tick(self: *Self) State { //const tab = self.getTab(); var key_state = SDL.getKeyboardState(); - for (self.action_list) |*action| { + 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;