Skip to content

Commit 6063e54

Browse files
committed
Make use of editor.arena for small allocations
1 parent 0b77ede commit 6063e54

21 files changed

+232
-188
lines changed

src/editor/Constants.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const std = @import("std");
2+
const pixi = @import("../pixi.zig");
3+
4+
pub const layer_name_max_length = 128;
5+
pub const animation_name_max_length = 128;
6+
pub const file_name_max_length = std.fs.max_path_bytes;

src/editor/Editor.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub const Recents = @import("Recents.zig");
1212
pub const Tools = @import("Tools.zig");
1313
pub const Theme = @import("Theme.zig");
1414

15+
pub const Constants = @import("Constants.zig");
16+
1517
const zip = @import("zip");
1618
const zstbi = @import("zstbi");
1719
const zgpu = @import("zgpu");

src/editor/artboard/Artboard.zig

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *App, editor: *Editor, packer
145145
imgui.pushIDInt(@as(c_int, @intCast(i)));
146146
defer imgui.popID();
147147

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

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

233232
// Now add to ruler children windows, since we have updated the camera.
234233
if (show_rulers) {
235-
try rulers.draw(file, app, core);
234+
try rulers.draw(file, editor);
236235
}
237236
}
238237
}
@@ -259,11 +258,16 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *App, editor: *Editor, packer
259258
.y = flipbook_height,
260259
}, imgui.ChildFlags_None, flipbook_flags)) {
261260
if (editor.getFile(editor.open_file_index)) |file| {
262-
try flipbook.menu.draw(file, artboard_flipbook_ratio, app, editor);
261+
try flipbook.menu.draw(file, artboard_flipbook_ratio, editor);
263262
if (editor.explorer.pane == .keyframe_animations or file.flipbook_view == .timeline) {
264-
try flipbook.timeline.draw(file, core, app);
263+
try flipbook.timeline.draw(file, editor);
265264
} else {
266-
if (imgui.beginChild("FlipbookCanvas", .{ .x = 0.0, .y = 0.0 }, imgui.ChildFlags_None, imgui.WindowFlags_ChildWindow)) {
265+
if (imgui.beginChild(
266+
"FlipbookCanvas",
267+
.{ .x = 0.0, .y = 0.0 },
268+
imgui.ChildFlags_None,
269+
imgui.WindowFlags_ChildWindow,
270+
)) {
267271
defer imgui.endChild();
268272
try flipbook.canvas.draw(file, app, editor);
269273
}

src/editor/artboard/flipbook/menu.zig

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const imgui = @import("zig-imgui");
1111

1212
const History = pixi.Internal.File.History;
1313

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

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

5958
imgui.setNextItemWidth(imgui.calcTextSize(frame).x + 40);
6059
if (imgui.beginCombo("Frame ", frame, imgui.ComboFlags_None)) {
6160
defer imgui.endCombo();
6261
for (0..animation.length) |i| {
63-
const other_frame = try std.fmt.allocPrintZ(app.allocator, "{d}/{d}", .{ i + 1, animation.length });
64-
defer app.allocator.free(other_frame);
65-
66-
if (imgui.selectableEx(other_frame, animation.start + i == file.selected_animation_index, imgui.SelectableFlags_None, .{ .x = 0.0, .y = 0.0 })) {
67-
file.flipbook_scroll_request = .{ .from = file.flipbook_scroll, .to = file.flipbookScrollFromSpriteIndex(animation.start + i), .state = file.selected_animation_state };
62+
const other_frame = try std.fmt.allocPrintZ(
63+
editor.arena.allocator(),
64+
"{d}/{d}",
65+
.{ i + 1, animation.length },
66+
);
67+
68+
if (imgui.selectableEx(
69+
other_frame,
70+
animation.start + i == file.selected_animation_index,
71+
imgui.SelectableFlags_None,
72+
.{ .x = 0.0, .y = 0.0 },
73+
)) {
74+
file.flipbook_scroll_request = .{
75+
.from = file.flipbook_scroll,
76+
.to = file.flipbookScrollFromSpriteIndex(animation.start + i),
77+
.state = file.selected_animation_state,
78+
};
6879
}
6980
}
7081
}
@@ -87,7 +98,7 @@ pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, app: *App, editor: *Edi
8798
// Apply history of animation state
8899
var change: History.Change = .{ .animation = .{
89100
.index = file.selected_animation_index,
90-
.name = [_:0]u8{0} ** 128,
101+
.name = [_:0]u8{0} ** Editor.Constants.animation_name_max_length,
91102
.fps = animation.fps,
92103
.start = animation.start,
93104
.length = animation.length,
@@ -123,9 +134,13 @@ pub fn draw(file: *pixi.Internal.File, mouse_ratio: f32, app: *App, editor: *Edi
123134
var keyframe_animation_index: usize = 0;
124135
while (keyframe_animation_index < file.keyframe_animations.slice().len) : (keyframe_animation_index += 1) {
125136
const a = &file.keyframe_animations.slice().get(keyframe_animation_index);
126-
if (imgui.selectableEx(a.name, keyframe_animation_index == file.selected_keyframe_animation_index, imgui.SelectableFlags_None, .{ .x = 0.0, .y = 0.0 })) {
137+
if (imgui.selectableEx(
138+
a.name,
139+
keyframe_animation_index == file.selected_keyframe_animation_index,
140+
imgui.SelectableFlags_None,
141+
.{ .x = 0.0, .y = 0.0 },
142+
))
127143
file.selected_keyframe_animation_index = keyframe_animation_index;
128-
}
129144
}
130145
}
131146
}

src/editor/artboard/flipbook/timeline.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const mach = @import("mach");
44

55
const Core = mach.Core;
66
const App = pixi.App;
7+
const Editor = pixi.Editor;
78

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

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

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

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

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

489489
// We are using a load on the gpu texture, so we need to clear this texture on the gpu after we are done
490490
@memset(file.keyframe_animation_texture.image.data, 0.0);
491-
file.keyframe_animation_texture.update(core.windows.get(app.window, .device));
491+
file.keyframe_animation_texture.update(pixi.core.windows.get(pixi.app.window, .device));
492492
}
493493
}
494494
}

src/editor/artboard/rulers.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const pixi = @import("../../pixi.zig");
33

44
const Core = @import("mach").Core;
55
const App = pixi.App;
6-
6+
const Editor = pixi.Editor;
77
const imgui = @import("zig-imgui");
88

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

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

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

72-
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);
70+
draw_list.addText(
71+
.{ .x = tl[0], .y = tl[1] + offset[1] + (tile_height / 2.0 * file.camera.zoom) - (text_size.y / 2.0) },
72+
pixi.editor.theme.text_secondary.toU32(),
73+
text.ptr,
74+
);
7375
}
7476
draw_list.addLineEx(
7577
.{ .x = tl[0], .y = tl[1] + offset[1] },

src/editor/explorer/Explorer.zig

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
8787
.files => {
8888
if (imgui.beginMenuBar()) {
8989
if (editor.hotkeys.hotkey(.{ .sidebar = .files })) |hotkey| {
90-
const title = try std.fmt.allocPrintZ(app.allocator, "Explorer ({s})", .{hotkey.shortcut});
91-
defer app.allocator.free(title);
92-
90+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Explorer ({s})", .{hotkey.shortcut});
9391
imgui.separatorText(title);
9492
} else {
9593
imgui.separatorText("Explorer");
@@ -103,9 +101,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
103101
.tools => {
104102
if (imgui.beginMenuBar()) {
105103
if (editor.hotkeys.hotkey(.{ .sidebar = .tools })) |hotkey| {
106-
const title = try std.fmt.allocPrintZ(app.allocator, "Tools ({s})", .{hotkey.shortcut});
107-
defer app.allocator.free(title);
108-
104+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Tools ({s})", .{hotkey.shortcut});
109105
imgui.separatorText(title);
110106
} else {
111107
imgui.separatorText("Tools");
@@ -120,9 +116,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
120116
.sprites => {
121117
if (imgui.beginMenuBar()) {
122118
if (editor.hotkeys.hotkey(.{ .sidebar = .sprites })) |hotkey| {
123-
const title = try std.fmt.allocPrintZ(app.allocator, "Sprites ({s})", .{hotkey.shortcut});
124-
defer app.allocator.free(title);
125-
119+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Sprites ({s})", .{hotkey.shortcut});
126120
imgui.separatorText(title);
127121
} else {
128122
imgui.separatorText("Sprites");
@@ -136,9 +130,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
136130
.animations => {
137131
if (imgui.beginMenuBar()) {
138132
if (editor.hotkeys.hotkey(.{ .sidebar = .animations })) |hotkey| {
139-
const title = try std.fmt.allocPrintZ(app.allocator, "Animations ({s})", .{hotkey.shortcut});
140-
defer app.allocator.free(title);
141-
133+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Animations ({s})", .{hotkey.shortcut});
142134
imgui.separatorText(title);
143135
} else {
144136
imgui.separatorText("Animations");
@@ -152,9 +144,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
152144
.keyframe_animations => {
153145
if (imgui.beginMenuBar()) {
154146
if (editor.hotkeys.hotkey(.{ .sidebar = .keyframe_animations })) |hotkey| {
155-
const title = try std.fmt.allocPrintZ(app.allocator, "Keyframe Animations ({s})", .{hotkey.shortcut});
156-
defer app.allocator.free(title);
157-
147+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Keyframe Animations ({s})", .{hotkey.shortcut});
158148
imgui.separatorText(title);
159149
} else {
160150
imgui.separatorText("Keyframe Animations");
@@ -168,9 +158,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
168158
.pack => {
169159
if (imgui.beginMenuBar()) {
170160
if (editor.hotkeys.hotkey(.{ .sidebar = .pack })) |hotkey| {
171-
const title = try std.fmt.allocPrintZ(app.allocator, "Packing ({s})", .{hotkey.shortcut});
172-
defer app.allocator.free(title);
173-
161+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Packing ({s})", .{hotkey.shortcut});
174162
imgui.separatorText(title);
175163
} else {
176164
imgui.separatorText("Packing");
@@ -184,9 +172,7 @@ pub fn draw(core: *Core, app: *App, editor: *Editor, explorer: *Explorer, packer
184172
.settings => {
185173
if (imgui.beginMenuBar()) {
186174
if (editor.hotkeys.hotkey(.{ .sidebar = .settings })) |hotkey| {
187-
const title = try std.fmt.allocPrintZ(app.allocator, "Settings ({s})", .{hotkey.shortcut});
188-
defer app.allocator.free(title);
189-
175+
const title = try std.fmt.allocPrintZ(editor.arena.allocator(), "Settings ({s})", .{hotkey.shortcut});
190176
imgui.separatorText(title);
191177
} else {
192178
imgui.separatorText("Settings");

0 commit comments

Comments
 (0)