From 9ba1ca0216606290c4524ebe7ae8073280b5d6d6 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Wed, 20 Jul 2022 20:17:31 +0200 Subject: [PATCH] Main Menu Action struct implementation --- src/Action.zig | 3 +-- src/MainMenu.zig | 63 +++++++++++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/Action.zig b/src/Action.zig index 32975af..e57319f 100644 --- a/src/Action.zig +++ b/src/Action.zig @@ -1,10 +1,9 @@ -const Game = @import("Game.zig"); const Self = @This(); activate: bool = false, holding: bool = false, code: u8, // SDL Keycode -callback: *const anyopaque, +callback: *const anyopaque, // MUST be a function with void return pub fn init(code: u8, callback: *const anyopaque) Self { return Self{ diff --git a/src/MainMenu.zig b/src/MainMenu.zig index 3b39fd6..2d68c82 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -5,9 +5,18 @@ const MenuSelection = @import("MenuSelection.zig"); const MenuTab = @import("MenuTab.zig"); const State = @import("flow.zig").State; +const Action = @import("Action.zig"); const Self = @This(); +action_list: [3]Action = .{ + // Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Right + // Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Left + Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Down + Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Down + Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Down +}, + tab_list: [2]MenuTab = [2]MenuTab{ MenuTab.init("1P", &.{ MenuSelection.init("Play", play), @@ -28,41 +37,46 @@ state: State = State.main_menu, holding_down: bool = false, holding_enter: bool = false, -fn getSel(self: *Self) *MenuSelection { - return &self.tab_list[self.tab].contents[self.sel]; -} - -fn getTab(self: *Self) *MenuTab { - return &self.tab_list[self.tab]; -} +pos_x: i32, +pos_i: i32, pub fn init() Self { return Self{}; } +fn getSel(self: *Self) MenuSelection { + return self.tab_list[self.tab].contents[self.sel]; +} + +fn getTab(self: *Self) MenuTab { + return self.tab_list[self.tab]; +} + pub fn tick(self: *Self) State { const sel = self.getSel(); const tab = self.getTab(); var key_state = SDL.SDL_GetKeyboardState(null); - if (key_state[SDL.SDL_SCANCODE_DOWN] == 1 and !self.holding_down) { - self.holding_down = true; - self.actionSelDown(); - } - if (key_state[SDL.SDL_SCANCODE_DOWN] == 0) { - self.holding_down = false; - } - if (key_state[SDL.SDL_SCANCODE_RETURN] == 1 and !self.holding_enter) { - self.holding_enter = true; - self.actionSelect(); - } - if (key_state[SDL.SDL_SCANCODE_RETURN] == 0) { - self.holding_enter = false; + + for (self.action_list) |*action| { + if (key_state[action.*.code] == 1 and !action.*.holding) { + action.*.holding = true; + action.*.activate = true; + } + if (key_state[action.*.code] == 0) { + action.*.holding = false; + } + + // Action + if (action.*.activate) { + action.*.activate = false; + action.*.call(self); + } } std.debug.print( \\Tab: {s} \\Selection: {s} \\ - , .{ tab.*.name, sel.*.name }); + , .{ tab.name, sel.name }); return self.state; } @@ -71,8 +85,13 @@ fn actionSelDown(self: *Self) void { self.sel %= self.tab_list[self.tab].contents.len; } +fn actionSelUp(self: *Self) void { + if (self.sel == 0) self.sel = self.tab_list[self.tab].contents.len - 1 + else self.sel -= 1; +} + fn actionSelect(self: *Self) void { - const action = self.getSel().*.action; + const action = self.getSel().action; @call(.{}, action, .{self}); }