микроправки

This commit is contained in:
2025-12-20 21:57:10 +03:00
parent 8f462cc93b
commit 6f58967049
2 changed files with 14 additions and 13 deletions

View File

@@ -15,17 +15,17 @@ pub const ImageRect = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
texture: ?dvui.Texture = null, texture: ?dvui.Texture = null,
visible_rect: ?ImageRect = null,
size: Size = .{ .w = 800, .h = 600 }, size: Size = .{ .w = 800, .h = 600 },
pos: dvui.Point = dvui.Point{ .x = 0, .y = 0 }, pos: dvui.Point = dvui.Point{ .x = 0, .y = 0 },
scroll: dvui.ScrollInfo = .{ scroll: dvui.ScrollInfo = .{
.vertical = .auto, .vertical = .auto,
.horizontal = .auto, .horizontal = .auto,
}, },
zoom: f32 = 1,
native_scaling: bool = false, native_scaling: bool = false,
gradient_start: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 }, gradient_start: Color.PMA = .{ .r = 0, .g = 0, .b = 0, .a = 255 },
gradient_end: Color.PMA = .{ .r = 255, .g = 255, .b = 255, .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 { pub fn init(allocator: std.mem.Allocator) Canvas {
return .{ .allocator = allocator }; return .{ .allocator = allocator };
@@ -44,7 +44,7 @@ pub fn redrawGradient(self: *Canvas) !void {
const full_w: u32 = full.w; const full_w: u32 = full.w;
const full_h: u32 = full.h; 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 (vis.w == 0 or vis.h == 0) {
if (self.texture) |tex| { if (self.texture) |tex| {
@@ -105,17 +105,21 @@ pub fn fillRandomGradient(self: *Canvas) !void {
try self.redrawGradient(); 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 { pub fn addZoom(self: *Canvas, value: f32) void {
self.zoom += value; self._zoom += value;
self.zoom = @max(self.zoom, 0.01); self._zoom = @max(self._zoom, 0.01);
} }
pub fn getScaledImageSize(self: Canvas) ImageRect { pub fn getScaledImageSize(self: Canvas) ImageRect {
return .{ return .{
.x = @intFromFloat(self.pos.x), .x = @intFromFloat(self.pos.x),
.y = @intFromFloat(self.pos.y), .y = @intFromFloat(self.pos.y),
.w = @intFromFloat(self.size.w * self.zoom), .w = @intFromFloat(self.size.w * self._zoom),
.h = @intFromFloat(self.size.h * 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 { pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) !void {
const next = computeVisibleImageRect(self.*, viewport, scroll_offset); const next = computeVisibleImageRect(self.*, viewport, scroll_offset);
var changed = false; 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; 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) { if (changed or self.texture == null) {
try self.redrawGradient(); 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); const inter_bottom: f32 = @min(view_bottom, img_bottom);
if (inter_right <= inter_left or inter_bottom <= inter_top) { 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 }; return .{ .x = 0, .y = 0, .w = 0, .h = 0 };
} }

View File

@@ -94,7 +94,7 @@ fn gui_frame(ctx: *WindowContext) bool {
std.debug.print("Error filling canvas: {}\n", .{err}); std.debug.print("Error filling canvas: {}\n", .{err});
}; };
canvas.pos = .{ .x = 400, .y = 400 }; canvas.pos = .{ .x = 400, .y = 400 };
canvas.zoom = dvui.windowNaturalScale(); canvas.setZoom(dvui.windowNaturalScale());
} }
if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {} if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {}
} }