Skip to content

Commit

Permalink
Make use of editor.arena for small allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
foxnne committed Jan 30, 2025
1 parent 0b77ede commit 6063e54
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 188 deletions.
6 changes: 6 additions & 0 deletions src/editor/Constants.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
const pixi = @import("../pixi.zig");

pub const layer_name_max_length = 128;
pub const animation_name_max_length = 128;
pub const file_name_max_length = std.fs.max_path_bytes;
2 changes: 2 additions & 0 deletions src/editor/Editor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub const Recents = @import("Recents.zig");
pub const Tools = @import("Tools.zig");
pub const Theme = @import("Theme.zig");

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

const zip = @import("zip");
const zstbi = @import("zstbi");
const zgpu = @import("zgpu");
Expand Down
16 changes: 10 additions & 6 deletions src/editor/artboard/Artboard.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *App, editor: *Editor, packer
imgui.pushIDInt(@as(c_int, @intCast(i)));
defer imgui.popID();

const label = try std.fmt.allocPrintZ(app.allocator, " {s} {s} ", .{ pixi.fa.file_powerpoint, file_name });
defer app.allocator.free(label);
const label = try std.fmt.allocPrintZ(editor.arena.allocator(), " {s} {s} ", .{ pixi.fa.file_powerpoint, file_name });

var file_tab_flags: imgui.TabItemFlags = 0;
file_tab_flags |= imgui.TabItemFlags_None;
Expand Down Expand Up @@ -232,7 +231,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *App, editor: *Editor, packer

