Something went wrong. Try again.
Fork of dvui, the immediate-mode Zig GUI, with a font face index for Pipit
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433pub fn applets() void { var vbox = dvui.box(@src(), .{}, .{ .expand = .both }); defer vbox.deinit();
var tabs = dvui.tabs(@src(), .{ .dir = .horizontal }, .{ .expand = .horizontal });
const active_tab = dvui.dataGetPtrDefault(null, vbox.data().id, "active_tab", usize, 0);
if (tabs.addTabLabel(active_tab.* == 0, "calculator", .{})) { active_tab.* = 0; } if (tabs.addTabLabel(active_tab.* == 1, "drawing", .{})) { active_tab.* = 1; } if (tabs.addTabLabel(active_tab.* == 2, "texture", .{})) { active_tab.* = 2; } if (tabs.addTabLabel(active_tab.* == 3, "sub rect", .{})) { active_tab.* = 3; }
tabs.deinit();
switch (active_tab.*) { 0 => calculator(), 1 => draw(), 2 => texture(), 3 => textureSubRect(), else => {}, }}
var calculation: f64 = 0;var calculand: ?f64 = null;var active_op: ?u8 = null;var next_op: ?u8 = null;var digits_after_dot: f64 = 0;var reset_on_digit: bool = false;
/// pub fn calculator() void { var vbox = dvui.box(@src(), .{}, .{}); defer vbox.deinit();
const loop_labels = [_]u8{ 'C', 'N', '%', '/', '7', '8', '9', 'x', '4', '5', '6', '-', '1', '2', '3', '+', '0', '.', '=' }; dvui.label(@src(), "{d}", .{if (calculand) |val| round(val) else round(calculation)}, .{ .gravity_x = 1.0 });
for (0..5) |row_i| { var b = dvui.box(@src(), .{ .dir = .horizontal }, .{ .min_size_content = .{ .w = 110 }, .id_extra = row_i }); defer b.deinit();
for (row_i * 4..(row_i + 1) * 4) |i| { if (i >= loop_labels.len) continue; const letter = loop_labels[i];
var opts = dvui.ButtonWidget.defaults.themeOverride(null).min_sizeM(3, 1); if (letter == '0') { const extra_space = opts.padSize(.{}).w; opts.min_size_content.?.w *= 2; // be twice as wide as normal opts.min_size_content.?.w += extra_space; // add the extra space between 2 buttons } if (dvui.button(@src(), &[_]u8{letter}, .{}, opts.override(.{ .id_extra = letter }))) { blk: switch (letter) { 'C' => { calculation = 0; calculand = null; active_op = null; next_op = null; digits_after_dot = 0; }, '/' => { next_op = '/'; continue :blk '='; }, 'x' => { next_op = 'x'; continue :blk '='; }, '-' => { next_op = '-'; continue :blk '='; }, '+' => { next_op = '+'; continue :blk '='; }, '.' => digits_after_dot = 1, 'N' => { calculation = -calculation; active_op = null; reset_on_digit = true; }, '%' => { calculation /= 100; active_op = null; reset_on_digit = true; }, '0'...'9' => { if (active_op == null) { if (reset_on_digit) { calculation = 0.0; reset_on_digit = false; } const letterDigit: f32 = @floatFromInt(letter - '0');
if (digits_after_dot > 0) { calculation += letterDigit / @exp(@log(10.0) * digits_after_dot); digits_after_dot += 1; } else { calculation *= 10; calculation += letterDigit; } } else { if (calculand == null) calculand = 0.0; const letterDigit: f64 = @floatFromInt(letter - '0'); if (digits_after_dot > 0) { calculand.? += letterDigit / @exp(@log(10.0) * digits_after_dot); digits_after_dot += 1; } else { calculand.? *= 10; calculand.? += letterDigit; } } }, '=' => if (active_op != null) { if (calculand) |val| { if (active_op == '/') calculation /= val; if (active_op == '-') calculation -= val; if (active_op == '+') calculation += val; if (active_op == 'x') calculation *= val; } active_op = next_op; if (active_op == null) { // User pressed equals. Start a new calc from here if a digit is pressed. reset_on_digit = true; } next_op = null; calculand = null; digits_after_dot = 0; } else { digits_after_dot = 0; }, else => unreachable, } if (active_op == null and next_op != null) { active_op = next_op; next_op = null; } } } }}
pub fn round(val: f64) f64 { const dec_places = 1_000_000; return @round(val * dec_places) / dec_places;}
const Tools = enum { pencil, eraser,};
pub fn draw() void { dvui.label(@src(), "Draws a slice of points every frame.", .{}, .{}); const uniqId = dvui.parentGet().extendId(@src(), 0); const active_tool = dvui.dataGetPtrDefault(null, uniqId, "active_tool", Tools, .pencil); { var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{}); defer hbox.deinit();
var group = dvui.radioGroup(@src(), .{}, .{ .label = .{ .text = "Drawing Tools" } }); defer group.deinit();
if (dvui.radio(@src(), active_tool.* == .pencil, "Pencil", .{})) { active_tool.* = .pencil; }
if (dvui.radio(@src(), active_tool.* == .eraser, "Eraser", .{})) { active_tool.* = .eraser; } }
var points: std.ArrayList(dvui.Point) = .empty; if (dvui.dataGetSlice(null, uniqId, "points", []dvui.Point)) |pts| { points.appendSlice(dvui.currentWindow().arena(), pts) catch @panic("OOM"); }
var canvas = dvui.box(@src(), .{}, .{ .expand = .both }); defer canvas.deinit(); const rs = canvas.data().contentRectScale();
const events = dvui.events(); for (events) |*e| { if (!dvui.eventMatchSimple(e, canvas.data())) continue;
switch (e.evt) { .mouse => |m| { switch (m.action) { .press, .motion => { if (m.action == .press and m.button == .left) { dvui.captureMouse(canvas.data(), e.num); }
if (dvui.captured(canvas.data().id)) { e.handle(@src(), canvas.data()); const newp = rs.pointFromPhysical(m.p); if (active_tool.* == .pencil) { points.append(dvui.currentWindow().arena(), newp) catch @panic("OOM"); dvui.refresh(null, @src(), canvas.data().id); } else if (active_tool.* == .eraser) { var i: usize = 0; while (i < points.items.len) { const p = points.items[i]; const dx = p.x - newp.x; const dy = p.y - newp.y; if ((dx * dx + dy * dy) < 5 * 5) { _ = points.swapRemove(i); } else { i += 1; } } } } }, .release => { if (dvui.captured(canvas.data().id)) { dvui.captureMouse(null, e.num); } }, else => {}, } }, else => {}, } }
for (points.items) |p| { dvui.Path.stroke(.{ .points = &.{rs.pointToPhysical(p)} }, .{ .color = dvui.Color{ .b = 120, .g = 12, .r = 212 }, .thickness = 5 * rs.s, }); }
dvui.dataSetSlice(null, uniqId, "points", points.items);}
pub fn appletTargetDestroy(ptr: *anyopaque) void { dvui.log.debug("appletTargetDestroy()", .{}); const tt: dvui.Texture.Target = @as(*dvui.Texture.Target, @ptrCast(@alignCast(ptr))).*; tt.destroyLater();}
pub fn texture() void { dvui.label(@src(), "Accumulates points into a target texture.", .{}, .{});
var vbox = dvui.box(@src(), .{}, .{}); defer vbox.deinit();
var clear = false; var destroy = false; { var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{ .gravity_x = 1 }); defer hbox.deinit();
if (dvui.button(@src(), "Clear", .{}, .{})) { clear = true; }
if (dvui.button(@src(), "Destroy", .{}, .{})) { destroy = true; } }
const size = 200; const scale: f32 = vbox.data().contentRectScale().s;
var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{}); defer hbox.deinit();
const tex: dvui.Texture.Target = dvui.dataGet(null, hbox.data().id, "tex", dvui.Texture.Target) orelse blk: { const t = dvui.Texture.Target.create(@intFromFloat(scale * size), @intFromFloat(scale * size), .linear, .rgba_32) catch { dvui.log.debug("Can't create target texture", .{}); return; }; dvui.dataSet(null, hbox.data().id, "tex", t); dvui.dataSetDeinitFunction(null, hbox.data().id, "tex", &appletTargetDestroy); break :blk t; };
if (clear) tex.clear(); if (destroy) { // only need to dataRemove because that will run our deinit func appletTargetDestroy() dvui.dataRemove(null, hbox.data().id, "tex"); }
{ var box = dvui.box(@src(), .{}, .{}); defer box.deinit(); dvui.label(@src(), "Draw Here", .{}, .{ .gravity_x = 0.5 }); var input = dvui.box(@src(), .{}, .{ .min_size_content = .all(size), .border = .all(1) }); defer input.deinit();
if (!destroy) { const target = dvui.renderTarget(.{ .texture = tex, .offset = input.data().contentRectScale().r.topLeft() }); defer _ = dvui.renderTarget(target);
for (dvui.events()) |*e| { if (!dvui.eventMatchSimple(e, input.data())) continue; switch (e.evt) { .mouse => |m| { if (m.action == .press and m.button == .left) { dvui.captureMouse(input.data(), e.num); }
if (dvui.captured(input.data().id)) { e.handle(@src(), input.data());
if (m.action == .release) { dvui.captureMouse(null, e.num); } else { dvui.Path.stroke(.{ .points = &.{e.evt.mouse.p} }, .{ .thickness = 5, .color = .red }); dvui.refresh(null, @src(), input.data().id); } } }, else => {}, } } } }
_ = dvui.spacer(@src(), .{ .min_size_content = .width(6) });
{ var box = dvui.box(@src(), .{}, .{}); defer box.deinit(); dvui.label(@src(), "Texture", .{}, .{ .gravity_x = 0.5 }); var output = dvui.box(@src(), .{}, .{ .min_size_content = .all(size), .border = .all(1) }); defer output.deinit();
if (!destroy) { // drawable is temporary, we don't need to destroy it const drawable = dvui.Texture.fromTargetTemp(tex) catch { dvui.log.debug("textureFromTargetTemp errored", .{}); return; }; dvui.renderTexture(drawable, output.data().contentRectScale(), .{}) catch {}; } }}
pub fn appletTextureDestroy(ptr: *anyopaque) void { dvui.log.debug("appletTextureDestroy()", .{}); const tt: dvui.Texture = @as(*dvui.Texture, @ptrCast(@alignCast(ptr))).*; tt.destroyLater();}
pub fn textureSubRect() void { dvui.label(@src(), "Randomly updates portions of a texture", .{}, .{});
var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{}); defer hbox.deinit();
const size = 200; const scale: f32 = hbox.data().contentRectScale().s;
var tex: *dvui.Texture = dvui.dataGetPtr(null, hbox.data().id, "tex", dvui.Texture) orelse blk: { const pixels = dvui.currentWindow().arena().alloc(dvui.Color.PMA, @intFromFloat(size * size * scale * scale)) catch @panic("OOM"); for (pixels) |*p| { p.* = .black; } const t = dvui.Texture.create(pixels, @intFromFloat(scale * size), @intFromFloat(scale * size), .linear, .rgba_32) catch { dvui.log.debug("Can't create texture", .{}); return; }; dvui.dataSet(null, hbox.data().id, "tex", t); dvui.dataSetDeinitFunction(null, hbox.data().id, "tex", &appletTextureDestroy); break :blk dvui.dataGetPtr(null, hbox.data().id, "tex", dvui.Texture).?; };
{ var box = dvui.box(@src(), .{}, .{ .min_size_content = .all(size) }); defer box.deinit(); dvui.renderTexture(tex.*, box.data().contentRectScale(), .{}) catch {}; }
if (dvui.button(@src(), "Update", .{}, .{})) { var rng: std.Random.DefaultPrng = .init(@intCast(dvui.frameTimeNS())); var r = rng.random(); const x = r.intRangeLessThan(u32, 0, @intFromFloat(size * scale)); const y = r.intRangeLessThan(u32, 0, @intFromFloat(size * scale)); const w = r.intRangeLessThan(u32, 0, @as(u32, @intFromFloat(size * scale)) - x); const h = r.intRangeLessThan(u32, 0, @as(u32, @intFromFloat(size * scale)) - y);
const pixels = dvui.currentWindow().arena().alloc(dvui.Color.PMA, @intFromFloat(size * size * scale * scale)) catch @panic("OOM"); var newp: dvui.Color.PMA = .{}; newp.r = r.intRangeLessThan(u8, 0, 255); newp.g = r.intRangeLessThan(u8, 0, 255); newp.b = r.intRangeLessThan(u8, 0, 255); for (pixels) |*p| { p.* = newp; } tex.updateSubRect(@ptrCast(pixels.ptr), x, y, w, h) catch |err| { dvui.logError(@src(), err, "Could not updateSubRect", .{}); }; }}
test { @import("std").testing.refAllDecls(@This());}
test "DOCIMG calculator" { var t = try dvui.testing.init(.{ .window_size = .{ .w = 250, .h = 250 } }); defer t.deinit();
const frame = struct { fn frame() !dvui.App.Result { var box = dvui.box(@src(), .{}, .{ .expand = .both, .background = true, .style = .window }); defer box.deinit(); calculator(); return .ok; } }.frame;
try dvui.testing.settle(frame); try t.saveImage(frame, null, "Examples-calculator.png");}
const dvui = @import("../dvui.zig");const std = @import("std");