refactor: make it possible to have multiple menus
parent
e39c75e19d
commit
771603b9d5
122
src/main.zig
122
src/main.zig
|
@ -23,13 +23,9 @@ var hovered_card: usize = 0;
|
||||||
var selected_deck: ?*std.ArrayListUnmanaged(Card) = null;
|
var selected_deck: ?*std.ArrayListUnmanaged(Card) = null;
|
||||||
var selected_card: usize = 0;
|
var selected_card: usize = 0;
|
||||||
|
|
||||||
var show_menu: bool = false;
|
const MenuEvent = union(enum) { up, down, draw: struct { scalef: f32 }, select, deselect };
|
||||||
var menu_item_selected: u32 = 0;
|
const MenuFn = *const fn (MenuEvent) anyerror!void;
|
||||||
const MENU_ITEMS = [_][]const u8{
|
var current_menu_fn: ?MenuFn = null;
|
||||||
"Continue",
|
|
||||||
"New Game",
|
|
||||||
"Quit",
|
|
||||||
};
|
|
||||||
|
|
||||||
var win_count: ?u32 = null;
|
var win_count: ?u32 = null;
|
||||||
var win_triggered: bool = false;
|
var win_triggered: bool = false;
|
||||||
|
@ -435,23 +431,8 @@ fn render(window: seizer.Window) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (show_menu) {
|
if (current_menu_fn) |menu_fn| {
|
||||||
canvas.rect(
|
try menu_fn(.{ .draw = .{ .scalef = scalef } });
|
||||||
.{ canvas.window_size[0] / 3, canvas.window_size[1] / 3 },
|
|
||||||
.{ canvas.window_size[0] / 3, canvas.window_size[1] / 3 },
|
|
||||||
.{ .color = .{ 0, 0, 0, 0xFF } },
|
|
||||||
);
|
|
||||||
const x: f32 = canvas.window_size[0] / 2;
|
|
||||||
var y: f32 = canvas.window_size[1] / 3 + (canvas.font.lineHeight * scalef);
|
|
||||||
for (MENU_ITEMS, 0..) |menu_item, i| {
|
|
||||||
if (i == menu_item_selected) {
|
|
||||||
const size = canvas.printText(.{ x, y }, "> {s} <", .{menu_item}, .{ .@"align" = .center, .scale = scalef });
|
|
||||||
y += size[1];
|
|
||||||
} else {
|
|
||||||
const size = canvas.writeText(.{ x, y }, menu_item, .{ .@"align" = .center, .scale = scalef });
|
|
||||||
y += size[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas.end();
|
canvas.end();
|
||||||
|
@ -488,23 +469,17 @@ pub fn makeStandardDeck(allocator: std.mem.Allocator) ![]Card {
|
||||||
|
|
||||||
pub fn deselect(pressed: bool) !void {
|
pub fn deselect(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
if (show_menu) return;
|
if (current_menu_fn) |menu_fn| {
|
||||||
|
try menu_fn(.deselect);
|
||||||
|
}
|
||||||
selected_deck = null;
|
selected_deck = null;
|
||||||
selected_card = 0;
|
selected_card = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn doSelectOrPlace(pressed: bool) !void {
|
pub fn doSelectOrPlace(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
if (show_menu) {
|
if (current_menu_fn) |menu_fn| {
|
||||||
const menu_item = MENU_ITEMS[menu_item_selected];
|
try menu_fn(.select);
|
||||||
if (std.mem.eql(u8, menu_item, "Continue")) {
|
|
||||||
show_menu = false;
|
|
||||||
} else if (std.mem.eql(u8, menu_item, "New Game")) {
|
|
||||||
try resetGame();
|
|
||||||
show_menu = false;
|
|
||||||
} else if (std.mem.eql(u8, menu_item, "Quit")) {
|
|
||||||
window_global.setShouldClose(true);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (selected_deck == null) {
|
if (selected_deck == null) {
|
||||||
|
@ -639,7 +614,10 @@ pub fn doSelectOrPlace(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn moveLeft(pressed: bool) !void {
|
pub fn moveLeft(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
if (show_menu) return;
|
if (current_menu_fn) |menu_fn| {
|
||||||
|
_ = menu_fn;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (hovered_deck == null) {
|
if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
|
@ -672,7 +650,10 @@ pub fn moveLeft(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn moveRight(pressed: bool) !void {
|
pub fn moveRight(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
if (show_menu) return;
|
if (current_menu_fn) |menu_fn| {
|
||||||
|
_ = menu_fn;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (hovered_deck == null) {
|
if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
|
@ -706,9 +687,8 @@ pub fn moveRight(pressed: bool) !void {
|
||||||
pub fn moveUp(pressed: bool) !void {
|
pub fn moveUp(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
|
|
||||||
if (show_menu) {
|
if (current_menu_fn) |menu_fn| {
|
||||||
if (menu_item_selected == 0) menu_item_selected = MENU_ITEMS.len;
|
try menu_fn(.up);
|
||||||
menu_item_selected -= 1;
|
|
||||||
} else if (hovered_deck == null) {
|
} else if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
|
@ -750,9 +730,8 @@ pub fn moveUp(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn moveDown(pressed: bool) !void {
|
pub fn moveDown(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
if (show_menu) {
|
if (current_menu_fn) |menu_fn| {
|
||||||
menu_item_selected += 1;
|
try menu_fn(.down);
|
||||||
menu_item_selected %= @intCast(MENU_ITEMS.len);
|
|
||||||
} else if (hovered_deck == null) {
|
} else if (hovered_deck == null) {
|
||||||
hovered_deck = &draw_pile;
|
hovered_deck = &draw_pile;
|
||||||
hovered_card = 0;
|
hovered_card = 0;
|
||||||
|
@ -803,7 +782,11 @@ pub fn undo(pressed: bool) !void {
|
||||||
|
|
||||||
pub fn toggleMenu(pressed: bool) !void {
|
pub fn toggleMenu(pressed: bool) !void {
|
||||||
if (!pressed) return;
|
if (!pressed) return;
|
||||||
show_menu = !show_menu;
|
if (current_menu_fn == null) {
|
||||||
|
current_menu_fn = menuMain;
|
||||||
|
} else {
|
||||||
|
current_menu_fn = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn indexOfTopOfStack(cards: []Card) usize {
|
pub fn indexOfTopOfStack(cards: []Card) usize {
|
||||||
|
@ -905,6 +888,57 @@ const Snapshot = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var main_menu_item_selected: u32 = 0;
|
||||||
|
const MAIN_MENU_ITEMS = [_][]const u8{
|
||||||
|
"Continue",
|
||||||
|
"New Game",
|
||||||
|
"Quit",
|
||||||
|
};
|
||||||
|
fn menuMain(event: MenuEvent) !void {
|
||||||
|
switch (event) {
|
||||||
|
.up => {
|
||||||
|
if (main_menu_item_selected == 0) main_menu_item_selected = MAIN_MENU_ITEMS.len;
|
||||||
|
main_menu_item_selected -= 1;
|
||||||
|
},
|
||||||
|
.down => {
|
||||||
|
main_menu_item_selected += 1;
|
||||||
|
main_menu_item_selected %= @intCast(MAIN_MENU_ITEMS.len);
|
||||||
|
},
|
||||||
|
.select => {
|
||||||
|
const menu_item = MAIN_MENU_ITEMS[main_menu_item_selected];
|
||||||
|
if (std.mem.eql(u8, menu_item, "Continue")) {
|
||||||
|
current_menu_fn = null;
|
||||||
|
} else if (std.mem.eql(u8, menu_item, "New Game")) {
|
||||||
|
try resetGame();
|
||||||
|
current_menu_fn = null;
|
||||||
|
} else if (std.mem.eql(u8, menu_item, "Quit")) {
|
||||||
|
window_global.setShouldClose(true);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.deselect => {
|
||||||
|
current_menu_fn = null;
|
||||||
|
},
|
||||||
|
.draw => |d| {
|
||||||
|
canvas.rect(
|
||||||
|
.{ canvas.window_size[0] / 3, canvas.window_size[1] / 3 },
|
||||||
|
.{ canvas.window_size[0] / 3, canvas.window_size[1] / 3 },
|
||||||
|
.{ .color = .{ 0, 0, 0, 0xFF } },
|
||||||
|
);
|
||||||
|
const x: f32 = canvas.window_size[0] / 2;
|
||||||
|
var y: f32 = canvas.window_size[1] / 3 + (canvas.font.lineHeight * d.scalef);
|
||||||
|
for (MAIN_MENU_ITEMS, 0..) |menu_item, i| {
|
||||||
|
if (i == main_menu_item_selected) {
|
||||||
|
const size = canvas.printText(.{ x, y }, "> {s} <", .{menu_item}, .{ .@"align" = .center, .scale = d.scalef });
|
||||||
|
y += size[1];
|
||||||
|
} else {
|
||||||
|
const size = canvas.writeText(.{ x, y }, menu_item, .{ .@"align" = .center, .scale = d.scalef });
|
||||||
|
y += size[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const DeckSprites = assets.DeckSprites;
|
const DeckSprites = assets.DeckSprites;
|
||||||
const Card = assets.Card;
|
const Card = assets.Card;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue