Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
zig-cache
zig-out
.zig-cache

vcpkg_installed
zig-out
34 changes: 14 additions & 20 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -34,44 +33,39 @@ 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 {
std.log.err("minipixel.o not found. Please use VS Developer Prompt and run\n\n" ++
"\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);
exe.root_module.addImport("data", data_mod);
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");
Expand All @@ -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");
Expand Down
23 changes: 12 additions & 11 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.{
.name = "Mini Pixel",
.name = .minipixel,
.version = "0.2.1",
.fingerprint = 0xc96373d8301039f5,
.paths = .{
"data",
"lib",
Expand All @@ -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",
},
},
}
6 changes: 3 additions & 3 deletions src/AboutDialogWidget.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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(),
Expand All @@ -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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/BlendModeWidget.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
Expand Down
32 changes: 16 additions & 16 deletions src/CanvasWidget.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -1077,20 +1077,20 @@ 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(),
}
}

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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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..]),
Expand All @@ -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();
}
}
16 changes: 9 additions & 7 deletions src/ColorBitmap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/ColorForegroundBackgroundWidget.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down
Loading