Compare commits
2 Commits
e39c75e19d
...
6ff5164721
Author | SHA1 | Date |
---|---|---|
LeRoyce Pearson | 6ff5164721 | |
LeRoyce Pearson | 771603b9d5 |
163
src/main.zig
163
src/main.zig
|
@ -23,13 +23,9 @@ var hovered_card: usize = 0;
|
|||
var selected_deck: ?*std.ArrayListUnmanaged(Card) = null;
|
||||
var selected_card: usize = 0;
|
||||
|
||||
var show_menu: bool = false;
|
||||
var menu_item_selected: u32 = 0;
|
||||
const MENU_ITEMS = [_][]const u8{
|
||||
"Continue",
|
||||
"New Game",
|
||||
"Quit",
|
||||
};
|
||||
const MenuEvent = union(enum) { up, down, draw: struct { scalef: f32 }, select, deselect };
|
||||
const MenuFn = *const fn (MenuEvent) anyerror!void;
|
||||
var current_menu_fn: ?MenuFn = null;
|
||||
|
||||
var win_count: ?u32 = null;
|
||||
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),
|
||||
};
|
||||
|
||||
_ = canvas.printText(
|
||||
.{
|
||||
0,
|
||||
canvas.window_size[1],
|
||||
},
|
||||
"{s}",
|
||||
.{build_options.version},
|
||||
.{ .@"align" = .left, .baseline = .bottom, .scale = scalef },
|
||||
);
|
||||
|
||||
if (win_count) |w| {
|
||||
_ = canvas.printText(
|
||||
.{
|
||||
|
@ -435,23 +421,8 @@ fn render(window: seizer.Window) !void {
|
|||
}
|
||||
}
|
||||
|
||||
if (show_menu) {
|
||||
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 * 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];
|
||||
}
|
||||
}
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.{ .draw = .{ .scalef = scalef } });
|
||||
}
|
||||
|
||||
canvas.end();
|
||||
|
@ -488,23 +459,17 @@ pub fn makeStandardDeck(allocator: std.mem.Allocator) ![]Card {
|
|||
|
||||
pub fn deselect(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (show_menu) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.deselect);
|
||||
}
|
||||
selected_deck = null;
|
||||
selected_card = 0;
|
||||
}
|
||||
|
||||
pub fn doSelectOrPlace(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (show_menu) {
|
||||
const menu_item = MENU_ITEMS[menu_item_selected];
|
||||
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);
|
||||
}
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.select);
|
||||
return;
|
||||
}
|
||||
if (selected_deck == null) {
|
||||
|
@ -639,7 +604,10 @@ pub fn doSelectOrPlace(pressed: bool) !void {
|
|||
|
||||
pub fn moveLeft(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (show_menu) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
_ = menu_fn;
|
||||
return;
|
||||
}
|
||||
if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -672,7 +640,10 @@ pub fn moveLeft(pressed: bool) !void {
|
|||
|
||||
pub fn moveRight(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (show_menu) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
_ = menu_fn;
|
||||
return;
|
||||
}
|
||||
if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -706,9 +677,8 @@ pub fn moveRight(pressed: bool) !void {
|
|||
pub fn moveUp(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
|
||||
if (show_menu) {
|
||||
if (menu_item_selected == 0) menu_item_selected = MENU_ITEMS.len;
|
||||
menu_item_selected -= 1;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.up);
|
||||
} else if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -750,9 +720,8 @@ pub fn moveUp(pressed: bool) !void {
|
|||
|
||||
pub fn moveDown(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (show_menu) {
|
||||
menu_item_selected += 1;
|
||||
menu_item_selected %= @intCast(MENU_ITEMS.len);
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.down);
|
||||
} else if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -803,7 +772,11 @@ pub fn undo(pressed: bool) !void {
|
|||
|
||||
pub fn toggleMenu(pressed: bool) !void {
|
||||
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 {
|
||||
|
@ -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 Card = assets.Card;
|
||||
|
||||
|
|
Loading…
Reference in New Issue