From a7fcbe2d73448b461154f9544f8dd7663b5c946c Mon Sep 17 00:00:00 2001 From: 00JCIV00 Date: Mon, 23 Sep 2024 22:00:30 -0400 Subject: [PATCH] view: continued initial implementation - Added proper bounds checking to fit the View within the provided Window. - Added a simple x/y return Type to the `toWin()` function so users can more easily maintain scrolling bounds. --- src/View.zig | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/View.zig b/src/View.zig index ae96655..1c6d395 100644 --- a/src/View.zig +++ b/src/View.zig @@ -57,7 +57,7 @@ pub fn deinit(self: *View) void { self.alloc.destroy(self.screen); } -///Render Config f/ `toWin()` +/// Render Config f/ `toWin()` pub const RenderConfig = struct { x: usize = 0, y: usize = 0, @@ -70,35 +70,37 @@ pub const RenderConfig = struct { }; }; /// Render a portion of this View to the provided Window (`win`). -pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !void { - //if (config.x >= self.screen.width or config.y >= self.screen.height) - // return error.PositionOutOfBounds; - const x = @min(self.screen.width - 1, config.x); - const y = @min(self.screen.height - 1, config.y); +/// This will return the bounded X (col), Y (row) coordinates based on the rendering. +pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !struct{ usize, usize } { + var x = @min(self.screen.width - 1, config.x); + var y = @min(self.screen.height - 1, config.y); const width = width: { - const width = switch (config.width) { + var width = switch (config.width) { .fit => win.width, .max => |w| @min(win.width, w), }; - break :width @min(width, self.screen.width -| x); + width = @min(width, self.screen.width); + break :width @min(width, self.screen.width -| 1 -| x +| win.width); }; const height = height: { - const height = switch (config.height) { + var height = switch (config.height) { .fit => win.height, .max => |h| @min(win.height, h), }; - break :height @min(height, self.screen.height -| y); + height = @min(height, self.screen.height); + break :height @min(height, self.screen.height -| 1 -| y +| win.height); }; - //win.clear(); + x = @min(x, self.screen.width -| width); + y = @min(y, self.screen.height -| height); + for (0..height) |row| { for (0..width) |col| { win.writeCell( col, row, self.win.readCell( - @min(self.screen.width, x +| col), - //self.screen.height -| 1 -| @min(self.screen.height, y +| row), - @min(self.screen.height, y +| row), + @min(self.screen.width -| 1, x +| col), + @min(self.screen.height -| 1, y +| row), ) orelse { std.log.err( \\ Position Out of Bounds: @@ -114,6 +116,7 @@ pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !void { ); } } + return .{ x, y }; } /// Writes a cell to the location in the View