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