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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213const std = @import("std");const dvui = @import("dvui");const RaylibBackend = @import("raylib-backend");comptime { std.debug.assert(@hasDecl(RaylibBackend, "RaylibBackend"));}
const window_icon_png = @embedFile("zig-favicon.png");
const vsync = true;var scale_val: f32 = 1.0;
var show_dialog_outside_frame: bool = false;
pub const c = RaylibBackend.c;
/// This example shows how to use the dvui for a normal application:/// - dvui renders the whole application/// - render frames only when neededpub fn main(init: std.process.Init) !void { if (@import("builtin").os.tag == .windows) { // optional // 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 {}; } RaylibBackend.enableRaylibLogging();
// init Raylib backend (creates OS window) // initWindow() means the backend calls CloseWindow for you in deinit() var backend = try RaylibBackend.initWindow(.{ .io = init.io, .gpa = init.gpa, .size = .{ .w = 800.0, .h = 600.0 }, .vsync = vsync, .title = "DVUI Raylib Standalone Example", .icon = window_icon_png, // can also call setIconFromFileContent() }); defer backend.deinit(); backend.log_events = true;
// turn off normal raylib behavior where escape closes the window c.SetExitKey(0);
var window_open = true; // init dvui Window (maps onto a single OS window) var win = try dvui.Window.init(@src(), init.gpa, backend.backend(), .{ // you can set the default theme here in the init options .theme = switch (backend.preferredColorScheme() orelse .light) { .light => dvui.Theme.builtin.adwaita_light, .dark => dvui.Theme.builtin.adwaita_dark, }, .open_flag = &window_open, }); defer win.deinit();
var interrupted = true;
main_loop: while (window_open) { c.BeginDrawing();
// beginWait coordinates with waitTime below to run frames only when needed const nstime = win.beginWait(interrupted);
// marks the beginning of a frame for dvui, can call dvui functions after this try win.begin(nstime);
// if dvui widgets might not cover the whole window, then need to clear // the previous frame's render backend.clear();
const keep_running = dvui_frame(); if (!keep_running) break :main_loop;
// 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(.{});
// waitTime and beginWait combine to achieve variable framerates const wait_event_micros = win.waitTime(end_micros);
// this adds events interrupted = backend.EndDrawingWaitEventTimeout(&win, wait_event_micros);
// Example of how to show a dialog from another thread (outside of win.begin/win.end) if (show_dialog_outside_frame) { show_dialog_outside_frame = false; dvui.dialog(@src(), .{}, .{ .window = &win, .modal = false, .title = "Dialog from Outside", .message = "This is a non modal dialog that was created outside win.begin()/win.end(), usually from another thread." }); } }}
// return true to keep runningfn dvui_frame() bool { { var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{ .style = .window, .background = true, .expand = .horizontal }); defer hbox.deinit();
var m = dvui.menu(@src(), .horizontal, .{}); defer m.deinit();
if (dvui.menuItemLabel(@src(), "File", .{ .submenu = true }, .{})) |r| { var fw = dvui.floatingMenu(@src(), .{ .from = r }, .{}); defer fw.deinit();
if (dvui.menuItemLabel(@src(), "Close Menu", .{}, .{ .expand = .horizontal }) != null) { m.close(); }
if (dvui.menuItemLabel(@src(), "Exit", .{}, .{ .expand = .horizontal }) != null) { return false; } }
if (dvui.menuItemLabel(@src(), "Edit", .{ .submenu = true }, .{})) |r| { var fw = dvui.floatingMenu(@src(), .{ .from = r }, .{}); defer fw.deinit(); _ = dvui.menuItemLabel(@src(), "Dummy", .{}, .{ .expand = .horizontal }); _ = dvui.menuItemLabel(@src(), "Dummy Long", .{}, .{ .expand = .horizontal }); _ = dvui.menuItemLabel(@src(), "Dummy Super Long", .{}, .{ .expand = .horizontal }); } }
var scroll = dvui.scrollArea(@src(), .{}, .{ .expand = .both }); defer scroll.deinit();
var tl = dvui.textLayout(@src(), .{}, .{ .expand = .horizontal, .font = .theme(.title) }); const lorem = "This example shows how to use dvui in a normal application."; tl.addText(lorem, .{}); tl.deinit();
var tl2 = dvui.textLayout(@src(), .{}, .{ .expand = .horizontal }); tl2.addText( \\DVUI \\- paints the entire window \\- can show floating windows and dialogs \\- example menu at the top of the window \\- rest of the window is a scroll area \\ \\ , .{}); tl2.addText("Framerate is set by Raylib (C api).\n\n", .{}); if (vsync) { tl2.addText("Framerate is capped by vsync.\n", .{}); } else { tl2.addText("Framerate is uncapped.\n", .{}); } tl2.addText("\n", .{}); tl2.addText("Cursor is always being set by dvui.\n\n", .{}); if (dvui.useFreeType) { tl2.addText("Fonts are being rendered by FreeType 2.", .{}); } else { tl2.addText("Fonts are being rendered by stb_truetype.", .{}); } tl2.deinit();
const label = if (dvui.Examples.show_demo_window) "Hide Demo Window" else "Show Demo Window"; if (dvui.button(@src(), label, .{}, .{})) { dvui.Examples.show_demo_window = !dvui.Examples.show_demo_window; }
if (dvui.button(@src(), "Debug Window", .{}, .{})) { dvui.toggleDebugWindow(); }
{ var scaler = dvui.scale(@src(), .{ .scale = &scale_val }, .{ .expand = .horizontal }); defer scaler.deinit();
{ var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{}); defer hbox.deinit();
if (dvui.button(@src(), "Zoom In", .{}, .{})) { scale_val = @round(dvui.themeGet().font_body.size * scale_val + 1.0) / dvui.themeGet().font_body.size; }
if (dvui.button(@src(), "Zoom Out", .{}, .{})) { scale_val = @round(dvui.themeGet().font_body.size * scale_val - 1.0) / dvui.themeGet().font_body.size; } }
dvui.labelNoFmt(@src(), "Below is drawn directly by the backend, not going through DVUI.", .{}, .{ .margin = .{ .x = 4 } });
var box = dvui.box(@src(), .{}, .{ .expand = .horizontal, .min_size_content = .{ .h = 40 }, .background = true, .margin = .{ .x = 8, .w = 8 } }); defer box.deinit();
// Here is some arbitrary drawing that doesn't have to go through DVUI. // It can be interleaved with DVUI drawing. // NOTE: This only works in the main window (not floating subwindows // like dialogs).
// get the screen rectangle for the box const rs = box.data().contentRectScale();
// rs.r is the pixel rectangle, rs.s is the scale factor (like for // hidpi screens or display scaling) // raylib multiplies everything internally by the monitor scale, so we // have to divide by that const r = RaylibBackend.dvuiRectToRaylib(rs.r); const s = rs.s / dvui.windowNaturalScale(); c.DrawText("Congrats! You created your first window!", @trunc(r.x + 10 * s), @trunc(r.y + 10 * s), @trunc(20 * s), c.LIGHTGRAY); }
if (dvui.button(@src(), "Show Dialog From\nOutside Frame", .{}, .{})) { show_dialog_outside_frame = true; }
// only shows the demo if dvui.Examples.show_demo_window is true // .full -> .lite or comment out to speed up compile times dvui.Examples.demo(.full);
return true;}