Skip to content

Commit

Permalink
fix(view): fixed several items from PR #82 review
Browse files Browse the repository at this point in the history
- Removed Window allocation in `init()`.
- Changed `max_width` and `max_height` to `width` and `height` in `Config`.
- Removed `x_pixel` and `y_pixel` from `Config`.
- Changed the `win` parameter of `toWin()` to Type `Window`.
- Fixed a typo in `fill()` so that it's no longer a recursion trap.
- Updated the example w/ corresponding fixes.
  • Loading branch information
00JCIV00 authored and rockorager committed Oct 3, 2024
1 parent d6f5ac2 commit 05bff97
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
14 changes: 10 additions & 4 deletions examples/view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ pub fn main() !void {

// Initialize Views
// - Large Map
var lg_map_view = try View.init(alloc, &vx.unicode, .{ .max_width = lg_map_width, .max_height = lg_map_height });
var lg_map_view = try View.init(alloc, &vx.unicode, .{ .width = lg_map_width, .height = lg_map_height });
defer lg_map_view.deinit();
//w = lg_map_view.screen.width;
//h = lg_map_view.screen.height;
var lg_map_buf: [lg_map_width * lg_map_height]u8 = undefined;
_ = mem.replace(u8, lg_world_map, "\n", "", lg_map_buf[0..]);
_ = try lg_map_view.printSegment(.{ .text = lg_map_buf[0..] }, .{ .wrap = .grapheme });
// - Small Map
var sm_map_view = try View.init(alloc, &vx.unicode, .{ .max_width = sm_map_width, .max_height = sm_map_height });
var sm_map_view = try View.init(alloc, &vx.unicode, .{ .width = sm_map_width, .height = sm_map_height });
defer sm_map_view.deinit();
w = sm_map_view.screen.width;
h = sm_map_view.screen.height;
Expand All @@ -86,7 +86,7 @@ pub fn main() !void {
// - Active Map
var map_view = lg_map_view;

//
// TUI Loop
while (true) {
const event = loop.nextEvent();
switch (event) {
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn main() !void {
// a. An x/y (col, row) position within the View as the start point of the render.
// b. A Width and Height extending Right and Down from the start point that will represent the square being rendered.
// It also returns the calculated x/y position to help maintain scrolloing boundaries.
x, y = try map_view.toWin(&map_win, if (!use_mini_view) .{
x, y = try map_view.toWin(map_win, if (!use_mini_view) .{
.x = x,
.y = y,
} else .{
Expand All @@ -173,6 +173,12 @@ pub fn main() !void {
.width = .{ .max = 45 },
.height = .{ .max = 15 },
});
if (use_mini_view) {
_ = try map_win.printSegment(
.{ .text = "This is a mini portion of the View." },
.{ .row_offset = 16, .col_offset = 5, .wrap = .word },
);
}

// Render the screen
try vx.render(writer);
Expand Down
43 changes: 20 additions & 23 deletions src/View.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,41 @@ alloc: mem.Allocator,
/// Underlying Screen
screen: *Screen,
/// Underlying Window
win: *Window,
win: Window,

/// View Initialization Config
pub const Config = struct {
max_width: usize = 10_000,
max_height: usize = 10_000,
x_pixel: usize = 0,
y_pixel: usize = 0,
width: usize,
height: usize,
};
/// Initialize a new View
pub fn init(alloc: mem.Allocator, unicode: *const Unicode, config: Config) !View {
const screen = try alloc.create(Screen);
screen.* = try Screen.init(
alloc,
.{
.cols = config.max_width,
.rows = config.max_height,
.x_pixel = config.x_pixel,
.y_pixel = config.y_pixel,
.cols = config.width,
.rows = config.height,
.x_pixel = 0,
.y_pixel = 0,
},
unicode,
);
const window = try alloc.create(Window);
window.* = .{
.x_off = 0,
.y_off = 0,
.width = config.max_width,
.height = config.max_height,
.screen = screen,
};
return .{
.alloc = alloc,
.screen = screen,
.win = window,
.win = .{
.x_off = 0,
.y_off = 0,
.width = config.width,
.height = config.height,
.screen = screen,
},
};
}

/// Deinitialize this View
pub fn deinit(self: *View) void {
self.alloc.destroy(self.win);
self.screen.deinit(self.alloc);
self.alloc.destroy(self.screen);
}
Expand All @@ -70,7 +68,7 @@ pub const RenderConfig = struct {
};
/// Render a portion of this View to the provided Window (`win`).
/// 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 } {
pub fn toWin(self: *View, win: 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: {
Expand Down Expand Up @@ -105,8 +103,7 @@ pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !struct { us
\\ Position Out of Bounds:
\\ - Pos: {d}, {d}
\\ - Size: {d}, {d}
,
.{
, .{
col, row,
self.screen.width, self.screen.height,
},
Expand Down Expand Up @@ -141,7 +138,7 @@ pub fn gwidth(self: View, str: []const u8) usize {

/// Fills the View with the provided cell
pub fn fill(self: View, cell: Cell) void {
self.fill(cell);
self.win.fill(cell);
}

/// Prints segments to the View. Returns true if the text overflowed with the
Expand Down

0 comments on commit 05bff97

Please sign in to comment.