diff --git a/src/Canvas.zig b/src/Canvas.zig index 804ac59..4be802a 100644 --- a/src/Canvas.zig +++ b/src/Canvas.zig @@ -15,17 +15,17 @@ pub const ImageRect = struct { allocator: std.mem.Allocator, texture: ?dvui.Texture = null, -visible_rect: ?ImageRect = null, size: Size = .{ .w = 800, .h = 600 }, pos: dvui.Point = dvui.Point{ .x = 0, .y = 0 }, scroll: dvui.ScrollInfo = .{ .vertical = .auto, .horizontal = .auto, }, -zoom: f32 = 1, native_scaling: bool = false, gradient_start: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 }, gradient_end: Color.PMA = .{ .r = 255, .g = 255, .b = 255, .a = 255 }, +_visible_rect: ?ImageRect = null, +_zoom: f32 = 1, pub fn init(allocator: std.mem.Allocator) Canvas { return .{ .allocator = allocator }; @@ -44,7 +44,7 @@ pub fn redrawGradient(self: *Canvas) !void { const full_w: u32 = full.w; const full_h: u32 = full.h; - const vis: ImageRect = self.visible_rect orelse ImageRect{ .x = 0, .y = 0, .w = 0, .h = 0 }; + 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| { @@ -105,17 +105,21 @@ pub fn fillRandomGradient(self: *Canvas) !void { try self.redrawGradient(); } +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); + self._zoom += value; + self._zoom = @max(self._zoom, 0.01); } pub fn getScaledImageSize(self: Canvas) ImageRect { return .{ .x = @intFromFloat(self.pos.x), .y = @intFromFloat(self.pos.y), - .w = @intFromFloat(self.size.w * self.zoom), - .h = @intFromFloat(self.size.h * self.zoom), + .w = @intFromFloat(self.size.w * self._zoom), + .h = @intFromFloat(self.size.h * self._zoom), }; } @@ -127,10 +131,10 @@ pub fn getScaledImageSize(self: Canvas) ImageRect { pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) !void { const next = computeVisibleImageRect(self.*, viewport, scroll_offset); var changed = false; - if (self.visible_rect) |vis| { + 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; + self._visible_rect = next; if (changed or self.texture == null) { try self.redrawGradient(); } @@ -158,9 +162,6 @@ fn computeVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvu const inter_bottom: f32 = @min(view_bottom, img_bottom); if (inter_right <= inter_left or inter_bottom <= inter_top) { - if (builtin.mode == .Debug) { - std.debug.print(" -> no intersection, return empty\n", .{}); - } return .{ .x = 0, .y = 0, .w = 0, .h = 0 }; } diff --git a/src/main.zig b/src/main.zig index 851fddc..2cb006f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -94,7 +94,7 @@ fn gui_frame(ctx: *WindowContext) bool { std.debug.print("Error filling canvas: {}\n", .{err}); }; canvas.pos = .{ .x = 400, .y = 400 }; - canvas.zoom = dvui.windowNaturalScale(); + canvas.setZoom(dvui.windowNaturalScale()); } if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {} }