Skip to content

Commit

Permalink
More modules Pt 1
Browse files Browse the repository at this point in the history
  • Loading branch information
foxnne committed Jan 14, 2025
1 parent 648285c commit f5b3dd4
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 297 deletions.
2 changes: 0 additions & 2 deletions src/Pixi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ pub fn init(_app: *App, _core: *Core, app_mod: mach.Mod(App), _editor: *Editor)
const path = std.fs.selfExeDirPath(buffer[0..]) catch ".";
std.posix.chdir(path) catch {};

std.log.debug("Root path: {s}", .{path});

const window = try core.windows.new(.{
.title = "Pixi",
.vsync_mode = .double,
Expand Down
43 changes: 12 additions & 31 deletions src/editor/Editor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const imgui = @import("zig-imgui");
const zmath = @import("zmath");

pub const Editor = @This();
pub const Sidebar = @import("sidebar/Sidebar1.zig");
pub const Explorer = @import("explorer/Explorer1.zig");
pub const Artboard = @import("artboard/Artboard1.zig");
pub const Popups = @import("popups/Popups.zig");

pub const mach_module = .editor;
pub const mach_systems = .{ .init, .tick, .deinit };
Expand All @@ -18,51 +22,28 @@ pub const Theme = @import("Theme.zig");

theme: Theme,

pub const sidebar = @import("sidebar/sidebar.zig");
pub const explorer = @import("explorer/explorer.zig");
pub const artboard = @import("artboard/artboard.zig");

pub const popup_rename = @import("popups/rename.zig");
pub const popup_file_setup = @import("popups/file_setup.zig");
pub const popup_about = @import("popups/about.zig");
pub const popup_file_confirm_close = @import("popups/file_confirm_close.zig");
pub const popup_layer_setup = @import("popups/layer_setup.zig");
pub const popup_export_to_png = @import("popups/export_png.zig");
pub const popup_animation = @import("popups/animation.zig");
pub const popup_heightmap = @import("popups/heightmap.zig");
pub const popup_references = @import("popups/references.zig");

pub fn init(app: *Pixi, editor: *Editor) !void {
pub fn init(core: *Core, app: *Pixi, editor: *Editor) !void {
const theme_path = try std.fs.path.joinZ(app.allocator, &.{ Pixi.assets.themes, app.settings.theme });
defer app.allocator.free(theme_path);

editor.* = .{
.theme = try Editor.Theme.loadFromFile(theme_path),
};

editor.theme.init(Pixi.core, app);
editor.theme.init(core, app);
}

pub fn tick(app: *Pixi, core: *Core, editor: *Editor) !void {
imgui.pushStyleVarImVec2(imgui.StyleVar_SeparatorTextAlign, .{ .x = Pixi.app.settings.explorer_title_align, .y = 0.5 });
pub fn tick(core: *Core, app: *Pixi, editor: *Editor) !void {
imgui.pushStyleVarImVec2(imgui.StyleVar_SeparatorTextAlign, .{ .x = app.settings.explorer_title_align, .y = 0.5 });
defer imgui.popStyleVar();

editor.theme.push(core, app);
defer editor.theme.pop();

try sidebar.draw();
try explorer.draw(core);
try artboard.draw(core);

try popup_rename.draw();
try popup_file_setup.draw();
try popup_about.draw();
try popup_file_confirm_close.draw();
try popup_layer_setup.draw();
try popup_export_to_png.draw();
try popup_animation.draw();
try popup_heightmap.draw();
try popup_references.draw();
try Sidebar.draw(core, app);
try Explorer.draw(core, app);
try Artboard.draw(core, app);
try Popups.draw(core, app);
}

pub fn setProjectFolder(path: [:0]const u8) !void {
Expand Down
116 changes: 58 additions & 58 deletions src/editor/artboard/artboard.zig → src/editor/artboard/Artboard1.zig

Large diffs are not rendered by default.

74 changes: 37 additions & 37 deletions src/editor/artboard/canvas.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ const Core = @import("mach").Core;
const imgui = @import("zig-imgui");
const zmath = @import("zmath");

pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core, app: *Pixi) !void {
const transforming = file.transform_texture != null;

{
const shadow_color = Pixi.math.Color.initFloats(0.0, 0.0, 0.0, Pixi.app.settings.shadow_opacity).toU32();
const shadow_color = Pixi.math.Color.initFloats(0.0, 0.0, 0.0, app.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) - Pixi.app.settings.shadow_length * Pixi.app.content_scale[1] },
.{ .x = pos.x, .y = (pos.y + height) - app.settings.shadow_length * app.content_scale[1] },
.{ .x = pos.x + width, .y = pos.y + height },
0x0,
0x0,
Expand Down Expand Up @@ -67,11 +67,11 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {

imgui.text("Transformation");
imgui.separator();
if (imgui.button("Confirm") or (core.keyPressed(Core.Key.enter) and Pixi.app.open_file_index == Pixi.Editor.getFileIndex(file.path).?)) {
if (imgui.button("Confirm") or (core.keyPressed(Core.Key.enter) and app.open_file_index == Pixi.Editor.getFileIndex(file.path).?)) {
transform_texture.confirm = true;
}
imgui.sameLine();
if (imgui.button("Cancel") or (core.keyPressed(Core.Key.escape) and Pixi.app.open_file_index == Pixi.Editor.getFileIndex(file.path).?)) {
if (imgui.button("Cancel") or (core.keyPressed(Core.Key.escape) and app.open_file_index == Pixi.Editor.getFileIndex(file.path).?)) {
var change = try file.buffers.stroke.toChange(@intCast(file.selected_layer_index));
change.pixels.temporary = true;
try file.history.append(change);
Expand Down Expand Up @@ -104,22 +104,22 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
//if (file.transform_texture == null)
file.temporary_layer.clear(true);

Pixi.app.selection_time += Pixi.app.delta_time;
if (Pixi.app.selection_time >= 0.3) {
app.selection_time += app.delta_time;
if (app.selection_time >= 0.3) {
for (file.selection_layer.pixels()) |*pixel| {
if (pixel[3] != 0) {
if (pixel[0] != 0) pixel[0] = 0 else pixel[0] = 255;
if (pixel[1] != 0) pixel[1] = 0 else pixel[1] = 255;
if (pixel[2] != 0) pixel[2] = 0 else pixel[2] = 255;
}
}
file.selection_layer.texture.update(Pixi.core.windows.get(Pixi.app.window, .device));
Pixi.app.selection_time = 0.0;
Pixi.app.selection_invert = !Pixi.app.selection_invert;
file.selection_layer.texture.update(core.windows.get(app.window, .device));
app.selection_time = 0.0;
app.selection_invert = !app.selection_invert;
}

if (imgui.isWindowHovered(imgui.HoveredFlags_None)) {
const mouse_position = Pixi.app.mouse.position;
const mouse_position = app.mouse.position;

if (file.camera.pixelCoordinates(.{
.texture_position = canvas_center_offset,
Expand All @@ -135,7 +135,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
const x = @as(f32, @floatFromInt(tile_column)) * tile_width + canvas_center_offset[0];
const y = @as(f32, @floatFromInt(tile_row)) * tile_height + canvas_center_offset[1];

if (Pixi.app.sidebar != .pack)
if (app.sidebar != .pack)
file.camera.drawTexture(file.background.view_handle, file.tile_width, file.tile_height, .{ x, y }, 0x88FFFFFF);

try file.processStrokeTool(.primary, .{});
Expand All @@ -144,14 +144,14 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
try file.processSampleTool(.primary, .{});
try file.processSelectionTool(.primary, .{});

if (Pixi.app.mouse.button(.primary)) |primary| {
if (app.mouse.button(.primary)) |primary| {
if (primary.pressed()) {
const tiles_wide = @divExact(@as(usize, @intCast(file.width)), @as(usize, @intCast(file.tile_width)));
const tile_index = tile_column + tile_row * tiles_wide;

if (Pixi.app.sidebar == .sprites or file.flipbook_view == .timeline) {
if (app.sidebar == .sprites or file.flipbook_view == .timeline) {
try file.makeSpriteSelection(tile_index);
} else if (Pixi.app.tools.current != .animation) {
} else if (app.tools.current != .animation) {
// Ensure we only set the request state on the first set.
if (file.flipbook_scroll_request) |*request| {
request.elapsed = 0.0;
Expand All @@ -164,9 +164,9 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
}
}
} else {
if (Pixi.app.mouse.button(.primary)) |primary| {
if (app.mouse.button(.primary)) |primary| {
if (primary.released()) {
if (Pixi.app.sidebar == .sprites or file.flipbook_view == .timeline) {
if (app.sidebar == .sprites or file.flipbook_view == .timeline) {
file.selected_sprites.clearAndFree();
}
}
Expand All @@ -190,9 +190,9 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
zmath.orthographicLh(width, height, -100, 100),
) };

try Pixi.app.batcher.begin(.{
.pipeline_handle = Pixi.app.pipeline_default,
.compute_pipeline_handle = Pixi.app.pipeline_compute,
try app.batcher.begin(.{
.pipeline_handle = app.pipeline_default,
.compute_pipeline_handle = app.pipeline_compute,
.bind_group_handle = transform_bindgroup,
.compute_bind_group_handle = compute_bindgroup,
.output_texture = &file.temporary_layer.texture,
Expand All @@ -211,7 +211,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
}
//pivot += zmath.loadArr2(.{ canvas_center_offset[0], canvas_center_offset[1] });

try Pixi.app.batcher.transformTexture(
try app.batcher.transformTexture(
transform_texture.vertices,
.{ canvas_center_offset[0], -canvas_center_offset[1] },
.{ pivot[0], -pivot[1] },
Expand All @@ -220,7 +220,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
},
);

try Pixi.app.batcher.end(uniforms, Pixi.app.uniform_buffer_default);
try app.batcher.end(uniforms, app.uniform_buffer_default);
}
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
}
}

if (Pixi.app.tools.current == .selection) {
if (app.tools.current == .selection) {
file.camera.drawLayer(file.selection_layer, canvas_center_offset);
}

Expand All @@ -265,14 +265,14 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
if (file.camera.zoom >= 30.0) {
if (file.camera.pixelCoordinates(.{
.texture_position = canvas_center_offset,
.position = Pixi.app.mouse.position,
.position = app.mouse.position,
.width = file.width,
.height = file.height,
})) |pixel_coord| {
const temp_x = @as(usize, @intFromFloat(pixel_coord[0]));
const temp_y = @as(usize, @intFromFloat(pixel_coord[1]));
const position = .{ pixel_coord[0] + canvas_center_offset[0] + 0.2, pixel_coord[1] + canvas_center_offset[1] + 0.25 };
try file.camera.drawText("{d}", .{Pixi.app.colors.height}, position, 0xFFFFFFFF);
try file.camera.drawText("{d}", .{app.colors.height}, position, 0xFFFFFFFF);

const min: [2]u32 = .{
@intCast(@max(@as(i32, @intCast(temp_x)) - 5, 0)),
Expand Down Expand Up @@ -305,7 +305,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
{
const tiles_wide = @divExact(file.width, file.tile_width);

if (Pixi.app.sidebar == .sprites and !transforming or file.flipbook_view == .timeline) {
if (app.sidebar == .sprites and !transforming or file.flipbook_view == .timeline) {
if (file.selected_sprites.items.len > 0) {
for (file.selected_sprites.items) |sprite_index| {
const column = @mod(@as(u32, @intCast(sprite_index)), tiles_wide);
Expand All @@ -332,7 +332,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
);
}
}
} else if (Pixi.app.sidebar != .pack and !transforming) {
} else if (app.sidebar != .pack and !transforming) {
const column = @mod(@as(u32, @intCast(file.selected_sprite_index)), tiles_wide);
const row = @divTrunc(@as(u32, @intCast(file.selected_sprite_index)), tiles_wide);
const x = @as(f32, @floatFromInt(column)) * tile_width + canvas_center_offset[0];
Expand All @@ -342,17 +342,17 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
file.camera.drawRect(rect, 3.0, Pixi.editor.theme.text.toU32());
}

if (Pixi.app.popups.animation_length > 0 and Pixi.app.tools.current == .animation and !transforming) {
if (Pixi.app.mouse.button(.primary)) |primary| {
if (primary.down() or Pixi.app.popups.animation) {
const start_column = @mod(@as(u32, @intCast(Pixi.app.popups.animation_start)), tiles_wide);
const start_row = @divTrunc(@as(u32, @intCast(Pixi.app.popups.animation_start)), tiles_wide);
if (app.popups.animation_length > 0 and app.tools.current == .animation and !transforming) {
if (app.mouse.button(.primary)) |primary| {
if (primary.down() or app.popups.animation) {
const start_column = @mod(@as(u32, @intCast(app.popups.animation_start)), tiles_wide);
const start_row = @divTrunc(@as(u32, @intCast(app.popups.animation_start)), tiles_wide);
const start_x = @as(f32, @floatFromInt(start_column)) * tile_width + canvas_center_offset[0];
const start_y = @as(f32, @floatFromInt(start_row)) * tile_height + canvas_center_offset[1];
const start_rect: [4]f32 = .{ start_x, start_y, tile_width, tile_height };

const end_column = @mod(@as(u32, @intCast(Pixi.app.popups.animation_start + Pixi.app.popups.animation_length - 1)), tiles_wide);
const end_row = @divTrunc(@as(u32, @intCast(Pixi.app.popups.animation_start + Pixi.app.popups.animation_length - 1)), tiles_wide);
const end_column = @mod(@as(u32, @intCast(app.popups.animation_start + app.popups.animation_length - 1)), tiles_wide);
const end_row = @divTrunc(@as(u32, @intCast(app.popups.animation_start + app.popups.animation_length - 1)), tiles_wide);
const end_x = @as(f32, @floatFromInt(end_column)) * tile_width + canvas_center_offset[0];
const end_y = @as(f32, @floatFromInt(end_row)) * tile_height + canvas_center_offset[1];
const end_rect: [4]f32 = .{ end_x, end_y, tile_width, tile_height };
Expand All @@ -363,7 +363,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
}

if (file.animations.slice().len > 0) {
if (Pixi.app.tools.current == .animation and !transforming) {
if (app.tools.current == .animation and !transforming) {
var i: usize = 0;
while (i < file.animations.slice().len) : (i += 1) {
const animation = &file.animations.slice().get(i);
Expand All @@ -379,10 +379,10 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) !void {
const end_y = @as(f32, @floatFromInt(end_row)) * tile_height + canvas_center_offset[1];
const end_rect: [4]f32 = .{ end_x, end_y, tile_width, tile_height };

const thickness: f32 = if (i == file.selected_animation_index and (if (Pixi.app.mouse.button(.primary)) |primary| primary.up() else false and !Pixi.app.popups.animation)) 4.0 else 2.0;
const thickness: f32 = if (i == file.selected_animation_index and (if (app.mouse.button(.primary)) |primary| primary.up() else false and !app.popups.animation)) 4.0 else 2.0;
file.camera.drawAnimationRect(start_rect, end_rect, thickness, Pixi.editor.theme.highlight_primary.toU32(), Pixi.editor.theme.text_red.toU32());
}
} else if (Pixi.app.sidebar != .pack and !transforming and Pixi.app.sidebar != .keyframe_animations) {
} else if (app.sidebar != .pack and !transforming and app.sidebar != .keyframe_animations) {
const animation = file.animations.slice().get(file.selected_animation_index);

const start_column = @mod(@as(u32, @intCast(animation.start)), tiles_wide);
Expand Down
10 changes: 5 additions & 5 deletions src/editor/artboard/canvas_pack.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const std = @import("std");
const Pixi = @import("../../Pixi.zig");
const core = @import("mach").core;
const Core = @import("mach").Core;
const imgui = @import("zig-imgui");

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

pub fn draw(mode: PackTexture) void {
pub fn draw(mode: PackTexture, app: *Pixi, _: *Core) void {
if (switch (mode) {
.diffusemap => Pixi.app.atlas.diffusemap,
.heightmap => Pixi.app.atlas.heightmap,
.diffusemap => app.atlas.diffusemap,
.heightmap => app.atlas.heightmap,
}) |texture| {
var canvas_flags: imgui.WindowFlags = 0;
canvas_flags |= imgui.WindowFlags_HorizontalScrollbar;
Expand All @@ -26,7 +26,7 @@ pub fn draw(mode: PackTexture) void {
const file_width = @as(f32, @floatFromInt(texture.image.width));
const file_height = @as(f32, @floatFromInt(texture.image.height));

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

// Handle zooming, panning and extents
{
Expand Down
Loading

0 comments on commit f5b3dd4

Please sign in to comment.