2024-01-19 06:17:57 +01:00
|
|
|
const std = @import("std");
|
2024-01-19 17:21:49 +01:00
|
|
|
const assert = std.debug.assert;
|
2024-01-19 06:17:57 +01:00
|
|
|
|
|
|
|
const Cell = @import("cell.zig").Cell;
|
2024-01-24 20:36:24 +01:00
|
|
|
const Shape = @import("Mouse.zig").Shape;
|
2024-01-30 14:14:34 +01:00
|
|
|
const Image = @import("image/image.zig").Image;
|
2024-01-19 06:17:57 +01:00
|
|
|
|
2024-01-19 19:21:14 +01:00
|
|
|
const log = std.log.scoped(.screen);
|
|
|
|
|
2024-01-19 06:17:57 +01:00
|
|
|
const Screen = @This();
|
|
|
|
|
2024-01-25 20:20:32 +01:00
|
|
|
pub const Placement = struct {
|
|
|
|
img: *Image,
|
|
|
|
placement_id: u32,
|
|
|
|
col: usize,
|
|
|
|
row: usize,
|
2024-01-30 14:14:34 +01:00
|
|
|
|
|
|
|
/// two placements are considered equal if their image id and their
|
|
|
|
/// placement id are equal
|
|
|
|
pub fn eql(self: Placement, tgt: Placement) bool {
|
|
|
|
if (self.img.getId() != tgt.img.getId()) return false;
|
|
|
|
if (self.placement_id != tgt.placement_id) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2024-01-25 20:20:32 +01:00
|
|
|
};
|
|
|
|
|
2024-01-19 20:13:20 +01:00
|
|
|
width: usize = 0,
|
|
|
|
height: usize = 0,
|
2024-01-19 06:17:57 +01:00
|
|
|
|
|
|
|
buf: []Cell = undefined,
|
|
|
|
|
2024-01-22 18:18:59 +01:00
|
|
|
cursor_row: usize = 0,
|
|
|
|
cursor_col: usize = 0,
|
|
|
|
cursor_vis: bool = false,
|
|
|
|
|
2024-01-24 13:12:39 +01:00
|
|
|
unicode: bool = false,
|
|
|
|
|
2024-01-24 20:36:24 +01:00
|
|
|
mouse_shape: Shape = .default,
|
|
|
|
|
2024-01-25 20:20:32 +01:00
|
|
|
images: std.ArrayList(Placement) = undefined,
|
|
|
|
|
2024-01-23 03:09:35 +01:00
|
|
|
pub fn init(alloc: std.mem.Allocator, w: usize, h: usize) !Screen {
|
|
|
|
var self = Screen{
|
|
|
|
.buf = try alloc.alloc(Cell, w * h),
|
|
|
|
.width = w,
|
|
|
|
.height = h,
|
2024-01-25 20:20:32 +01:00
|
|
|
.images = std.ArrayList(Placement).init(alloc),
|
2024-01-23 03:09:35 +01:00
|
|
|
};
|
2024-01-19 20:13:20 +01:00
|
|
|
for (self.buf, 0..) |_, i| {
|
|
|
|
self.buf[i] = .{};
|
|
|
|
}
|
2024-01-23 03:09:35 +01:00
|
|
|
return self;
|
2024-01-19 06:17:57 +01:00
|
|
|
}
|
|
|
|
pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void {
|
|
|
|
alloc.free(self.buf);
|
2024-01-25 20:20:32 +01:00
|
|
|
self.images.deinit();
|
2024-01-19 06:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// writes a cell to a location. 0 indexed
|
2024-01-19 19:21:14 +01:00
|
|
|
pub fn writeCell(self: *Screen, col: usize, row: usize, cell: Cell) void {
|
2024-01-19 06:17:57 +01:00
|
|
|
if (self.width < col) {
|
|
|
|
// column out of bounds
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (self.height < row) {
|
|
|
|
// height out of bounds
|
|
|
|
return;
|
|
|
|
}
|
2024-01-19 19:21:14 +01:00
|
|
|
const i = (row * self.width) + col;
|
2024-01-19 17:21:49 +01:00
|
|
|
assert(i < self.buf.len);
|
2024-01-19 06:17:57 +01:00
|
|
|
self.buf[i] = cell;
|
|
|
|
}
|
2024-01-25 20:20:32 +01:00
|
|
|
|
|
|
|
pub fn writeImage(
|
|
|
|
self: *Screen,
|
|
|
|
col: usize,
|
|
|
|
row: usize,
|
|
|
|
img: *Image,
|
|
|
|
placement_id: u32,
|
|
|
|
) !void {
|
|
|
|
const p = Placement{
|
|
|
|
.img = img,
|
|
|
|
.placement_id = placement_id,
|
|
|
|
.col = col,
|
|
|
|
.row = row,
|
|
|
|
};
|
|
|
|
try self.images.append(p);
|
|
|
|
}
|