правильное определение видимой области

This commit is contained in:
2025-12-21 19:22:38 +03:00
parent d640269cad
commit 070c8bad2d
4 changed files with 64 additions and 60 deletions

View File

@@ -109,54 +109,24 @@ pub fn updateVisibleImageRect(self: *Canvas, viewport: dvui.Rect, scroll_offset:
fn computeVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) ImageRect {
const image_rect = self.getScaledImageSize();
const img_w_f: f32 = @floatFromInt(image_rect.w);
const img_h_f: f32 = @floatFromInt(image_rect.h);
const img_w: u32 = image_rect.w;
const img_h: u32 = image_rect.h;
const view_left: f32 = scroll_offset.x;
const view_top: f32 = scroll_offset.y;
const view_right: f32 = scroll_offset.x + viewport.w;
const view_bottom: f32 = scroll_offset.y + viewport.h;
// Видимый размер всегда равен размеру viewport, но не больше холста
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 img_left: f32 = @floatFromInt(image_rect.x);
const img_top: f32 = @floatFromInt(image_rect.y);
const img_right: f32 = img_left + img_w_f;
const img_bottom: f32 = img_top + img_h_f;
// Вычисляем x и y на основе scroll_offset, clamped чтобы не выходить за границы
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 inter_left: f32 = @max(view_left, img_left);
const inter_top: f32 = @max(view_top, img_top);
const inter_right: f32 = @min(view_right, img_right);
const inter_bottom: f32 = @min(view_bottom, img_bottom);
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)));
if (inter_right <= inter_left or inter_bottom <= inter_top) {
return .{ .x = 0, .y = 0, .w = 0, .h = 0 };
}
// Переводим из координат контента в локальные координаты картинки
const local_x0: f32 = inter_left - img_left;
const local_y0: f32 = inter_top - img_top;
const local_x1: f32 = inter_right - img_left;
const local_y1: f32 = inter_bottom - img_top;
// Консервативно округляем до пикселей: начало вниз (floor), конец вверх (ceil)
const x0_u32: u32 = floatToClampedU32(@floor(local_x0), image_rect.w);
const y0_u32: u32 = floatToClampedU32(@floor(local_y0), image_rect.h);
const x1_u32: u32 = floatToClampedU32(@ceil(local_x1), image_rect.w);
const y1_u32: u32 = floatToClampedU32(@ceil(local_y1), image_rect.h);
const out: ImageRect = .{
.x = x0_u32,
.y = y0_u32,
.w = if (x1_u32 > x0_u32) x1_u32 - x0_u32 else 0,
.h = if (y1_u32 > y0_u32) y1_u32 - y0_u32 else 0,
return .{
.x = vis_x,
.y = vis_y,
.w = vis_w,
.h = vis_h,
};
return out;
}
fn floatToClampedU32(value: f32, max_inclusive: u32) u32 {
if (max_inclusive == 0) return 0;
if (value <= 0) return 0;
const max_f: f32 = @floatFromInt(max_inclusive);
if (value >= max_f) return max_inclusive;
return @intFromFloat(value);
}