Skip to content

Commit

Permalink
utils.signal: add disconnect method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Dec 3, 2024
1 parent 9000294 commit d243cb6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions examples/easing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ const EasingBlock = struct {
}
};

fn finish_easing(v: *jok.Point, from: jok.Point, to: jok.Point, et: easing.EasingType) void {
fn finishEase(v: *jok.Point, from: jok.Point, to: jok.Point, duration: f32, et: easing.EasingType) void {
point_easing_system.add(
v,
et,
easing.easePoint,
2,
duration,
to,
from,
.{ .wait_time = 1 },
Expand All @@ -58,7 +58,7 @@ fn finish_easing(v: *jok.Point, from: jok.Point, to: jok.Point, et: easing.Easin
pub fn init(ctx: jok.Context) !void {
batchpool = try @TypeOf(batchpool).init(ctx);
point_easing_system = try easing.EasingSystem(jok.Point).create(ctx.allocator());
try point_easing_system.sig.connect(finish_easing);
try point_easing_system.sig.connect(finishEase);
for (&blocks, 0..) |*b, i| {
b.* = .{
.id = @intCast(i),
Expand Down
4 changes: 2 additions & 2 deletions src/utils/easing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn EasingSystem(comptime T: type) type {
to: T,
finish: ?Finish,
};
const EasingSignal = signal.Signal(&.{ *T, T, T, EasingType });
const EasingSignal = signal.Signal(&.{ *T, T, T, f32, EasingType });
const Self = @This();

allocator: std.mem.Allocator,
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn EasingSystem(comptime T: type) type {
if (ev.finish) |fs| {
fs.callback(ev.v, fs.data1, fs.data2);
}
self.sig.emit(.{ ev.v, ev.from, ev.to, ev.easing_type });
self.sig.emit(.{ ev.v, ev.from, ev.to, ev.life_total, ev.easing_type });
_ = self.vars.swapRemove(i);
size -= 1;
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/signal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub fn Signal(comptime types: []const type) type {
try self.connected.put(fp, {});
}

pub fn disconnect(self: *@This(), fp: *const FunType) void {
_ = self.connected.remove(fp);
}

pub fn emit(self: @This(), args: ArgsType) void {
if (self.connected.count() == 0) return;
var it = self.connected.keyIterator();
Expand Down Expand Up @@ -79,4 +83,8 @@ test "signal" {
try sig.connect(S.fun3);
sig.emit(.{ 10, 20, 30 });
try std.testing.expectEqual(60, S.counter);

sig.disconnect(S.fun3);
sig.emit(.{ 10, 20, 30 });
try std.testing.expectEqual(90, S.counter);
}

0 comments on commit d243cb6

Please sign in to comment.