From a7bc7e87e044c9a50f456c0e673a07b04cdd62d0 Mon Sep 17 00:00:00 2001 From: geemili Date: Mon, 26 Feb 2024 20:18:39 -0700 Subject: [PATCH] replace `Spread` with `CardElements` and an `HBox` --- src/main.zig | 85 +++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/src/main.zig b/src/main.zig index 7cd9fdb..e619105 100644 --- a/src/main.zig +++ b/src/main.zig @@ -398,12 +398,12 @@ pub const Page = struct { }; const child_min = [2]f32{ - pos_in_parent[0] - pos_in_child[0], - pos_in_parent[1] - pos_in_child[1], + @max(min[0], pos_in_parent[0] - pos_in_child[0]), + @max(min[1], pos_in_parent[1] - pos_in_child[1]), }; const child_max = [2]f32{ - pos_in_parent[0] + (child_size[0] - pos_in_child[0]), - pos_in_parent[1] + (child_size[1] - pos_in_child[1]), + @min(max[0], pos_in_parent[0] + (child_size[0] - pos_in_child[0])), + @min(max[1], pos_in_parent[1] + (child_size[1] - pos_in_child[1])), }; child.element.interface.render(child.element.pointer, canvas, render_resources, child_min, child_max); @@ -509,9 +509,11 @@ pub const HBox = struct { const empty_space = parent_size[0] - filled_space; - const space_around = empty_space / @as(f32, @floatFromInt((this.children.items.len + 1))); + const num_spaces = if (empty_space > 0) this.children.items.len + 1 else this.children.items.len - 1; - var x: f32 = min[0] + space_around; + const space_around = empty_space / @as(f32, @floatFromInt(num_spaces)); + + var x: f32 = min[0] + @max(space_around, 0); for (this.children.items) |child| { const child_size = child.interface.minimum_size(child.pointer, render_resources); @@ -644,21 +646,24 @@ pub const Pile = struct { } }; -pub const Spread = struct { +pub const CardElement = struct { allocator: std.mem.Allocator, - cards: []Card, - hidden: bool = false, + visual: Visual, + command: ?[]const u8, - pub fn create(allocator: std.mem.Allocator, cards: []const Card) !*@This() { + const Visual = union(enum) { + back, + card: Card, + }; + + pub fn create(allocator: std.mem.Allocator, options: struct { visual: Visual, command: ?[]const u8 }) !*@This() { const this = try allocator.create(@This()); errdefer allocator.destroy(this); - const cards_owned = try allocator.dupe(Card, cards); - errdefer allocator.free(cards_owned); - this.* = .{ .allocator = allocator, - .cards = cards_owned, + .visual = options.visual, + .command = options.command, }; return this; } @@ -676,9 +681,10 @@ pub const Spread = struct { pub fn element_minimum_size(pointer: ?*anyopaque, render_resources: RenderResources) [2]f32 { const this: *@This() = @ptrCast(@alignCast(pointer)); + _ = this; return .{ - @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])) * @as(f32, @floatFromInt(this.cards.len)), + @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])), @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[1])), }; } @@ -686,19 +692,19 @@ pub const Spread = struct { pub fn element_render(pointer: ?*anyopaque, canvas: *seizer.Canvas, render_resources: RenderResources, min: [2]f32, max: [2]f32) void { const this: *@This() = @ptrCast(@alignCast(pointer)); - for (this.cards, 0..) |card, i| { - const ox = @as(f32, @floatFromInt(i)) * @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])); - if (this.hidden) { - render_resources.deck.tilesheet.renderTile(canvas, render_resources.deck.back, .{ - min[0] + ox, - min[1], - }, .{}); - } else { - render_resources.deck.tilesheet.renderTile(canvas, render_resources.deck.getTileForCard(card), .{ - min[0] + ox, - min[1], - }, .{}); - } + switch (this.visual) { + .back => render_resources.deck.tilesheet.renderTile( + canvas, + render_resources.deck.back, + .{ min[0], min[1] }, + .{}, + ), + .card => |card| render_resources.deck.tilesheet.renderTile( + canvas, + render_resources.deck.getTileForCard(card), + .{ min[0], min[1] }, + .{}, + ), } if (render_resources.hovered != null and render_resources.hovered.?.pointer == @as(?*anyopaque, this)) { @@ -713,18 +719,15 @@ pub const Spread = struct { pub fn element_get_actions(pointer: ?*anyopaque, actions: *std.ArrayList(Action), render_resources: RenderResources, min: [2]f32, max: [2]f32) Element.Error!void { const this: *@This() = @ptrCast(@alignCast(pointer)); - _ = max; + _ = render_resources; - for (this.cards, 0..) |card, i| { - const ox = @as(f32, @floatFromInt(i)) * @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])); - - _ = card; + if (this.command) |command| { try actions.append(.{ .center = [2]f32{ - min[0] + ox, - min[1], + (min[0] + max[0]) / 2, + (min[1] + max[1]) / 2, }, - .command = "mark", + .command = command, .element = this.element(), }); } @@ -737,7 +740,15 @@ fn drawCardHandler(arena: std.mem.Allocator, request: Request) HandlerError!Resp draw_pile.hidden = true; var discard_pile = try Pile.create(arena, request.game_state.discard_pile.items); - var hand = try Spread.create(arena, request.game_state.hands[0].items); + + var hand = try HBox.create(arena); + for (request.game_state.hands[0].items) |card| { + var card_element = try CardElement.create(arena, .{ + .visual = .{ .card = card }, + .command = "mark", + }); + try hand.addElement(card_element.element()); + } var draw_discard_hbox = try HBox.create(arena); try draw_discard_hbox.addElement(draw_pile.element());