Compare commits
No commits in common. "6ff5164721c4156e2a22611da7385afd615b2302" and "e39c75e19d58a9f324934d77562f80cfa45a8e32" have entirely different histories.
6ff5164721
...
e39c75e19d
163
src/main.zig
163
src/main.zig
|
@ -23,9 +23,13 @@ var hovered_card: usize = 0;
|
|||
var selected_deck: ?*std.ArrayListUnmanaged(Card) = null;
|
||||
var selected_card: usize = 0;
|
||||
|
||||
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 show_menu: bool = false;
|
||||
var menu_item_selected: u32 = 0;
|
||||
const MENU_ITEMS = [_][]const u8{
|
||||
"Continue",
|
||||
"New Game",
|
||||
"Quit",
|
||||
};
|
||||
|
||||
var win_count: ?u32 = null;
|
||||
var win_triggered: bool = false;
|
||||
|
@ -255,6 +259,16 @@ 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(
|
||||
.{
|
||||
|
@ -421,8 +435,23 @@ fn render(window: seizer.Window) !void {
|
|||
}
|
||||
}
|
||||
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.{ .draw = .{ .scalef = scalef } });
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
canvas.end();
|
||||
|
@ -459,17 +488,23 @@ pub fn makeStandardDeck(allocator: std.mem.Allocator) ![]Card {
|
|||
|
||||
pub fn deselect(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.deselect);
|
||||
}
|
||||
if (show_menu) return;
|
||||
selected_deck = null;
|
||||
selected_card = 0;
|
||||
}
|
||||
|
||||
pub fn doSelectOrPlace(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.select);
|
||||
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);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (selected_deck == null) {
|
||||
|
@ -604,10 +639,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
|
|||
|
||||
pub fn moveLeft(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
_ = menu_fn;
|
||||
return;
|
||||
}
|
||||
if (show_menu) return;
|
||||
if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -640,10 +672,7 @@ pub fn moveLeft(pressed: bool) !void {
|
|||
|
||||
pub fn moveRight(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
_ = menu_fn;
|
||||
return;
|
||||
}
|
||||
if (show_menu) return;
|
||||
if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -677,8 +706,9 @@ pub fn moveRight(pressed: bool) !void {
|
|||
pub fn moveUp(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.up);
|
||||
if (show_menu) {
|
||||
if (menu_item_selected == 0) menu_item_selected = MENU_ITEMS.len;
|
||||
menu_item_selected -= 1;
|
||||
} else if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -720,8 +750,9 @@ pub fn moveUp(pressed: bool) !void {
|
|||
|
||||
pub fn moveDown(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (current_menu_fn) |menu_fn| {
|
||||
try menu_fn(.down);
|
||||
if (show_menu) {
|
||||
menu_item_selected += 1;
|
||||
menu_item_selected %= @intCast(MENU_ITEMS.len);
|
||||
} else if (hovered_deck == null) {
|
||||
hovered_deck = &draw_pile;
|
||||
hovered_card = 0;
|
||||
|
@ -772,11 +803,7 @@ pub fn undo(pressed: bool) !void {
|
|||
|
||||
pub fn toggleMenu(pressed: bool) !void {
|
||||
if (!pressed) return;
|
||||
if (current_menu_fn == null) {
|
||||
current_menu_fn = menuMain;
|
||||
} else {
|
||||
current_menu_fn = null;
|
||||
}
|
||||
show_menu = !show_menu;
|
||||
}
|
||||
|
||||
pub fn indexOfTopOfStack(cards: []Card) usize {
|
||||
|
@ -878,88 +905,6 @@ 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