remove `element` from `Action`, rely only on `command`

dev
LeRoyce Pearson 2024-02-26 20:39:10 -07:00
parent a7bc7e87e0
commit 45bd514bd3
1 changed files with 21 additions and 21 deletions

View File

@ -153,28 +153,26 @@ pub fn main() !void {
if (hovered_action) |hovered| {
if (input_state.left) {
for (actions.items) |action| {
if (action.element.pointer == hovered.element.pointer) {
if (std.mem.eql(u8, action.command, hovered.command)) {
continue;
}
if (action.center[0] < hovered.center[0]) {
hovered_action = Action{
.center = .{ action.center[0], hovered.center[1] },
.command = action.command,
.element = action.element,
};
break;
}
}
} else if (input_state.right) {
for (actions.items) |action| {
if (action.element.pointer == hovered.element.pointer) {
if (std.mem.eql(u8, action.command, hovered.command)) {
continue;
}
if (action.center[0] > hovered.center[0]) {
hovered_action = Action{
.center = .{ action.center[0], hovered.center[1] },
.command = action.command,
.element = action.element,
};
break;
}
@ -238,7 +236,7 @@ pub fn main() !void {
root.pointer,
&canvas,
.{
.hovered = if (hovered_action) |ha| ha.element else null,
.hovered = if (hovered_action) |ha| ha.command else null,
.deck = deck_sprites,
},
.{ 0, 0 },
@ -259,7 +257,7 @@ pub fn main() !void {
root.pointer,
&actions,
.{
.hovered = if (hovered_action) |ha| ha.element else null,
.hovered = if (hovered_action) |ha| ha.command else null,
.deck = deck_sprites,
},
.{ 0, 0 },
@ -276,11 +274,10 @@ const Action = struct {
center: [2]f32,
/// A string representing what should occur if this action is taken
command: []const u8,
element: Element,
};
const RenderResources = struct {
hovered: ?Element,
hovered: ?[]const u8,
deck: DeckSprites,
};
@ -565,6 +562,7 @@ pub const Pile = struct {
allocator: std.mem.Allocator,
cards: []Card,
hidden: bool = false,
command: ?[]const u8 = null,
pub fn create(allocator: std.mem.Allocator, cards: []const Card) !*@This() {
const this = try allocator.create(@This());
@ -620,7 +618,7 @@ pub const Pile = struct {
}
}
if (render_resources.hovered != null and render_resources.hovered.?.pointer == @as(?*anyopaque, this)) {
if (render_resources.hovered != null and this.command != null and std.mem.eql(u8, render_resources.hovered.?, this.command.?)) {
const oy = -@as(f32, @floatFromInt(this.cards.len)) * (@as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[1])) / (52.0 * 4));
canvas.rect(
.{ min[0], start_y + oy },
@ -633,16 +631,17 @@ pub const Pile = 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));
const center = [2]f32{
min[0] + @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])) / 2,
max[1] - @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[1])) * (2.0 - @as(f32, @floatFromInt(this.cards.len)) / (52.0 * 4)),
};
if (this.command) |command| {
const center = [2]f32{
min[0] + @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])) / 2,
max[1] - @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[1])) * (2.0 - @as(f32, @floatFromInt(this.cards.len)) / (52.0 * 4)),
};
try actions.append(.{
.center = center,
.command = "take",
.element = this.element(),
});
try actions.append(.{
.center = center,
.command = command,
});
}
}
};
@ -707,7 +706,7 @@ pub const CardElement = struct {
),
}
if (render_resources.hovered != null and render_resources.hovered.?.pointer == @as(?*anyopaque, this)) {
if (render_resources.hovered != null and this.command != null and std.mem.eql(u8, render_resources.hovered.?, this.command.?)) {
canvas.rect(
min,
.{ max[0] - min[0], max[1] - min[1] },
@ -728,7 +727,6 @@ pub const CardElement = struct {
(min[1] + max[1]) / 2,
},
.command = command,
.element = this.element(),
});
}
}
@ -738,14 +736,16 @@ pub const CardElement = struct {
fn drawCardHandler(arena: std.mem.Allocator, request: Request) HandlerError!Response {
var draw_pile = try Pile.create(arena, request.game_state.draw_pile.items);
draw_pile.hidden = true;
draw_pile.command = "draw draw_pile";
var discard_pile = try Pile.create(arena, request.game_state.discard_pile.items);
discard_pile.command = "draw discard_pile";
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",
.command = try std.fmt.allocPrint(arena, "mark {} of {s}", .{ card.rank, @tagName(card.suit) }),
});
try hand.addElement(card_element.element());
}