const std = @import("std"); const builtin = @import("builtin"); const dvui = @import("dvui"); const Document = @import("models/Document.zig"); const RenderEngine = @import("render/RenderEngine.zig").RenderEngine; const ImageRect = @import("models/basic_models.zig").ImageRect; const Point2 = @import("models/basic_models.zig").Point2; const Color = dvui.Color; const Canvas = @This(); allocator: std.mem.Allocator, document: *Document, render_engine: RenderEngine, texture: ?dvui.Texture = null, pos: dvui.Point = dvui.Point{ .x = 400, .y = 400 }, scroll: dvui.ScrollInfo = .{ .vertical = .auto, .horizontal = .auto, }, native_scaling: bool = true, redraw_throttle_ms: u32 = 50, _visible_rect: ?ImageRect = null, _zoom: f32 = 1, _redraw_pending: bool = false, _last_redraw_time_ms: i64 = 0, cursor_document_point: ?Point2 = null, pub fn init(allocator: std.mem.Allocator, document: *Document, engine: RenderEngine) Canvas { return .{ .allocator = allocator, .document = document, .render_engine = engine, }; } pub fn deinit(self: *Canvas) void { if (self.texture) |texture| { dvui.Texture.destroyLater(texture); self.texture = null; } } pub fn redrawExample(self: *Canvas) !void { const full = self.getZoomedImageSize(); const vis: ImageRect = self._visible_rect orelse ImageRect{ .x = 0, .y = 0, .w = 0, .h = 0 }; if (vis.w == 0 or vis.h == 0) { if (self.texture) |tex| { dvui.Texture.destroyLater(tex); self.texture = null; } return; } const new_texture = self.render_engine.example(.{ .w = full.w, .h = full.h }, vis) catch null; if (new_texture) |tex| { if (self.texture) |old_tex| { dvui.Texture.destroyLater(old_tex); } self.texture = tex; } self._last_redraw_time_ms = std.time.milliTimestamp(); } pub fn exampleReset(self: *Canvas) !void { self.render_engine.exampleReset(); try self.redrawExample(); } pub fn setZoom(self: *Canvas, value: f32) void { self._zoom = @max(value, 0.01); } pub fn addZoom(self: *Canvas, value: f32) void { self._zoom += value; self._zoom = @max(self._zoom, 0.01); } pub fn requestRedraw(self: *Canvas) void { self._redraw_pending = true; } pub fn processPendingRedraw(self: *Canvas) !void { if (!self._redraw_pending) return; if (self.redraw_throttle_ms == 0) { self._redraw_pending = false; try self.redrawExample(); return; } const now_ms = std.time.milliTimestamp(); const elapsed: i64 = if (self._last_redraw_time_ms == 0) self.redraw_throttle_ms else now_ms - self._last_redraw_time_ms; if (elapsed < @as(i64, @intCast(self.redraw_throttle_ms))) return; self._redraw_pending = false; try self.redrawExample(); } pub fn getZoomedImageSize(self: Canvas) ImageRect { const doc = self.document; return .{ .x = @intFromFloat(self.pos.x), .y = @intFromFloat(self.pos.y), .w = @intFromFloat(doc.size.width * self._zoom), .h = @intFromFloat(doc.size.height * self._zoom), }; } pub fn contentPointToDocument(self: Canvas, content_point: dvui.Point, natural_scale: f32) ?Point2 { const img = self.getZoomedImageSize(); const left_n = @as(f32, @floatFromInt(img.x)) / natural_scale; const top_n = @as(f32, @floatFromInt(img.y)) / natural_scale; const right_n = @as(f32, @floatFromInt(img.x + img.w)) / natural_scale; const bottom_n = @as(f32, @floatFromInt(img.y + img.h)) / natural_scale; if (content_point.x < left_n or content_point.x >= right_n or content_point.y < top_n or content_point.y >= bottom_n) return null; const px_x = content_point.x * natural_scale - @as(f32, @floatFromInt(img.x)); const px_y = content_point.y * natural_scale - @as(f32, @floatFromInt(img.y)); return Point2{ .x = px_x / self._zoom, .y = px_y / self._zoom, }; } pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) bool { const next = computeVisibleImageRect(self.*, viewport, scroll_offset); var changed = false; if (self._visible_rect) |vis| { changed |= next.x != vis.x or next.y != vis.y or next.w != vis.w or next.h != vis.h; } self._visible_rect = next; if (changed) { std.debug.print("Visible Image Rect: {{ x: {}, y: {}, w: {}, h: {} }}\n", .{ next.x, next.y, next.w, next.h }); } if (changed or self.texture == null) { return true; } return false; } fn computeVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) ImageRect { const image_rect = self.getZoomedImageSize(); const img_w: u32 = image_rect.w; const img_h: u32 = image_rect.h; const vis_w: u32 = @min(@as(u32, @intFromFloat(viewport.w)), img_w); const vis_h: u32 = @min(@as(u32, @intFromFloat(viewport.h)), img_h); const raw_x: i64 = @intFromFloat(scroll_offset.x - @as(f32, @floatFromInt(image_rect.x))); const raw_y: i64 = @intFromFloat(scroll_offset.y - @as(f32, @floatFromInt(image_rect.y))); const vis_x: u32 = @intCast(std.math.clamp(raw_x, 0, @as(i64, img_w) - @as(i64, vis_w))); const vis_y: u32 = @intCast(std.math.clamp(raw_y, 0, @as(i64, img_h) - @as(i64, vis_h))); return ImageRect{ .x = vis_x, .y = vis_y, .w = vis_w, .h = vis_h, }; }