Skip to content

Commit

Permalink
Remove useage of content_scale in editor code
Browse files Browse the repository at this point in the history
  • Loading branch information
foxnne committed Jan 17, 2025
1 parent 08248d0 commit aeefd02
Show file tree
Hide file tree
Showing 26 changed files with 178 additions and 169 deletions.
48 changes: 20 additions & 28 deletions src/Pixi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const imgui_mach = imgui.backends.mach;
const Core = mach.Core;
pub const App = @This();
pub const Editor = @import("editor/Editor.zig");
pub const Popups = @import("editor/popups/Popups.zig");

// Global pointers
pub var core: *Core = undefined;
Expand Down Expand Up @@ -132,13 +131,17 @@ pub fn init(_app: *App, _core: *Core, app_mod: mach.Mod(App), _editor: *Editor,
pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {
const window = core.windows.getValue(app.window);

// Now that we have a valid device, we can initialize our pipelines
try gfx.init(app);

// Initialize zstbi to load assets
zstbi.init(app.allocator);

// Load assets
app.loaded_assets = try LoadedAssets.init(app.allocator);
app.mouse = try input.Mouse.initDefault(app.allocator);

app.mouse = try input.Mouse.initDefault(app.allocator);
app.packer = try Packer.init(app.allocator);

app.batcher = try gfx.Batcher.init(app.allocator, 1000);

app.window_size = .{ @floatFromInt(window.width), @floatFromInt(window.height) };
Expand All @@ -147,12 +150,6 @@ pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {
app.framebuffer_size[0] / app.window_size[0],
app.framebuffer_size[1] / app.window_size[1],
};
// TODO: Remove usage of content_scale if it isn't needed
app.content_scale = .{ 1.0, 1.0 };

const scale_factor = app.content_scale[1];

try gfx.init(app);

imgui.setZigAllocator(&app.allocator);

Expand All @@ -168,6 +165,7 @@ pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {
io.config_flags |= imgui.ConfigFlags_NavEnableKeyboard;
io.display_framebuffer_scale = .{ .x = app.content_scale[0], .y = app.content_scale[1] };
io.font_global_scale = 1.0;

var cozette_config: imgui.FontConfig = std.mem.zeroes(imgui.FontConfig);
cozette_config.font_data_owned_by_atlas = true;
cozette_config.oversample_h = 2;
Expand All @@ -177,7 +175,7 @@ pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {
cozette_config.rasterizer_density = 1.0;
cozette_config.ellipsis_char = imgui.UNICODE_CODEPOINT_MAX;

_ = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/CozetteVector.ttf", editor.settings.font_size * scale_factor, &cozette_config, null);
_ = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/CozetteVector.ttf", editor.settings.font_size, &cozette_config, null);

var fa_config: imgui.FontConfig = std.mem.zeroes(imgui.FontConfig);
fa_config.merge_mode = true;
Expand All @@ -190,8 +188,8 @@ pub fn lateInit(editor_mod: mach.Mod(Editor)) !void {
fa_config.ellipsis_char = imgui.UNICODE_CODEPOINT_MAX;
const ranges: []const u16 = &.{ 0xf000, 0xf976, 0 };

app.fonts.fa_standard_solid = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/fa-solid-900.ttf", editor.settings.font_size * scale_factor, &fa_config, @ptrCast(ranges.ptr)).?;
app.fonts.fa_standard_regular = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/fa-regular-400.ttf", editor.settings.font_size * scale_factor, &fa_config, @ptrCast(ranges.ptr)).?;
app.fonts.fa_standard_solid = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/fa-solid-900.ttf", editor.settings.font_size, &fa_config, @ptrCast(ranges.ptr)).?;
app.fonts.fa_standard_regular = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/fa-regular-400.ttf", editor.settings.font_size, &fa_config, @ptrCast(ranges.ptr)).?;

// Initialize the editor which loads our theme
editor_mod.call(.lateInit);
Expand All @@ -217,6 +215,7 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
editor.hotkeys.setHotkeyState(key_release.key, key_release.mods, .release);
},
.mouse_scroll => |mouse_scroll| {
// TODO: Fix this in the editor code, we dont want to block mouse input based on popups
if (!editor.popups.anyPopupOpen()) { // Only record mouse scrolling for canvases when popups are closed
app.mouse.scroll_x = mouse_scroll.xoffset;
app.mouse.scroll_y = mouse_scroll.yoffset;
Expand All @@ -226,7 +225,7 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
app.mouse.magnify = gesture.zoom;
},
.mouse_motion => |mouse_motion| {
app.mouse.position = .{ @floatCast(mouse_motion.pos.x * app.content_scale[0]), @floatCast(mouse_motion.pos.y * app.content_scale[1]) };
app.mouse.position = .{ @floatCast(mouse_motion.pos.x), @floatCast(mouse_motion.pos.y) };
},
.mouse_press => |mouse_press| {
app.mouse.setButtonState(mouse_press.button, mouse_press.mods, .press);
Expand All @@ -235,19 +234,9 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
app.mouse.setButtonState(mouse_release.button, mouse_release.mods, .release);
},
.close => {
var should_close = true;
for (editor.open_files.items) |file| {
if (file.dirty()) {
should_close = false;
}
}

if (!should_close and !editor.popups.file_confirm_close_exit) {
editor.popups.file_confirm_close = true;
editor.popups.file_confirm_close_state = .all;
editor.popups.file_confirm_close_exit = true;
}
app.should_close = should_close;
// Currently, just pass along this message to the editor
// and allow the editor to set the app.should_close or not
editor_mod.call(.close);
},
.window_resize => |resize| {
const window = core.windows.getValue(app.window);
Expand All @@ -262,7 +251,7 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
// Currently content scale is set to 1.0x1.0 because the scaling is handled by
// zig-imgui. Tested both on Windows (1.0 content scale) and macOS (2.0 content scale)
// If we can confirm that this is not needed, we can purge the use of content_scale from editor files
app.content_scale = .{ 1.0, 1.0 };
//app.content_scale = .{ 1.0, 1.0 };
},

else => {},
Expand All @@ -276,6 +265,8 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
// New imgui frame
try imgui_mach.newFrame();
imgui.newFrame();

// Update times
app.delta_time = app.timer.lap();
app.total_time += app.delta_time;

Expand All @@ -288,6 +279,7 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {
// Render imgui
imgui.render();

// Pass commands to the window queue for presenting
if (window.swap_chain.getCurrentTextureView()) |back_buffer_view| {
defer back_buffer_view.release();

Expand Down Expand Up @@ -340,8 +332,8 @@ pub fn tick(app_mod: mach.Mod(App), editor_mod: mach.Mod(Editor)) !void {

app.mouse.previous_position = app.mouse.position;

// Finally, close if we should and aren't in the middle of saving
if (app.should_close and !editor.saving()) {
// Close!
core.exit();
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/editor/Editor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub const mach_systems = .{
.lateInit,
.processDialogRequest,
.tick,
.close,
.deinit,
};

Expand Down Expand Up @@ -208,6 +209,22 @@ pub fn tick(
}
}

pub fn close(app: *Pixi, editor: *Editor) void {
var should_close = true;
for (editor.open_files.items) |file| {
if (file.dirty()) {
should_close = false;
}
}

if (!should_close and !editor.popups.file_confirm_close_exit) {
editor.popups.file_confirm_close = true;
editor.popups.file_confirm_close_state = .all;
editor.popups.file_confirm_close_exit = true;
}
app.should_close = should_close;
}

pub fn setProjectFolder(editor: *Editor, path: [:0]const u8) !void {
if (editor.project_folder) |folder| {
Pixi.app.allocator.free(folder);
Expand Down
2 changes: 1 addition & 1 deletion src/editor/Sidebar.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn draw(app: *Pixi, editor: *Editor) !void {
.y = 0.0,
}, imgui.Cond_Always);
imgui.setNextWindowSize(.{
.x = editor.settings.sidebar_width * app.content_scale[0],
.x = editor.settings.sidebar_width,
.y = app.window_size[1],
}, imgui.Cond_None);
imgui.pushStyleVarImVec2(imgui.StyleVar_SelectableTextAlign, .{ .x = 0.5, .y = 0.5 });
Expand Down
37 changes: 28 additions & 9 deletions src/editor/artboard/Artboard.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void
imgui.pushStyleVar(imgui.StyleVar_WindowRounding, 0.0);
defer imgui.popStyleVar();
imgui.setNextWindowPos(.{
.x = (editor.settings.sidebar_width + editor.settings.explorer_width + editor.settings.explorer_grip) * app.content_scale[0],
.x = editor.settings.sidebar_width + editor.settings.explorer_width + editor.settings.explorer_grip,
.y = 0.0,
}, imgui.Cond_Always);
imgui.setNextWindowSize(.{
.x = (app.window_size[0] - ((editor.settings.explorer_width + editor.settings.sidebar_width + editor.settings.explorer_grip)) * app.content_scale[0]),
.y = (app.window_size[1] + 5.0) * app.content_scale[1],
.x = app.window_size[0] - editor.settings.explorer_width - editor.settings.sidebar_width - editor.settings.explorer_grip,
.y = app.window_size[1] + 5.0,
}, imgui.Cond_None);

imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 0.0, .y = 0.5 });
Expand All @@ -57,7 +57,26 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void
art_flags |= imgui.WindowFlags_NoBringToFrontOnFocus;

if (imgui.begin("Art", null, art_flags)) {
try menu.draw(app, core, editor);
try menu.draw(editor);

defer {
const shadow_color = Pixi.math.Color.initFloats(0.0, 0.0, 0.0, editor.settings.shadow_opacity).toU32();
// Draw a shadow fading from bottom to top
const pos = imgui.getWindowPos();
const height = imgui.getWindowHeight();
const width = imgui.getWindowWidth();

if (imgui.getWindowDrawList()) |draw_list| {
draw_list.addRectFilledMultiColor(
.{ .x = pos.x, .y = (pos.y + height) - editor.settings.shadow_length },
.{ .x = pos.x + width, .y = pos.y + height },
0x0,
0x0,
shadow_color,
shadow_color,
);
}
}

const art_width = imgui.getWindowWidth();

Expand Down Expand Up @@ -154,7 +173,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void
}

if (imgui.isItemHovered(imgui.HoveredFlags_DelayNormal)) {
imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 4.0 * app.content_scale[0], .y = 4.0 * app.content_scale[1] });
imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 4.0, .y = 4.0 });
defer imgui.popStyleVar();
if (imgui.beginTooltip()) {
defer imgui.endTooltip();
Expand Down Expand Up @@ -226,7 +245,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void

if (editor.explorer.pane != .pack) {
if (editor.open_files.items.len > 0) {
const flipbook_height = window_height - artboard_height - editor.settings.info_bar_height * app.content_scale[1];
const flipbook_height = window_height - artboard_height - editor.settings.info_bar_height;

var flipbook_flags: imgui.WindowFlags = 0;
flipbook_flags |= imgui.WindowFlags_MenuBar;
Expand All @@ -253,7 +272,7 @@ pub fn draw(artboard: *Artboard, core: *Core, app: *Pixi, editor: *Editor) !void
imgui.pushStyleColorImVec4(imgui.Col_ChildBg, Pixi.editor.theme.highlight_primary.toImguiVec4());
defer imgui.popStyleColor();
if (imgui.beginChild("InfoBar", .{ .x = -1.0, .y = 0.0 }, imgui.ChildFlags_None, imgui.WindowFlags_ChildWindow)) {
infobar.draw(app, core, editor);
infobar.draw(editor);
}
imgui.endChild();
}
Expand All @@ -280,8 +299,8 @@ pub fn drawLogoScreen(app: *Pixi, editor: *Editor) !void {
@floatFromInt(logo_sprite.source[3]),
};

const w = src[2] * 32.0 * app.content_scale[0];
const h = src[3] * 32.0 * app.content_scale[0];
const w = src[2] * 32.0;
const h = src[3] * 32.0;
const center: [2]f32 = .{ imgui.getWindowWidth() / 2.0, imgui.getWindowHeight() / 2.0 };

const inv_w = 1.0 / @as(f32, @floatFromInt(app.loaded_assets.atlas_png.image.width));
Expand Down
19 changes: 0 additions & 19 deletions src/editor/artboard/canvas.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,6 @@ const zmath = @import("zmath");
pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core, app: *Pixi, editor: *Editor) !void {
const transforming = file.transform_texture != null;

{
const shadow_color = Pixi.math.Color.initFloats(0.0, 0.0, 0.0, editor.settings.shadow_opacity).toU32();
// Draw a shadow fading from bottom to top
const pos = imgui.getWindowPos();
const height = imgui.getWindowHeight();
const width = imgui.getWindowWidth();

if (imgui.getWindowDrawList()) |draw_list| {
draw_list.addRectFilledMultiColor(
.{ .x = pos.x, .y = (pos.y + height) - editor.settings.shadow_length * app.content_scale[1] },
.{ .x = pos.x + width, .y = pos.y + height },
0x0,
0x0,
shadow_color,
shadow_color,
);
}
}

const window_width = imgui.getWindowWidth();
const window_height = imgui.getWindowHeight();
const file_width = @as(f32, @floatFromInt(file.width));
Expand Down
8 changes: 4 additions & 4 deletions src/editor/artboard/flipbook/menu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, mouse_ratio: f32, app: *Pixi,
const animation = &file.animations.slice().get(file.selected_animation_index);

{ // Animation Selection
imgui.setNextItemWidth(imgui.calcTextSize(animation.name).x + 40 * app.content_scale[0]);
imgui.setNextItemWidth(imgui.calcTextSize(animation.name).x + 40);
if (imgui.beginCombo("Animation ", animation.name, imgui.ComboFlags_HeightLargest)) {
defer imgui.endCombo();
var animation_index: usize = 0;
Expand All @@ -54,7 +54,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, mouse_ratio: f32, app: *Pixi,
const frame = try std.fmt.allocPrintZ(app.allocator, "{d}/{d}", .{ current_frame + 1, animation.length });
defer app.allocator.free(frame);

imgui.setNextItemWidth(imgui.calcTextSize(frame).x + 40 * app.content_scale[0]);
imgui.setNextItemWidth(imgui.calcTextSize(frame).x + 40);
if (imgui.beginCombo("Frame ", frame, imgui.ComboFlags_None)) {
defer imgui.endCombo();
for (0..animation.length) |i| {
Expand All @@ -69,7 +69,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, mouse_ratio: f32, app: *Pixi,
}

{ // FPS Selection
imgui.setNextItemWidth(100 * app.content_scale[0]);
imgui.setNextItemWidth(100);
var fps = @as(i32, @intCast(animation.fps));
var changed: bool = false;
if (imgui.sliderInt(
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, mouse_ratio: f32, app: *Pixi,
const animation = &file.keyframe_animations.slice().get(file.selected_keyframe_animation_index);

{ // Animation Selection
imgui.setNextItemWidth(imgui.calcTextSize(animation.name).x + 40 * app.content_scale[0]);
imgui.setNextItemWidth(imgui.calcTextSize(animation.name).x + 40);
if (imgui.beginCombo("Animation ", animation.name, imgui.ComboFlags_HeightLargest)) {
defer imgui.endCombo();
var keyframe_animation_index: usize = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/editor/artboard/infobar.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ const imgui = @import("zig-imgui");

const spacer: [:0]const u8 = " ";

pub fn draw(app: *Pixi, _: *Core, editor: *Editor) void {
pub fn draw(editor: *Editor) void {
imgui.pushStyleColorImVec4(imgui.Col_Text, Pixi.editor.theme.foreground.toImguiVec4());
defer imgui.popStyleColor();

const h = imgui.getTextLineHeightWithSpacing() + 6.0 * app.content_scale[1];
const h = imgui.getTextLineHeightWithSpacing() + 6.0;
const y = (imgui.getContentRegionAvail().y - h) / 2;
const spacing: f32 = 3.0 * app.content_scale[0];
const spacing: f32 = 3.0;
imgui.setCursorPosY(y);
imgui.setCursorPosX(5.0 * app.content_scale[0]);
imgui.setCursorPosX(5.0);

if (editor.project_folder) |path| {
imgui.setCursorPosY(y + 2.0 * app.content_scale[1]);
imgui.setCursorPosY(y + 2.0);
imgui.textColored(editor.theme.foreground.toImguiVec4(), Pixi.fa.folder_open);
imgui.setCursorPosY(y);
imgui.sameLineEx(0.0, spacing);
Expand Down
6 changes: 3 additions & 3 deletions src/editor/artboard/menu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const zstbi = @import("zstbi");
const nfd = @import("nfd");
const imgui = @import("zig-imgui");

pub fn draw(app: *Pixi, _: *Core, editor: *Editor) !void {
imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 10.0 * app.content_scale[0], .y = 10.0 * app.content_scale[1] });
imgui.pushStyleVarImVec2(imgui.StyleVar_ItemSpacing, .{ .x = 6.0 * app.content_scale[0], .y = 6.0 * app.content_scale[1] });
pub fn draw(editor: *Editor) !void {
imgui.pushStyleVarImVec2(imgui.StyleVar_WindowPadding, .{ .x = 10.0, .y = 10.0 });
imgui.pushStyleVarImVec2(imgui.StyleVar_ItemSpacing, .{ .x = 6.0, .y = 6.0 });
defer imgui.popStyleVarEx(2);
imgui.pushStyleColorImVec4(imgui.Col_Text, editor.theme.text_secondary.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_PopupBg, editor.theme.foreground.toImguiVec4());
Expand Down
2 changes: 1 addition & 1 deletion src/editor/artboard/rulers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, app: *Pixi, _: *Core) !void {
defer app.allocator.free(text);

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 * app.content_scale[1] },
.{ .x = tl[0] + offset[0] + (tile_width / 2.0 * file.camera.zoom) - (text_size.x / 2.0), .y = tl[1] + 4.0 },
Pixi.editor.theme.text_secondary.toU32(),
text.ptr,
);
Expand Down
8 changes: 4 additions & 4 deletions src/editor/explorer/Explorer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ pub fn draw(core: *Core, app: *Pixi, editor: *Editor, explorer: *Explorer) !void
const explorer_width = editor.settings.explorer_width;

imgui.setNextWindowPos(.{
.x = editor.settings.sidebar_width * app.content_scale[0],
.x = editor.settings.sidebar_width,
.y = 0,
}, imgui.Cond_Always);
imgui.setNextWindowSize(.{
.x = explorer_width * app.content_scale[0],
.x = explorer_width,
.y = app.window_size[1],
}, imgui.Cond_None);

Expand All @@ -72,8 +72,8 @@ pub fn draw(core: *Core, app: *Pixi, editor: *Editor, explorer: *Explorer) !void
defer imgui.end();
imgui.popStyleVarEx(3);

imgui.pushStyleVarImVec2(imgui.StyleVar_ItemSpacing, .{ .x = 4.0 * app.content_scale[0], .y = 6.0 * app.content_scale[1] });
imgui.pushStyleVarImVec2(imgui.StyleVar_FramePadding, .{ .x = 0.0, .y = 8.0 * app.content_scale[1] });
imgui.pushStyleVarImVec2(imgui.StyleVar_ItemSpacing, .{ .x = 4.0, .y = 6.0 });
imgui.pushStyleVarImVec2(imgui.StyleVar_FramePadding, .{ .x = 0.0, .y = 8.0 });
defer imgui.popStyleVarEx(2);

imgui.pushStyleColorImVec4(imgui.Col_Separator, editor.theme.text_background.toImguiVec4());
Expand Down
Loading

0 comments on commit aeefd02

Please sign in to comment.