remove `element` from `Action`, rely only on `command`
parent
a7bc7e87e0
commit
45bd514bd3
28
src/main.zig
28
src/main.zig
|
@ -153,28 +153,26 @@ pub fn main() !void {
|
||||||
if (hovered_action) |hovered| {
|
if (hovered_action) |hovered| {
|
||||||
if (input_state.left) {
|
if (input_state.left) {
|
||||||
for (actions.items) |action| {
|
for (actions.items) |action| {
|
||||||
if (action.element.pointer == hovered.element.pointer) {
|
if (std.mem.eql(u8, action.command, hovered.command)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (action.center[0] < hovered.center[0]) {
|
if (action.center[0] < hovered.center[0]) {
|
||||||
hovered_action = Action{
|
hovered_action = Action{
|
||||||
.center = .{ action.center[0], hovered.center[1] },
|
.center = .{ action.center[0], hovered.center[1] },
|
||||||
.command = action.command,
|
.command = action.command,
|
||||||
.element = action.element,
|
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (input_state.right) {
|
} else if (input_state.right) {
|
||||||
for (actions.items) |action| {
|
for (actions.items) |action| {
|
||||||
if (action.element.pointer == hovered.element.pointer) {
|
if (std.mem.eql(u8, action.command, hovered.command)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (action.center[0] > hovered.center[0]) {
|
if (action.center[0] > hovered.center[0]) {
|
||||||
hovered_action = Action{
|
hovered_action = Action{
|
||||||
.center = .{ action.center[0], hovered.center[1] },
|
.center = .{ action.center[0], hovered.center[1] },
|
||||||
.command = action.command,
|
.command = action.command,
|
||||||
.element = action.element,
|
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -238,7 +236,7 @@ pub fn main() !void {
|
||||||
root.pointer,
|
root.pointer,
|
||||||
&canvas,
|
&canvas,
|
||||||
.{
|
.{
|
||||||
.hovered = if (hovered_action) |ha| ha.element else null,
|
.hovered = if (hovered_action) |ha| ha.command else null,
|
||||||
.deck = deck_sprites,
|
.deck = deck_sprites,
|
||||||
},
|
},
|
||||||
.{ 0, 0 },
|
.{ 0, 0 },
|
||||||
|
@ -259,7 +257,7 @@ pub fn main() !void {
|
||||||
root.pointer,
|
root.pointer,
|
||||||
&actions,
|
&actions,
|
||||||
.{
|
.{
|
||||||
.hovered = if (hovered_action) |ha| ha.element else null,
|
.hovered = if (hovered_action) |ha| ha.command else null,
|
||||||
.deck = deck_sprites,
|
.deck = deck_sprites,
|
||||||
},
|
},
|
||||||
.{ 0, 0 },
|
.{ 0, 0 },
|
||||||
|
@ -276,11 +274,10 @@ const Action = struct {
|
||||||
center: [2]f32,
|
center: [2]f32,
|
||||||
/// A string representing what should occur if this action is taken
|
/// A string representing what should occur if this action is taken
|
||||||
command: []const u8,
|
command: []const u8,
|
||||||
element: Element,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const RenderResources = struct {
|
const RenderResources = struct {
|
||||||
hovered: ?Element,
|
hovered: ?[]const u8,
|
||||||
deck: DeckSprites,
|
deck: DeckSprites,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -565,6 +562,7 @@ pub const Pile = struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
cards: []Card,
|
cards: []Card,
|
||||||
hidden: bool = false,
|
hidden: bool = false,
|
||||||
|
command: ?[]const u8 = null,
|
||||||
|
|
||||||
pub fn create(allocator: std.mem.Allocator, cards: []const Card) !*@This() {
|
pub fn create(allocator: std.mem.Allocator, cards: []const Card) !*@This() {
|
||||||
const this = try allocator.create(@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));
|
const oy = -@as(f32, @floatFromInt(this.cards.len)) * (@as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[1])) / (52.0 * 4));
|
||||||
canvas.rect(
|
canvas.rect(
|
||||||
.{ min[0], start_y + oy },
|
.{ min[0], start_y + oy },
|
||||||
|
@ -633,6 +631,7 @@ 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 {
|
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));
|
||||||
|
|
||||||
|
if (this.command) |command| {
|
||||||
const center = [2]f32{
|
const center = [2]f32{
|
||||||
min[0] + @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[0])) / 2,
|
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)),
|
max[1] - @as(f32, @floatFromInt(render_resources.deck.tilesheet.tile_size[1])) * (2.0 - @as(f32, @floatFromInt(this.cards.len)) / (52.0 * 4)),
|
||||||
|
@ -640,10 +639,10 @@ pub const Pile = struct {
|
||||||
|
|
||||||
try actions.append(.{
|
try actions.append(.{
|
||||||
.center = center,
|
.center = center,
|
||||||
.command = "take",
|
.command = command,
|
||||||
.element = this.element(),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const CardElement = struct {
|
pub const CardElement = struct {
|
||||||
|
@ -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(
|
canvas.rect(
|
||||||
min,
|
min,
|
||||||
.{ max[0] - min[0], max[1] - min[1] },
|
.{ max[0] - min[0], max[1] - min[1] },
|
||||||
|
@ -728,7 +727,6 @@ pub const CardElement = struct {
|
||||||
(min[1] + max[1]) / 2,
|
(min[1] + max[1]) / 2,
|
||||||
},
|
},
|
||||||
.command = command,
|
.command = command,
|
||||||
.element = this.element(),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -738,14 +736,16 @@ pub const CardElement = struct {
|
||||||
fn drawCardHandler(arena: std.mem.Allocator, request: Request) HandlerError!Response {
|
fn drawCardHandler(arena: std.mem.Allocator, request: Request) HandlerError!Response {
|
||||||
var draw_pile = try Pile.create(arena, request.game_state.draw_pile.items);
|
var draw_pile = try Pile.create(arena, request.game_state.draw_pile.items);
|
||||||
draw_pile.hidden = true;
|
draw_pile.hidden = true;
|
||||||
|
draw_pile.command = "draw draw_pile";
|
||||||
|
|
||||||
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);
|
||||||
|
discard_pile.command = "draw discard_pile";
|
||||||
|
|
||||||
var hand = try HBox.create(arena);
|
var hand = try HBox.create(arena);
|
||||||
for (request.game_state.hands[0].items) |card| {
|
for (request.game_state.hands[0].items) |card| {
|
||||||
var card_element = try CardElement.create(arena, .{
|
var card_element = try CardElement.create(arena, .{
|
||||||
.visual = .{ .card = card },
|
.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());
|
try hand.addElement(card_element.element());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue