Compare commits
5 Commits
sdl-multiw
...
bb825d0225
| Author | SHA1 | Date | |
|---|---|---|---|
| bb825d0225 | |||
| b852384322 | |||
| d4a2f41a51 | |||
| bca66e3815 | |||
| 7a5f9d62a8 |
1
src/Document.zig
Normal file
1
src/Document.zig
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// Файл векторного документа
|
||||||
88
src/WindowContext.zig
Normal file
88
src/WindowContext.zig
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const dvui = @import("dvui");
|
||||||
|
const Color = dvui.Color;
|
||||||
|
|
||||||
|
const WindowContext = @This();
|
||||||
|
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
canvas_texture: ?dvui.Texture = null,
|
||||||
|
canvas_width: u32 = 400,
|
||||||
|
canvas_height: u32 = 300,
|
||||||
|
canvas_pos: dvui.Point = dvui.Point{ .x = 0, .y = 0 },
|
||||||
|
canvas_scroll: dvui.ScrollInfo = .{
|
||||||
|
.vertical = .given,
|
||||||
|
.horizontal = .given,
|
||||||
|
.virtual_size = .{ .w = 2000, .h = 2000 },
|
||||||
|
},
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator) WindowContext {
|
||||||
|
return .{
|
||||||
|
.allocator = allocator,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Заполнить canvas случайным цветом на CPU
|
||||||
|
pub fn fillRandomColor(self: *WindowContext) !void {
|
||||||
|
var prng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp()));
|
||||||
|
const random = prng.random();
|
||||||
|
|
||||||
|
// Выделить буфер пиксельных данных
|
||||||
|
const pixels = try self.allocator.alloc(Color.PMA, @as(usize, self.canvas_width) * self.canvas_height);
|
||||||
|
defer self.allocator.free(pixels);
|
||||||
|
|
||||||
|
// Заполнить случайными цветами
|
||||||
|
const r = random.int(u8);
|
||||||
|
const g = random.int(u8);
|
||||||
|
const b = random.int(u8);
|
||||||
|
|
||||||
|
var prev: dvui.Color.PMA = .{
|
||||||
|
.r = r,
|
||||||
|
.g = g,
|
||||||
|
.b = b,
|
||||||
|
.a = 255,
|
||||||
|
};
|
||||||
|
for (pixels) |*pixel| {
|
||||||
|
const r_delta = random.intRangeAtMost(i16, -1, 1);
|
||||||
|
const g_delta = random.intRangeAtMost(i16, -1, 1);
|
||||||
|
const b_delta = random.intRangeAtMost(i16, -1, 1);
|
||||||
|
|
||||||
|
const r_new: i16 = @as(i16, prev.r) + r_delta;
|
||||||
|
const g_new: i16 = @as(i16, prev.g) + g_delta;
|
||||||
|
const b_new: i16 = @as(i16, prev.b) + b_delta;
|
||||||
|
|
||||||
|
pixel.* = .{
|
||||||
|
.r = @intCast(std.math.clamp(r_new, 0, 255)),
|
||||||
|
.g = @intCast(std.math.clamp(g_new, 0, 255)),
|
||||||
|
.b = @intCast(std.math.clamp(b_new, 0, 255)),
|
||||||
|
.a = 255,
|
||||||
|
};
|
||||||
|
prev = pixel.*;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Удалить старую текстуру
|
||||||
|
if (self.canvas_texture) |tex| {
|
||||||
|
dvui.Texture.destroyLater(tex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Создать новую текстуру из пиксельных данных
|
||||||
|
self.canvas_texture = try dvui.textureCreate(pixels, self.canvas_width, self.canvas_height, .linear);
|
||||||
|
|
||||||
|
// Дать скроллам ощутимый диапазон сразу (минимум 2000x2000)
|
||||||
|
self.canvas_scroll.virtual_size = .{
|
||||||
|
.w = @max(2000, @as(f32, @floatFromInt(self.canvas_width))),
|
||||||
|
.h = @max(2000, @as(f32, @floatFromInt(self.canvas_height))),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Отобразить canvas в UI
|
||||||
|
pub fn render(self: WindowContext, rect: dvui.Rect.Physical) !void {
|
||||||
|
if (self.canvas_texture) |texture| {
|
||||||
|
try dvui.renderTexture(texture, .{ .r = rect }, .{});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *WindowContext) void {
|
||||||
|
if (self.canvas_texture) |texture| {
|
||||||
|
dvui.Texture.destroyLater(texture);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const WindowData = @This();
|
|
||||||
|
|
||||||
frame: u64,
|
|
||||||
|
|
||||||
pub fn init() WindowData {
|
|
||||||
return WindowData{
|
|
||||||
.frame = 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
324
src/main.zig
324
src/main.zig
@@ -1,227 +1,163 @@
|
|||||||
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 WindowData = @import("WindowData.zig");
|
const Allocator = std.mem.Allocator;
|
||||||
const dbprint = std.debug.print;
|
const Color = dvui.Color;
|
||||||
|
|
||||||
const WindowContext = struct {
|
|
||||||
backend: SDLBackend,
|
|
||||||
backend_id: u32,
|
|
||||||
window: dvui.Window,
|
|
||||||
title: [:0]u8,
|
|
||||||
id: usize,
|
|
||||||
data: WindowData,
|
|
||||||
|
|
||||||
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,
|
|
||||||
.size = .{ .w = 800.0, .h = 600.0 },
|
|
||||||
.title = ctx.title,
|
|
||||||
.vsync = true,
|
|
||||||
});
|
|
||||||
errdefer destroyBackendKeepingSDL(&ctx.backend);
|
|
||||||
|
|
||||||
ctx.backend_id = @intCast(sdl_c.SDL_GetWindowID(ctx.backend.window));
|
|
||||||
|
|
||||||
const theme = switch (ctx.backend.preferredColorScheme() orelse .light) {
|
|
||||||
.light => dvui.Theme.builtin.adwaita_light,
|
|
||||||
.dark => dvui.Theme.builtin.adwaita_dark,
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.window = try dvui.Window.init(@src(), allocator, ctx.backend.backend(), .{ .theme = theme });
|
|
||||||
errdefer ctx.window.deinit();
|
|
||||||
|
|
||||||
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 {
|
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(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.deinit();
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var windows = try std.ArrayList(*WindowContext).initCapacity(allocator, 0);
|
var backend = try SDLBackend.initWindow(.{
|
||||||
defer {
|
.allocator = allocator,
|
||||||
for (windows.items, 0..) |ctx, i| {
|
.size = .{ .w = 800.0, .h = 600.0 },
|
||||||
const last = i + 1 == windows.items.len;
|
.title = "My DVUI App",
|
||||||
ctx.deinit(allocator, last);
|
.vsync = true,
|
||||||
allocator.destroy(ctx);
|
});
|
||||||
}
|
defer backend.deinit();
|
||||||
windows.deinit(allocator);
|
|
||||||
}
|
|
||||||
|
|
||||||
var next_window_id: usize = 1;
|
var win = try dvui.Window.init(@src(), allocator, backend.backend(), .{
|
||||||
const first_ctx = try WindowContext.init(allocator, next_window_id);
|
.theme = switch (backend.preferredColorScheme() orelse .light) {
|
||||||
next_window_id += 1;
|
.light => dvui.Theme.builtin.adwaita_light,
|
||||||
try windows.append(allocator, first_ctx);
|
.dark => dvui.Theme.builtin.adwaita_dark,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
defer win.deinit();
|
||||||
|
|
||||||
|
var ctx = WindowContext.init(allocator);
|
||||||
|
defer ctx.deinit();
|
||||||
|
|
||||||
|
var interrupted = false;
|
||||||
|
|
||||||
main_loop: while (true) {
|
main_loop: while (true) {
|
||||||
if (windows.items.len == 0) break :main_loop;
|
// beginWait coordinates with waitTime below to run frames only when needed
|
||||||
|
const nstime = win.beginWait(interrupted);
|
||||||
|
|
||||||
const saw_events = try pumpEvents(&windows);
|
// marks the beginning of a frame for dvui, can call dvui functions after this
|
||||||
|
try win.begin(nstime);
|
||||||
|
|
||||||
var min_wait_event_micros: u32 = std.math.maxInt(u32);
|
// send all SDL events to dvui for processing
|
||||||
var idx: usize = 0;
|
try backend.addAllEvents(&win);
|
||||||
while (saw_events and idx < windows.items.len) {
|
|
||||||
const ctx = windows.items[idx];
|
|
||||||
|
|
||||||
const flags = sdl_c.SDL_GetWindowFlags(ctx.backend.window);
|
// if dvui widgets might not cover the whole window, then need to clear
|
||||||
const occluded = (flags & (sdl_c.SDL_WINDOW_HIDDEN | sdl_c.SDL_WINDOW_MINIMIZED | sdl_c.SDL_WINDOW_OCCLUDED)) != 0;
|
// the previous frame's render
|
||||||
if (occluded) {
|
_ = SDLBackend.c.SDL_SetRenderDrawColor(backend.renderer, 0, 0, 0, 255);
|
||||||
idx += 1;
|
_ = SDLBackend.c.SDL_RenderClear(backend.renderer);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const nstime = @max(ctx.window.frame_time_ns, ctx.backend.nanoTime());
|
const keep_running = gui_frame(&ctx);
|
||||||
|
if (!keep_running) break :main_loop;
|
||||||
|
|
||||||
try ctx.window.begin(nstime);
|
// marks end of dvui frame, don't call dvui functions after this
|
||||||
|
// - 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);
|
// cursor management
|
||||||
_ = sdl_c.SDL_RenderClear(ctx.backend.renderer);
|
try backend.setCursor(win.cursorRequested());
|
||||||
|
try backend.textInputRect(win.textInputRequested());
|
||||||
|
|
||||||
const keep_open = try gui_frame(ctx, allocator, &windows, &next_window_id);
|
// render frame to OS
|
||||||
if (!keep_open) {
|
try backend.renderPresent();
|
||||||
closeWindow(&windows, allocator, idx);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const end = try ctx.window.end(.{});
|
// waitTime and beginWait combine to achieve variable framerates
|
||||||
|
const wait_event_micros = win.waitTime(end_micros);
|
||||||
// cursor management
|
interrupted = try backend.waitEventTimeout(wait_event_micros);
|
||||||
try ctx.backend.setCursor(ctx.window.cursorRequested());
|
|
||||||
try ctx.backend.textInputRect(ctx.window.textInputRequested());
|
|
||||||
|
|
||||||
// render frame to OS
|
|
||||||
try ctx.backend.renderPresent();
|
|
||||||
|
|
||||||
const sleep_time = ctx.window.waitTime(end);
|
|
||||||
min_wait_event_micros = @min(min_wait_event_micros, sleep_time);
|
|
||||||
|
|
||||||
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 closeWindow(windows: *std.ArrayList(*WindowContext), allocator: std.mem.Allocator, idx: usize) void {
|
fn gui_frame(ctx: *WindowContext) bool {
|
||||||
const last = windows.items.len == 1;
|
for (dvui.events()) |*e| {
|
||||||
const ctx = windows.swapRemove(idx);
|
|
||||||
ctx.deinit(allocator, last);
|
|
||||||
allocator.destroy(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn eventWindowId(event: sdl_c.SDL_Event) ?u32 {
|
|
||||||
return switch (event.type) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return saw_event;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
if (e.evt == .window and e.evt.window.action == .close) return false;
|
||||||
|
if (e.evt == .app and e.evt.app.action == .quit) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var root = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .both, .padding = dvui.Rect.all(12), .background = true, .style = .window });
|
const root = dvui.box(
|
||||||
|
@src(),
|
||||||
|
.{ .dir = .horizontal },
|
||||||
|
.{ .expand = .both, .background = true, .style = .window },
|
||||||
|
);
|
||||||
defer root.deinit();
|
defer root.deinit();
|
||||||
|
|
||||||
dvui.label(@src(), "Window #{d}", .{ctx.id}, .{ .font_style = .title_2 });
|
// Левая панель с фиксированной шириной
|
||||||
dvui.label(@src(), "Open windows: {d}", .{windows.items.len}, .{});
|
var left_panel = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .vertical, .min_size_content = .{ .w = 200 }, .background = true });
|
||||||
dvui.label(@src(), "Frame {d}, fps: {d}", .{ ctx.data.frame, dvui.FPS() }, .{});
|
{
|
||||||
|
dvui.label(@src(), "Tools", .{}, .{});
|
||||||
ctx.data.frame += 1;
|
if (dvui.button(@src(), "Fill Random Color", .{}, .{})) {
|
||||||
|
ctx.fillRandomColor() catch |err| {
|
||||||
if (dvui.button(@src(), "New window", .{}, .{})) {
|
std.debug.print("Error filling canvas: {}\n", .{err});
|
||||||
const id = next_window_id.*;
|
};
|
||||||
next_window_id.* += 1;
|
ctx.canvas_pos = .{ .x = 400, .y = 400 };
|
||||||
const new_ctx = try WindowContext.init(allocator, id);
|
}
|
||||||
try windows.append(allocator, new_ctx);
|
|
||||||
}
|
}
|
||||||
|
left_panel.deinit();
|
||||||
|
|
||||||
|
// Правая панель - занимает оставшееся пространство
|
||||||
|
const back = dvui.box(
|
||||||
|
@src(),
|
||||||
|
.{ .dir = .horizontal },
|
||||||
|
.{ .expand = .both, .padding = dvui.Rect.all(12), .background = true },
|
||||||
|
);
|
||||||
|
{
|
||||||
|
const fill_color = Color.white.opacity(0.5);
|
||||||
|
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 canvas_box = dvui.box(
|
||||||
|
@src(),
|
||||||
|
.{ .dir = .vertical },
|
||||||
|
.{ .expand = .both },
|
||||||
|
);
|
||||||
|
{
|
||||||
|
dvui.label(@src(), "Canvas", .{}, .{ .gravity_x = 0.5, .gravity_y = 0.0 });
|
||||||
|
|
||||||
|
var scroll = dvui.scrollArea(
|
||||||
|
@src(),
|
||||||
|
.{
|
||||||
|
.scroll_info = &ctx.canvas_scroll,
|
||||||
|
.vertical_bar = .auto,
|
||||||
|
.horizontal_bar = .auto,
|
||||||
|
},
|
||||||
|
.{ .expand = .both, .background = false },
|
||||||
|
);
|
||||||
|
{
|
||||||
|
// Отобразить canvas внутри scroll area.
|
||||||
|
// ScrollArea сам двигает дочерние виджеты, поэтому margin не нужен.
|
||||||
|
if (ctx.canvas_texture) |texture| {
|
||||||
|
_ = dvui.image(@src(), .{
|
||||||
|
.source = .{ .texture = texture },
|
||||||
|
}, .{
|
||||||
|
.margin = .{ .x = ctx.canvas_pos.x, .y = ctx.canvas_pos.y },
|
||||||
|
.min_size_content = .{
|
||||||
|
.w = @floatFromInt(ctx.canvas_width),
|
||||||
|
.h = @floatFromInt(ctx.canvas_height),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scroll.deinit();
|
||||||
|
}
|
||||||
|
canvas_box.deinit();
|
||||||
|
}
|
||||||
|
textured.deinit();
|
||||||
|
}
|
||||||
|
right_panel.deinit();
|
||||||
|
}
|
||||||
|
back.deinit();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
1
src/render/IRenderEngine.zig
Normal file
1
src/render/IRenderEngine.zig
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// Интерфейс для рендеринга документа
|
||||||
8
src/ui/dvui_ext.zig
Normal file
8
src/ui/dvui_ext.zig
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// Расширения для 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);
|
||||||
|
}
|
||||||
38
src/ui/types/TexturedBox.zig
Normal file
38
src/ui/types/TexturedBox.zig
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Отрисовка дочернего контента как текстуры с параметрами скругления
|
||||||
|
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