diff --git a/res/config.ini b/res/config.ini index b9c912c..7a29aa6 100644 --- a/res/config.ini +++ b/res/config.ini @@ -167,20 +167,15 @@ doom_bottom_color = 0x00FFFFFF # Dur file path dur_file_path = $CONFIG_DIRECTORY/ly/example.dur -# Dur file alignment (center is the default for example) -# |-----------------| -# |1 2 3| -# | | -# |4 0 5| -# | | -# |6 7 8| -# |-----------------| -dur_offset_alignment = 0 +# Dur file alignment +# The dur file can be aligned with a direction and centered easily with the flags below +# Available inputs: topleft, topcenter, topright, centerleft, center, centerright, bottomleft, bottomcenter, bottomright +dur_offset_alignment = center -# Dur offset x direction +# Dur offset x direction (value is added to the current position determined by alignment, negatives are supported) dur_x_offset = 0 -# Dur offset y direction +# Dur offset y direction (value is added to the current position determined by alignment, negatives are supported) dur_y_offset = 0 # Set margin to the edges of the DM (useful for curved monitors) diff --git a/src/animations/DurFile.zig b/src/animations/DurFile.zig index d1b08ef..86c3587 100644 --- a/src/animations/DurFile.zig +++ b/src/animations/DurFile.zig @@ -2,6 +2,8 @@ const std = @import("std"); const Animation = @import("../tui/Animation.zig"); const Cell = @import("../tui/Cell.zig"); const TerminalBuffer = @import("../tui/TerminalBuffer.zig"); +const enums = @import("../enums.zig"); +const DurOffsetAlignment = enums.DurOffsetAlignment; const Color = TerminalBuffer.Color; const Styling = TerminalBuffer.Styling; const Allocator = std.mem.Allocator; @@ -286,25 +288,12 @@ fn convert_256_to_rgb(color_256: u32) u32 { return rgb_color; } -const UVec2 = @Vector(2, u16); -const IVec2 = @Vector(2, i32); +const UVec2 = @Vector(2, u32); +const IVec2 = @Vector(2, i64); const VEC_X = 0; const VEC_Y = 1; -const Alignment = struct { - pub const CENTER = 0; // Center is the default so we have it out of order - pub const TOP_LEFT = 1; - pub const TOP_CENTER = 2; - pub const TOP_RIGHT = 3; - pub const CENTER_LEFT = 4; - pub const CENTER_RIGHT = 5; - pub const BOTTOM_LEFT = 6; - pub const BOTTOM_CENTER = 7; - pub const BOTTOM_RIGHT = 8; -}; - - const DurFile = @This(); allocator: Allocator, @@ -318,7 +307,12 @@ frame_time: u32, time_previous: i64, is_color_format_16: bool, -pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer: *std.io.Writer, file_path: []const u8, offset_alignment: u32, x_offset: i32, y_offset: i32, full_color: bool) !DurFile { +// if the user has an even number of columns or rows, we will default to the left or higher position (e.g. 4 columns center = .x..) +fn center(v: u32) i64 { + return @intCast((v / 2) + (v % 2)); +} + +pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer: *std.io.Writer, file_path: []const u8, offset_alignment: DurOffsetAlignment, x_offset: i32, y_offset: i32, full_color: bool) !DurFile { var dur_movie: DurFormat = .init(allocator); dur_movie.create_from_file(allocator, file_path) catch |err| switch (err) { @@ -340,31 +334,26 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer: return error.InvalidColorFormat; } - const buf_width: u16 = @intCast(terminal_buffer.width); - const buf_height: u16 = @intCast(terminal_buffer.height); + const buf_width: u32 = @intCast(terminal_buffer.width); + const buf_height: u32 = @intCast(terminal_buffer.height); - const movie_width: u16 = @intCast(dur_movie.columns.?); - const movie_height: u16 = @intCast(dur_movie.lines.?); + const movie_width: u32 = @intCast(dur_movie.columns.?); + const movie_height: u32 = @intCast(dur_movie.lines.?); - // if the user has an even number of columns or rows, we will default to the left or higher position (e.g. 4 columns center = .x..) - const center = struct { - fn center(v: u16) i32 { return @intCast((v / 2) + (v % 2)); } - }.center; - var start_pos = switch (offset_alignment) { - Alignment.CENTER => IVec2{ center(buf_width) - center(movie_width), center(buf_height) - center(movie_height) }, - Alignment.TOP_LEFT => IVec2{ 0, 0 }, - Alignment.TOP_CENTER => IVec2{ center(buf_width) - center(movie_width), 0 }, - Alignment.TOP_RIGHT => IVec2{ buf_width - movie_width, 0 }, - Alignment.CENTER_LEFT => IVec2{ 0, center(buf_width) - center(movie_width) }, - Alignment.CENTER_RIGHT => IVec2{ buf_width - movie_width, center(buf_width) - center(movie_width) }, - Alignment.BOTTOM_LEFT => IVec2{ 0, buf_width - movie_height }, - Alignment.BOTTOM_CENTER => IVec2{ center(buf_width) - center(movie_width), buf_width - movie_height }, - Alignment.BOTTOM_RIGHT => IVec2{ buf_width - movie_width, buf_height - movie_height }, - else => return error.InvalidDurOffsetAlignment, + var start_pos: IVec2 = switch (offset_alignment) { + DurOffsetAlignment.center => .{ center(buf_width) - center(movie_width), center(buf_height) - center(movie_height) }, + DurOffsetAlignment.topleft => .{ 0, 0 }, + DurOffsetAlignment.topcenter => .{ center(buf_width) - center(movie_width), 0 }, + DurOffsetAlignment.topright => .{ buf_width - movie_width, 0 }, + DurOffsetAlignment.centerleft => .{ 0, center(buf_width) - center(movie_width) }, + DurOffsetAlignment.centerright => .{ buf_width - movie_width, center(buf_width) - center(movie_width) }, + DurOffsetAlignment.bottomleft => .{ 0, buf_width - movie_height }, + DurOffsetAlignment.bottomcenter => .{ center(buf_width) - center(movie_width), buf_width - movie_height }, + DurOffsetAlignment.bottomright => .{ buf_width - movie_width, buf_height - movie_height }, }; - start_pos += IVec2{ x_offset, y_offset }; + start_pos += .{ x_offset, y_offset }; // Draw only the needed amount if movie smaller than screen. If movie is bigger, we will just draw entire screen const frame_width = if (movie_width < buf_width) movie_width else buf_width; @@ -378,7 +367,7 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_writer: .terminal_buffer = terminal_buffer, .frames = 0, .time_previous = std.time.milliTimestamp(), - .frame_size = UVec2{ frame_width, frame_height }, + .frame_size = .{ frame_width, frame_height }, .start_pos = start_pos, .full_color = full_color, .dur_movie = dur_movie, @@ -400,20 +389,20 @@ fn realloc(_: *DurFile) anyerror!void {} fn draw(self: *DurFile) void { const current_frame = self.dur_movie.frames.items[self.frames]; - const buf_width: u16 = @intCast(self.terminal_buffer.width); - const buf_height: u16 = @intCast(self.terminal_buffer.height); + const buf_width: u32 = @intCast(self.terminal_buffer.width); + const buf_height: u32 = @intCast(self.terminal_buffer.height); // y is used as an iterator in the durformat, while cell_y gives us the correct placement for the cell (same for x) for (0..self.frame_size[VEC_Y]) |y| { - const y_offset_i: i32 = @as(i32, @intCast(y)) + self.start_pos[VEC_Y]; + const y_offset_i = @as(i32, @intCast(y)) + self.start_pos[VEC_Y]; // we skip the pass if it falls outside of the draw window (ensure no int underflow) const cell_y: u32 = if (y_offset_i >= 0 and y_offset_i < buf_height) @intCast(y_offset_i) else continue; var iter = std.unicode.Utf8View.initUnchecked(current_frame.contents[y]).iterator(); for (0..self.frame_size[VEC_X]) |x| { - const x_offset_i: i32 = @as(i32, @intCast(x)) + self.start_pos[VEC_X]; - // skip pass, same as y but also incriment the codepoint iter to fetch correct values in later passes + const x_offset_i = @as(i32, @intCast(x)) + self.start_pos[VEC_X]; + // skip pass, same as y but also increment the codepoint iter to fetch correct values in later passes const cell_x: u32 = if (x_offset_i >= 0 and x_offset_i < buf_width) @intCast(x_offset_i) else { _ = iter.nextCodepoint().?; continue; diff --git a/src/config/Config.zig b/src/config/Config.zig index b1b45b8..3e0cf1e 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -5,6 +5,7 @@ const Animation = enums.Animation; const Input = enums.Input; const ViMode = enums.ViMode; const Bigclock = enums.Bigclock; +const DurOffsetAlignment = enums.DurOffsetAlignment; allow_empty_password: bool = true, animation: Animation = .none, @@ -43,7 +44,7 @@ doom_top_color: u32 = 0x00FF0000, doom_middle_color: u32 = 0x00FFFF00, doom_bottom_color: u32 = 0x00FFFFFF, dur_file_path: []const u8 = build_options.config_directory ++ "/ly/example.dur", -dur_offset_alignment: u32 = 0, +dur_offset_alignment: DurOffsetAlignment = .center, dur_x_offset: i32 = 0, dur_y_offset: i32 = 0, edge_margin: u8 = 0, diff --git a/src/enums.zig b/src/enums.zig index 2cec482..47771da 100644 --- a/src/enums.zig +++ b/src/enums.zig @@ -54,3 +54,15 @@ pub const Bigclock = enum { en, fa, }; + +pub const DurOffsetAlignment = enum { + topleft, + topcenter, + topright, + centerleft, + center, + centerright, + bottomleft, + bottomcenter, + bottomright, +};