Skip to content

Commit

Permalink
Packer: Move to module
Browse files Browse the repository at this point in the history
  • Loading branch information
foxnne committed Jan 19, 2025
1 parent e940818 commit ebaeb84
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 78 deletions.
16 changes: 11 additions & 5 deletions src/Pixi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ const Core = mach.Core;
pub const App = @This();
pub const Editor = @import("editor/Editor.zig");

pub const Packer = @import("tools/Packer.zig");

// Global pointers
pub var core: *Core = undefined;
pub var app: *App = undefined;
pub var editor: *Editor = undefined;
pub var packer: *Packer = undefined;

// Mach module, systems, and main
pub const mach_module = .app;
Expand All @@ -27,6 +30,7 @@ pub const main = mach.schedule(.{
.{ Core, .init },
.{ App, .init },
.{ Editor, .init },
.{ Packer, .init },
.{ Core, .main },
});

Expand All @@ -43,7 +47,6 @@ total_time: f32 = 0.0,
assets: Assets = undefined,
fonts: Fonts = .{},
batcher: gfx.Batcher = undefined,
packer: Packer = undefined,
pipeline_default: *gpu.RenderPipeline = undefined,
pipeline_compute: *gpu.ComputePipeline = undefined,
uniform_buffer_default: *gpu.Buffer = undefined,
Expand All @@ -54,12 +57,14 @@ should_close: bool = false,

pub const version: std.SemanticVersion = .{ .major = 0, .minor = 2, .patch = 0 };

pub const Packer = @import("tools/Packer.zig");

// Generated files, these contain helpers for autocomplete
// So you can get a named index into assets.atlas.sprites
pub const paths = @import("assets.zig");
pub const atlas = paths.pixi_atlas;
pub const animations = @import("animations.zig");
pub const shaders = @import("shaders.zig");

// Other helpers and namespaces
pub const fs = @import("tools/fs.zig");
pub const fa = @import("tools/font_awesome.zig");
pub const math = @import("math/math.zig");
Expand Down Expand Up @@ -97,12 +102,14 @@ pub fn init(
_app: *App,
_core: *Core,
_editor: *Editor,
_packer: *Packer,
app_mod: mach.Mod(App),
) !void {
// Store our global pointers so we can access them from non-mach functions for now
app = _app;
core = _core;
editor = _editor;
packer = _packer;

core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;
Expand Down Expand Up @@ -145,7 +152,6 @@ pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {

// Setup
app.mouse = try input.Mouse.initDefault(app.allocator);
app.packer = try Packer.init(app.allocator);
app.batcher = try gfx.Batcher.init(app.allocator, 1000);

// Store information about the window in float format
Expand Down Expand Up @@ -345,7 +351,7 @@ pub fn deinit(editor_mod: mach.Mod(Editor)) !void {

app.allocator.free(app.mouse.buttons);

app.packer.deinit();
packer.deinit();

app.batcher.deinit();
app.pipeline_default.release();
Expand Down
12 changes: 7 additions & 5 deletions src/editor/artboard/Artboard.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const std = @import("std");
const Pixi = @import("../../Pixi.zig");
const Core = @import("mach").Core;
const Editor = Pixi.Editor;
const Packer = Pixi.Packer;

const nfd = @import("nfd");
const imgui = @import("zig-imgui");

Expand Down Expand Up @@ -31,7 +33,7 @@ pub fn init(artboard: *Artboard) void {

pub fn deinit() void {}

pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void {
pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor, packer: *Packer) !void {
imgui.pushStyleVar(imgui.StyleVar_WindowRounding, 0.0);
defer imgui.popStyleVar();
imgui.setNextWindowPos(.{
Expand Down Expand Up @@ -124,7 +126,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void
const mouse_clicked: bool = app.mouse.anyButtonDown();

if (editor.explorer.pane == .pack) {
drawCanvasPack(core, app, editor);
drawCanvasPack(editor, packer);
} else if (editor.open_files.items.len > 0) {
var files_flags: imgui.TabBarFlags = 0;
files_flags |= imgui.TabBarFlags_Reorderable;
Expand Down Expand Up @@ -386,7 +388,7 @@ pub fn drawGrip(window_width: f32, app: *Pixi, editor: *Editor) void {
imgui.textColored(color, Pixi.fa.grip_lines_vertical);
}

pub fn drawCanvasPack(core: *Core, app: *Pixi, editor: *Editor) void {
pub fn drawCanvasPack(editor: *Editor, packer: *Packer) void {
var packed_textures_flags: imgui.TabBarFlags = 0;
packed_textures_flags |= imgui.TabBarFlags_Reorderable;

Expand All @@ -399,7 +401,7 @@ pub fn drawCanvasPack(core: *Core, app: *Pixi, editor: *Editor) void {
imgui.TabItemFlags_None,
)) {
defer imgui.endTabItem();
canvas_pack.draw(.diffusemap, app, core, editor);
canvas_pack.draw(.diffusemap, editor, packer);
}

if (imgui.beginTabItem(
Expand All @@ -408,7 +410,7 @@ pub fn drawCanvasPack(core: *Core, app: *Pixi, editor: *Editor) void {
imgui.TabItemFlags_None,
)) {
defer imgui.endTabItem();
canvas_pack.draw(.heightmap, app, core, editor);
canvas_pack.draw(.heightmap, editor, packer);
}
}
}
5 changes: 3 additions & 2 deletions src/editor/artboard/canvas_pack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ const std = @import("std");
const Pixi = @import("../../Pixi.zig");
const Core = @import("mach").Core;
const Editor = Pixi.Editor;
const Packer = Pixi.Packer;
const imgui = @import("zig-imgui");

pub const PackTexture = enum {
diffusemap,
heightmap,
};

pub fn draw(mode: PackTexture, app: *Pixi, _: *Core, editor: *Editor) void {
pub fn draw(mode: PackTexture, editor: *Editor, packer: *Packer) void {
if (switch (mode) {
.diffusemap => editor.atlas.diffusemap,
.heightmap => editor.atlas.heightmap,
Expand All @@ -27,7 +28,7 @@ pub fn draw(mode: PackTexture, app: *Pixi, _: *Core, editor: *Editor) void {
const file_width = @as(f32, @floatFromInt(texture.image.width));
const file_height = @as(f32, @floatFromInt(texture.image.height));

var camera = &app.packer.camera;
var camera = &packer.camera;

// Handle zooming, panning and extents
{
Expand Down
5 changes: 3 additions & 2 deletions src/editor/explorer/Explorer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const std = @import("std");
const Pixi = @import("../../Pixi.zig");
const Core = @import("mach").Core;
const Editor = Pixi.Editor;
const Packer = Pixi.Packer;

const nfd = @import("nfd");
const imgui = @import("zig-imgui");
Expand Down Expand Up @@ -41,7 +42,7 @@ pub fn deinit() void {
// TODO: Free memory
}

pub fn draw(core: *Core, app: *Pixi, editor: *Editor, explorer: *Explorer) !void {
pub fn draw(core: *Core, app: *Pixi, editor: *Editor, explorer: *Explorer, packer: *Packer) !void {
imgui.pushStyleVar(imgui.StyleVar_WindowRounding, 0.0);
imgui.pushStyleVar(imgui.StyleVar_WindowBorderSize, 0.0);
imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 0.0, .y = 0.0 });
Expand Down Expand Up @@ -176,7 +177,7 @@ pub fn draw(core: *Core, app: *Pixi, editor: *Editor, explorer: *Explorer) !void
}
imgui.spacing();
imgui.spacing();
try pack.draw(core, app, editor);
try pack.draw(app, editor, packer);
},
.settings => {
if (imgui.beginMenuBar()) {
Expand Down
39 changes: 21 additions & 18 deletions src/editor/explorer/pack.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const std = @import("std");

const Pixi = @import("../../Pixi.zig");
const Core = @import("mach").Core;
const Editor = Pixi.Editor;
const Packer = Pixi.Packer;

const nfd = @import("nfd");
const imgui = @import("zig-imgui");

pub fn draw(_: *Core, app: *Pixi, editor: *Editor) !void {
pub fn draw(app: *Pixi, editor: *Editor, packer: *Packer) !void {
imgui.pushStyleVarImVec2(imgui.StyleVar_FramePadding, .{ .x = 6.0, .y = 5.0 });
defer imgui.popStyleVar();
imgui.pushStyleColorImVec4(imgui.Col_Button, editor.theme.highlight_secondary.toImguiVec4());
Expand All @@ -15,20 +18,20 @@ pub fn draw(_: *Core, app: *Pixi, editor: *Editor) !void {

const window_size = imgui.getContentRegionAvail();

switch (app.packer.target) {
switch (packer.target) {
.all_open => {
if (editor.open_files.items.len <= 1) {
app.packer.target = .project;
packer.target = .project;
}
},
.single_open => {
if (editor.open_files.items.len == 0)
app.packer.target = .project;
packer.target = .project;
},
else => {},
}

const preview_text = switch (app.packer.target) {
const preview_text = switch (packer.target) {
.project => "Full Project",
.all_open => "All Open Files",
.single_open => "Current Open File",
Expand All @@ -37,20 +40,20 @@ pub fn draw(_: *Core, app: *Pixi, editor: *Editor) !void {
if (imgui.beginCombo("Files", preview_text.ptr, imgui.ComboFlags_None)) {
defer imgui.endCombo();
if (imgui.menuItem("Full Project")) {
app.packer.target = .project;
packer.target = .project;
}

{
const enabled = if (editor.getFile(editor.open_file_index)) |_| true else false;
if (imgui.menuItemEx("Current Open File", null, false, enabled)) {
app.packer.target = .single_open;
packer.target = .single_open;
}
}

{
const enabled = if (editor.open_files.items.len > 1) true else false;
if (imgui.menuItemEx("All Open Files", null, false, enabled)) {
app.packer.target = .all_open;
packer.target = .all_open;
}
}
}
Expand All @@ -65,8 +68,8 @@ pub fn draw(_: *Core, app: *Pixi, editor: *Editor) !void {

{
var packable: bool = true;
if (app.packer.target == .project and editor.project_folder == null) packable = false;
if (app.packer.target == .all_open and editor.open_files.items.len <= 1) packable = false;
if (packer.target == .project and editor.project_folder == null) packable = false;
if (packer.target == .all_open and editor.open_files.items.len <= 1) packable = false;
if (editor.saving()) {
imgui.pushStyleColorImVec4(imgui.Col_Text, editor.theme.text_background.toImguiVec4());
defer imgui.popStyleColor();
Expand All @@ -77,31 +80,31 @@ pub fn draw(_: *Core, app: *Pixi, editor: *Editor) !void {
if (!packable)
imgui.beginDisabled(true);
if (imgui.buttonEx("Pack", .{ .x = window_size.x, .y = 0.0 })) {
switch (app.packer.target) {
switch (packer.target) {
.project => {
if (editor.project_folder) |folder| {
try Pixi.Packer.recurseFiles(app.allocator, folder);
try app.packer.packAndClear();
try Pixi.Packer.recurseFiles(folder);
try packer.packAndClear();
}
},
.all_open => {
for (editor.open_files.items) |*file| {
try app.packer.append(file);
try packer.append(file);
}
try app.packer.packAndClear();
try packer.packAndClear();
},
.single_open => {
if (editor.getFile(editor.open_file_index)) |file| {
try app.packer.append(file);
try app.packer.packAndClear();
try packer.append(file);
try packer.packAndClear();
}
},
}
}
if (!packable)
imgui.endDisabled();

if (app.packer.target == .project and editor.project_folder == null) {
if (packer.target == .project and editor.project_folder == null) {
imgui.pushStyleColorImVec4(imgui.Col_Text, editor.theme.text_background.toImguiVec4());
defer imgui.popStyleColor();
imgui.textWrapped("Select a project folder to pack.");
Expand Down
1 change: 1 addition & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Modules = mach.Modules(.{
@import("editor/explorer/Explorer.zig"),
@import("editor/artboard/Artboard.zig"),
@import("editor/Sidebar.zig"),
@import("tools/Packer.zig"),
});

// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
Expand Down
10 changes: 5 additions & 5 deletions src/storage/internal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1803,9 +1803,9 @@ pub const PixiFile = struct {
}
}

Pixi.app.packer.ldtk = true;
defer Pixi.app.packer.ldtk = false;
try Pixi.app.packer.appendProject();
Pixi.packer.ldtk = true;
defer Pixi.packer.ldtk = false;
try Pixi.packer.appendProject();

const ldtk_atlas_save_path = try std.fmt.allocPrintZ(Pixi.app.allocator, "{s}{c}pixi-ldtk.json", .{ project_folder_path, std.fs.path.sep });
defer Pixi.app.allocator.free(ldtk_atlas_save_path);
Expand All @@ -1816,10 +1816,10 @@ pub const PixiFile = struct {
const out_stream = handle.writer();
const options: std.json.StringifyOptions = .{};

const output: Pixi.Packer.LDTKTileset.LDTKCompatibility = .{ .tilesets = Pixi.app.packer.ldtk_tilesets.items };
const output: Pixi.Packer.LDTKTileset.LDTKCompatibility = .{ .tilesets = Pixi.packer.ldtk_tilesets.items };

try std.json.stringify(output, options, out_stream);
Pixi.app.packer.clearAndFree();
Pixi.packer.clearAndFree();
}
}

Expand Down
Loading

0 comments on commit ebaeb84

Please sign in to comment.