Add Widget.calculateTimeout function

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-02-12 00:27:07 +01:00
parent 7c7aed9cb2
commit 5564fed664
15 changed files with 149 additions and 15 deletions

View File

@@ -32,6 +32,7 @@ pub fn widget(self: *Cascade) Widget {
draw,
null,
null,
null,
);
}

View File

@@ -24,6 +24,7 @@ start_time: TimeOfDay,
terminal_buffer: *TerminalBuffer,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
frames: u64,
pattern_cos_mod: f32,
pattern_sin_mod: f32,
@@ -36,12 +37,14 @@ pub fn init(
col3: u32,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
) !ColorMix {
return .{
.start_time = try interop.getTimeOfDay(),
.terminal_buffer = terminal_buffer,
.animate = animate,
.timeout_sec = timeout_sec,
.frame_delay = frame_delay,
.frames = 0,
.pattern_cos_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
.pattern_sin_mod = terminal_buffer.random.float(f32) * math.pi * 2.0,
@@ -71,6 +74,7 @@ pub fn widget(self: *ColorMix) Widget {
draw,
update,
null,
calculateTimeout,
);
}
@@ -116,3 +120,7 @@ fn update(self: *ColorMix, _: *anyopaque) !void {
self.animate.* = false;
}
}
fn calculateTimeout(self: *ColorMix, _: *anyopaque) !?usize {
return self.frame_delay;
}

View File

@@ -20,6 +20,7 @@ allocator: Allocator,
terminal_buffer: *TerminalBuffer,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
buffer: []u8,
height: u8,
spread: u8,
@@ -35,6 +36,7 @@ pub fn init(
fire_spread: u8,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
) !Doom {
const buffer = try allocator.alloc(u8, terminal_buffer.width * terminal_buffer.height);
initBuffer(buffer, terminal_buffer.width);
@@ -62,6 +64,7 @@ pub fn init(
.terminal_buffer = terminal_buffer,
.animate = animate,
.timeout_sec = timeout_sec,
.frame_delay = frame_delay,
.buffer = buffer,
.height = @min(HEIGHT_MAX, fire_height),
.spread = @min(SPREAD_MAX, fire_spread),
@@ -78,6 +81,7 @@ pub fn widget(self: *Doom) Widget {
draw,
update,
null,
calculateTimeout,
);
}
@@ -147,3 +151,7 @@ fn update(self: *Doom, _: *anyopaque) !void {
self.animate.* = false;
}
}
fn calculateTimeout(self: *Doom, _: *anyopaque) !?usize {
return self.frame_delay;
}

View File

@@ -312,6 +312,7 @@ start_pos: IVec2,
full_color: bool,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
frame_time: u32,
time_previous: i64,
is_color_format_16: bool,
@@ -373,6 +374,7 @@ pub fn init(
full_color: bool,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
) !DurFile {
var dur_movie: DurFormat = .init(allocator);
@@ -414,6 +416,7 @@ pub fn init(
.full_color = full_color,
.animate = animate,
.timeout_sec = timeout_sec,
.frame_delay = frame_delay,
.dur_movie = dur_movie,
.frame_time = frame_time,
.is_color_format_16 = eql(u8, dur_movie.colorFormat.?, "16"),
@@ -431,6 +434,7 @@ pub fn widget(self: *DurFile) Widget {
draw,
update,
null,
calculateTimeout,
);
}
@@ -508,3 +512,7 @@ fn update(self: *DurFile, _: *anyopaque) !void {
self.animate.* = false;
}
}
fn calculateTimeout(self: *DurFile, _: *anyopaque) !?usize {
return self.frame_delay;
}

View File

@@ -33,6 +33,7 @@ frame_delay: usize,
initial_density: f32,
animate: *bool,
timeout_sec: u12,
animation_frame_delay: u16,
dead_cell: Cell,
width: usize,
height: usize,
@@ -46,6 +47,7 @@ pub fn init(
initial_density: f32,
animate: *bool,
timeout_sec: u12,
animation_frame_delay: u16,
) !GameOfLife {
const width = terminal_buffer.width;
const height = terminal_buffer.height;
@@ -68,6 +70,7 @@ pub fn init(
.initial_density = initial_density,
.animate = animate,
.timeout_sec = timeout_sec,
.animation_frame_delay = animation_frame_delay,
.dead_cell = .{ .ch = DEAD_CHAR, .fg = @intCast(TerminalBuffer.Color.DEFAULT), .bg = terminal_buffer.bg },
.width = width,
.height = height,
@@ -88,6 +91,7 @@ pub fn widget(self: *GameOfLife) Widget {
draw,
update,
null,
calculateTimeout,
);
}
@@ -149,6 +153,10 @@ fn update(self: *GameOfLife, _: *anyopaque) !void {
}
}
fn calculateTimeout(self: *GameOfLife, _: *anyopaque) !?usize {
return self.animation_frame_delay;
}
fn updateGeneration(self: *GameOfLife) void {
// Conway's Game of Life rules with optimized neighbor counting
for (0..self.height) |y| {

View File

@@ -41,6 +41,7 @@ min_codepoint: u16,
max_codepoint: u16,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
default_cell: Cell,
pub fn init(
@@ -52,6 +53,7 @@ pub fn init(
max_codepoint: u16,
animate: *bool,
timeout_sec: u12,
frame_delay: u16,
) !Matrix {
const dots = try allocator.alloc(Dot, terminal_buffer.width * (terminal_buffer.height + 1));
const lines = try allocator.alloc(Line, terminal_buffer.width);
@@ -72,6 +74,7 @@ pub fn init(
.max_codepoint = max_codepoint - min_codepoint,
.animate = animate,
.timeout_sec = timeout_sec,
.frame_delay = frame_delay,
.default_cell = .{ .ch = ' ', .fg = fg, .bg = terminal_buffer.bg },
};
}
@@ -85,6 +88,7 @@ pub fn widget(self: *Matrix) Widget {
draw,
update,
null,
calculateTimeout,
);
}
@@ -205,6 +209,10 @@ fn update(self: *Matrix, _: *anyopaque) !void {
}
}
fn calculateTimeout(self: *Matrix, _: *anyopaque) !?usize {
return self.frame_delay;
}
fn initBuffers(dots: []Dot, lines: []Line, width: usize, height: usize, random: Random) void {
var y: usize = 0;
while (y <= height) : (y += 1) {