diff --git a/.gitignore b/.gitignore index 44dbeed..5a26686 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -zig-cache -zig-out \ No newline at end of file +.zig-cache + +vcpkg_installed +zig-out diff --git a/build.zig b/build.zig index f8e64da..70e6245 100644 --- a/build.zig +++ b/build.zig @@ -18,14 +18,13 @@ pub fn build(b: *std.Build) !void { const nanovg_dep = b.dependency("nanovg", .{ .target = target, .optimize = optimize }); const nanovg_mod = nanovg_dep.module("nanovg"); - const gui_mod = b.createModule(.{ .root_source_file = .{ .path = "src/gui/gui.zig" } }); + const gui_mod = b.createModule(.{ .root_source_file = b.path("src/gui/gui.zig") }); gui_mod.addImport("nanovg", nanovg_mod); - const data_mod = b.createModule(.{ .root_source_file = .{ .path = "data/data.zig" } }); - + const data_mod = b.createModule(.{ .root_source_file = b.path("data/data.zig") }); const exe = b.addExecutable(.{ .name = "minipixel", - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); @@ -34,15 +33,8 @@ pub fn build(b: *std.Build) !void { exe.root_module.addOptions("build_options", exe_options); exe_options.addOption(bool, "automated_testing", automated_testing); - exe.addIncludePath(.{ .path = "lib/gl2/include" }); + exe.addIncludePath(b.path("lib/gl2/include")); if (target.result.os.tag == .windows) { - // exe.addVcpkgPaths(.dynamic) catch @panic("vcpkg not installed"); - // if (exe.vcpkg_bin_path) |bin_path| { - // for (&[_][]const u8{ "libpng16.dll", "zlib1.dll" }) |dll| { - // const src_dll = try std.fs.path.join(b.allocator, &.{ bin_path, dll }); - // b.installBinFile(src_dll, dll); - // } - // } exe.subsystem = .Windows; exe.linkSystemLibrary("shell32"); std.fs.cwd().access("minipixel.o", .{}) catch { @@ -50,17 +42,17 @@ pub fn build(b: *std.Build) !void { "\trc /fo minipixel.o minipixel.rc\n\nbefore continuing\n", .{}); return error.FileNotFound; }; - exe.addObjectFile(.{ .path = "minipixel.o" }); // add icon + exe.addObjectFile(b.path("minipixel.o")); // add icon } else if (target.result.os.tag == .macos) { - exe.addCSourceFile(.{ .file = .{ .path = "src/c/sdl_hacks.m" }, .flags = &.{} }); + exe.addCSourceFile(.{ .file = b.path("src/c/sdl_hacks.m"), .flags = &.{} }); } const c_flags: []const []const u8 = if (optimize == .Debug) &.{ "-std=c99", "-D_CRT_SECURE_NO_WARNINGS", "-O0", "-g" } else &.{ "-std=c99", "-D_CRT_SECURE_NO_WARNINGS" }; - exe.addCSourceFile(.{ .file = .{ .path = "src/c/png_image.c" }, .flags = &.{"-std=c99"} }); - exe.addCSourceFile(.{ .file = .{ .path = "lib/gl2/src/glad.c" }, .flags = c_flags }); - exe.root_module.addImport("win32", zigwin32_dep.module("zigwin32")); + exe.addCSourceFile(.{ .file = b.path("src/c/png_image.c"), .flags = &.{"-std=c99"} }); + exe.addCSourceFile(.{ .file = b.path("lib/gl2/src/glad.c"), .flags = c_flags }); + exe.root_module.addImport("win32", zigwin32_dep.module("win32")); exe.root_module.addImport("nfd", nfd_dep.module("nfd")); exe.root_module.addImport("nanovg", nanovg_mod); exe.root_module.addImport("gui", gui_mod); @@ -68,10 +60,12 @@ pub fn build(b: *std.Build) !void { exe.linkLibrary(sdl_dep.artifact("SDL2")); if (target.result.os.tag == .windows) { // Workaround for CI: Zig detects pkg-config and resolves -lpng16 which doesn't exist + exe.addIncludePath(b.path("vcpkg_installed/x64-windows/include")); + exe.addLibraryPath(b.path("vcpkg_installed/x64-windows/bin")); exe.linkSystemLibrary("libpng16"); } else if (target.result.os.tag == .macos) { - exe.addIncludePath(.{ .path = "/opt/homebrew/include" }); - exe.addLibraryPath(.{ .path = "/opt/homebrew/lib" }); + exe.addIncludePath(b.path("/opt/homebrew/include")); + exe.addLibraryPath(b.path("/opt/homebrew/lib")); exe.linkSystemLibrary("png"); } else { exe.linkSystemLibrary("libpng16"); @@ -90,7 +84,7 @@ pub fn build(b: *std.Build) !void { installPalFiles(b); const test_cmd = b.addTest(.{ - .root_source_file = .{ .path = "src/tests.zig" }, + .root_source_file = b.path("src/tests.zig"), .optimize = optimize, }); const test_step = b.step("test", "Run tests"); diff --git a/build.zig.zon b/build.zig.zon index ea9ea2d..0011013 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,7 @@ .{ - .name = "Mini Pixel", + .name = .minipixel, .version = "0.2.1", + .fingerprint = 0xc96373d8301039f5, .paths = .{ "data", "lib", @@ -10,21 +11,21 @@ "LICENSE", }, .dependencies = .{ + .nanovg = .{ + .url = "git+https://github.com/fabioarnold/nanovg-zig.git#5a26c03f3acc3c3d7610108bd2c6663500296718", + .hash = "nanovg_zig-0.0.0-dfizXhAgNgB1frMIoQM_XTPT1RbVBLZJ-DvxVi-BnQbD", + }, .nfd = .{ - .url = "https://github.com/fabioarnold/nfd-zig/archive/91b199d3cbb314c52c630b7bf6050c4d665c0628.tar.gz", - .hash = "122020466a938b490e0d4963eae2157d976990aa0acc53a1e32f9ac39d63a14cf977", + .url = "git+https://github.com/fabioarnold/nfd-zig.git#ad81729d33da30d5f4fd23718debec48245121ca", + .hash = "N-V-__8AAPOHBgCmeTgIR1EyYsjFxHTUpBX57MSSHIxq7729", }, .sdl = .{ - .url = "https://github.com/andrewrk/SDL/archive/db4a162db2f6f59f737d03f441455dc9524d5793.tar.gz", - .hash = "1220c5360c9c71c215baa41b46ec18d0711059b48416a2b1cf96c7c2d87b2e8e4cf6", + .url = "git+https://github.com/andrewrk/SDL.git#35d2548e41d5c3ea1e6bd22631b1ff1d352fd174", + .hash = "SDL-2.32.6-JToi3zqUEgG6r4ADMT5x-s1d5htGsWVMCJorr3GWEvLw", }, .zigwin32 = .{ - .url = "https://github.com/marlersoft/zigwin32/archive/6777f1db221d0cb50322842f558f03e3c3a4099f.tar.gz", - .hash = "1220f8d5028adceac0c6fc8ef554d82efef49ca48913393e9c2f5d73a66b58c8aaad", - }, - .nanovg = .{ - .url = "https://github.com/fabioarnold/nanovg-zig/archive/b3396d4e65da5862c6ff088f64b3301aa50f3a9f.tar.gz", - .hash = "1220908cb7b3e907526744c1ab23d21d312a6062e3813dc1facc6f03980fdd3c04f6", + .url = "git+https://github.com/marlersoft/zigwin32.git#d21b419d808215e1f82605fdaddc49750bfa3bca", + .hash = "zigwin32-25.0.28-preview-AAAAAI0J-wP-8_KKo4Yjr33XLhVhlJmeagGUedByaOnX", }, }, } diff --git a/src/AboutDialogWidget.zig b/src/AboutDialogWidget.zig index 42ade12..9cd8372 100644 --- a/src/AboutDialogWidget.zig +++ b/src/AboutDialogWidget.zig @@ -69,7 +69,7 @@ fn onMouseDown(widget: *gui.Widget, mouse_event: *gui.MouseEvent) void { } fn onMouseMove(widget: *gui.Widget, mouse_event: *gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); _ = self; const link_itchio_rect = Rect(f32).make( link_itchio_bounds[0], @@ -88,7 +88,7 @@ fn onMouseMove(widget: *gui.Widget, mouse_event: *gui.MouseEvent) void { } fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); switch (event.key) { .Return, .Escape => self.close(), else => event.event.ignore(), @@ -97,7 +97,7 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { fn onCloseButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(Self, "widget", parent); + var self: *Self = @fieldParentPtr("widget", parent); self.close(); } } diff --git a/src/BlendModeWidget.zig b/src/BlendModeWidget.zig index 7b473a1..f409377 100644 --- a/src/BlendModeWidget.zig +++ b/src/BlendModeWidget.zig @@ -60,7 +60,7 @@ fn setActive(self: *Self, active: BlendMode) void { fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (!widget.enabled) return; if (event.button == .left) { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const point = Point(f32).make(event.x, event.y); for (self.rects, 0..) |rect, i| { if (rect.contains(point)) { @@ -72,7 +72,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.save(); diff --git a/src/CanvasWidget.zig b/src/CanvasWidget.zig index d79ab7b..28dd04d 100644 --- a/src/CanvasWidget.zig +++ b/src/CanvasWidget.zig @@ -851,14 +851,14 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document, vg: nvg) self.horizontal_scrollbar.onChangedFn = struct { fn changed(scrollbar: *gui.Scrollbar) void { - const canvas = @fieldParentPtr(Self, "widget", scrollbar.widget.parent.?); + const canvas: *Self = @fieldParentPtr("widget", scrollbar.widget.parent.?); const client_w = canvas.getClientRect().w; canvas.translation.x = @round(0.5 * client_w - scrollbar.value); } }.changed; self.vertical_scrollbar.onChangedFn = struct { fn changed(scrollbar: *gui.Scrollbar) void { - const canvas = @fieldParentPtr(Self, "widget", scrollbar.widget.parent.?); + const canvas: *Self = @fieldParentPtr("widget", scrollbar.widget.parent.?); const client_h = canvas.getClientRect().h; canvas.translation.y = @round(0.5 * client_h - scrollbar.value); } @@ -959,13 +959,13 @@ pub fn centerAndZoomDocument(self: *Self) void { fn onResize(widget: *gui.Widget, event: *const gui.ResizeEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.updateLayout(); self.updateScrollbars(); } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); // translate view if (event.isButtonPressed(.middle)) { @@ -988,7 +988,7 @@ fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { } fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); if (event.isButtonPressed(.middle)) { self.scroll_offset = self.toDocumentSpace(event.x, event.y); @@ -1005,7 +1005,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { } fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); if (event.button == .middle) { self.scroll_offset = null; @@ -1028,7 +1028,7 @@ fn onMouseWheel(widget: *gui.Widget, event: *const gui.MouseEvent) void { const up = event.wheel_y < 0; const down = event.wheel_y > 0; - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); if (event.isModifierPressed(.ctrl)) { if (up) { @@ -1052,20 +1052,20 @@ fn onMouseWheel(widget: *gui.Widget, event: *const gui.MouseEvent) void { } fn onTouchPan(widget: *gui.Widget, event: *const gui.TouchEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.setTranslation(self.translation.x + event.dx, self.translation.y + event.dy); self.updateToolMousePreview(event.x, event.y); } fn onTouchZoom(widget: *gui.Widget, event: *const gui.TouchEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const factor = 1.0 + event.zoom; self.zoom(factor, event.x, event.y); self.updateToolMousePreview(event.x, event.y); } fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.baseOnKeyDownFn(widget, event); if (event.event.is_accepted) return; switch (self.tool) { @@ -1077,7 +1077,7 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { } fn onKeyUp(widget: *gui.Widget, event: *gui.KeyEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); switch (self.tool) { .draw => self.draw_tool.onKeyUp(self, event), else => event.event.ignore(), @@ -1085,12 +1085,12 @@ fn onKeyUp(widget: *gui.Widget, event: *gui.KeyEvent) void { } fn onEnter(widget: *gui.Widget) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.hovered = true; } fn onLeave(widget: *gui.Widget) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.hovered = false; if (self.tool == .draw) self.draw_tool.onLeave(self); @@ -1177,7 +1177,7 @@ fn notifyColorPicked(self: *Self) void { } fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.save(); vg.scissor(rect.x, rect.y, rect.w, rect.h); @@ -1329,7 +1329,7 @@ fn drawSelection(self: Self, selection: Document.Selection, rect: Rect(f32), vg: fn updateStatusBar(self: Self) void { if (self.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); editor.tool_status_label.text = switch (self.tool) { .crop => self.crop_tool.getStatusText(editor.tool_text[0..]), @@ -1342,7 +1342,7 @@ fn updateStatusBar(self: Self) void { fn updateImageStatus(self: Self) void { if (self.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); editor.updateImageStatus(); } } diff --git a/src/ColorBitmap.zig b/src/ColorBitmap.zig index be602a4..7d78bec 100644 --- a/src/ColorBitmap.zig +++ b/src/ColorBitmap.zig @@ -6,6 +6,8 @@ const col = @import("color.zig"); const Color = col.Color; const IndexedBitmap = @import("IndexedBitmap.zig"); +const Point = @import("Point.zig"); + width: u32, height: u32, pixels: []u8, @@ -218,39 +220,39 @@ pub fn floodFill(self: ColorBitmap, allocator: Allocator, x: i32, y: i32, color: const old_color = self.getPixel(x, y) orelse return; if (col.eql(old_color, color)) return; - const start_coords = .{ .x = @as(u32, @intCast(x)), .y = @as(u32, @intCast(y)) }; + const start_coords: Point = .{ .x = @as(u32, @intCast(x)), .y = @as(u32, @intCast(y)) }; self.setPixelUnchecked(start_coords.x, start_coords.y, color); - var stack = std.ArrayList(struct { x: u32, y: u32 }).init(allocator); + var stack = std.ArrayList(Point).init(allocator); try stack.ensureTotalCapacity(self.width * self.height / 2); defer stack.deinit(); try stack.append(start_coords); while (stack.items.len > 0) { - const coords = stack.pop(); + const coords = stack.pop().?; if (coords.y > 0) { - const new_coords = .{ .x = coords.x, .y = coords.y - 1 }; + const new_coords: Point = .{ .x = coords.x, .y = coords.y - 1 }; if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) { self.setPixelUnchecked(new_coords.x, new_coords.y, color); stack.appendAssumeCapacity(new_coords); } } if (coords.y < self.height - 1) { - const new_coords = .{ .x = coords.x, .y = coords.y + 1 }; + const new_coords: Point = .{ .x = coords.x, .y = coords.y + 1 }; if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) { self.setPixelUnchecked(new_coords.x, new_coords.y, color); stack.appendAssumeCapacity(new_coords); } } if (coords.x > 0) { - const new_coords = .{ .x = coords.x - 1, .y = coords.y }; + const new_coords: Point = .{ .x = coords.x - 1, .y = coords.y }; if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) { self.setPixelUnchecked(new_coords.x, new_coords.y, color); stack.appendAssumeCapacity(new_coords); } } if (coords.x < self.width - 1) { - const new_coords = .{ .x = coords.x + 1, .y = coords.y }; + const new_coords: Point = .{ .x = coords.x + 1, .y = coords.y }; if (col.eql(self.getPixelUnchecked(new_coords.x, new_coords.y), old_color)) { self.setPixelUnchecked(new_coords.x, new_coords.y, color); stack.appendAssumeCapacity(new_coords); diff --git a/src/ColorForegroundBackgroundWidget.zig b/src/ColorForegroundBackgroundWidget.zig index fe15f59..4d606f4 100644 --- a/src/ColorForegroundBackgroundWidget.zig +++ b/src/ColorForegroundBackgroundWidget.zig @@ -103,7 +103,7 @@ pub fn setActiveRgba(self: *Self, color: []const u8) void { fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.button == .left) { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const point = Point(f32).make(event.x, event.y); for (self.rects, 0..) |rect, i| { if (rect.contains(point)) { @@ -116,7 +116,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.button == .left) { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const point = Point(f32).make(event.x, event.y); const swap_rect = Rect(f32).make(10, 42, 14, 14); if (swap_rect.contains(point)) { @@ -145,7 +145,7 @@ fn drawSwapArrows(vg: nvg) void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.save(); diff --git a/src/ColorPaletteWidget.zig b/src/ColorPaletteWidget.zig index 07a1ed0..550a16b 100644 --- a/src/ColorPaletteWidget.zig +++ b/src/ColorPaletteWidget.zig @@ -70,7 +70,7 @@ const pal_version = "0100"; const line_ending = "\r\n"; pub fn loadPalContents(self: *Self, contents: []const u8) !void { - var lines = std.mem.tokenize(u8, contents, line_ending); + var lines = std.mem.tokenizeAny(u8, contents, line_ending); const header = lines.next() orelse return error.UnexpectedEnd; if (!std.mem.eql(u8, header, pal_header)) return error.InvalidHeader; @@ -84,7 +84,7 @@ pub fn loadPalContents(self: *Self, contents: []const u8) !void { var i: usize = 0; while (i < count) : (i += 1) { const line = lines.next() orelse return error.UnexpectedEnd; - var components = std.mem.tokenize(u8, line, " "); + var components = std.mem.tokenizeAny(u8, line, " "); const red_string = components.next() orelse return error.UnexpectedEndOfLine; const green_string = components.next() orelse return error.UnexpectedEndOfLine; const blue_string = components.next() orelse return error.UnexpectedEndOfLine; @@ -121,7 +121,7 @@ pub fn writePal(self: *Self, filename: []const u8) !void { } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (event.isButtonPressed(.left) and self.selected != null) { const rect = widget.relative_rect; const ix = @as(u8, @intFromFloat(std.math.clamp(16 * event.x / rect.w, 0, 15))); @@ -133,7 +133,7 @@ fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.button == .left) { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; const ix = std.math.clamp(@as(u8, @intFromFloat(16 * event.x / rect.w)), 0, 15); const iy = std.math.clamp(@as(u8, @intFromFloat(16 * event.y / rect.h)), 0, 15); @@ -147,7 +147,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; gui.drawPanel(vg, rect.x, rect.y, rect.w, rect.h, 1, false, false); diff --git a/src/ColorPickerWidget.zig b/src/ColorPickerWidget.zig index c42f703..c3ef09f 100644 --- a/src/ColorPickerWidget.zig +++ b/src/ColorPickerWidget.zig @@ -78,7 +78,7 @@ fn SliderChangedFn(comptime color_index: comptime_int) type { return struct { fn changed(slider: *gui.Slider(f32)) void { if (slider.widget.parent) |parent| { - var picker = @fieldParentPtr(Self, "widget", parent); + var picker: *Self = @fieldParentPtr("widget", parent); const value = @as(u8, @intFromFloat(slider.value * 255.0)); if (picker.color[color_index] != value) { picker.color[color_index] = value; @@ -94,7 +94,7 @@ fn SpinnerChangedFn(comptime color_index: comptime_int) type { return struct { fn changed(spinner: *gui.Spinner(i32)) void { if (spinner.widget.parent) |parent| { - var picker = @fieldParentPtr(Self, "widget", parent); + var picker: *Self = @fieldParentPtr("widget", parent); if (picker.color[color_index] != spinner.value) { picker.color[color_index] = @as(u8, @intCast(spinner.value)); picker.sliders[color_index].setValue(@as(f32, @floatFromInt(spinner.value)) / 255.0); @@ -109,7 +109,7 @@ fn SliderDrawFn(comptime color_index: u2) type { return struct { fn draw(widget: *gui.Widget, vg: nvg) void { if (widget.parent) |parent| { - const picker = @fieldParentPtr(Self, "widget", parent); + const picker: *Self = @fieldParentPtr("widget", parent); const rect = widget.relative_rect; diff --git a/src/EditorWidget.zig b/src/EditorWidget.zig index e1d51c8..5f29b82 100644 --- a/src/EditorWidget.zig +++ b/src/EditorWidget.zig @@ -216,7 +216,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.color_palette.onSelectionChangedFn = struct { fn selectionChanged(color_palette: *ColorPaletteWidget) void { if (color_palette.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); if (color_palette.selected) |selected| { const color = color_palette.colors[4 * selected ..][0..4]; switch (editor.document.getBitmapType()) { @@ -243,7 +243,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.canvas.onColorPickedFn = struct { fn colorPicked(canvas: *CanvasWidget) void { if (canvas.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); switch (editor.document.getBitmapType()) { .color => editor.color_foreground_background.setRgba(.foreground, &editor.document.foreground_color), .indexed => { @@ -260,7 +260,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.canvas.onScaleChangedFn = struct { fn zoomChanged(canvas: *CanvasWidget, zoom: f32) void { if (canvas.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); editor.zoom_spinner.setValue(zoom); } } @@ -269,7 +269,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.color_picker.onChangedFn = struct { fn changed(color_picker: *ColorPickerWidget) void { if (color_picker.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); if (editor.color_palette.selected) |selected| { @memcpy(editor.color_palette.colors[4 * selected ..][0..4], &color_picker.color); editor.updateDocumentPaletteAt(selected); @@ -286,7 +286,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.color_foreground_background.onChangedFn = struct { fn changed(color_foreground_background: *ColorForegroundBackgroundWidget, change_type: ColorForegroundBackgroundWidget.ChangeType) void { if (color_foreground_background.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); const color = color_foreground_background.getActiveRgba(); switch (editor.document.getBitmapType()) { .color => { @@ -320,7 +320,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.blend_mode.onChangedFn = struct { fn changed(blend_mode: *BlendModeWidget) void { if (blend_mode.widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); editor.document.blend_mode = blend_mode.active; } } @@ -330,7 +330,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), vg: nvg) !*Self { self.timeline.widget.onResizeFn = struct { fn onResize(widget: *gui.Widget, event: *gui.ResizeEvent) void { if (widget.parent) |parent| { - const editor = @fieldParentPtr(Self, "widget", parent); + const editor: *Self = @fieldParentPtr("widget", parent); editor.timeline_on_resize_base_fn(widget, event); editor.updateLayout(); } @@ -408,7 +408,7 @@ fn initMenubar(self: *Self) !void { fn changed(spinner: *gui.Spinner(i32)) void { if (spinner.widget.parent) |menu_bar_widget| { if (menu_bar_widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); editor.canvas.custom_grid_spacing_x = @as(u32, @intCast(spinner.value)); } } @@ -423,7 +423,7 @@ fn initMenubar(self: *Self) !void { fn changed(spinner: *gui.Spinner(i32)) void { if (spinner.widget.parent) |menu_bar_widget| { if (menu_bar_widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); editor.canvas.custom_grid_spacing_y = @as(u32, @intCast(spinner.value)); } } @@ -439,7 +439,7 @@ fn initMenubar(self: *Self) !void { fn changed(spinner: *gui.Spinner(f32)) void { if (spinner.widget.parent) |menu_bar_widget| { if (menu_bar_widget.parent) |parent| { - var editor = @fieldParentPtr(EditorWidget, "widget", parent); + var editor: *EditorWidget = @fieldParentPtr("widget", parent); const factor = spinner.value / editor.canvas.scale; editor.canvas.zoomToDocumentCenter(factor); } @@ -547,12 +547,12 @@ pub fn deinit(self: *Self, vg: nvg) void { fn onResize(widget: *gui.Widget, event: *const gui.ResizeEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.updateLayout(); } fn onKeyDown(widget: *gui.Widget, key_event: *gui.KeyEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const shift_held = key_event.isModifierPressed(.shift); if (key_event.isModifierPressed(.ctrl)) { switch (key_event.key) { @@ -616,7 +616,7 @@ pub fn onUndoChanged(self: *Self, document: *Document) void { } fn onClipboardUpdate(widget: *gui.Widget) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.checkClipboard(); } @@ -1171,7 +1171,8 @@ fn copyWithExtension(allocator: Allocator, filename: []const u8, extension: []co fn getEditorFromMenuButton(menu_button: *gui.Button) *Self { if (menu_button.widget.parent) |menu_bar_widget| { if (menu_bar_widget.parent) |parent| { - return @fieldParentPtr(EditorWidget, "widget", parent); + const self: *EditorWidget = @fieldParentPtr("widget", parent); + return self; } } unreachable; // forgot to add button to the menu_bar diff --git a/src/IndexedBitmap.zig b/src/IndexedBitmap.zig index 7f8ca0c..8ed63cc 100644 --- a/src/IndexedBitmap.zig +++ b/src/IndexedBitmap.zig @@ -4,6 +4,8 @@ const testing = std.testing; const ColorBitmap = @import("ColorBitmap.zig"); +const Point = @import("Point.zig"); + width: u32, height: u32, indices: []u8, @@ -160,39 +162,39 @@ pub fn floodFill(self: IndexedBitmap, allocator: Allocator, x: i32, y: i32, inde const old_index = self.getIndex(x, y) orelse return; if (old_index == index) return; - const start_coords = .{ .x = @as(u32, @intCast(x)), .y = @as(u32, @intCast(y)) }; + const start_coords: Point = .{ .x = @as(u32, @intCast(x)), .y = @as(u32, @intCast(y)) }; self.setIndexUnchecked(start_coords.x, start_coords.y, index); - var stack = std.ArrayList(struct { x: u32, y: u32 }).init(allocator); + var stack = std.ArrayList(Point).init(allocator); try stack.ensureTotalCapacity(self.width * self.height / 2); defer stack.deinit(); try stack.append(start_coords); while (stack.items.len > 0) { - const coords = stack.pop(); + const coords = stack.pop().?; if (coords.y > 0) { - const new_coords = .{ .x = coords.x, .y = coords.y - 1 }; + const new_coords: Point = .{ .x = coords.x, .y = coords.y - 1 }; if (self.getIndexUnchecked(new_coords.x, new_coords.y) == old_index) { self.setIndexUnchecked(new_coords.x, new_coords.y, index); stack.appendAssumeCapacity(new_coords); } } if (coords.y < self.height - 1) { - const new_coords = .{ .x = coords.x, .y = coords.y + 1 }; + const new_coords: Point = .{ .x = coords.x, .y = coords.y + 1 }; if (self.getIndexUnchecked(new_coords.x, new_coords.y) == old_index) { self.setIndexUnchecked(new_coords.x, new_coords.y, index); stack.appendAssumeCapacity(new_coords); } } if (coords.x > 0) { - const new_coords = .{ .x = coords.x - 1, .y = coords.y }; + const new_coords: Point = .{ .x = coords.x - 1, .y = coords.y }; if (self.getIndexUnchecked(new_coords.x, new_coords.y) == old_index) { self.setIndexUnchecked(new_coords.x, new_coords.y, index); stack.appendAssumeCapacity(new_coords); } } if (coords.x < self.width - 1) { - const new_coords = .{ .x = coords.x + 1, .y = coords.y }; + const new_coords: Point = .{ .x = coords.x + 1, .y = coords.y }; if (self.getIndexUnchecked(new_coords.x, new_coords.y) == old_index) { self.setIndexUnchecked(new_coords.x, new_coords.y, index); stack.appendAssumeCapacity(new_coords); diff --git a/src/LayerListWidget.zig b/src/LayerListWidget.zig index 8f566c5..7cb7621 100644 --- a/src/LayerListWidget.zig +++ b/src/LayerListWidget.zig @@ -101,21 +101,21 @@ pub fn onDocumentChanged(self: *Self) void { } fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (event.button == .left) { self.selectFrameAndLayer(event.x, event.y); } } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (event.isButtonPressed(.left)) { self.selectFrameAndLayer(event.x, event.y); } } fn onVisibleButtonClicked(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); const layer_count = self.document.getLayerCount(); var all_visible: bool = true; var i: u32 = 0; @@ -133,7 +133,7 @@ fn onVisibleButtonClicked(button: *gui.Button) void { } fn onLockButtonClicked(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); const layer_count = self.document.getLayerCount(); var all_unlocked: bool = true; var i: u32 = 0; @@ -151,7 +151,7 @@ fn onLockButtonClicked(button: *gui.Button) void { } fn onLinkButtonClicked(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); const layer_count = self.document.getLayerCount(); var all_linked: bool = true; var i: u32 = 0; @@ -224,7 +224,7 @@ fn selectFrameAndLayer(self: *Self, mouse_x: f32, mouse_y: f32) void { } fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.beginPath(); diff --git a/src/LayerWidget.zig b/src/LayerWidget.zig index 9c67fd8..1691603 100644 --- a/src/LayerWidget.zig +++ b/src/LayerWidget.zig @@ -65,25 +65,25 @@ pub fn deinit(self: *Self) void { } pub fn onVisibleButtonClicked(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); const visible = self.document.isLayerVisible(self.layer_index); self.document.setLayerVisible(self.layer_index, !visible); - const layer_list = @fieldParentPtr(LayerListWidget, "widget", self.widget.parent.?); + const layer_list: *LayerListWidget = @fieldParentPtr("widget", self.widget.parent.?); layer_list.updateVisibleButtons(); } pub fn onLockButtonClicked(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); const locked = self.document.isLayerLocked(self.layer_index); self.document.setLayerLocked(self.layer_index, !locked); - const layer_list = @fieldParentPtr(LayerListWidget, "widget", self.widget.parent.?); + const layer_list: *LayerListWidget = @fieldParentPtr("widget", self.widget.parent.?); layer_list.updateLockButtons(); } pub fn onLinkButtonClicked(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); const linked = self.document.isLayerLinked(self.layer_index); self.document.setLayerLinked(self.layer_index, !linked); - const layer_list = @fieldParentPtr(LayerListWidget, "widget", self.widget.parent.?); + const layer_list: *LayerListWidget = @fieldParentPtr("widget", self.widget.parent.?); layer_list.updateLinkButtons(); } diff --git a/src/MessageBoxWidget.zig b/src/MessageBoxWidget.zig index bde91b7..e55ac64 100644 --- a/src/MessageBoxWidget.zig +++ b/src/MessageBoxWidget.zig @@ -133,7 +133,7 @@ pub fn configure(self: *MessageBoxWidget, icon: Icon, buttons: Buttons, message: fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { widget.onKeyDown(event); - var self = @fieldParentPtr(MessageBoxWidget, "widget", widget); + var self: *MessageBoxWidget = @fieldParentPtr("widget", widget); switch (event.key) { .Return => self.setResult(if (self.ok_button.widget.visible) .ok else .yes), .Escape => self.setResult(if (self.cancel_button.widget.visible) .cancel else .none), @@ -143,28 +143,28 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { fn onOkButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(MessageBoxWidget, "widget", parent); + var self: *MessageBoxWidget = @fieldParentPtr("widget", parent); self.setResult(.ok); } } fn onCancelButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(MessageBoxWidget, "widget", parent); + var self: *MessageBoxWidget = @fieldParentPtr("widget", parent); self.setResult(.cancel); } } fn onYesButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(MessageBoxWidget, "widget", parent); + var self: *MessageBoxWidget = @fieldParentPtr("widget", parent); self.setResult(.yes); } } fn onNoButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(MessageBoxWidget, "widget", parent); + var self: *MessageBoxWidget = @fieldParentPtr("widget", parent); self.setResult(.no); } } @@ -246,7 +246,7 @@ fn drawQuestionIcon(vg: nvg, x: f32, y: f32) void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - var self = @fieldParentPtr(MessageBoxWidget, "widget", widget); + var self: *MessageBoxWidget = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.beginPath(); diff --git a/src/NewDocumentWidget.zig b/src/NewDocumentWidget.zig index 892d2e7..d87a5e8 100644 --- a/src/NewDocumentWidget.zig +++ b/src/NewDocumentWidget.zig @@ -89,7 +89,7 @@ pub fn deinit(self: *Self) void { } fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); switch (event.key) { .Return => self.accept(), .Escape => self.cancel(), @@ -99,7 +99,7 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { fn onIndexedRadioClick(radio: *gui.RadioButton) void { if (radio.widget.parent) |parent| { - var self = @fieldParentPtr(Self, "widget", parent); + var self: *Self = @fieldParentPtr("widget", parent); self.indexed_radio.checked = true; self.truecolor_radio.checked = false; } @@ -107,7 +107,7 @@ fn onIndexedRadioClick(radio: *gui.RadioButton) void { fn onTruecolorRadioClick(radio: *gui.RadioButton) void { if (radio.widget.parent) |parent| { - var self = @fieldParentPtr(Self, "widget", parent); + var self: *Self = @fieldParentPtr("widget", parent); self.indexed_radio.checked = false; self.truecolor_radio.checked = true; } @@ -115,14 +115,14 @@ fn onTruecolorRadioClick(radio: *gui.RadioButton) void { fn onOkButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(Self, "widget", parent); + var self: *Self = @fieldParentPtr("widget", parent); self.accept(); } } fn onCancelButtonClick(button: *gui.Button) void { if (button.widget.parent) |parent| { - var self = @fieldParentPtr(Self, "widget", parent); + var self: *Self = @fieldParentPtr("widget", parent); self.cancel(); } } diff --git a/src/Point.zig b/src/Point.zig new file mode 100644 index 0000000..f05ef61 --- /dev/null +++ b/src/Point.zig @@ -0,0 +1,4 @@ +x: u32, +y: u32, + +const Self = @This(); diff --git a/src/PreviewWidget.zig b/src/PreviewWidget.zig index c89d8f5..f068e9f 100644 --- a/src/PreviewWidget.zig +++ b/src/PreviewWidget.zig @@ -48,25 +48,25 @@ pub fn deinit(self: *Self, vg: nvg) void { } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); if (self.drag_offset) |drag_offset| { self.translation = Point(f32).make(event.x, event.y).subtracted(drag_offset); } } fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.drag_offset = Point(f32).make(event.x, event.y).subtracted(self.translation); } fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { _ = event; // unused - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.drag_offset = null; } pub fn draw(widget: *gui.Widget, vg: nvg) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.save(); diff --git a/src/TimelineWidget.zig b/src/TimelineWidget.zig index 9d4ebf0..3a58085 100644 --- a/src/TimelineWidget.zig +++ b/src/TimelineWidget.zig @@ -71,7 +71,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.begin_button.icon_y = 4; self.begin_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.gotoFirstFrame(); } }.click; @@ -80,7 +80,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.left_button.icon_y = 4; self.left_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.gotoPrevFrame(); } }.click; @@ -89,7 +89,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.play_button.icon_y = 4; self.play_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.togglePlayback(); } }.click; @@ -98,7 +98,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.right_button.icon_y = 4; self.right_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.gotoNextFrame(); } }.click; @@ -107,7 +107,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.end_button.icon_y = 4; self.end_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.gotoLastFrame(); } }.click; @@ -117,7 +117,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.add_frame_button.icon_y = 3; self.add_frame_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.newFrame(); } }.click; @@ -126,7 +126,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.delete_frame_button.icon_y = 3; self.delete_frame_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.deleteFrame(timeline.document.selected_frame) catch {}; // TODO: handle? } }.click; @@ -135,7 +135,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.add_layer_button.icon_y = 3; self.add_layer_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.addLayer() catch {}; // TODO: handle? timeline.document.selectLayer(timeline.document.getLayerCount() - 1); } @@ -145,7 +145,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.delete_layer_button.icon_y = 3; self.delete_layer_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.deleteLayer(timeline.document.selected_layer) catch {}; // TODO: handle? } }.click; @@ -155,7 +155,7 @@ pub fn init(allocator: Allocator, rect: Rect(f32), document: *Document) !*Self { self.onion_skinning_button.iconFn = icons.iconOnionSkinning; self.onion_skinning_button.onClickFn = struct { fn click(button: *gui.Button) void { - const timeline = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const timeline: *Self = @fieldParentPtr("widget", button.widget.parent.?); timeline.document.onion_skinning = !timeline.document.onion_skinning; } }.click; @@ -204,21 +204,21 @@ pub fn onDocumentChanged(self: *Self) void { } fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (event.button == .left and event.y < header_h) { self.drag_y = event.y; } } fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (event.button == .left) { self.drag_y = null; } } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (event.isButtonPressed(.left)) { if (self.drag_y) |drag_y| { const min_y = 16 + 24; @@ -252,7 +252,7 @@ fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { } fn onResize(widget: *gui.Widget, event: *const gui.ResizeEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.layer_list_widget.widget.setSize(event.new_width - 2 * padding, event.new_height - header_h - padding); } diff --git a/src/automated_testing.zig b/src/automated_testing.zig index 5fa17f9..4850e7a 100644 --- a/src/automated_testing.zig +++ b/src/automated_testing.zig @@ -4,7 +4,7 @@ const gui = @import("gui"); const EditorWidget = @import("EditorWidget.zig"); pub fn runTests(window: *gui.Window) !void { - const editor_widget = @fieldParentPtr(EditorWidget, "widget", window.main_widget.?); + const editor_widget: *EditorWidget = @fieldParentPtr("widget", window.main_widget.?); try testFloodFill(window, editor_widget); try testSetIndividualPixels(window, editor_widget); } diff --git a/src/c.zig b/src/c.zig index c4233a3..1d1c294 100644 --- a/src/c.zig +++ b/src/c.zig @@ -1,9 +1,5 @@ -const builtin = @import("builtin"); - -const sdl2_include_prefix = if (builtin.os.tag == .windows or builtin.os.tag == .macos) "SDL2/" else ""; - pub usingnamespace @cImport({ - @cInclude(sdl2_include_prefix ++ "SDL.h"); - @cInclude(sdl2_include_prefix ++ "SDL_opengl.h"); - @cInclude(sdl2_include_prefix ++ "SDL_syswm.h"); + @cInclude("SDL2/SDL.h"); + @cInclude("SDL2/SDL_opengl.h"); + @cInclude("SDL2/SDL_syswm.h"); }); diff --git a/src/gui/Widget.zig b/src/gui/Widget.zig index cce2553..885f06f 100644 --- a/src/gui/Widget.zig +++ b/src/gui/Widget.zig @@ -263,34 +263,67 @@ pub fn dispatchEvent(self: *Self, e: *event.Event) void { } pub fn handleEvent(self: *Self, e: *event.Event) void { - const resize_event = @fieldParentPtr(event.ResizeEvent, "event", e); - const mouse_event = @fieldParentPtr(event.MouseEvent, "event", e); - const touch_event = @fieldParentPtr(event.TouchEvent, "event", e); - const key_event = @fieldParentPtr(event.KeyEvent, "event", e); - const text_input_event = @fieldParentPtr(event.TextInputEvent, "event", e); - const focus_event = @fieldParentPtr(event.FocusEvent, "event", e); - switch (e.type) { - .Resize => self.onResizeFn(self, resize_event), - .MouseMove => self.onMouseMoveFn(self, mouse_event), + .Resize => { + const resize_event: *event.ResizeEvent = @alignCast(@fieldParentPtr("event", e)); + self.onResizeFn(self, resize_event); + }, + .MouseMove => { + const mouse_event: *event.MouseEvent = @alignCast(@fieldParentPtr("event", e)); + self.onMouseMoveFn(self, mouse_event); + }, .MouseDown => { if (self.acceptsFocus(.mouse)) { self.setFocus(true, .mouse); } + const mouse_event: *event.MouseEvent = @alignCast(@fieldParentPtr("event", e)); self.onMouseDownFn(self, mouse_event); }, - .MouseUp => self.onMouseUpFn(self, mouse_event), - .MouseWheel => self.onMouseWheelFn(self, mouse_event), - .TouchPan => self.onTouchPanFn(self, touch_event), - .TouchZoom => self.onTouchZoomFn(self, touch_event), - .KeyDown => self.onKeyDownFn(self, key_event), - .KeyUp => self.onKeyUpFn(self, key_event), - .TextInput => self.onTextInputFn(self, text_input_event), - .Focus => self.onFocusFn(self, focus_event), - .Blur => self.onBlurFn(self, focus_event), - .Enter => self.onEnterFn(self), - .Leave => self.onLeaveFn(self), - .ClipboardUpdate => self.onClipboardUpdateFn(self), + .MouseUp => { + const mouse_event: *event.MouseEvent = @alignCast(@fieldParentPtr("event", e)); + self.onMouseUpFn(self, mouse_event); + }, + .MouseWheel => { + const mouse_event: *event.MouseEvent = @alignCast(@fieldParentPtr("event", e)); + self.onMouseWheelFn(self, mouse_event); + }, + .TouchPan => { + const touch_event: *event.TouchEvent = @alignCast(@fieldParentPtr("event", e)); + self.onTouchPanFn(self, touch_event); + }, + .TouchZoom => { + const touch_event: *event.TouchEvent = @alignCast(@fieldParentPtr("event", e)); + self.onTouchZoomFn(self, touch_event); + }, + .KeyDown => { + const key_event: *event.KeyEvent = @alignCast(@fieldParentPtr("event", e)); + self.onKeyDownFn(self, key_event); + }, + .KeyUp => { + const key_event: *event.KeyEvent = @alignCast(@fieldParentPtr("event", e)); + self.onKeyUpFn(self, key_event); + }, + .TextInput => { + const text_input_event: *event.TextInputEvent = @alignCast(@fieldParentPtr("event", e)); + self.onTextInputFn(self, text_input_event); + }, + .Focus => { + const focus_event: *event.FocusEvent = @alignCast(@fieldParentPtr("event", e)); + self.onFocusFn(self, focus_event); + }, + .Blur => { + const focus_event: *event.FocusEvent = @alignCast(@fieldParentPtr("event", e)); + self.onBlurFn(self, focus_event); + }, + .Enter => { + self.onEnterFn(self); + }, + .Leave => { + self.onLeaveFn(self); + }, + .ClipboardUpdate => { + self.onClipboardUpdateFn(self); + }, } } diff --git a/src/gui/Window.zig b/src/gui/Window.zig index 755b44c..5ddf544 100644 --- a/src/gui/Window.zig +++ b/src/gui/Window.zig @@ -152,11 +152,15 @@ pub fn handleEvent(self: *Self, e: *event.Event) void { if (e.type != .Leave) return; } - const mouse_event = @fieldParentPtr(event.MouseEvent, "event", e); - const touch_event = @fieldParentPtr(event.TouchEvent, "event", e); switch (e.type) { - .MouseMove, .MouseDown, .MouseUp, .MouseWheel => self.handleMouseEvent(mouse_event), - .TouchPan, .TouchZoom => self.handleTouchEvent(touch_event), + .MouseMove, .MouseDown, .MouseUp, .MouseWheel => { + const mouse_event: *event.MouseEvent = @alignCast(@fieldParentPtr("event", e)); + self.handleMouseEvent(mouse_event); + }, + .TouchPan, .TouchZoom => { + const touch_event: *event.TouchEvent = @alignCast(@fieldParentPtr("event", e)); + self.handleTouchEvent(touch_event); + }, .KeyDown, .KeyUp, .TextInput => self.handleKeyEvent(e), .Enter => self.setHoveredWidget(self.main_widget), .Leave => self.setHoveredWidget(null), diff --git a/src/gui/widgets/Button.zig b/src/gui/widgets/Button.zig index 3936da4..fa3bc51 100644 --- a/src/gui/widgets/Button.zig +++ b/src/gui/widgets/Button.zig @@ -78,7 +78,7 @@ fn click(self: *Self) void { pub fn onMouseDown(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void { if (!widget.isEnabled()) return; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const mouse_position = Point(f32).make(mouse_event.x, mouse_event.y); self.hovered = widget.getRect().contains(mouse_position); if (mouse_event.button == .left) { @@ -94,7 +94,7 @@ pub fn onMouseDown(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void fn onMouseUp(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void { if (!widget.isEnabled()) return; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const mouse_position = Point(f32).make(mouse_event.x, mouse_event.y); self.hovered = widget.getRect().contains(mouse_position); if (mouse_event.button == .left) { @@ -108,14 +108,14 @@ fn onMouseUp(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void { fn onKeyDown(widget: *gui.Widget, key_event: *gui.KeyEvent) void { widget.onKeyDown(key_event); - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (key_event.key == .Space) { self.pressed = true; } } fn onKeyUp(widget: *gui.Widget, key_event: *gui.KeyEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (key_event.key == .Space and self.pressed) { self.pressed = false; self.click(); @@ -123,23 +123,23 @@ fn onKeyUp(widget: *gui.Widget, key_event: *gui.KeyEvent) void { } fn onFocus(widget: *gui.Widget, focus_event: *gui.FocusEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.focused = focus_event.source == .keyboard; } fn onBlur(widget: *gui.Widget, _: *gui.FocusEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.pressed = false; } fn onEnter(widget: *gui.Widget) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.hovered = true; if (self.onEnterFn) |enterFn| enterFn(self); } fn onLeave(widget: *gui.Widget) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.hovered = false; if (self.onLeaveFn) |leaveFn| leaveFn(self); } @@ -152,7 +152,7 @@ fn onAutoRepeatTimerElapsed(context: usize) void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.save(); diff --git a/src/gui/widgets/Label.zig b/src/gui/widgets/Label.zig index 5fcdb42..9a267ff 100644 --- a/src/gui/widgets/Label.zig +++ b/src/gui/widgets/Label.zig @@ -32,7 +32,7 @@ pub fn deinit(self: *Self) void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; vg.save(); diff --git a/src/gui/widgets/ListView.zig b/src/gui/widgets/ListView.zig index 42c5f73..273d2a1 100644 --- a/src/gui/widgets/ListView.zig +++ b/src/gui/widgets/ListView.zig @@ -61,7 +61,7 @@ pub fn deinit(self: *Self) void { fn onResize(widget: *gui.Widget, event: *const gui.ResizeEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.updateLayout(); } @@ -81,13 +81,13 @@ fn updateLayout(self: *Self) void { fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); _ = self; } fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.button == .left) { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); //const rect = widget.getRect(); const i = @as(usize, @intFromFloat(event.y / item_h)); self.model.selectFn(self.model.ctx, i); @@ -96,12 +96,12 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); _ = self; } fn draw(widget: *gui.Widget) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; diff --git a/src/gui/widgets/RadioButton.zig b/src/gui/widgets/RadioButton.zig index 4baac8c..6371c9c 100644 --- a/src/gui/widgets/RadioButton.zig +++ b/src/gui/widgets/RadioButton.zig @@ -54,7 +54,7 @@ fn click(self: *RadioButton) void { pub fn onMouseDown(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void { if (!widget.isEnabled()) return; - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); const mouse_position = Point(f32).make(mouse_event.x, mouse_event.y); self.hovered = widget.getRect().contains(mouse_position); if (mouse_event.button == .left) { @@ -66,7 +66,7 @@ pub fn onMouseDown(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void fn onMouseUp(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void { if (!widget.isEnabled()) return; - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); const mouse_position = Point(f32).make(mouse_event.x, mouse_event.y); self.hovered = widget.getRect().contains(mouse_position); if (mouse_event.button == .left) { @@ -79,29 +79,29 @@ fn onMouseUp(widget: *gui.Widget, mouse_event: *const gui.MouseEvent) void { fn onKeyDown(widget: *gui.Widget, key_event: *gui.KeyEvent) void { widget.onKeyDown(key_event); - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); if (key_event.key == .Space) { self.click(); } } fn onFocus(widget: *gui.Widget, focus_event: *gui.FocusEvent) void { - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); self.focused = focus_event.source == .keyboard; } fn onEnter(widget: *gui.Widget) void { - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); self.hovered = true; } fn onLeave(widget: *gui.Widget) void { - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); self.hovered = false; } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(RadioButton, "widget", widget); + const self: *RadioButton = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; // const enabled = widget.isEnabled(); // TODO diff --git a/src/gui/widgets/Scrollbar.zig b/src/gui/widgets/Scrollbar.zig index eb6b069..533a7d2 100644 --- a/src/gui/widgets/Scrollbar.zig +++ b/src/gui/widgets/Scrollbar.zig @@ -101,12 +101,12 @@ pub fn setMaxValue(self: *Self, max_value: f32) void { } fn decrementClick(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); self.setValue(self.value - scroll_speed); } fn incrementClick(button: *gui.Button) void { - const self = @fieldParentPtr(Self, "widget", button.widget.parent.?); + const self: *Self = @fieldParentPtr("widget", button.widget.parent.?); self.setValue(self.value + scroll_speed); } @@ -148,13 +148,13 @@ fn updateThumbSize(self: *Self) void { fn thumbMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { gui.Button.onMouseDown(widget, event); - const self = @fieldParentPtr(Self, "widget", widget.parent.?); + const self: *Self = @fieldParentPtr("widget", widget.parent.?); self.mouse_offset = if (self.orientation == .vertical) event.y else event.x; } fn thumbMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.isButtonPressed(.left)) { - const self = @fieldParentPtr(Self, "widget", widget.parent.?); + const self: *Self = @fieldParentPtr("widget", widget.parent.?); if (self.orientation == .vertical) { const y = widget.relative_rect.y + event.y - self.mouse_offset; const max_y = self.widget.relative_rect.h - (button_size - 1) - widget.relative_rect.h; @@ -175,7 +175,7 @@ fn thumbMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { fn onResize(widget: *gui.Widget, event: *const gui.ResizeEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.updateLayout(); } @@ -209,13 +209,13 @@ fn updateLayout(self: *Self) void { } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); _ = self; _ = event; } fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.getRect(); _ = self; _ = rect; @@ -223,7 +223,7 @@ fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { } fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); _ = self; _ = event; } diff --git a/src/gui/widgets/Slider.zig b/src/gui/widgets/Slider.zig index 29d063b..6620b63 100644 --- a/src/gui/widgets/Slider.zig +++ b/src/gui/widgets/Slider.zig @@ -45,7 +45,7 @@ pub fn Slider(comptime T: type) type { } fn onMouseMove(widget: *gui.Widget, event: *const gui.MouseEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); if (self.pressed) { const rect = widget.getRect(); @@ -57,7 +57,7 @@ pub fn Slider(comptime T: type) type { fn onMouseDown(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.button == .left) { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.pressed = true; const rect = widget.getRect(); @@ -69,7 +69,7 @@ pub fn Slider(comptime T: type) type { fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (event.button == .left) { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.pressed = false; } } @@ -79,7 +79,7 @@ pub fn Slider(comptime T: type) type { } fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; gui.drawPanelInset(vg, rect.x, rect.y + 0.5 * rect.h - 1, rect.w, 2, 1); diff --git a/src/gui/widgets/Spinner.zig b/src/gui/widgets/Spinner.zig index 5de1903..cdfb0ff 100644 --- a/src/gui/widgets/Spinner.zig +++ b/src/gui/widgets/Spinner.zig @@ -55,7 +55,7 @@ pub fn Spinner(comptime T: type) type { fn changed(text_box: *gui.TextBox) void { const error_color = nvg.rgbf(1, 0.82, 0.8); if (text_box.widget.parent) |parent| { - const spinner = @fieldParentPtr(Spinner(T), "widget", parent); + const spinner: *Spinner(T) = @fieldParentPtr("widget", parent); if (text_box.text.items.len > 0) { const text = text_box.text.items; if (switch (T) { @@ -82,7 +82,7 @@ pub fn Spinner(comptime T: type) type { self.text_box.widget.onBlurFn = struct { fn blur(widget: *gui.Widget, event: *gui.FocusEvent) void { if (widget.parent) |parent| { - const spinner = @fieldParentPtr(Spinner(T), "widget", parent); + const spinner: *Spinner(T) = @fieldParentPtr("widget", parent); spinner.up_button.pressed = false; spinner.down_button.pressed = false; spinner.baseTextBoxBlurFn(widget, event); @@ -96,7 +96,7 @@ pub fn Spinner(comptime T: type) type { self.up_button.onClickFn = struct { fn click(button: *gui.Button) void { if (button.widget.parent) |parent| { - const spinner = @fieldParentPtr(Spinner(T), "widget", parent); + const spinner: *Spinner(T) = @fieldParentPtr("widget", parent); spinner.increment(); spinner.updateTextBox(); // ignore focus } @@ -108,7 +108,7 @@ pub fn Spinner(comptime T: type) type { self.down_button.onClickFn = struct { fn click(button: *gui.Button) void { if (button.widget.parent) |parent| { - const spinner = @fieldParentPtr(Spinner(T), "widget", parent); + const spinner: *Spinner(T) = @fieldParentPtr("widget", parent); spinner.decrement(); spinner.updateTextBox(); // ignore focus } @@ -132,12 +132,12 @@ pub fn Spinner(comptime T: type) type { fn onResize(widget: *gui.Widget, event: *gui.ResizeEvent) void { _ = event; - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.updateLayout(); } fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); switch (event.key) { .Up => { self.up_button.pressed = true; @@ -154,7 +154,7 @@ pub fn Spinner(comptime T: type) type { } fn onKeyUp(widget: *gui.Widget, event: *gui.KeyEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); switch (event.key) { .Up => self.up_button.pressed = false, .Down => self.down_button.pressed = false, @@ -208,18 +208,28 @@ pub fn Spinner(comptime T: type) type { } fn updateTextBox(self: *Self) void { - var buf: [50]u8 = undefined; - var fbs = std.io.fixedBufferStream(&buf); + var buf: [64]u8 = undefined; switch (T) { - i32 => std.fmt.formatInt(self.value, 10, .lower, .{}, fbs.writer()) catch unreachable, - f32 => std.fmt.formatFloatDecimal(self.value, .{ .precision = 2 }, fbs.writer()) catch unreachable, + i32 => { + var fbs = std.io.fixedBufferStream(&buf); + std.fmt.formatInt(self.value, 10, .lower, .{}, fbs.writer()) catch unreachable; + self.text_box.setText(buf[0..fbs.pos]) catch {}; // TODO + }, + f32 => { + const out = std.fmt.formatFloat( + &buf, + self.value, + .{ .precision = 2, .mode = .decimal }, + ) catch unreachable; + var pos = out.len; + if (std.mem.indexOfScalar(u8, buf[0..pos], '.')) |dec| { // trim zeroes + while (buf[pos - 1] == '0') pos -= 1; + if (pos - 1 == dec) pos -= 1; + } + self.text_box.setText(buf[0..pos]) catch {}; // TODO + }, else => unreachable, } - if (std.mem.indexOfScalar(u8, buf[0..fbs.pos], '.')) |dec| { // trim zeroes - while (buf[fbs.pos - 1] == '0') fbs.pos -= 1; - if (fbs.pos - 1 == dec) fbs.pos -= 1; - } - self.text_box.setText(buf[0..fbs.pos]) catch {}; // TODO } fn notifyChanged(self: *Self) void { diff --git a/src/gui/widgets/TextBox.zig b/src/gui/widgets/TextBox.zig index f3ba707..1df634a 100644 --- a/src/gui/widgets/TextBox.zig +++ b/src/gui/widgets/TextBox.zig @@ -88,7 +88,7 @@ fn getCursorPositionFromMousePosition(self: Self, mouse_position_x: f32) usize { fn onMouseMove(widget: *gui.Widget, event: *gui.MouseEvent) void { if (!widget.isEnabled()) return; - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const mouse_position = Point(f32).make(event.x, event.y); @@ -107,7 +107,7 @@ fn onMouseMove(widget: *gui.Widget, event: *gui.MouseEvent) void { fn onMouseDown(widget: *gui.Widget, event: *gui.MouseEvent) void { if (!widget.isEnabled()) return; - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); const mouse_position = Point(f32).make(event.x, event.y); @@ -127,7 +127,7 @@ fn onMouseDown(widget: *gui.Widget, event: *gui.MouseEvent) void { fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { if (!widget.isEnabled()) return; _ = event; - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); if (self.hovered) { self.blink = true; @@ -136,30 +136,30 @@ fn onMouseUp(widget: *gui.Widget, event: *const gui.MouseEvent) void { } fn onEnter(widget: *gui.Widget) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.hovered = true; } fn onLeave(widget: *gui.Widget) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.hovered = false; self.show_cursor_position_preview = false; } fn onFocus(widget: *gui.Widget, event: *gui.FocusEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.blink_timer.start(blink_interval); if (event.source == .keyboard) self.selectAll(); } fn onBlur(widget: *gui.Widget, event: *gui.FocusEvent) void { _ = event; - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.blink_timer.stop(); } fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); self.base_key_down_fn(widget, event); if (event.event.is_accepted) return; event.event.accept(); @@ -244,7 +244,7 @@ fn onKeyDown(widget: *gui.Widget, event: *gui.KeyEvent) void { } fn onTextInput(widget: *gui.Widget, event: *const gui.TextInputEvent) void { - var self = @fieldParentPtr(Self, "widget", widget); + var self: *Self = @fieldParentPtr("widget", widget); if (self.hasSelection()) { self.deleteSelection(); @@ -397,7 +397,7 @@ pub fn setText(self: *Self, text: []const u8) !void { } pub fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; const enabled = widget.isEnabled(); diff --git a/src/gui/widgets/Toolbar.zig b/src/gui/widgets/Toolbar.zig index 1ae0c85..af9c0ac 100644 --- a/src/gui/widgets/Toolbar.zig +++ b/src/gui/widgets/Toolbar.zig @@ -41,7 +41,7 @@ pub fn deinit(self: *Self) void { } fn onResize(widget: *gui.Widget, _: *gui.ResizeEvent) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); self.updateLayout(); } @@ -104,7 +104,7 @@ fn drawSeparator(widget: *gui.Widget, vg: nvg) void { } fn draw(widget: *gui.Widget, vg: nvg) void { - const self = @fieldParentPtr(Self, "widget", widget); + const self: *Self = @fieldParentPtr("widget", widget); const rect = widget.relative_rect; gui.drawPanel(vg, rect.x, rect.y, rect.w, rect.h, 1, false, false); diff --git a/src/main.zig b/src/main.zig index e402967..843f338 100644 --- a/src/main.zig +++ b/src/main.zig @@ -109,9 +109,9 @@ const SdlWindow = struct { if (builtin.os.tag == .windows) { if (sys_info.subsystem == c.SDL_SYSWM_WINDOWS) { const hwnd = @as(foundation.HWND, @ptrCast(sys_info.info.win.window)); - const style = windows.GetWindowLong(hwnd, windows.GWL_STYLE); - const no_minimizebox = ~@as(i32, @bitCast(@intFromEnum(windows.WS_MINIMIZEBOX))); - _ = windows.SetWindowLong(hwnd, windows.GWL_STYLE, style & no_minimizebox); + const style = windows.GetWindowLongW(hwnd, windows.GWL_STYLE); + const no_minimizebox = ~@as(i32, @bitCast(windows.WS_MINIMIZEBOX)); + _ = windows.SetWindowLongW(hwnd, windows.GWL_STYLE, style & no_minimizebox); } } } @@ -556,7 +556,7 @@ fn sdlProcessTextInput(text_event: c.SDL_TextInputEvent) void { if (first_surrogate_half) |first_surrogate0| { const utf16 = [_]u16{ first_surrogate0, surrogate }; var utf8 = [_]u8{0} ** 4; - _ = std.unicode.utf16leToUtf8(&utf8, &utf16) catch unreachable; + _ = std.unicode.utf16LeToUtf8(&utf8, &utf16) catch unreachable; first_surrogate_half = null; var te = gui.TextInputEvent{