Скейлинг

This commit is contained in:
2025-12-20 20:47:37 +03:00
parent b49ee3e46c
commit 643aaee926
2 changed files with 127 additions and 51 deletions

View File

@@ -15,14 +15,15 @@ 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,
// .virtual_size = .{ .w = 2000, .h = 2000 },
},
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 },
@@ -39,9 +40,26 @@ pub fn deinit(self: *Canvas) void {
/// Заполнить canvas градиентом
pub fn redrawGradient(self: *Canvas) !void {
const size = self.getScaledImageSize();
const width: u32 = size.w;
const height: u32 = size.h;
const full = self.getScaledImageSize();
const full_w: u32 = full.w;
const full_h: u32 = full.h;
var vis: ImageRect = self.visible_rect orelse ImageRect{ .x = 0, .y = 0, .w = 0, .h = 0 };
if (vis.w == 0 or vis.h == 0) {
// Если viewport ещё не известен, рисуем целиком.
vis = .{ .x = 0, .y = 0, .w = full_w, .h = full_h };
}
if (vis.w == 0 or vis.h == 0) {
if (self.texture) |tex| {
dvui.Texture.destroyLater(tex);
self.texture = null;
}
return;
}
const width: u32 = vis.w;
const height: u32 = vis.h;
// Выделить буфер пиксельных данных
const pixels = try self.allocator.alloc(Color.PMA, @as(usize, width) * height);
@@ -51,10 +69,22 @@ pub fn redrawGradient(self: *Canvas) !void {
while (y < height) : (y += 1) {
var x: u32 = 0;
while (x < width) : (x += 1) {
const factor = (@as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width - 1)) + @as(f32, @floatFromInt(y)) / @as(f32, @floatFromInt(height - 1))) / 2;
const r = @as(u8, @intFromFloat(@as(f32, @floatFromInt(self.gradient_start.r)) + factor * (@as(f32, @floatFromInt(self.gradient_end.r)) - @as(f32, @floatFromInt(self.gradient_start.r)))));
const g = @as(u8, @intFromFloat(@as(f32, @floatFromInt(self.gradient_start.g)) + factor * (@as(f32, @floatFromInt(self.gradient_end.g)) - @as(f32, @floatFromInt(self.gradient_start.g)))));
const b = @as(u8, @intFromFloat(@as(f32, @floatFromInt(self.gradient_start.b)) + factor * (@as(f32, @floatFromInt(self.gradient_end.b)) - @as(f32, @floatFromInt(self.gradient_start.b)))));
const gx: u32 = vis.x + x;
const gy: u32 = vis.y + y;
const denom_x: f32 = if (full_w > 1) @as(f32, @floatFromInt(full_w - 1)) else 1;
const denom_y: f32 = if (full_h > 1) @as(f32, @floatFromInt(full_h - 1)) else 1;
const fx: f32 = @as(f32, @floatFromInt(gx)) / denom_x;
const fy: f32 = @as(f32, @floatFromInt(gy)) / denom_y;
const factor: f32 = std.math.clamp((fx + fy) / 2, 0, 1);
const r_f: f32 = @as(f32, @floatFromInt(self.gradient_start.r)) + factor * (@as(f32, @floatFromInt(self.gradient_end.r)) - @as(f32, @floatFromInt(self.gradient_start.r)));
const g_f: f32 = @as(f32, @floatFromInt(self.gradient_start.g)) + factor * (@as(f32, @floatFromInt(self.gradient_end.g)) - @as(f32, @floatFromInt(self.gradient_start.g)));
const b_f: f32 = @as(f32, @floatFromInt(self.gradient_start.b)) + factor * (@as(f32, @floatFromInt(self.gradient_end.b)) - @as(f32, @floatFromInt(self.gradient_start.b)));
const r: u8 = @intFromFloat(std.math.clamp(r_f, 0, 255));
const g: u8 = @intFromFloat(std.math.clamp(g_f, 0, 255));
const b: u8 = @intFromFloat(std.math.clamp(b_f, 0, 255));
pixels[y * width + x] = .{ .r = r, .g = g, .b = b, .a = 255 };
}
}
@@ -93,15 +123,26 @@ pub fn getScaledImageSize(self: Canvas) ImageRect {
};
}
/// Видимая часть изображения, которую реально видит пользователь.
/// Обновить видимую часть изображения (в пикселях изображения) и сохранить в `visible_rect`.
///
/// Возвращает прямоугольник в координатах изображения (пиксели).
/// `viewport` и `scroll_offset` ожидаются в *physical* пикселях (т.е. уже умноженные на windowNaturalScale).
///
/// Важно: функция НЕ зависит от `texture` и может вызываться до её создания.
/// Параметры:
/// - `viewport`: размер видимой области scroll-area (x/y игнорируются)
/// - `scroll_offset`: текущий scroll (виртуальные координаты контента)
pub fn getVisibleImageRect(self: Canvas, viewport: dvui.Rect, scroll_offset: dvui.Point) 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| {
changed |= next.x != vis.x or next.y != vis.y or next.w != vis.w or next.h != vis.h;
}
self.visible_rect = next;
std.debug.print("Visible: {any}\n", .{next});
if (changed or self.texture == null) {
try self.redrawGradient();
}
}
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);
@@ -160,8 +201,8 @@ fn floatToClampedU32(value: f32, max_inclusive: u32) u32 {
}
/// Отобразить canvas в UI
pub fn render(self: Canvas, rect: dvui.Rect.Physical) !void {
pub fn render(self: Canvas, rect: dvui.RectScale) !void {
if (self.texture) |texture| {
try dvui.renderTexture(texture, .{ .r = rect }, .{});
try dvui.renderTexture(texture, rect, .{});
}
}