+10
-4
examples/view.zig
+10
-4
examples/view.zig
···
68
68
69
69
// Initialize Views
70
70
// - Large Map
71
-
var lg_map_view = try View.init(alloc, &vx.unicode, .{ .max_width = lg_map_width, .max_height = lg_map_height });
71
+
var lg_map_view = try View.init(alloc, &vx.unicode, .{ .width = lg_map_width, .height = lg_map_height });
72
72
defer lg_map_view.deinit();
73
73
//w = lg_map_view.screen.width;
74
74
//h = lg_map_view.screen.height;
···
76
76
_ = mem.replace(u8, lg_world_map, "\n", "", lg_map_buf[0..]);
77
77
_ = try lg_map_view.printSegment(.{ .text = lg_map_buf[0..] }, .{ .wrap = .grapheme });
78
78
// - Small Map
79
-
var sm_map_view = try View.init(alloc, &vx.unicode, .{ .max_width = sm_map_width, .max_height = sm_map_height });
79
+
var sm_map_view = try View.init(alloc, &vx.unicode, .{ .width = sm_map_width, .height = sm_map_height });
80
80
defer sm_map_view.deinit();
81
81
w = sm_map_view.screen.width;
82
82
h = sm_map_view.screen.height;
···
86
86
// - Active Map
87
87
var map_view = lg_map_view;
88
88
89
-
//
89
+
// TUI Loop
90
90
while (true) {
91
91
const event = loop.nextEvent();
92
92
switch (event) {
···
164
164
// a. An x/y (col, row) position within the View as the start point of the render.
165
165
// b. A Width and Height extending Right and Down from the start point that will represent the square being rendered.
166
166
// It also returns the calculated x/y position to help maintain scrolloing boundaries.
167
-
x, y = try map_view.toWin(&map_win, if (!use_mini_view) .{
167
+
x, y = try map_view.toWin(map_win, if (!use_mini_view) .{
168
168
.x = x,
169
169
.y = y,
170
170
} else .{
···
173
173
.width = .{ .max = 45 },
174
174
.height = .{ .max = 15 },
175
175
});
176
+
if (use_mini_view) {
177
+
_ = try map_win.printSegment(
178
+
.{ .text = "This is a mini portion of the View." },
179
+
.{ .row_offset = 16, .col_offset = 5, .wrap = .word },
180
+
);
181
+
}
176
182
177
183
// Render the screen
178
184
try vx.render(writer);
+20
-23
src/View.zig
+20
-23
src/View.zig
···
15
15
/// Underlying Screen
16
16
screen: *Screen,
17
17
/// Underlying Window
18
-
win: *Window,
18
+
win: Window,
19
19
20
+
/// View Initialization Config
20
21
pub const Config = struct {
21
-
max_width: usize = 10_000,
22
-
max_height: usize = 10_000,
23
-
x_pixel: usize = 0,
24
-
y_pixel: usize = 0,
22
+
width: usize,
23
+
height: usize,
25
24
};
25
+
/// Initialize a new View
26
26
pub fn init(alloc: mem.Allocator, unicode: *const Unicode, config: Config) !View {
27
27
const screen = try alloc.create(Screen);
28
28
screen.* = try Screen.init(
29
29
alloc,
30
30
.{
31
-
.cols = config.max_width,
32
-
.rows = config.max_height,
33
-
.x_pixel = config.x_pixel,
34
-
.y_pixel = config.y_pixel,
31
+
.cols = config.width,
32
+
.rows = config.height,
33
+
.x_pixel = 0,
34
+
.y_pixel = 0,
35
35
},
36
36
unicode,
37
37
);
38
-
const window = try alloc.create(Window);
39
-
window.* = .{
40
-
.x_off = 0,
41
-
.y_off = 0,
42
-
.width = config.max_width,
43
-
.height = config.max_height,
44
-
.screen = screen,
45
-
};
46
38
return .{
47
39
.alloc = alloc,
48
40
.screen = screen,
49
-
.win = window,
41
+
.win = .{
42
+
.x_off = 0,
43
+
.y_off = 0,
44
+
.width = config.width,
45
+
.height = config.height,
46
+
.screen = screen,
47
+
},
50
48
};
51
49
}
52
50
51
+
/// Deinitialize this View
53
52
pub fn deinit(self: *View) void {
54
-
self.alloc.destroy(self.win);
55
53
self.screen.deinit(self.alloc);
56
54
self.alloc.destroy(self.screen);
57
55
}
···
70
68
};
71
69
/// Render a portion of this View to the provided Window (`win`).
72
70
/// This will return the bounded X (col), Y (row) coordinates based on the rendering.
73
-
pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !struct { usize, usize } {
71
+
pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { usize, usize } {
74
72
var x = @min(self.screen.width - 1, config.x);
75
73
var y = @min(self.screen.height - 1, config.y);
76
74
const width = width: {
···
105
103
\\ Position Out of Bounds:
106
104
\\ - Pos: {d}, {d}
107
105
\\ - Size: {d}, {d}
108
-
,
109
-
.{
106
+
, .{
110
107
col, row,
111
108
self.screen.width, self.screen.height,
112
109
},
···
141
138
142
139
/// Fills the View with the provided cell
143
140
pub fn fill(self: View, cell: Cell) void {
144
-
self.fill(cell);
141
+
self.win.fill(cell);
145
142
}
146
143
147
144
/// Prints segments to the View. Returns true if the text overflowed with the