Compare commits

...

2 Commits

Author SHA1 Message Date
LeRoyce Pearson 6ff5164721 feat: add About menu with version info
/ build-dist (push) Successful in 1m16s Details
2024-05-30 12:48:20 -06:00
LeRoyce Pearson 771603b9d5 refactor: make it possible to have multiple menus 2024-05-30 12:25:54 -06:00
1 changed files with 109 additions and 54 deletions

View File

@ -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;
@ -259,16 +255,6 @@ fn render(window: seizer.Window) !void {
@floor((canvas.window_size[1] - space_taken_by_board[1]) / 2), @floor((canvas.window_size[1] - space_taken_by_board[1]) / 2),
}; };
_ = canvas.printText(
.{
0,
canvas.window_size[1],
},
"{s}",
.{build_options.version},
.{ .@"align" = .left, .baseline = .bottom, .scale = scalef },
);
if (win_count) |w| { if (win_count) |w| {
_ = canvas.printText( _ = canvas.printText(
.{ .{
@ -435,23 +421,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 +459,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 +604,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 +640,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 +677,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 +720,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 +772,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 +878,88 @@ const Snapshot = struct {
} }
}; };
var main_menu_item_selected: u32 = 0;
const MAIN_MENU_ITEMS = [_][]const u8{
"Continue",
"New Game",
"About",
"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, "About")) {
current_menu_fn = menuAbout;
} 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];
}
}
},
}
}
fn menuAbout(event: MenuEvent) !void {
switch (event) {
.up => {},
.down => {},
.select => {},
.deselect => {
current_menu_fn = menuMain;
},
.draw => |d| {
const version_text_size = canvas.font.fmtTextSize("version = {s}", .{build_options.version}, d.scalef);
const margin = [2]f32{ d.scalef * 10, d.scalef * 10 };
const menu_size = [2]f32{ version_text_size[0] + 2 * margin[0], version_text_size[1] + 2 * margin[1] };
const menu_offset = [2]f32{ @floor((canvas.window_size[0] - menu_size[0]) / 2), @floor((canvas.window_size[1] - menu_size[1]) / 2) };
canvas.rect(
menu_offset,
menu_size,
.{ .color = .{ 0, 0, 0, 0xFF } },
);
const x: f32 = canvas.window_size[0] / 2;
const y: f32 = menu_offset[1] + margin[1];
_ = canvas.printText(.{ x, y }, "version = {s}", .{build_options.version}, .{ .@"align" = .center, .scale = d.scalef });
},
}
}
const DeckSprites = assets.DeckSprites; const DeckSprites = assets.DeckSprites;
const Card = assets.Card; const Card = assets.Card;