Skip to content

Commit

Permalink
Pixi: Editor: Move remaining editor-specific logic from Pixi to `Ed…
Browse files Browse the repository at this point in the history
…itor`
  • Loading branch information
foxnne committed Jan 17, 2025
1 parent 36ad062 commit 4377e1c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 84 deletions.
85 changes: 2 additions & 83 deletions src/Pixi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,8 @@ pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {
}

pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
if (editor.popups.file_dialog_request) |request| {
defer editor.popups.file_dialog_request = null;
const initial = if (request.initial) |initial| initial else editor.project_folder;

if (switch (request.state) {
.file => try nfd.openFileDialog(request.filter, initial),
.folder => try nfd.openFolderDialog(initial),
.save => try nfd.saveFileDialog(request.filter, initial),
}) |path| {
editor.popups.file_dialog_response = .{
.path = path,
.type = request.type,
};
}
}
// Process dialog requests
editor_mod.call(.processDialogRequest);

// Process events
while (core.nextEvent()) |event| {
Expand Down Expand Up @@ -347,73 +334,6 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
}
}

// Accept transformations
{
for (editor.open_files.items) |*file| {
if (file.transform_texture) |*transform_texture| {
if (transform_texture.confirm) {
// Blit temp layer to selected layer
if (file.transform_staging_buffer) |staging_buffer| {
const buffer_size: usize = @as(usize, @intCast(file.width * file.height));

var response: gpu.Buffer.MapAsyncStatus = undefined;
const callback = (struct {
pub inline fn callback(ctx: *gpu.Buffer.MapAsyncStatus, status: gpu.Buffer.MapAsyncStatus) void {
ctx.* = status;
}
}).callback;

staging_buffer.mapAsync(.{ .read = true }, 0, buffer_size * @sizeOf([4]f32), &response, callback);
while (true) {
if (response == gpu.Buffer.MapAsyncStatus.success) {
break;
} else {
window.device.tick();
}
}

const layer_index = file.selected_layer_index;
const write_layer = file.layers.get(file.selected_layer_index);

if (staging_buffer.getConstMappedRange([4]f32, 0, buffer_size)) |buffer_mapped| {
for (write_layer.pixels(), buffer_mapped, 0..) |*p, b, i| {
if (b[3] != 0.0) {
try file.buffers.stroke.append(i, p.*);

const out: [4]u8 = .{
@as(u8, @intFromFloat(b[0] * 255.0)),
@as(u8, @intFromFloat(b[1] * 255.0)),
@as(u8, @intFromFloat(b[2] * 255.0)),
@as(u8, @intFromFloat(b[3] * 255.0)),
};
p.* = out;
}
}
}

// Submit the stroke change buffer
if (file.buffers.stroke.indices.items.len > 0) {
const change = try file.buffers.stroke.toChange(@intCast(layer_index));
try file.history.append(change);
}

staging_buffer.unmap();

var texture: *gfx.Texture = &file.layers.items(.texture)[file.selected_layer_index];
texture.update(window.device);
}

transform_texture.texture.deinit();
file.transform_texture = null;
}
}
}
}

for (editor.hotkeys.hotkeys) |*hotkey| {
hotkey.previous_state = hotkey.state;
}

for (app.mouse.buttons) |*button| {
button.previous_state = button.state;
}
Expand Down Expand Up @@ -444,7 +364,6 @@ pub fn deinit(editor_mod: mach.Mod(Editor)) !void {

zstbi.deinit();
app.allocator.free(app.root_path);

app.arena_allocator.deinit();

_ = gpa.detectLeaks();
Expand Down
94 changes: 93 additions & 1 deletion src/editor/Editor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ pub const Artboard = @import("artboard/Artboard.zig");
pub const Popups = @import("popups/Popups.zig");

pub const mach_module = .editor;
pub const mach_systems = .{ .init, .lateInit, .tick, .deinit };
pub const mach_systems = .{
.init,
.lateInit,
.processDialogRequest,
.tick,
.deinit,
};

pub const Theme = @import("Theme.zig");

Expand Down Expand Up @@ -95,6 +101,24 @@ pub fn lateInit(core: *Core, app: *Pixi, editor: *Editor) !void {
editor.theme.init(core, app);
}

pub fn processDialogRequest(editor: *Editor) !void {
if (editor.popups.file_dialog_request) |request| {
defer editor.popups.file_dialog_request = null;
const initial = if (request.initial) |initial| initial else editor.project_folder;

if (switch (request.state) {
.file => try nfd.openFileDialog(request.filter, initial),
.folder => try nfd.openFolderDialog(initial),
.save => try nfd.saveFileDialog(request.filter, initial),
}) |path| {
editor.popups.file_dialog_response = .{
.path = path,
.type = request.type,
};
}
}
}

pub fn tick(
core: *Core,
app: *Pixi,
Expand All @@ -114,6 +138,74 @@ pub fn tick(
explorer_mod.call(.draw);
artboard_mod.call(.draw);
popups_mod.call(.draw);

// Accept transformations
{
const window = core.windows.getValue(app.window);
for (editor.open_files.items) |*file| {
if (file.transform_texture) |*transform_texture| {
if (transform_texture.confirm) {
// Blit temp layer to selected layer
if (file.transform_staging_buffer) |staging_buffer| {
const buffer_size: usize = @as(usize, @intCast(file.width * file.height));

var response: mach.gpu.Buffer.MapAsyncStatus = undefined;
const callback = (struct {
pub inline fn callback(ctx: *mach.gpu.Buffer.MapAsyncStatus, status: mach.gpu.Buffer.MapAsyncStatus) void {
ctx.* = status;
}
}).callback;

staging_buffer.mapAsync(.{ .read = true }, 0, buffer_size * @sizeOf([4]f32), &response, callback);
while (true) {
if (response == mach.gpu.Buffer.MapAsyncStatus.success) {
break;
} else {
window.device.tick();
}
}

const layer_index = file.selected_layer_index;
const write_layer = file.layers.get(file.selected_layer_index);

if (staging_buffer.getConstMappedRange([4]f32, 0, buffer_size)) |buffer_mapped| {
for (write_layer.pixels(), buffer_mapped, 0..) |*p, b, i| {
if (b[3] != 0.0) {
try file.buffers.stroke.append(i, p.*);

const out: [4]u8 = .{
@as(u8, @intFromFloat(b[0] * 255.0)),
@as(u8, @intFromFloat(b[1] * 255.0)),
@as(u8, @intFromFloat(b[2] * 255.0)),
@as(u8, @intFromFloat(b[3] * 255.0)),
};
p.* = out;
}
}
}

// Submit the stroke change buffer
if (file.buffers.stroke.indices.items.len > 0) {
const change = try file.buffers.stroke.toChange(@intCast(layer_index));
try file.history.append(change);
}

staging_buffer.unmap();

var texture: *Pixi.gfx.Texture = &file.layers.items(.texture)[file.selected_layer_index];
texture.update(window.device);
}

transform_texture.texture.deinit();
file.transform_texture = null;
}
}
}
}

for (editor.hotkeys.hotkeys) |*hotkey| {
hotkey.previous_state = hotkey.state;
}
}

pub fn setProjectFolder(editor: *Editor, path: [:0]const u8) !void {
Expand Down

0 comments on commit 4377e1c

Please sign in to comment.