Убрано масштабирование от системы
This commit is contained in:
@@ -39,9 +39,9 @@ pub fn deinit(self: *Canvas) void {
|
||||
|
||||
/// Заполнить canvas градиентом
|
||||
pub fn redrawGradient(self: *Canvas) !void {
|
||||
const size = self.getScaledSize();
|
||||
const width: u32 = @intFromFloat(size.w);
|
||||
const height: u32 = @intFromFloat(size.h);
|
||||
const size = self.getScaledImageSize();
|
||||
const width: u32 = size.w;
|
||||
const height: u32 = size.h;
|
||||
|
||||
// Выделить буфер пиксельных данных
|
||||
const pixels = try self.allocator.alloc(Color.PMA, @as(usize, width) * height);
|
||||
@@ -84,37 +84,38 @@ pub fn addZoom(self: *Canvas, value: f32) void {
|
||||
self.zoom = @max(self.zoom, 0.01);
|
||||
}
|
||||
|
||||
pub fn getScaledSize(self: Canvas) Size {
|
||||
pub fn getScaledImageSize(self: Canvas) ImageRect {
|
||||
return .{
|
||||
.w = self.size.w * self.zoom,
|
||||
.h = self.size.h * self.zoom,
|
||||
.x = @intFromFloat(self.pos.x),
|
||||
.y = @intFromFloat(self.pos.y),
|
||||
.w = @intFromFloat(self.size.w * self.zoom),
|
||||
.h = @intFromFloat(self.size.h * self.zoom),
|
||||
};
|
||||
}
|
||||
|
||||
/// Видимая часть изображения, которую реально видит пользователь.
|
||||
///
|
||||
/// Возвращает прямоугольник в координатах самой картинки (пиксели текстуры):
|
||||
/// - (0,0) — левый верхний пиксель изображения
|
||||
/// - размеры ограничены размером текстуры
|
||||
/// Возвращает прямоугольник в координатах изображения (пиксели).
|
||||
///
|
||||
/// Важно: функция НЕ зависит от `texture` и может вызываться до её создания.
|
||||
/// Параметры:
|
||||
/// - `viewport`: размер видимой области scroll-area (x/y игнорируются)
|
||||
/// - `scroll_offset`: текущий scroll (виртуальные координаты контента)
|
||||
pub fn getVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) ImageRect {
|
||||
const tex = self.texture orelse return .{ .x = 0, .y = 0, .w = 0, .h = 0 };
|
||||
const image_rect = self.getScaledImageSize();
|
||||
|
||||
const img_w_f: f32 = @floatFromInt(tex.width);
|
||||
const img_h_f: f32 = @floatFromInt(tex.height);
|
||||
const img_w_f: f32 = @floatFromInt(image_rect.w);
|
||||
const img_h_f: f32 = @floatFromInt(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;
|
||||
|
||||
const img_left: f32 = self.pos.x;
|
||||
const img_top: f32 = self.pos.y;
|
||||
const img_right: f32 = self.pos.x + img_w_f;
|
||||
const img_bottom: f32 = self.pos.y + img_h_f;
|
||||
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;
|
||||
|
||||
const inter_left: f32 = @max(view_left, img_left);
|
||||
const inter_top: f32 = @max(view_top, img_top);
|
||||
@@ -135,10 +136,10 @@ pub fn getVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvu
|
||||
const local_y1: f32 = inter_bottom - img_top;
|
||||
|
||||
// Консервативно округляем до пикселей: начало вниз (floor), конец вверх (ceil)
|
||||
const x0_u32: u32 = floatToClampedU32(@floor(local_x0), tex.width);
|
||||
const y0_u32: u32 = floatToClampedU32(@floor(local_y0), tex.height);
|
||||
const x1_u32: u32 = floatToClampedU32(@ceil(local_x1), tex.width);
|
||||
const y1_u32: u32 = floatToClampedU32(@ceil(local_y1), tex.height);
|
||||
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,
|
||||
|
||||
31
src/main.zig
31
src/main.zig
@@ -94,6 +94,7 @@ fn gui_frame(ctx: *WindowContext) bool {
|
||||
std.debug.print("Error filling canvas: {}\n", .{err});
|
||||
};
|
||||
canvas.pos = .{ .x = 800, .y = 400 };
|
||||
canvas.zoom = dvui.windowNaturalScale();
|
||||
}
|
||||
}
|
||||
left_panel.deinit();
|
||||
@@ -141,21 +142,41 @@ fn gui_frame(ctx: *WindowContext) bool {
|
||||
// Отобразить canvas внутри scroll area.
|
||||
// ScrollArea сам двигает дочерние виджеты, поэтому margin не нужен.
|
||||
if (canvas.texture) |texture| {
|
||||
const img_size = canvas.getScaledSize();
|
||||
const natural_scale = dvui.windowNaturalScale();
|
||||
const img_size = canvas.getScaledImageSize();
|
||||
_ = dvui.image(@src(), .{
|
||||
.source = .{ .texture = texture },
|
||||
}, .{
|
||||
.margin = .{ .x = canvas.pos.x, .y = canvas.pos.y },
|
||||
.margin = .{
|
||||
// img_size.* считаем в пикселях картинки (physical).
|
||||
// dvui.image ожидает размеры в natural, поэтому делим на scale.
|
||||
.x = @as(f32, @floatFromInt(img_size.x)) / natural_scale,
|
||||
.y = @as(f32, @floatFromInt(img_size.y)) / natural_scale,
|
||||
},
|
||||
.min_size_content = .{
|
||||
.w = img_size.w,
|
||||
.h = img_size.h,
|
||||
.w = @as(f32, @floatFromInt(img_size.w)) / natural_scale,
|
||||
.h = @as(f32, @floatFromInt(img_size.h)) / natural_scale,
|
||||
},
|
||||
});
|
||||
|
||||
// Получить viewport и scroll offset
|
||||
const viewport_rect = scroll.data().contentRect();
|
||||
const scroll_current = dvui.Point{ .x = canvas.scroll.viewport.x, .y = canvas.scroll.viewport.y };
|
||||
const visible_rect = canvas.getVisibleImageRect(viewport_rect, scroll_current);
|
||||
|
||||
// viewport_rect/scroll_current — в natural единицах.
|
||||
// Для видимой области в пикселях изображения переводим в physical.
|
||||
const viewport_px = dvui.Rect{
|
||||
.x = viewport_rect.x * natural_scale,
|
||||
.y = viewport_rect.y * natural_scale,
|
||||
.w = viewport_rect.w * natural_scale,
|
||||
.h = viewport_rect.h * natural_scale,
|
||||
};
|
||||
const scroll_px = dvui.Point{
|
||||
.x = scroll_current.x * natural_scale,
|
||||
.y = scroll_current.y * natural_scale,
|
||||
};
|
||||
|
||||
const visible_rect = canvas.getVisibleImageRect(viewport_px, scroll_px);
|
||||
std.debug.print("Visible image rect: {any}\n", .{visible_rect});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user