Первые попытки проверки видимой части

This commit is contained in:
2025-12-20 18:05:28 +03:00
parent e22051c1c1
commit b5d60d67dd
3 changed files with 125 additions and 30 deletions

View File

@@ -93,7 +93,7 @@ fn gui_frame(ctx: *WindowContext) bool {
canvas.fillRandomGradient() catch |err| {
std.debug.print("Error filling canvas: {}\n", .{err});
};
canvas.pos = .{ .x = 400, .y = 400 };
canvas.pos = .{ .x = 800, .y = 400 };
}
}
left_panel.deinit();
@@ -120,14 +120,11 @@ fn gui_frame(ctx: *WindowContext) bool {
{
var textured = dvui_ext.texturedBox(right_panel.data().contentRectScale(), dvui.Rect.all(20));
{
var canvas_box = dvui.box(
var overlay = dvui.overlay(
@src(),
.{ .dir = .vertical },
.{ .expand = .both },
);
{
dvui.label(@src(), "Canvas", .{}, .{ .gravity_x = 0.5, .gravity_y = 0.0 });
var scroll = dvui.scrollArea(
@src(),
.{
@@ -144,42 +141,51 @@ fn gui_frame(ctx: *WindowContext) bool {
// Отобразить canvas внутри scroll area.
// ScrollArea сам двигает дочерние виджеты, поэтому margin не нужен.
if (canvas.texture) |texture| {
const img_size = canvas.getScaledSize();
_ = dvui.image(@src(), .{
.source = .{ .texture = texture },
}, .{
.margin = .{ .x = canvas.pos.x, .y = canvas.pos.y },
.min_size_content = .{
.w = @floatFromInt(texture.width),
.h = @floatFromInt(texture.height),
.w = img_size.w,
.h = img_size.h,
},
});
}
}
if (ctrl) {
for (dvui.events()) |*e| {
switch (e.evt) {
.mouse => |mouse| {
const action = mouse.action;
if (dvui.eventMatchSimple(e, scroll.data()) and (action == .wheel_x or action == .wheel_y)) {
switch (action) {
.wheel_y => |y| {
canvas.addZoom(y / 1000);
canvas.redrawGradient() catch {};
},
else => {},
// Получить 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);
std.debug.print("Visible image rect: {any}\n", .{visible_rect});
}
// Заблокировать события скролла, если нажат ctrl
if (ctrl) {
for (dvui.events()) |*e| {
switch (e.evt) {
.mouse => |mouse| {
const action = mouse.action;
if (dvui.eventMatchSimple(e, scroll.data()) and (action == .wheel_x or action == .wheel_y)) {
switch (action) {
.wheel_y => |y| {
canvas.addZoom(y / 1000);
canvas.redrawGradient() catch {};
},
else => {},
}
e.handled = true;
}
e.handled = true;
}
},
else => {},
},
else => {},
}
}
}
}
scroll.deinit();
dvui.label(@src(), "Canvas", .{}, .{ .gravity_x = 0.5, .gravity_y = 0.0 });
}
canvas_box.deinit();
overlay.deinit();
}
textured.deinit();
}