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 basic_models = @import("models/basic_models.zig"); const Rect_i = basic_models.Rect_i; const Size_i = basic_models.Size_i; const Point2_f = @import("models/basic_models.zig").Point2_f; const Color = dvui.Color; const tools = @import("tools.zig"); const Toolbar = @import("Toolbar.zig"); const random_document = @import("random_document.zig"); const Canvas = @This(); allocator: std.mem.Allocator, document: *Document, render_engine: RenderEngine, toolbar: Toolbar, 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: ?Rect_i = null, _zoom: f32 = 1, _redraw_pending: bool = false, _last_redraw_time_ms: i64 = 0, cursor_document_point: ?Point2_f = null, draw_document: bool = true, /// Rect тулбара (из предыдущего кадра) для исключения кликов по нему из handleCanvasMouse. toolbar_rect_scale: ?dvui.RectScale = null, pub fn init(allocator: std.mem.Allocator, document: *Document, engine: RenderEngine) Canvas { return .{ .allocator = allocator, .document = document, .render_engine = engine, .toolbar = Toolbar.init(&tools.default_tools), }; } pub fn deinit(self: *Canvas) void { self.toolbar.deinit(); if (self.texture) |texture| { dvui.Texture.destroyLater(texture); self.texture = null; } } fn redraw(self: *Canvas) !void { const full = self.getZoomedImageSize(); const vis: Rect_i = self._visible_rect orelse Rect_i{ .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 canvas_size: Size_i = .{ .w = full.w, .h = full.h }; const new_texture = if (self.draw_document) self.render_engine.render(self.document, canvas_size, vis) catch null else self.render_engine.example(canvas_size, 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.redraw(); } pub fn addRandomShapes(self: *Canvas) !void { try random_document.addRandomShapes(self.document, std.crypto.random); self.requestRedraw(); } 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 getZoom(self: Canvas) f32 { return self._zoom; } 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.redraw(); 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.redraw(); } pub fn getZoomedImageSize(self: Canvas) Rect_i { const doc = self.document; return .{ .x = @intFromFloat(self.pos.x), .y = @intFromFloat(self.pos.y), .w = @intFromFloat(doc.size.w * self._zoom), .h = @intFromFloat(doc.size.h * self._zoom), }; } /// Точка контента -> координаты документа. pub fn contentPointToDocument(self: Canvas, content_point: dvui.Point, natural_scale: f32) Point2_f { const img = self.getZoomedImageSize(); 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 .{ .x = px_x / self._zoom, .y = px_y / self._zoom, }; } /// Точка контента внутри холста. pub fn isContentPointOnDocument(self: Canvas, content_point: dvui.Point, natural_scale: f32) bool { 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; return content_point.x >= left_n and content_point.x < right_n and content_point.y >= top_n and content_point.y < bottom_n; } 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 or self.texture == null) { return true; } return false; } fn computeVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) Rect_i { 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 Rect_i{ .x = vis_x, .y = vis_y, .w = vis_w, .h = vis_h, }; }