Skip to content

Commit

Permalink
Move theme to Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
foxnne committed Jan 3, 2025
1 parent 563cf06 commit 5116bf8
Show file tree
Hide file tree
Showing 37 changed files with 476 additions and 471 deletions.
32 changes: 12 additions & 20 deletions src/Pixi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ settings: Settings = undefined,
hotkeys: input.Hotkeys = undefined,
mouse: input.Mouse = undefined,
sidebar: Sidebar = .files,
theme: Editor.Theme = undefined,
//theme: Editor.Theme = undefined,
project_folder: ?[:0]const u8 = null,
root_path: [:0]const u8 = undefined,
recents: Recents = undefined,
Expand Down Expand Up @@ -88,6 +88,8 @@ test {

pub var state: *App = undefined;
pub var core: *Core = undefined;
pub var editor: *Editor = undefined;

pub var content_scale: [2]f32 = undefined;
pub var window_size: [2]f32 = undefined;
pub var framebuffer_size: [2]f32 = undefined;
Expand Down Expand Up @@ -139,9 +141,10 @@ pub const PackTarget = enum {
single_open,
};

pub fn init(app: *App, _core: *Core, app_mod: mach.Mod(App)) !void {
pub fn init(app: *App, _core: *Core, app_mod: mach.Mod(App), _editor: *Editor) !void {
state = app;
core = _core;
editor = _editor;

core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;
Expand All @@ -166,17 +169,12 @@ pub fn init(app: *App, _core: *Core, app_mod: mach.Mod(App)) !void {

/// This is called from the event fired when the window is done being
/// initialized by the platform
fn lateInit(app: *App, _: *Core) !void {
fn lateInit(app: *App, editor_mod: mach.Mod(Editor)) !void {
const window = core.windows.getValue(app.window);

app.json_allocator = std.heap.ArenaAllocator.init(app.allocator);
app.settings = try Settings.init(app.json_allocator.allocator());

const theme_path = try std.fs.path.joinZ(app.allocator, &.{ assets.themes, app.settings.theme });
defer app.allocator.free(theme_path);

app.theme = try Editor.Theme.loadFromFile(theme_path);

zstbi.init(app.allocator);

app.open_files = std.ArrayList(storage.Internal.PixiFile).init(app.allocator);
Expand Down Expand Up @@ -247,7 +245,7 @@ fn lateInit(app: *App, _: *Core) !void {
app.fonts.fa_small_solid = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/fa-solid-900.ttf", 10 * scale_factor, &fa_config, @ptrCast(ranges.ptr)).?;
app.fonts.fa_small_regular = io.fonts.?.addFontFromFileTTF(assets.root ++ "fonts/fa-regular-400.ttf", 10 * scale_factor, &fa_config, @ptrCast(ranges.ptr)).?;

app.theme.init(core, app);
editor_mod.call(.init);
}

pub fn tick(app: *App, editor_mod: mach.Mod(Editor)) !void {
Expand All @@ -270,7 +268,7 @@ pub fn tick(app: *App, editor_mod: mach.Mod(Editor)) !void {
while (core.nextEvent()) |event| {
switch (event) {
.window_open => {
try lateInit(app, core);
try lateInit(app, editor_mod);
},
.key_press => |key_press| {
state.hotkeys.setHotkeyState(key_press.key, key_press.mods, .press);
Expand Down Expand Up @@ -340,14 +338,8 @@ pub fn tick(app: *App, editor_mod: mach.Mod(Editor)) !void {

try input.process();

state.theme.push(core, app);

//imgui.showDemoWindow(null);

editor_mod.call(.tick);

state.theme.pop();

imgui.render();

// TODO: Fix title when mach supports it
Expand All @@ -370,9 +362,9 @@ pub fn tick(app: *App, editor_mod: mach.Mod(Editor)) !void {
defer encoder.release();

const background: gpu.Color = .{
.r = @floatCast(state.theme.foreground.value[0]),
.g = @floatCast(state.theme.foreground.value[1]),
.b = @floatCast(state.theme.foreground.value[2]),
.r = @floatCast(editor.theme.foreground.value[0]),
.g = @floatCast(editor.theme.foreground.value[1]),
.b = @floatCast(editor.theme.foreground.value[2]),
.a = 1.0,
};

Expand Down Expand Up @@ -494,7 +486,7 @@ pub fn deinit(editor_mod: mach.Mod(Editor)) !void {
//free everything allocated by the json_allocator
state.json_allocator.deinit();

state.allocator.free(state.theme.name);
state.allocator.free(editor.theme.name);

state.allocator.free(state.hotkeys.hotkeys);
state.allocator.free(state.mouse.buttons);
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/brezenham.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");
const pixi = @import("../Pixi.zig");
const Pixi = @import("../Pixi.zig");

pub fn process(start: [2]f32, end: [2]f32) ![][2]f32 {
var output = std.ArrayList([2]f32).init(pixi.state.allocator);
var output = std.ArrayList([2]f32).init(Pixi.state.allocator);

var x1 = start[0];
var y1 = start[1];
Expand Down
22 changes: 18 additions & 4 deletions src/editor/Editor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const nfd = @import("nfd");
const imgui = @import("zig-imgui");
const zmath = @import("zmath");

pub const Theme = @import("theme_temp.zig");

pub const Editor = @This();

pub const mach_module = .editor;
pub const mach_systems = .{ .tick, .deinit };
pub const mach_systems = .{ .init, .tick, .deinit };

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

theme: Theme,

pub const sidebar = @import("sidebar/sidebar.zig");
pub const explorer = @import("explorer/explorer.zig");
Expand All @@ -30,10 +32,22 @@ 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 tick(core: *Core) void {
pub fn init(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),
};
}

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

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

sidebar.draw();
explorer.draw(core);
artboard.draw(core);
Expand Down
2 changes: 1 addition & 1 deletion src/editor/theme_temp.zig → src/editor/Theme.zig
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub fn save(theme: Theme, path: [:0]const u8) !void {
try std.json.stringify(theme, options, out_stream);
}

pub fn pop(theme: Theme) void {
pub fn pop(theme: *Theme) void {
_ = theme;
imgui.popStyleColorEx(28);
}
Expand Down
22 changes: 11 additions & 11 deletions src/editor/artboard/artboard.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn draw(core: *Core) void {

const not_active: bool = (artboard_0 and artboard_0_open_file_index != Pixi.state.open_file_index) or (!artboard_0 and !artboard_grip and artboard_1_open_file_index != Pixi.state.open_file_index);

const artboard_color: Pixi.math.Color = if (artboard_grip or (not_active and Pixi.state.settings.split_artboard)) Pixi.state.theme.foreground else Pixi.state.theme.background;
const artboard_color: Pixi.math.Color = if (artboard_grip or (not_active and Pixi.state.settings.split_artboard)) Pixi.editor.theme.foreground else Pixi.editor.theme.background;

imgui.pushStyleColor(imgui.Col_ChildBg, artboard_color.toU32());
defer imgui.popStyleColor();
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn draw(core: *Core) void {
defer imgui.popStyleVar();
if (imgui.beginTooltip()) {
defer imgui.endTooltip();
imgui.textColored(Pixi.state.theme.text_secondary.toImguiVec4(), file.path);
imgui.textColored(Pixi.editor.theme.text_secondary.toImguiVec4(), file.path);
}
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn draw(core: *Core) void {
imgui.endChild();

if (Pixi.state.project_folder != null or Pixi.state.open_files.items.len > 0) {
imgui.pushStyleColorImVec4(imgui.Col_ChildBg, Pixi.state.theme.highlight_primary.toImguiVec4());
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();
Expand Down Expand Up @@ -284,11 +284,11 @@ pub fn draw(core: *Core) void {
}

pub fn drawLogoScreen() void {
imgui.pushStyleColorImVec4(imgui.Col_Button, Pixi.state.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Border, Pixi.state.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_ButtonActive, Pixi.state.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_ButtonHovered, Pixi.state.theme.foreground.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Text, Pixi.state.theme.text_background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Button, Pixi.editor.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Border, Pixi.editor.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_ButtonActive, Pixi.editor.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_ButtonHovered, Pixi.editor.theme.foreground.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Text, Pixi.editor.theme.text_background.toImguiVec4());
defer imgui.popStyleColorEx(5);
{ // Draw semi-transparent logo
const logo_sprite = Pixi.state.loaded_assets.atlas.sprites[Pixi.assets.pixi_atlas.logo_0_default];
Expand Down Expand Up @@ -346,7 +346,7 @@ pub fn drawGrip(window_width: f32) void {
const avail = imgui.getContentRegionAvail().y;
const curs_y = imgui.getCursorPosY();

var color = Pixi.state.theme.text_background.toImguiVec4();
var color = Pixi.editor.theme.text_background.toImguiVec4();

_ = imgui.invisibleButton("ArtboardGripButton", .{
.x = Pixi.state.settings.explorer_grip,
Expand All @@ -359,15 +359,15 @@ pub fn drawGrip(window_width: f32) void {

if (imgui.isItemHovered(hovered_flags)) {
imgui.setMouseCursor(imgui.MouseCursor_ResizeEW);
color = Pixi.state.theme.text.toImguiVec4();
color = Pixi.editor.theme.text.toImguiVec4();

if (imgui.isMouseDoubleClicked(imgui.MouseButton_Left)) {
Pixi.state.settings.split_artboard = !Pixi.state.settings.split_artboard;
}
}

if (imgui.isItemActive()) {
color = Pixi.state.theme.text.toImguiVec4();
color = Pixi.editor.theme.text.toImguiVec4();
const prev = Pixi.state.mouse.previous_position;
const cur = Pixi.state.mouse.position;

Expand Down
20 changes: 10 additions & 10 deletions src/editor/artboard/canvas.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) void {
imgui.pushStyleVar(imgui.StyleVar_WindowRounding, 8.0);
defer imgui.popStyleVarEx(3);

imgui.pushStyleColorImVec4(imgui.Col_WindowBg, Pixi.state.theme.foreground.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Button, Pixi.state.theme.background.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_WindowBg, Pixi.editor.theme.foreground.toImguiVec4());
imgui.pushStyleColorImVec4(imgui.Col_Button, Pixi.editor.theme.background.toImguiVec4());
defer imgui.popStyleColorEx(2);

var open: bool = true;
Expand Down Expand Up @@ -242,7 +242,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) void {
file.camera.drawLayer(file.temporary_layer, canvas_center_offset);

// Draw grid
file.camera.drawGrid(canvas_center_offset, file_width, file_height, @as(usize, @intFromFloat(file_width / tile_width)), @as(usize, @intFromFloat(file_height / tile_height)), Pixi.state.theme.text_secondary.toU32(), false);
file.camera.drawGrid(canvas_center_offset, file_width, file_height, @as(usize, @intFromFloat(file_width / tile_width)), @as(usize, @intFromFloat(file_height / tile_height)), Pixi.editor.theme.text_secondary.toU32(), false);

if (file.transform_texture) |*transform_texture|
file.processTransformTextureControls(transform_texture, .{});
Expand Down Expand Up @@ -314,20 +314,20 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) void {
const y = @as(f32, @floatFromInt(row)) * tile_height + canvas_center_offset[1];
const rect: [4]f32 = .{ x, y, tile_width, tile_height };

file.camera.drawRect(rect, 3.0, Pixi.state.theme.text.toU32());
file.camera.drawRect(rect, 3.0, Pixi.editor.theme.text.toU32());

// Draw the origin
const sprite: Pixi.storage.Internal.Sprite = file.sprites.items[sprite_index];
file.camera.drawLine(
.{ x + sprite.origin_x, y },
.{ x + sprite.origin_x, y + tile_height },
Pixi.state.theme.text_red.toU32(),
Pixi.editor.theme.text_red.toU32(),
2.0,
);
file.camera.drawLine(
.{ x, y + sprite.origin_y },
.{ x + tile_width, y + sprite.origin_y },
Pixi.state.theme.text_red.toU32(),
Pixi.editor.theme.text_red.toU32(),
2.0,
);
}
Expand All @@ -339,7 +339,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) void {
const y = @as(f32, @floatFromInt(row)) * tile_height + canvas_center_offset[1];
const rect: [4]f32 = .{ x, y, tile_width, tile_height };

file.camera.drawRect(rect, 3.0, Pixi.state.theme.text.toU32());
file.camera.drawRect(rect, 3.0, Pixi.editor.theme.text.toU32());
}

if (Pixi.state.popups.animation_length > 0 and Pixi.state.tools.current == .animation and !transforming) {
Expand All @@ -357,7 +357,7 @@ 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 };

file.camera.drawAnimationRect(start_rect, end_rect, 6.0, Pixi.state.theme.highlight_primary.toU32(), Pixi.state.theme.text_red.toU32());
file.camera.drawAnimationRect(start_rect, end_rect, 6.0, Pixi.editor.theme.highlight_primary.toU32(), Pixi.editor.theme.text_red.toU32());
}
}
}
Expand All @@ -378,7 +378,7 @@ pub fn draw(file: *Pixi.storage.Internal.PixiFile, core: *Core) void {
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.state.mouse.button(.primary)) |primary| primary.up() else false and !Pixi.state.popups.animation)) 4.0 else 2.0;
file.camera.drawAnimationRect(start_rect, end_rect, thickness, Pixi.state.theme.highlight_primary.toU32(), Pixi.state.theme.text_red.toU32());
file.camera.drawAnimationRect(start_rect, end_rect, thickness, Pixi.editor.theme.highlight_primary.toU32(), Pixi.editor.theme.text_red.toU32());
}
} else if (Pixi.state.sidebar != .pack and !transforming and Pixi.state.sidebar != .keyframe_animations) {
const animation = file.animations.items[file.selected_animation_index];
Expand All @@ -395,7 +395,7 @@ 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 };

file.camera.drawAnimationRect(start_rect, end_rect, 4.0, Pixi.state.theme.highlight_primary.toU32(), Pixi.state.theme.text_red.toU32());
file.camera.drawAnimationRect(start_rect, end_rect, 4.0, Pixi.editor.theme.highlight_primary.toU32(), Pixi.editor.theme.text_red.toU32());
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/editor/artboard/canvas_pack.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const pixi = @import("../../Pixi.zig");
const Pixi = @import("../../Pixi.zig");
const core = @import("mach").core;
const imgui = @import("zig-imgui");

Expand All @@ -10,19 +10,19 @@ pub const PackTexture = enum {

pub fn draw(mode: PackTexture) void {
if (switch (mode) {
.diffusemap => pixi.state.atlas.diffusemap,
.heightmap => pixi.state.atlas.heightmap,
.diffusemap => Pixi.state.atlas.diffusemap,
.heightmap => Pixi.state.atlas.heightmap,
}) |texture| {
const window_width = imgui.getWindowWidth();
const window_height = imgui.getWindowHeight();
const file_width = @as(f32, @floatFromInt(texture.image.width));
const file_height = @as(f32, @floatFromInt(texture.image.height));

var camera = &pixi.state.pack_camera;
var camera = &Pixi.state.pack_camera;

// Handle zooming, panning and extents
{
var sprite_camera: pixi.gfx.Camera = .{
var sprite_camera: Pixi.gfx.Camera = .{
.zoom = @min(window_width / file_width, window_height / file_height),
};
sprite_camera.setNearestZoomFloor();
Expand All @@ -47,7 +47,7 @@ pub fn draw(mode: PackTexture) void {

const center_offset: [2]f32 = .{ -width / 2.0, -height / 2.0 };
camera.drawTexture(texture.view_handle, texture.image.width, texture.image.height, center_offset, 0xFFFFFFFF);
camera.drawRect(.{ center_offset[0], center_offset[1], width, height }, 2.0, pixi.state.theme.text_secondary.toU32());
camera.drawRect(.{ center_offset[0], center_offset[1], width, height }, 2.0, Pixi.editor.theme.text_secondary.toU32());
}
}
}
Loading

0 comments on commit 5116bf8

Please sign in to comment.