105 lines
2.6 KiB
Zig
105 lines
2.6 KiB
Zig
const std = @import("std");
|
|
const SDL = @import("sdl2");
|
|
|
|
const MenuSelection = @import("MainMenu/MenuSelection.zig");
|
|
const MenuTab = @import("MainMenu/MenuTab.zig");
|
|
const Renderer = @import("MainMenu/Renderer.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), // Tab Right
|
|
// Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // 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
|
|
},
|
|
|
|
tab_list: [2]MenuTab = [2]MenuTab{
|
|
MenuTab.init("1P", &.{
|
|
MenuSelection.init("Play", play),
|
|
MenuSelection.init("Print", print),
|
|
}),
|
|
MenuTab.init("Options", &.{
|
|
MenuSelection.init("Play", play),
|
|
MenuSelection.init("Print", print),
|
|
}),
|
|
},
|
|
|
|
renderer: Renderer,
|
|
|
|
// Current tab and selection index
|
|
tab: usize = 0,
|
|
sel: usize = 0,
|
|
|
|
state: State = State.main_menu,
|
|
|
|
holding_down: bool = false,
|
|
holding_enter: bool = false,
|
|
|
|
pub fn init(renderer: Renderer.Renderer) Self {
|
|
return Self{.renderer = Renderer.init(renderer)};
|
|
}
|
|
|
|
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.getKeyboardState();
|
|
|
|
for (self.action_list) |*action| {
|
|
if (key_state.isPressed(action.*.scancode) and !action.*.holding) {
|
|
action.*.holding = true;
|
|
action.*.activate = true;
|
|
}
|
|
if (!key_state.isPressed(action.*.scancode)) {
|
|
action.*.holding = false;
|
|
}
|
|
|
|
// Action
|
|
if (action.*.activate) {
|
|
action.*.activate = false;
|
|
action.*.call(self);
|
|
}
|
|
}
|
|
std.debug.print(
|
|
\\Tab: {s}
|
|
\\Selection: {s}
|
|
\\
|
|
, .{ tab.name, sel.name });
|
|
return self.state;
|
|
}
|
|
|
|
fn actionSelDown(self: *Self) void {
|
|
self.sel += 1;
|
|
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;
|
|
@call(.{}, action, .{self});
|
|
}
|
|
|
|
fn play(self: *Self) void {
|
|
self.state = State.game;
|
|
}
|
|
|
|
fn print(self: *Self) void {
|
|
_ = self;
|
|
std.debug.print("Print\n", .{});
|
|
}
|