// Now add to ruler children windows, since we have updated the camera.
if (show_rulers) {
try rulers.draw(file, app, core);
try rulers.draw(file, editor);
}
}
}
Expand All @@ -259,11 +258,16 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *App, editor: *Editor, packer
.y = flipbook_height,
}, imgui.ChildFlags_None, flipbook_flags)) {
if (editor.getFile(editor.open_file_index)) |file| {
try flipbook.menu.draw(file, artboard_flipbook_ratio, app, editor);
try flipbook.menu.draw(file, artboard_flipbook_ratio, editor);
if (editor.explorer.pane == .keyframe_animations or file.flipbook_view == .timeline) {
try flipbook.timeline.draw(file, core, app);
try flipbook.timeline.draw(file, editor);
} else {
if (imgui.beginChild("FlipbookCanvas", .{ .x = 0.0, .y = 0.0 }, imgui.ChildFlags_None, imgui.WindowFlags_ChildWindow)) {
if (imgui.beginChild(
"FlipbookCanvas",
.{ .x = 0.0, .y = 0.0 },
imgui.ChildFlags_None,
imgui.WindowFlags_ChildWindow,
)) {
defer imgui.endChild();
try flipbook.canvas.draw(file, app, editor);
}
Expand Down
37 changes: 26 additions & 11 deletions src/editor/artboard/flipbook/menu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const imgui = @import("zig-imgui");

const History = pixi.Internal.File.History;

pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, app: *App, editor: *Editor) !void {
pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, editor: *Editor) !void {
imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 10.0, .y = 10.0 });
defer imgui.popStyleVar();
imgui.pushStyleColorImVec4(imgui.Col_Text, editor.theme.text.toImguiVec4());
Expand Down Expand Up @@ -53,18 +53,29 @@ pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, app: *App, editor: *Edi

{ // Frame Selection
const current_frame = if (file.selected_sprite_index > animation.start) file.selected_sprite_index - animation.start else 0;
const frame = try std.fmt.allocPrintZ(app.allocator, "{d}/{d}", .{ current_frame + 1, animation.length });
defer app.allocator.free(frame);
const frame = try std.fmt.allocPrintZ(editor.arena.allocator(), "{d}/{d}", .{ current_frame + 1, animation.length });

imgui.setNextItemWidth(imgui.calcTextSize(frame).x + 40);
if (imgui.beginCombo("Frame ", frame, imgui.ComboFlags_None)) {
defer imgui.endCombo();
for (0..animation.length) |i| {
const other_frame = try std.fmt.allocPrintZ(app.allocator, "{d}/{d}", .{ i + 1, animation.length });
defer app.allocator.free(other_frame);

if (imgui.selectableEx(other_frame, animation.start + i == file.selected_animation_index, imgui.SelectableFlags_None, .{ .x = 0.0, .y = 0.0 })) {
file.flipbook_scroll_request = .{ .from = file.flipbook_scroll, .to = file.flipbookScrollFromSpriteIndex(animation.start + i), .state = file.selected_animation_state };
const other_frame = try std.fmt.allocPrintZ(
editor.arena.allocator(),
"{d}/{d}",
.{ i + 1, animation.length },
);

if (imgui.selectableEx(
other_frame,
animation.start + i == file.selected_animation_index,
imgui.SelectableFlags_None,
.{ .x = 0.0, .y = 0.0 },
)) {
file.flipbook_scroll_request = .{
.from = file.flipbook_scroll,
.to = file.flipbookScrollFromSpriteIndex(animation.start + i),
.state = file.selected_animation_state,
};
}
}
}
Expand All @@ -87,7 +98,7 @@ pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, app: *App, editor: *Edi
// Apply history of animation state
var change: History.Change = .{ .animation = .{
.index = file.selected_animation_index,
.name = [_:0]u8{0} ** 128,
.name = [_:0]u8{0} ** Editor.Constants.animation_name_max_length,
.fps = animation.fps,
.start = animation.start,
.length = animation.length,
Expand Down Expand Up @@ -123,9 +134,13 @@ pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, app: *App, editor: *Edi
var keyframe_animation_index: usize = 0;
while (keyframe_animation_index < file.keyframe_animations.slice().len) : (keyframe_animation_index += 1) {
const a = &file.keyframe_animations.slice().get(keyframe_animation_index);
if (imgui.selectableEx(a.name, keyframe_animation_index == file.selected_keyframe_animation_index, imgui.SelectableFlags_None, .{ .x = 0.0, .y = 0.0 })) {
if (imgui.selectableEx(
a.name,
keyframe_animation_index == file.selected_keyframe_animation_index,
imgui.SelectableFlags_None,
.{ .x = 0.0, .y = 0.0 },
))
file.selected_keyframe_animation_index = keyframe_animation_index;
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/editor/artboard/flipbook/timeline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const mach = @import("mach");

const Core = mach.Core;
const App = pixi.App;
const Editor = pixi.Editor;

const imgui = @import("zig-imgui");
const zmath = @import("zmath");
Expand All @@ -26,7 +27,7 @@ var ms_hovered: ?usize = null;
var keyframe_dragging: ?u32 = null;
var mouse_scroll_delta_y: f32 = 0.0;

pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App) !void {
pub fn draw(file: *pixi.Internal.File, editor: *Editor) !void {
const window_height = imgui.getWindowHeight();
const window_width = imgui.getWindowWidth();
const tile_width = @as(f32, @floatFromInt(file.tile_width));
Expand Down Expand Up @@ -142,10 +143,9 @@ pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App) !void {
const unit = if (@mod(ms, 1000) == 0) "s" else "ms";
const value = if (@mod(ms, 1000) == 0) @divExact(ms, 1000) else ms;

const text = try std.fmt.allocPrintZ(pixi.app.allocator, "{d} {s}", .{ value, unit });
defer pixi.app.allocator.free(text);
const text = try std.fmt.allocPrintZ(editor.arena.allocator(), "{d} {s}", .{ value, unit });

draw_list.addText(.{ .x = x, .y = y }, pixi.editor.theme.text_background.toU32(), text);
draw_list.addText(.{ .x = x, .y = y }, editor.theme.text_background.toU32(), text);
}
}
}
Expand Down Expand Up @@ -366,7 +366,7 @@ pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App) !void {

// We are using a load on the gpu texture, so we need to clear this texture on the gpu after we are done
@memset(file.keyframe_animation_texture.image.data, 0.0);
file.keyframe_animation_texture.update(core.windows.get(app.window, .device));
file.keyframe_animation_texture.update(pixi.core.windows.get(pixi.app.window, .device));
}
}
}
Expand Down Expand Up @@ -488,7 +488,7 @@ pub fn draw(file: *pixi.Internal.File, core: *Core, app: *App) !void {

// We are using a load on the gpu texture, so we need to clear this texture on the gpu after we are done
@memset(file.keyframe_animation_texture.image.data, 0.0);
file.keyframe_animation_texture.update(core.windows.get(app.window, .device));
file.keyframe_animation_texture.update(pixi.core.windows.get(pixi.app.window, .device));
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/editor/artboard/rulers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const pixi = @import("../../pixi.zig");

const Core = @import("mach").Core;
const App = pixi.App;

const Editor = pixi.Editor;
const imgui = @import("zig-imgui");

pub fn draw(file: *pixi.Internal.File, app: *App, _: *Core) !void {
pub fn draw(file: *pixi.Internal.File, editor: *Editor) !void {
const file_width = @as(f32, @floatFromInt(file.width));
const file_height = @as(f32, @floatFromInt(file.height));
const tile_width = @as(f32, @floatFromInt(file.tile_width));
Expand All @@ -29,8 +29,7 @@ pub fn draw(file: *pixi.Internal.File, app: *App, _: *Core) !void {
while (i < @as(usize, @intCast(tiles_wide))) : (i += 1) {
const offset = .{ (@as(f32, @floatFromInt(i)) * tile_width) * file.camera.zoom, 0.0 };
if (tile_width * file.camera.zoom > text_size.x * 4.0) {
const text = try std.fmt.allocPrintZ(app.allocator, "{d}", .{i});
defer app.allocator.free(text);
const text = try std.fmt.allocPrintZ(editor.arena.allocator(), "{d}", .{i});

draw_list.addText(
.{ .x = tl[0] + offset[0] + (tile_width / 2.0 * file.camera.zoom) - (text_size.x / 2.0), .y = tl[1] + 4.0 },
Expand Down Expand Up @@ -66,10 +65,13 @@ pub fn draw(file: *pixi.Internal.File, app: *App, _: *Core) !void {
const offset = .{ 0.0, @as(f32, @floatFromInt(i)) * tile_height * file.camera.zoom };

if (tile_height * file.camera.zoom > text_size.x * 4.0) {
const text = try std.fmt.allocPrintZ(app.allocator, "{d}", .{i});
defer app.allocator.free(text);
const text = try std.fmt.allocPrintZ(editor.arena.allocator(), "{d}", .{i});

draw_list.addText(.{ .x = tl[0], .y = tl[1] + offset[1] + (tile_height / 2.0 * file.camera.zoom) - (text_size.y / 2.0) }, pixi.editor.theme.text_secondary.toU32(), text.ptr);
draw_list.addText(
.{ .x = tl[0], .y = tl[1] + offset[1] + (tile_height / 2.0 * file.camera.zoom) - (text_size.y / 2.0) },
pixi.editor.theme.text_secondary.toU32(),
text.ptr,
);
}
draw_list.addLineEx(
.{ .x = tl[0], .y = tl[1] + offset[1] },
Expand Down
28 changes: 7 additions & 21 deletions src/editor/explorer/Explorer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.files => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .files })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Explorer ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Explorer ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Explorer");
Expand All @@ -103,9 +101,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.tools => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .tools })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Tools ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Tools ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Tools");
Expand All @@ -120,9 +116,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.sprites => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .sprites })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Sprites ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Sprites ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Sprites");
Expand All @@ -136,9 +130,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.animations => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .animations })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Animations ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Animations ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Animations");
Expand All @@ -152,9 +144,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.keyframe_animations => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .keyframe_animations })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Keyframe Animations ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Keyframe Animations ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Keyframe Animations");
Expand All @@ -168,9 +158,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.pack => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .pack })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Packing ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Packing ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Packing");
Expand All @@ -184,9 +172,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
.settings => {
if (imgui.beginMenuBar()) {
if (editor.hotkeys.hotkey(.{ .sidebar = .settings })) |hotkey| {
const title = try std.fmt.allocPrintZ(app.allocator, "Settings ({s})", .{hotkey.shortcut});
defer app.allocator.free(title);

const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Settings ({s})", .{hotkey.shortcut});
imgui.separatorText(title);
} else {
imgui.separatorText("Settings");
Expand Down
Loading

0 comments on commit 6063e54

Please sign in to comment.