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);
            dvui.renderTexture(t, self.rs, .{
                .corner_radius = self.corner_radius,
            }) catch {};
        }
    }
}
