usg/src/MainMenu.zig

135 lines
3.2 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: [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
},
tab_list: [3]MenuTab = .{
MenuTab.init("1P", .{245, 155, 20}, &.{
MenuSelection.init("Play", play),
MenuSelection.init("Print", print),
}),
MenuTab.init("Options", .{162, 86, 179}, &.{
MenuSelection.init("Play", play),
MenuSelection.init("Print", print),
}),
MenuTab.init("Online", .{105, 179, 86}, &.{
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) };
}
pub fn getSel(self: Self) MenuSelection {
return self.tab_list[self.tab].contents[self.sel];
}
pub 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 });
self.renderer.render(self.*);
return self.state;
}
pub fn getPrevTab(self: Self) usize {
if (self.tab == 0) {
return self.tab_list.len - 1;
} else {
return self.tab - 1;
}
}
pub fn getNextTab(self: Self) usize {
if (self.tab == self.tab_list.len - 1) {
return 0;
} else {
return self.tab + 1;
}
}
fn actionTabLeft(self: *Self) void {
self.tab = self.getPrevTab();
}
fn actionTabRight(self: *Self) void {
self.tab = self.getNextTab();
}
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", .{});
}