Main Menu Action struct implementation
This commit is contained in:
parent
d8e86e24d4
commit
9ba1ca0216
|
@ -1,10 +1,9 @@
|
||||||
const Game = @import("Game.zig");
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
activate: bool = false,
|
activate: bool = false,
|
||||||
holding: bool = false,
|
holding: bool = false,
|
||||||
code: u8, // SDL Keycode
|
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 {
|
pub fn init(code: u8, callback: *const anyopaque) Self {
|
||||||
return Self{
|
return Self{
|
||||||
|
|
|
@ -5,9 +5,18 @@ const MenuSelection = @import("MenuSelection.zig");
|
||||||
const MenuTab = @import("MenuTab.zig");
|
const MenuTab = @import("MenuTab.zig");
|
||||||
|
|
||||||
const State = @import("flow.zig").State;
|
const State = @import("flow.zig").State;
|
||||||
|
const Action = @import("Action.zig");
|
||||||
|
|
||||||
const Self = @This();
|
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{
|
tab_list: [2]MenuTab = [2]MenuTab{
|
||||||
MenuTab.init("1P", &.{
|
MenuTab.init("1P", &.{
|
||||||
MenuSelection.init("Play", play),
|
MenuSelection.init("Play", play),
|
||||||
|
@ -28,41 +37,46 @@ state: State = State.main_menu,
|
||||||
holding_down: bool = false,
|
holding_down: bool = false,
|
||||||
holding_enter: bool = false,
|
holding_enter: bool = false,
|
||||||
|
|
||||||
fn getSel(self: *Self) *MenuSelection {
|
pos_x: i32,
|
||||||
return &self.tab_list[self.tab].contents[self.sel];
|
pos_i: i32,
|
||||||
}
|
|
||||||
|
|
||||||
fn getTab(self: *Self) *MenuTab {
|
|
||||||
return &self.tab_list[self.tab];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init() Self {
|
pub fn init() Self {
|
||||||
return 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 {
|
pub fn tick(self: *Self) State {
|
||||||
const sel = self.getSel();
|
const sel = self.getSel();
|
||||||
const tab = self.getTab();
|
const tab = self.getTab();
|
||||||
var key_state = SDL.SDL_GetKeyboardState(null);
|
var key_state = SDL.SDL_GetKeyboardState(null);
|
||||||
if (key_state[SDL.SDL_SCANCODE_DOWN] == 1 and !self.holding_down) {
|
|
||||||
self.holding_down = true;
|
for (self.action_list) |*action| {
|
||||||
self.actionSelDown();
|
if (key_state[action.*.code] == 1 and !action.*.holding) {
|
||||||
}
|
action.*.holding = true;
|
||||||
if (key_state[SDL.SDL_SCANCODE_DOWN] == 0) {
|
action.*.activate = true;
|
||||||
self.holding_down = false;
|
}
|
||||||
}
|
if (key_state[action.*.code] == 0) {
|
||||||
if (key_state[SDL.SDL_SCANCODE_RETURN] == 1 and !self.holding_enter) {
|
action.*.holding = false;
|
||||||
self.holding_enter = true;
|
}
|
||||||
self.actionSelect();
|
|
||||||
}
|
// Action
|
||||||
if (key_state[SDL.SDL_SCANCODE_RETURN] == 0) {
|
if (action.*.activate) {
|
||||||
self.holding_enter = false;
|
action.*.activate = false;
|
||||||
|
action.*.call(self);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\Tab: {s}
|
\\Tab: {s}
|
||||||
\\Selection: {s}
|
\\Selection: {s}
|
||||||
\\
|
\\
|
||||||
, .{ tab.*.name, sel.*.name });
|
, .{ tab.name, sel.name });
|
||||||
return self.state;
|
return self.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,8 +85,13 @@ fn actionSelDown(self: *Self) void {
|
||||||
self.sel %= self.tab_list[self.tab].contents.len;
|
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 {
|
fn actionSelect(self: *Self) void {
|
||||||
const action = self.getSel().*.action;
|
const action = self.getSel().action;
|
||||||
@call(.{}, action, .{self});
|
@call(.{}, action, .{self});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue