Initial Commit

This commit is contained in:
2022-12-31 00:40:03 -06:00
commit 8692808640
31 changed files with 76432 additions and 0 deletions

68
src/backend_glfw_wgpu.zig Normal file
View File

@@ -0,0 +1,68 @@
const gui = @import("gui.zig");
pub const TextureFilterMode = enum(u32) {
nearest,
linear,
};
pub const Config = extern struct {
pipeline_multisample_count: u32 = 1,
texture_filter_mode: TextureFilterMode = .linear,
};
// This call will install GLFW callbacks to handle GUI interactions.
// Those callbacks will chain-call user's previously installed callbacks, if any.
// This means that custom user's callbacks need to be installed *before* calling zgpu.gui.init().
pub fn initWithConfig(
window: *const anyopaque, // zglfw.Window
wgpu_device: *const anyopaque, // wgpu.Device
wgpu_swap_chain_format: u32, // wgpu.TextureFormat
config: Config,
) void {
if (!ImGui_ImplGlfw_InitForOther(window, true)) {
unreachable;
}
if (!ImGui_ImplWGPU_Init(wgpu_device, 1, wgpu_swap_chain_format, &config)) {
unreachable;
}
}
pub fn init(window: *const anyopaque, wgpu_device: *const anyopaque, wgpu_swap_chain_format: u32) void {
initWithConfig(window, wgpu_device, wgpu_swap_chain_format, .{});
}
pub fn deinit() void {
ImGui_ImplWGPU_Shutdown();
ImGui_ImplGlfw_Shutdown();
}
pub fn newFrame(fb_width: u32, fb_height: u32) void {
ImGui_ImplWGPU_NewFrame();
ImGui_ImplGlfw_NewFrame();
gui.io.setDisplaySize(@intToFloat(f32, fb_width), @intToFloat(f32, fb_height));
gui.io.setDisplayFramebufferScale(1.0, 1.0);
gui.newFrame();
}
pub fn draw(wgpu_render_pass: *const anyopaque) void {
gui.render();
ImGui_ImplWGPU_RenderDrawData(gui.getDrawData(), wgpu_render_pass);
}
// Those functions are defined in `imgui_impl_glfw.cpp` and 'imgui_impl_wgpu.cpp`
// (they include few custom changes).
extern fn ImGui_ImplGlfw_InitForOther(window: *const anyopaque, install_callbacks: bool) bool;
extern fn ImGui_ImplGlfw_NewFrame() void;
extern fn ImGui_ImplGlfw_Shutdown() void;
extern fn ImGui_ImplWGPU_Init(
device: *const anyopaque,
num_frames_in_flight: u32,
rt_format: u32,
config: *const Config,
) bool;
extern fn ImGui_ImplWGPU_NewFrame() void;
extern fn ImGui_ImplWGPU_RenderDrawData(draw_data: *const anyopaque, pass_encoder: *const anyopaque) void;
extern fn ImGui_ImplWGPU_Shutdown() void;

3918
src/gui.zig Normal file

File diff suppressed because it is too large Load Diff

15
src/main.zig Normal file
View File

@@ -0,0 +1,15 @@
//--------------------------------------------------------------------------------------------------
//
// Zig bindings for 'dear imgui' library. Easy to use, hand-crafted API with default arguments,
// named parameters and Zig style text formatting.
//
//--------------------------------------------------------------------------------------------------
pub const version = @import("std").SemanticVersion{ .major = 0, .minor = 9, .patch = 4 };
pub usingnamespace @import("gui.zig");
pub const plot = @import("plot.zig");
pub const backend = switch (@import("zgui_options").backend) {
.glfw_wgpu => @import("backend_glfw_wgpu.zig"),
.win32_dx12 => .{}, // TODO:
.no_backend => .{},
};

504
src/plot.zig Normal file
View File

@@ -0,0 +1,504 @@
//--------------------------------------------------------------------------------------------------
const assert = @import("std").debug.assert;
const gui = @import("gui.zig");
//--------------------------------------------------------------------------------------------------
pub fn init() void {
if (zguiPlot_GetCurrentContext() == null) {
_ = zguiPlot_CreateContext();
}
}
pub fn deinit() void {
if (zguiPlot_GetCurrentContext() != null) {
zguiPlot_DestroyContext(null);
}
}
const Context = *opaque {};
extern fn zguiPlot_GetCurrentContext() ?Context;
extern fn zguiPlot_CreateContext() Context;
extern fn zguiPlot_DestroyContext(ctx: ?Context) void;
//--------------------------------------------------------------------------------------------------
pub const Marker = enum(i32) {
none = -1,
circle = 0,
square,
diamond,
up,
down,
left,
right,
cross,
plus,
asterisk,
};
pub const Colormap = enum(u32) {
deep,
dark,
pastel,
paired,
viridis,
plasma,
hot,
cool,
pink,
jet,
twilight,
rd_bu,
br_b_g,
pi_y_g,
spectral,
greys,
};
pub const Style = extern struct {
line_weight: f32,
marker: Marker,
marker_size: f32,
marker_weight: f32,
fill_alpha: f32,
error_bar_size: f32,
error_bar_weight: f32,
digital_bit_height: f32,
digital_bit_gap: f32,
plot_border_size: f32,
minor_alpha: f32,
major_tick_len: [2]f32,
minor_tick_len: [2]f32,
major_tick_size: [2]f32,
minor_tick_size: [2]f32,
major_grid_size: [2]f32,
minor_grid_size: [2]f32,
plot_padding: [2]f32,
label_padding: [2]f32,
legend_padding: [2]f32,
legend_inner_padding: [2]f32,
legend_spacing: [2]f32,
mouse_pos_padding: [2]f32,
annotation_padding: [2]f32,
fit_padding: [2]f32,
plot_default_size: [2]f32,
plot_min_size: [2]f32,
colors: [@typeInfo(StyleCol).Enum.fields.len][4]f32,
colormap: Colormap,
use_local_time: bool,
use_iso_8601: bool,
use_24h_clock: bool,
/// `pub fn init() Style`
pub const init = zguiPlotStyle_Init;
extern fn zguiPlotStyle_Init() Style;
pub fn getColor(style: Style, idx: StyleCol) [4]f32 {
return style.colors[@enumToInt(idx)];
}
pub fn setColor(style: *Style, idx: StyleCol, color: [4]f32) void {
style.colors[@enumToInt(idx)] = color;
}
};
/// `pub fn getStyle() *Style`
pub const getStyle = zguiPlot_GetStyle;
extern fn zguiPlot_GetStyle() *Style;
//--------------------------------------------------------------------------------------------------
pub const StyleCol = enum(u32) {
line,
fill,
marker_outline,
marker_fill,
error_bar,
frame_bg,
plot_bg,
plot_border,
legend_bg,
legend_border,
legend_text,
title_text,
inlay_text,
axis_text,
axis_grid,
axis_tick,
axis_bg,
axis_bg_hovered,
axis_bg_active,
selection,
crosshairs,
};
const PushStyleColor4f = struct {
idx: StyleCol,
c: [4]f32,
};
pub fn pushStyleColor4f(args: PushStyleColor4f) void {
zguiPlot_PushStyleColor4f(args.idx, &args.c);
}
const PushStyleColor1u = struct {
idx: StyleCol,
c: u32,
};
pub fn pushStyleColor1u(args: PushStyleColor1u) void {
zguiPlot_PushStyleColor1u(args.idx, args.c);
}
const PopStyleColor = struct {
count: i32 = 1,
};
pub fn popStyleColor(args: PopStyleColor) void {
zguiPlot_PopStyleColor(args.count);
}
extern fn zguiPlot_PushStyleColor4f(idx: StyleCol, col: *const [4]f32) void;
extern fn zguiPlot_PushStyleColor1u(idx: StyleCol, col: u32) void;
extern fn zguiPlot_PopStyleColor(count: i32) void;
//--------------------------------------------------------------------------------------------------
pub const StyleVar = enum(u32) {
line_weight, // 1f
marker, // 1i
marker_size, // 1f
marker_weight, // 1f
fill_alpha, // 1f
error_bar_size, // 1f
error_bar_weight, // 1f
digital_bit_height, // 1f
digital_bit_gap, // 1f
plot_border_size, // 1f
minor_alpha, // 1f
major_tick_len, // 2f
minor_tick_len, // 2f
major_tick_size, // 2f
minor_tick_size, // 2f
major_grid_size, // 2f
minor_grid_size, // 2f
plot_padding, // 2f
label_padding, // 2f
legend_padding, // 2f
legend_inner_padding, // 2f
legend_spacing, // 2f
mouse_pos_padding, // 2f
annotation_padding, // 2f
fit_padding, // 2f
plot_default_size, // 2f
plot_min_size, // 2f
};
const PushStyleVar1i = struct {
idx: StyleVar,
v: i32,
};
pub fn pushStyleVar1i(args: PushStyleVar1i) void {
zguiPlot_PushStyleVar1i(args.idx, args.v);
}
const PushStyleVar1f = struct {
idx: StyleVar,
v: f32,
};
pub fn pushStyleVar1f(args: PushStyleVar1f) void {
zguiPlot_PushStyleVar1f(args.idx, args.v);
}
const PushStyleVar2f = struct {
idx: StyleVar,
v: [2]f32,
};
pub fn pushStyleVar2f(args: PushStyleVar2f) void {
zguiPlot_PushStyleVar2f(args.idx, &args.v);
}
const PopStyleVar = struct {
count: i32 = 1,
};
pub fn popStyleVar(args: PopStyleVar) void {
zguiPlot_PopStyleVar(args.count);
}
extern fn zguiPlot_PushStyleVar1i(idx: StyleVar, v: i32) void;
extern fn zguiPlot_PushStyleVar1f(idx: StyleVar, v: f32) void;
extern fn zguiPlot_PushStyleVar2f(idx: StyleVar, v: *const [2]f32) void;
extern fn zguiPlot_PopStyleVar(count: i32) void;
//--------------------------------------------------------------------------------------------------
pub const PlotLocation = packed struct(u32) {
north: bool = false,
south: bool = false,
west: bool = false,
east: bool = false,
_padding: u28 = 0,
pub const north_west = PlotLocation{ .north = true, .west = true };
pub const north_east = PlotLocation{ .north = true, .east = true };
pub const south_west = PlotLocation{ .south = true, .west = true };
pub const south_east = PlotLocation{ .south = true, .east = true };
};
pub const LegendFlags = packed struct(u32) {
no_buttons: bool = false,
no_highlight_item: bool = false,
no_highlight_axis: bool = false,
no_menus: bool = false,
outside: bool = false,
horizontal: bool = false,
_padding: u26 = 0,
};
pub fn setupLegend(location: PlotLocation, flags: LegendFlags) void {
zguiPlot_SetupLegend(location, flags);
}
extern fn zguiPlot_SetupLegend(location: PlotLocation, flags: LegendFlags) void;
//--------------------------------------------------------------------------------------------------
pub const AxisFlags = packed struct(u32) {
no_label: bool = false,
no_grid_lines: bool = false,
no_tick_marks: bool = false,
no_tick_labels: bool = false,
no_initial_fit: bool = false,
no_menus: bool = false,
no_side_switch: bool = false,
no_highlight: bool = false,
opposite: bool = false,
foreground: bool = false,
invert: bool = false,
auto_fit: bool = false,
range_fit: bool = false,
pan_stretch: bool = false,
lock_min: bool = false,
lock_max: bool = false,
_padding: u16 = 0,
pub const lock = AxisFlags{
.lock_min = true,
.lock_max = true,
};
pub const no_decorations = AxisFlags{
.no_label = true,
.no_grid_lines = true,
.no_tick_marks = true,
.no_tick_labels = true,
};
pub const aux_default = AxisFlags{
.no_grid_lines = true,
.opposite = true,
};
};
pub const Axis = enum(u32) { x1, x2, x3, y1, y2, y3 };
pub const SetupAxis = struct {
label: ?[:0]const u8 = null,
flags: AxisFlags = .{},
};
pub fn setupAxis(axis: Axis, args: SetupAxis) void {
zguiPlot_SetupAxis(axis, if (args.label) |l| l else null, args.flags);
}
extern fn zguiPlot_SetupAxis(axis: Axis, label: ?[*:0]const u8, flags: AxisFlags) void;
//----------------------------------------------------------------------------------------------
pub const Condition = enum(u32) {
none = @enumToInt(gui.Condition.none),
always = @enumToInt(gui.Condition.always),
once = @enumToInt(gui.Condition.once),
};
const SetupAxisLimits = struct {
min: f64,
max: f64,
cond: Condition = .once,
};
pub fn setupAxisLimits(axis: Axis, args: SetupAxisLimits) void {
zguiPlot_SetupAxisLimits(axis, args.min, args.max, args.cond);
}
extern fn zguiPlot_SetupAxisLimits(axis: Axis, min: f64, max: f64, cond: Condition) void;
//----------------------------------------------------------------------------------------------
/// `pub fn setupFinish() void`
pub const setupFinish = zguiPlot_SetupFinish;
extern fn zguiPlot_SetupFinish() void;
//----------------------------------------------------------------------------------------------
pub const Flags = packed struct(u32) {
no_title: bool = false,
no_legend: bool = false,
no_mouse_text: bool = false,
no_inputs: bool = false,
no_menus: bool = false,
no_box_select: bool = false,
no_child: bool = false,
no_frame: bool = false,
equal: bool = false,
crosshairs: bool = false,
_padding: u22 = 0,
pub const canvas_only = Flags{
.no_title = true,
.no_legend = true,
.no_menus = true,
.no_box_select = true,
.no_mouse_text = true,
};
};
pub const BeginPlot = struct {
w: f32 = -1.0,
h: f32 = 0.0,
flags: Flags = .{},
};
pub fn beginPlot(title_id: [:0]const u8, args: BeginPlot) bool {
return zguiPlot_BeginPlot(title_id, args.w, args.h, args.flags);
}
extern fn zguiPlot_BeginPlot(title_id: [*:0]const u8, width: f32, height: f32, flags: Flags) bool;
//----------------------------------------------------------------------------------------------
pub const LineFlags = packed struct(u32) {
_reserved0: bool = false,
_reserved1: bool = false,
_reserved2: bool = false,
_reserved3: bool = false,
_reserved4: bool = false,
_reserved5: bool = false,
_reserved6: bool = false,
_reserved7: bool = false,
_reserved8: bool = false,
_reserved9: bool = false,
segments: bool = false,
loop: bool = false,
skip_nan: bool = false,
no_clip: bool = false,
shaded: bool = false,
_padding: u17 = 0,
};
fn PlotLineValuesGen(comptime T: type) type {
return struct {
v: []const T,
xscale: f64 = 1.0,
xstart: f64 = 0.0,
flags: LineFlags = .{},
offset: i32 = 0,
stride: i32 = @sizeOf(T),
};
}
pub fn plotLineValues(label_id: [:0]const u8, comptime T: type, args: PlotLineValuesGen(T)) void {
zguiPlot_PlotLineValues(
label_id,
gui.typeToDataTypeEnum(T),
args.v.ptr,
@intCast(i32, args.v.len),
args.xscale,
args.xstart,
args.flags,
args.offset,
args.stride,
);
}
extern fn zguiPlot_PlotLineValues(
label_id: [*:0]const u8,
data_type: gui.DataType,
values: *const anyopaque,
count: i32,
xscale: f64,
xstart: f64,
flags: LineFlags,
offset: i32,
stride: i32,
) void;
//----------------------------------------------------------------------------------------------
fn PlotLineGen(comptime T: type) type {
return struct {
xv: []const T,
yv: []const T,
flags: LineFlags = .{},
offset: i32 = 0,
stride: i32 = @sizeOf(T),
};
}
pub fn plotLine(label_id: [:0]const u8, comptime T: type, args: PlotLineGen(T)) void {
assert(args.xv.len == args.yv.len);
zguiPlot_PlotLine(
label_id,
gui.typeToDataTypeEnum(T),
args.xv.ptr,
args.yv.ptr,
@intCast(i32, args.xv.len),
args.flags,
args.offset,
args.stride,
);
}
extern fn zguiPlot_PlotLine(
label_id: [*:0]const u8,
data_type: gui.DataType,
xv: *const anyopaque,
yv: *const anyopaque,
count: i32,
flags: LineFlags,
offset: i32,
stride: i32,
) void;
//----------------------------------------------------------------------------------------------
pub const ScatterFlags = packed struct(u32) {
_reserved0: bool = false,
_reserved1: bool = false,
_reserved2: bool = false,
_reserved3: bool = false,
_reserved4: bool = false,
_reserved5: bool = false,
_reserved6: bool = false,
_reserved7: bool = false,
_reserved8: bool = false,
_reserved9: bool = false,
no_clip: bool = false,
_padding: u21 = 0,
};
fn PlotScatterValuesGen(comptime T: type) type {
return struct {
v: []const T,
xscale: f64 = 1.0,
xstart: f64 = 0.0,
flags: ScatterFlags = .{},
offset: i32 = 0,
stride: i32 = @sizeOf(T),
};
}
pub fn plotScatterValues(label_id: [:0]const u8, comptime T: type, args: PlotScatterValuesGen(T)) void {
zguiPlot_PlotScatterValues(
label_id,
gui.typeToDataTypeEnum(T),
args.v.ptr,
@intCast(i32, args.v.len),
args.xscale,
args.xstart,
args.flags,
args.offset,
args.stride,
);
}
extern fn zguiPlot_PlotScatterValues(
label_id: [*:0]const u8,
data_type: gui.DataType,
values: *const anyopaque,
count: i32,
xscale: f64,
xstart: f64,
flags: ScatterFlags,
offset: i32,
stride: i32,
) void;
//----------------------------------------------------------------------------------------------
fn PlotScatterGen(comptime T: type) type {
return struct {
xv: []const T,
yv: []const T,
flags: ScatterFlags = .{},
offset: i32 = 0,
stride: i32 = @sizeOf(T),
};
}
pub fn plotScatter(label_id: [:0]const u8, comptime T: type, args: PlotScatterGen(T)) void {
assert(args.xv.len == args.yv.len);
zguiPlot_PlotScatter(
label_id,
gui.typeToDataTypeEnum(T),
args.xv.ptr,
args.yv.ptr,
@intCast(i32, args.xv.len),
args.flags,
args.offset,
args.stride,
);
}
extern fn zguiPlot_PlotScatter(
label_id: [*:0]const u8,
data_type: gui.DataType,
xv: *const anyopaque,
yv: *const anyopaque,
count: i32,
flags: ScatterFlags,
offset: i32,
stride: i32,
) void;
//----------------------------------------------------------------------------------------------
/// `pub fn endPlot() void`
pub const endPlot = zguiPlot_EndPlot;
extern fn zguiPlot_EndPlot() void;
//----------------------------------------------------------------------------------------------

2177
src/zgui.cpp Normal file

File diff suppressed because it is too large Load Diff