Skip to content

Commit

Permalink
enhance utils.easing: support query var's existence
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Dec 3, 2024
1 parent 584765a commit 3e60419
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/utils/easing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ pub fn EasingSystem(comptime T: type) type {
pub const EasingApplyFn = *const fn (x: f32, from: T, to: T) T;
const EasingList = std.DoublyLinkedList(EasingValue);
const EasingPool = std.heap.MemoryPool(EasingList.Node);
const EasingSearchMap = std.AutoHashMap(*const T, void);
const Self = @This();

allocator: std.mem.Allocator,
pool: EasingPool,
vars: EasingList,
search_tree: EasingSearchMap,
sig: *EasingSignal, // Notify finished easing job

pub fn create(allocator: std.mem.Allocator) !*Self {
Expand All @@ -82,13 +84,15 @@ pub fn EasingSystem(comptime T: type) type {
.allocator = allocator,
.pool = EasingPool.init(allocator),
.vars = .{},
.search_tree = EasingSearchMap.init(allocator),
.sig = try EasingSignal.create(allocator),
};
return self;
}

pub fn destroy(self: *Self) void {
self.sig.destroy();
self.search_tree.deinit();
self.pool.deinit();
self.allocator.destroy(self);
}
Expand All @@ -113,12 +117,16 @@ pub fn EasingSystem(comptime T: type) type {
const x = ev.easing_fn(@min(1.0, ev.life_passed / ev.life_total));
ev.v.* = ev.easing_apply_fn(x, ev.from, ev.to);
if (ev.life_passed >= ev.life_total) {
// Remove from search tree
_ = self.search_tree.remove(ev.v);

// Notify subscribers
if (ev.finish) |fs| {
fs.callback(ev.*, fs.data);
}
self.sig.emit(.{ ev.v, ev.from, ev.to, ev.life_total, ev.easing_type });

// remove current node
// remove node from var list
const next_node = n.next;
defer node = next_node;
self.vars.remove(n);
Expand Down Expand Up @@ -160,8 +168,14 @@ pub fn EasingSystem(comptime T: type) type {
.finish = opt.finish,
},
};
errdefer self.pool.destroy(node);
try self.search_tree.put(v, {});
self.vars.append(node);
}

pub fn has(self: Self, v: *const T) bool {
return self.search_tree.get(v) != null;
}
};
}

Expand Down

0 comments on commit 3e60419

Please sign in to comment.