replace `Spread` with `CardElements` and an `HBox`

dev
LeRoyce Pearson 2024-02-26 20:18:39 -07:00
parent 91f896c5ef
commit a7bc7e87e0
1 changed files with 48 additions and 37 deletions

View File

@ -398,12 +398,12 @@ pub const Page = struct {
}; };
const child_min = [2]f32{ const child_min = [2]f32{
pos_in_parent[0] - pos_in_child[0], @max(min[0], pos_in_parent[0] - pos_in_child[0]),
pos_in_parent[1] - pos_in_child[1], @max(min[1], pos_in_parent[1] - pos_in_child[1]),
}; };
const child_max = [2]f32{ const child_max = [2]f32{
pos_in_parent[0] + (child_size[0] - pos_in_child[0]), @min(max[0], pos_in_parent[0] + (child_size[0] - pos_in_child[0])),
pos_in_parent[1] + (child_size[1] - pos_in_child[1]), @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); 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 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| { for (this.children.items) |child| {
const child_size = child.interface.minimum_size(child.pointer, render_resources); 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, allocator: std.mem.Allocator,
cards: []Card, visual: Visual,
hidden: bool = false, 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()); const this = try allocator.create(@This());
errdefer allocator.destroy(this); errdefer allocator.destroy(this);
const cards_owned = try allocator.dupe(Card, cards);
errdefer allocator.free(cards_owned);
this.* = .{ this.* = .{
.allocator = allocator, .allocator = allocator,
.cards = cards_owned, .visual = options.visual,
.command = options.command,
}; };
return this; return this;
} }
@ -676,9 +681,10 @@ pub const Spread = struct {
pub fn element_minimum_size(pointer: ?*anyopaque, render_resources: RenderResources) [2]f32 { pub fn element_minimum_size(pointer: ?*anyopaque, render_resources: RenderResources) [2]f32 {
const this: *@This() = @ptrCast(@alignCast(pointer)); const this: *@This() = @ptrCast(@alignCast(pointer));
_ = this;
return .{ 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])), @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 { 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)); const this: *@This() = @ptrCast(@alignCast(pointer));
for (this.cards, 0..) |card, i| { switch (this.visual) {
const ox = @as(f32, @floatFromInt(i)) * @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])); .back => render_resources.deck.tilesheet.renderTile(
if (this.hidden) { canvas,
render_resources.deck.tilesheet.renderTile(canvas, render_resources.deck.back, .{ render_resources.deck.back,
min[0] + ox, .{ min[0], min[1] },
min[1], .{},
}, .{}); ),
} else { .card => |card| render_resources.deck.tilesheet.renderTile(
render_resources.deck.tilesheet.renderTile(canvas, render_resources.deck.getTileForCard(card), .{ canvas,
min[0] + ox, render_resources.deck.getTileForCard(card),
min[1], .{ min[0], min[1] },
}, .{}); .{},
} ),
} }
if (render_resources.hovered != null and render_resources.hovered.?.pointer == @as(?*anyopaque, this)) { 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 { 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)); const this: *@This() = @ptrCast(@alignCast(pointer));
_ = max; _ = render_resources;
for (this.cards, 0..) |card, i| { if (this.command) |command| {
const ox = @as(f32, @floatFromInt(i)) * @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0]));
_ = card;
try actions.append(.{ try actions.append(.{
.center = [2]f32{ .center = [2]f32{
min[0] + ox, (min[0] + max[0]) / 2,
min[1], (min[1] + max[1]) / 2,
}, },
.command = "mark", .command = command,
.element = this.element(), .element = this.element(),
}); });
} }
@ -737,7 +740,15 @@ fn drawCardHandler(arena: std.mem.Allocator, request: Request) HandlerError!Resp
draw_pile.hidden = true; draw_pile.hidden = true;
var discard_pile = try Pile.create(arena, request.game_state.discard_pile.items); 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); var draw_discard_hbox = try HBox.create(arena);
try draw_discard_hbox.addElement(draw_pile.element()); try draw_discard_hbox.addElement(draw_pile.element());