Compare commits
1 Commits
8f462cc93b
...
sdl-multiw
| Author | SHA1 | Date | |
|---|---|---|---|
| 03ae8eccb6 |
7
.vscode/launch.json
vendored
7
.vscode/launch.json
vendored
@@ -10,12 +10,5 @@
|
|||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"preLaunchTask": "zig: build"
|
"preLaunchTask": "zig: build"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "Zig: Debug (gdb)",
|
|
||||||
"type": "gdb",
|
|
||||||
"request": "launch",
|
|
||||||
"program": "${workspaceFolder}/zig-out/bin/Zivro",
|
|
||||||
"preLaunchTask": "zig: build"
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
5
.vscode/tasks.json
vendored
5
.vscode/tasks.json
vendored
@@ -10,6 +10,11 @@
|
|||||||
"isDefault": true
|
"isDefault": true
|
||||||
},
|
},
|
||||||
"problemMatcher": ["$gcc"],
|
"problemMatcher": ["$gcc"],
|
||||||
|
"presentation": {
|
||||||
|
"reveal": "always",
|
||||||
|
"panel": "shared",
|
||||||
|
"showReuseMessage": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
202
src/Canvas.zig
202
src/Canvas.zig
@@ -1,202 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const builtin = @import("builtin");
|
|
||||||
const dvui = @import("dvui");
|
|
||||||
const Size = dvui.Size;
|
|
||||||
const Color = dvui.Color;
|
|
||||||
|
|
||||||
const Canvas = @This();
|
|
||||||
|
|
||||||
pub const ImageRect = struct {
|
|
||||||
x: u32,
|
|
||||||
y: u32,
|
|
||||||
w: u32,
|
|
||||||
h: u32,
|
|
||||||
};
|
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
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 },
|
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) Canvas {
|
|
||||||
return .{ .allocator = allocator };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *Canvas) void {
|
|
||||||
if (self.texture) |texture| {
|
|
||||||
dvui.Texture.destroyLater(texture);
|
|
||||||
self.texture = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Заполнить canvas градиентом
|
|
||||||
pub fn redrawGradient(self: *Canvas) !void {
|
|
||||||
const full = self.getScaledImageSize();
|
|
||||||
const full_w: u32 = full.w;
|
|
||||||
const full_h: u32 = full.h;
|
|
||||||
|
|
||||||
const vis: ImageRect = self.visible_rect orelse ImageRect{ .x = 0, .y = 0, .w = 0, .h = 0 };
|
|
||||||
|
|
||||||
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);
|
|
||||||
defer self.allocator.free(pixels);
|
|
||||||
|
|
||||||
var y: u32 = 0;
|
|
||||||
while (y < height) : (y += 1) {
|
|
||||||
var x: u32 = 0;
|
|
||||||
while (x < width) : (x += 1) {
|
|
||||||
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 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Удалить старую текстуру
|
|
||||||
if (self.texture) |tex| {
|
|
||||||
dvui.Texture.destroyLater(tex);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Создать новую текстуру из пиксельных данных
|
|
||||||
self.texture = try dvui.textureCreate(pixels, width, height, .nearest);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Заполнить canvas случайным градиентом
|
|
||||||
pub fn fillRandomGradient(self: *Canvas) !void {
|
|
||||||
// Сгенерировать случайные цвета градиента
|
|
||||||
var prng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp()));
|
|
||||||
const random = prng.random();
|
|
||||||
self.gradient_start = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 };
|
|
||||||
self.gradient_end = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 };
|
|
||||||
|
|
||||||
try self.redrawGradient();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn addZoom(self: *Canvas, value: f32) void {
|
|
||||||
self.zoom += value;
|
|
||||||
self.zoom = @max(self.zoom, 0.01);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getScaledImageSize(self: Canvas) ImageRect {
|
|
||||||
return .{
|
|
||||||
.x = @intFromFloat(self.pos.x),
|
|
||||||
.y = @intFromFloat(self.pos.y),
|
|
||||||
.w = @intFromFloat(self.size.w * self.zoom),
|
|
||||||
.h = @intFromFloat(self.size.h * self.zoom),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Обновить видимую часть изображения (в пикселях холста) и сохранить в `visible_rect`.
|
|
||||||
///
|
|
||||||
/// `viewport` и `scroll_offset` ожидаются в *physical* пикселях (т.е. уже умноженные на windowNaturalScale).
|
|
||||||
///
|
|
||||||
/// После обновления (или если текстуры ещё нет) перерисовывает текстуру, чтобы она содержала только видимую часть.
|
|
||||||
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;
|
|
||||||
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);
|
|
||||||
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 = @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);
|
|
||||||
const inter_right: f32 = @min(view_right, img_right);
|
|
||||||
const inter_bottom: f32 = @min(view_bottom, img_bottom);
|
|
||||||
|
|
||||||
if (inter_right <= inter_left or inter_bottom <= inter_top) {
|
|
||||||
if (builtin.mode == .Debug) {
|
|
||||||
std.debug.print(" -> no intersection, return empty\n", .{});
|
|
||||||
}
|
|
||||||
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 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Отобразить canvas в UI
|
|
||||||
pub fn render(self: Canvas, rect: dvui.RectScale) !void {
|
|
||||||
if (self.texture) |texture| {
|
|
||||||
try dvui.renderTexture(texture, rect, .{});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
// Файл векторного документа
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const dvui = @import("dvui");
|
|
||||||
const Canvas = @import("Canvas.zig");
|
|
||||||
|
|
||||||
const WindowContext = @This();
|
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
|
||||||
canvas: Canvas,
|
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) WindowContext {
|
|
||||||
return .{
|
|
||||||
.allocator = allocator,
|
|
||||||
.canvas = Canvas.init(allocator),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *WindowContext) void {
|
|
||||||
self.canvas.deinit();
|
|
||||||
}
|
|
||||||
11
src/WindowData.zig
Normal file
11
src/WindowData.zig
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const WindowData = @This();
|
||||||
|
|
||||||
|
frame: u64,
|
||||||
|
|
||||||
|
pub fn init() WindowData {
|
||||||
|
return WindowData{
|
||||||
|
.frame = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
369
src/main.zig
369
src/main.zig
@@ -1,226 +1,227 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const dvui = @import("dvui");
|
const dvui = @import("dvui");
|
||||||
const dvui_ext = @import("./ui/dvui_ext.zig");
|
|
||||||
const SDLBackend = @import("sdl-backend");
|
const SDLBackend = @import("sdl-backend");
|
||||||
const Document = @import("Document.zig");
|
|
||||||
const WindowContext = @import("WindowContext.zig");
|
|
||||||
const sdl_c = SDLBackend.c;
|
const sdl_c = SDLBackend.c;
|
||||||
const Allocator = std.mem.Allocator;
|
const WindowData = @import("WindowData.zig");
|
||||||
const Color = dvui.Color;
|
const dbprint = std.debug.print;
|
||||||
|
|
||||||
pub fn main() !void {
|
const WindowContext = struct {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
backend: SDLBackend,
|
||||||
const allocator = gpa.allocator();
|
backend_id: u32,
|
||||||
|
window: dvui.Window,
|
||||||
|
title: [:0]u8,
|
||||||
|
id: usize,
|
||||||
|
data: WindowData,
|
||||||
|
|
||||||
var backend = try SDLBackend.initWindow(.{
|
fn init(allocator: std.mem.Allocator, id: usize) !*WindowContext {
|
||||||
|
var ctx = try allocator.create(WindowContext);
|
||||||
|
errdefer allocator.destroy(ctx);
|
||||||
|
|
||||||
|
ctx.id = id;
|
||||||
|
|
||||||
|
const title_bytes = try std.fmt.allocPrint(allocator, "My DVUI App #{d}", .{id});
|
||||||
|
defer allocator.free(title_bytes);
|
||||||
|
|
||||||
|
ctx.title = try allocator.allocSentinel(u8, title_bytes.len, 0);
|
||||||
|
@memcpy(ctx.title[0..title_bytes.len], title_bytes);
|
||||||
|
|
||||||
|
ctx.backend = try SDLBackend.initWindow(.{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.size = .{ .w = 800.0, .h = 600.0 },
|
.size = .{ .w = 800.0, .h = 600.0 },
|
||||||
.title = "My DVUI App",
|
.title = ctx.title,
|
||||||
.vsync = true,
|
.vsync = true,
|
||||||
});
|
});
|
||||||
defer backend.deinit();
|
errdefer destroyBackendKeepingSDL(&ctx.backend);
|
||||||
|
|
||||||
var win = try dvui.Window.init(@src(), allocator, backend.backend(), .{
|
ctx.backend_id = @intCast(sdl_c.SDL_GetWindowID(ctx.backend.window));
|
||||||
.theme = switch (backend.preferredColorScheme() orelse .light) {
|
|
||||||
|
const theme = switch (ctx.backend.preferredColorScheme() orelse .light) {
|
||||||
.light => dvui.Theme.builtin.adwaita_light,
|
.light => dvui.Theme.builtin.adwaita_light,
|
||||||
.dark => dvui.Theme.builtin.adwaita_dark,
|
.dark => dvui.Theme.builtin.adwaita_dark,
|
||||||
},
|
};
|
||||||
});
|
|
||||||
defer win.deinit();
|
|
||||||
|
|
||||||
var ctx = WindowContext.init(allocator);
|
ctx.window = try dvui.Window.init(@src(), allocator, ctx.backend.backend(), .{ .theme = theme });
|
||||||
defer ctx.deinit();
|
errdefer ctx.window.deinit();
|
||||||
|
|
||||||
var interrupted = false;
|
ctx.data = WindowData.init();
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deinit(self: *WindowContext, allocator: std.mem.Allocator, last_backend: bool) void {
|
||||||
|
self.window.deinit();
|
||||||
|
|
||||||
|
if (last_backend) {
|
||||||
|
self.backend.deinit();
|
||||||
|
} else {
|
||||||
|
destroyBackendKeepingSDL(&self.backend);
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator.free(self.title);
|
||||||
|
self.* = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn destroyBackendKeepingSDL(backend: *SDLBackend) void {
|
||||||
|
sdl_c.SDL_DestroyRenderer(backend.renderer);
|
||||||
|
sdl_c.SDL_DestroyWindow(backend.window);
|
||||||
|
backend.we_own_window = false;
|
||||||
|
backend.deinit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
if (@import("builtin").os.tag == .windows) {
|
||||||
|
// on windows graphical apps have no console, so output goes to nowhere - attach it manually. related: https://github.com/ziglang/zig/issues/4196
|
||||||
|
dvui.Backend.Common.windowsAttachConsole() catch {};
|
||||||
|
}
|
||||||
|
SDLBackend.enableSDLLogging();
|
||||||
|
std.log.info("SDL version: {f}", .{SDLBackend.getSDLVersion()});
|
||||||
|
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var windows = try std.ArrayList(*WindowContext).initCapacity(allocator, 0);
|
||||||
|
defer {
|
||||||
|
for (windows.items, 0..) |ctx, i| {
|
||||||
|
const last = i + 1 == windows.items.len;
|
||||||
|
ctx.deinit(allocator, last);
|
||||||
|
allocator.destroy(ctx);
|
||||||
|
}
|
||||||
|
windows.deinit(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
var next_window_id: usize = 1;
|
||||||
|
const first_ctx = try WindowContext.init(allocator, next_window_id);
|
||||||
|
next_window_id += 1;
|
||||||
|
try windows.append(allocator, first_ctx);
|
||||||
|
|
||||||
main_loop: while (true) {
|
main_loop: while (true) {
|
||||||
// beginWait coordinates with waitTime below to run frames only when needed
|
if (windows.items.len == 0) break :main_loop;
|
||||||
const nstime = win.beginWait(interrupted);
|
|
||||||
|
|
||||||
// marks the beginning of a frame for dvui, can call dvui functions after this
|
const saw_events = try pumpEvents(&windows);
|
||||||
try win.begin(nstime);
|
|
||||||
|
|
||||||
// send all SDL events to dvui for processing
|
var min_wait_event_micros: u32 = std.math.maxInt(u32);
|
||||||
try backend.addAllEvents(&win);
|
var idx: usize = 0;
|
||||||
|
while (saw_events and idx < windows.items.len) {
|
||||||
|
const ctx = windows.items[idx];
|
||||||
|
|
||||||
// if dvui widgets might not cover the whole window, then need to clear
|
const flags = sdl_c.SDL_GetWindowFlags(ctx.backend.window);
|
||||||
// the previous frame's render
|
const occluded = (flags & (sdl_c.SDL_WINDOW_HIDDEN | sdl_c.SDL_WINDOW_MINIMIZED | sdl_c.SDL_WINDOW_OCCLUDED)) != 0;
|
||||||
_ = SDLBackend.c.SDL_SetRenderDrawColor(backend.renderer, 0, 0, 0, 255);
|
if (occluded) {
|
||||||
_ = SDLBackend.c.SDL_RenderClear(backend.renderer);
|
idx += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const keep_running = gui_frame(&ctx);
|
const nstime = @max(ctx.window.frame_time_ns, ctx.backend.nanoTime());
|
||||||
if (!keep_running) break :main_loop;
|
|
||||||
|
|
||||||
// marks end of dvui frame, don't call dvui functions after this
|
try ctx.window.begin(nstime);
|
||||||
// - sends all dvui stuff to backend for rendering, must be called before renderPresent()
|
|
||||||
const end_micros = try win.end(.{});
|
_ = sdl_c.SDL_SetRenderDrawColor(ctx.backend.renderer, 0, 0, 0, 255);
|
||||||
|
_ = sdl_c.SDL_RenderClear(ctx.backend.renderer);
|
||||||
|
|
||||||
|
const keep_open = try gui_frame(ctx, allocator, &windows, &next_window_id);
|
||||||
|
if (!keep_open) {
|
||||||
|
closeWindow(&windows, allocator, idx);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const end = try ctx.window.end(.{});
|
||||||
|
|
||||||
// cursor management
|
// cursor management
|
||||||
try backend.setCursor(win.cursorRequested());
|
try ctx.backend.setCursor(ctx.window.cursorRequested());
|
||||||
try backend.textInputRect(win.textInputRequested());
|
try ctx.backend.textInputRect(ctx.window.textInputRequested());
|
||||||
|
|
||||||
// render frame to OS
|
// render frame to OS
|
||||||
try backend.renderPresent();
|
try ctx.backend.renderPresent();
|
||||||
|
|
||||||
// waitTime and beginWait combine to achieve variable framerates
|
const sleep_time = ctx.window.waitTime(end);
|
||||||
const wait_event_micros = win.waitTime(end_micros);
|
min_wait_event_micros = @min(min_wait_event_micros, sleep_time);
|
||||||
interrupted = try backend.waitEventTimeout(wait_event_micros);
|
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (min_wait_event_micros != std.math.maxInt(u32)) {
|
||||||
|
min_wait_event_micros = @max(min_wait_event_micros, 1 / 120 * std.time.us_per_s);
|
||||||
|
std.Thread.sleep(min_wait_event_micros * std.time.ns_per_us);
|
||||||
|
} else {
|
||||||
|
std.Thread.sleep(1 / 60 * std.time.ns_per_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (windows.items.len == 0) break :main_loop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gui_frame(ctx: *WindowContext) bool {
|
fn closeWindow(windows: *std.ArrayList(*WindowContext), allocator: std.mem.Allocator, idx: usize) void {
|
||||||
const canvas = &ctx.canvas;
|
const last = windows.items.len == 1;
|
||||||
const ctrl: bool = dvui.currentWindow().modifiers.control();
|
const ctx = windows.swapRemove(idx);
|
||||||
|
ctx.deinit(allocator, last);
|
||||||
|
allocator.destroy(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
for (dvui.events()) |*e| {
|
fn eventWindowId(event: sdl_c.SDL_Event) ?u32 {
|
||||||
if (e.evt == .window and e.evt.window.action == .close) return false;
|
return switch (event.type) {
|
||||||
if (e.evt == .app and e.evt.app.action == .quit) return false;
|
sdl_c.SDL_EVENT_KEY_DOWN => @intCast(event.key.windowID),
|
||||||
|
sdl_c.SDL_EVENT_KEY_UP => @intCast(event.key.windowID),
|
||||||
|
sdl_c.SDL_EVENT_TEXT_INPUT => @intCast(event.text.windowID),
|
||||||
|
sdl_c.SDL_EVENT_TEXT_EDITING => @intCast(event.edit.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_MOTION => @intCast(event.motion.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_BUTTON_DOWN => @intCast(event.button.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_BUTTON_UP => @intCast(event.button.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_WHEEL => @intCast(event.wheel.windowID),
|
||||||
|
sdl_c.SDL_EVENT_WINDOW_FIRST...sdl_c.SDL_EVENT_WINDOW_LAST => @intCast(event.window.windowID),
|
||||||
|
else => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pumpEvents(windows: *std.ArrayList(*WindowContext)) !bool {
|
||||||
|
var event: sdl_c.SDL_Event = undefined;
|
||||||
|
const poll_got_event = if (SDLBackend.sdl3) true else 1;
|
||||||
|
var saw_event = false;
|
||||||
|
|
||||||
|
while (sdl_c.SDL_PollEvent(&event) == poll_got_event) {
|
||||||
|
saw_event = true;
|
||||||
|
|
||||||
|
if (eventWindowId(event)) |wid| {
|
||||||
|
for (windows.items) |ctx| {
|
||||||
|
if (ctx.backend_id == wid) {
|
||||||
|
_ = try ctx.backend.addEvent(&ctx.window, event);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//std.debug.print("null with: {any}\n", .{event.type});
|
||||||
|
// broadcast events without a window target (like SDL_QUIT)
|
||||||
|
for (windows.items) |ctx| {
|
||||||
|
_ = try ctx.backend.addEvent(&ctx.window, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const root = dvui.box(
|
return saw_event;
|
||||||
@src(),
|
}
|
||||||
.{ .dir = .horizontal },
|
|
||||||
.{ .expand = .both, .background = true, .style = .window },
|
fn gui_frame(ctx: *WindowContext, allocator: std.mem.Allocator, windows: *std.ArrayList(*WindowContext), next_window_id: *usize) !bool {
|
||||||
);
|
for (ctx.window.events.items) |*e| {
|
||||||
|
if (e.evt == .window and e.evt.window.action == .close) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var root = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .both, .padding = dvui.Rect.all(12), .background = true, .style = .window });
|
||||||
defer root.deinit();
|
defer root.deinit();
|
||||||
|
|
||||||
// Левая панель с фиксированной шириной
|
dvui.label(@src(), "Window #{d}", .{ctx.id}, .{ .font_style = .title_2 });
|
||||||
var left_panel = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .vertical, .min_size_content = .{ .w = 200 }, .background = true });
|
dvui.label(@src(), "Open windows: {d}", .{windows.items.len}, .{});
|
||||||
{
|
dvui.label(@src(), "Frame {d}, fps: {d}", .{ ctx.data.frame, dvui.FPS() }, .{});
|
||||||
dvui.label(@src(), "Tools", .{}, .{});
|
|
||||||
if (dvui.button(@src(), "Fill Random Color", .{}, .{})) {
|
|
||||||
canvas.fillRandomGradient() catch |err| {
|
|
||||||
std.debug.print("Error filling canvas: {}\n", .{err});
|
|
||||||
};
|
|
||||||
canvas.pos = .{ .x = 400, .y = 400 };
|
|
||||||
canvas.zoom = dvui.windowNaturalScale();
|
|
||||||
}
|
|
||||||
if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {}
|
|
||||||
}
|
|
||||||
left_panel.deinit();
|
|
||||||
|
|
||||||
// Правая панель - занимает оставшееся пространство
|
ctx.data.frame += 1;
|
||||||
const back = dvui.box(
|
|
||||||
@src(),
|
|
||||||
.{ .dir = .horizontal },
|
|
||||||
.{ .expand = .both, .padding = dvui.Rect.all(12), .background = true },
|
|
||||||
);
|
|
||||||
{
|
|
||||||
const fill_color = Color.black.opacity(0.25);
|
|
||||||
var right_panel = dvui.box(
|
|
||||||
@src(),
|
|
||||||
.{ .dir = .vertical },
|
|
||||||
.{
|
|
||||||
.expand = .both,
|
|
||||||
.background = true,
|
|
||||||
.padding = dvui.Rect.all(5),
|
|
||||||
.corner_radius = dvui.Rect.all(24),
|
|
||||||
.color_fill = fill_color,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
{
|
|
||||||
var textured = dvui_ext.texturedBox(right_panel.data().contentRectScale(), dvui.Rect.all(20));
|
|
||||||
{
|
|
||||||
var overlay = dvui.overlay(
|
|
||||||
@src(),
|
|
||||||
.{ .expand = .both },
|
|
||||||
);
|
|
||||||
{
|
|
||||||
var scroll = dvui.scrollArea(
|
|
||||||
@src(),
|
|
||||||
.{
|
|
||||||
.scroll_info = &canvas.scroll,
|
|
||||||
.vertical_bar = .auto,
|
|
||||||
.horizontal_bar = .auto,
|
|
||||||
},
|
|
||||||
.{
|
|
||||||
.expand = .both,
|
|
||||||
.background = false,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
{
|
|
||||||
const natural_scale = if (canvas.native_scaling) 1 else dvui.windowNaturalScale();
|
|
||||||
const img_size = canvas.getScaledImageSize();
|
|
||||||
|
|
||||||
// Получить viewport и scroll offset
|
if (dvui.button(@src(), "New window", .{}, .{})) {
|
||||||
const viewport_rect = scroll.data().contentRect();
|
const id = next_window_id.*;
|
||||||
const scroll_current = dvui.Point{ .x = canvas.scroll.viewport.x, .y = canvas.scroll.viewport.y };
|
next_window_id.* += 1;
|
||||||
|
const new_ctx = try WindowContext.init(allocator, id);
|
||||||
// viewport_rect/scroll_current — в natural единицах.
|
try windows.append(allocator, new_ctx);
|
||||||
// Для расчёта видимой области в пикселях изображения переводим в 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,
|
|
||||||
};
|
|
||||||
|
|
||||||
canvas.updateVisibleImageRect(viewport_px, scroll_px) catch |err| {
|
|
||||||
std.debug.print("updateVisibleImageRect error: {}\n", .{err});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (canvas.texture) |tex| {
|
|
||||||
_ = dvui.image(
|
|
||||||
@src(),
|
|
||||||
.{ .source = .{ .texture = tex } },
|
|
||||||
.{
|
|
||||||
.background = false,
|
|
||||||
.margin = .{
|
|
||||||
.x = @as(f32, @floatFromInt(img_size.x)) / natural_scale,
|
|
||||||
.y = @as(f32, @floatFromInt(img_size.y)) / natural_scale,
|
|
||||||
.w = @as(f32, @floatFromInt(img_size.x)) / natural_scale,
|
|
||||||
.h = @as(f32, @floatFromInt(img_size.y)) / natural_scale,
|
|
||||||
},
|
|
||||||
.min_size_content = .{
|
|
||||||
.w = @as(f32, @floatFromInt(img_size.w)) / natural_scale,
|
|
||||||
.h = @as(f32, @floatFromInt(img_size.h)) / natural_scale,
|
|
||||||
},
|
|
||||||
.max_size_content = .{
|
|
||||||
.w = @as(f32, @floatFromInt(img_size.w)) / natural_scale,
|
|
||||||
.h = @as(f32, @floatFromInt(img_size.h)) / natural_scale,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Заблокировать события скролла, если нажат 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;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scroll.deinit();
|
|
||||||
|
|
||||||
dvui.label(@src(), "Canvas", .{}, .{ .gravity_x = 0.5, .gravity_y = 0.0 });
|
|
||||||
}
|
|
||||||
overlay.deinit();
|
|
||||||
}
|
|
||||||
textured.deinit();
|
|
||||||
}
|
|
||||||
right_panel.deinit();
|
|
||||||
}
|
|
||||||
back.deinit();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
// Интерфейс для рендеринга документа
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
// Расширения для dvui
|
|
||||||
const std = @import("std");
|
|
||||||
const dvui = @import("dvui");
|
|
||||||
const TexturedBox = @import("./types/TexturedBox.zig");
|
|
||||||
|
|
||||||
pub fn texturedBox(rs: dvui.RectScale, corner_radius: dvui.Rect) TexturedBox {
|
|
||||||
return TexturedBox.init(rs, corner_radius);
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
// Отрисовка дочернего контента как текстуры с параметрами скругления
|
|
||||||
const std = @import("std");
|
|
||||||
const dvui = @import("dvui");
|
|
||||||
const TexturedBox = @This();
|
|
||||||
|
|
||||||
parent: dvui.Widget,
|
|
||||||
rs: dvui.RectScale,
|
|
||||||
pic: ?dvui.Picture,
|
|
||||||
corner_radius: dvui.Rect,
|
|
||||||
|
|
||||||
pub fn init(rs: dvui.RectScale, corner_radius: dvui.Rect) TexturedBox {
|
|
||||||
const parent = dvui.parentGet();
|
|
||||||
const pic = dvui.Picture.start(rs.r);
|
|
||||||
return .{
|
|
||||||
.parent = parent,
|
|
||||||
.corner_radius = corner_radius,
|
|
||||||
.rs = rs,
|
|
||||||
.pic = pic,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *TexturedBox) void {
|
|
||||||
if (self.pic) |*picture| {
|
|
||||||
picture.stop();
|
|
||||||
|
|
||||||
const tex = dvui.textureFromTarget(picture.texture) catch null;
|
|
||||||
if (tex) |t| {
|
|
||||||
dvui.Texture.destroyLater(t);
|
|
||||||
// self.rs.r.y -= 2;
|
|
||||||
// self.rs.r.x -= 2;
|
|
||||||
// self.rs.r.h += 2;
|
|
||||||
// self.rs.r.w += 2;
|
|
||||||
dvui.renderTexture(t, self.rs, .{
|
|
||||||
.corner_radius = self.corner_radius,
|
|
||||||
}) catch {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user