diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/gui.zig | 101 | ||||
| -rw-r--r-- | src/gui/pet.zig | 215 |
2 files changed, 312 insertions, 4 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 4f82d246..e5ca645d 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -13,6 +13,7 @@ const look = @import("../look.zig"); const message = pardes.Messages.Message; const file_watch = @import("../file_watch.zig"); const deck = @import("deck.zig"); +const pet = @import("pet.zig"); const fonts = @import("../fonts.zig"); // the fallback faces const selection_pipe = @import("../selection_pipe.zig"); const c_heap = @import("c_heap"); @@ -239,9 +240,9 @@ const LoadedFallback = struct { const max_touch_points: usize = 16; const touch_scroll_tick: f32 = 0.02; -// The triangle overlay holds only the cursor's glide quad: the chrome is -// decor. -const max_overlay_vertices: usize = 6; +// The triangle overlay holds only the pet and the cursor's glide quad: the +// chrome is decor. +const max_overlay_vertices: usize = pet.max_vertices + 6; const font_px_step: f32 = 2.0; const font_px_min: f32 = 8.0; @@ -991,6 +992,8 @@ const Gui = struct { words: Words = .{}, touch: Touch = .{}, + /// The workspace tag's pet (Pet), on the window's own clock. + pet_state: pet.State = .{}, live_ctrl: bool = false, live_alt: bool = false, @@ -5418,7 +5421,7 @@ fn renderFrame( if (!pdf_focus) core.frames.scroll = g.glide.follow.offset; core.frames.caret = caretRow(g, surface) orelse 0; const cursors_shown = !quad_mode; - var overlay_count = buildOverlay(g, surface, sw, sh); + var overlay_count = buildOverlay(g, core.settings.pet, surface, sw, sh); if (overlay_count != 0 and !uploadOverlayGpu(g, cmd, overlay_count)) overlay_count = 0; if (!prepareNativeImages(g, gpa, cmd, surface, &groups, sw, sh)) { @@ -8056,16 +8059,96 @@ fn cursorDecor(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface, /// The triangle overlay: the cursor's glide, over everything. fn buildOverlay( g: *Gui, + which: pardes.config.Runtime.Pet, surface: *const pardes.Surface, sw: u32, sh: u32, ) u32 { if (g.overlay_vertices.len == 0 or sw == 0 or sh == 0) return 0; var builder = OverlayBuilder{ .vertices = g.overlay_vertices, .win_w = @floatFromInt(sw), .win_h = @floatFromInt(sh) }; + appendWorkspacePet(g, which, surface, &builder); appendCursorGlide(g, surface, &builder); return @intCast(builder.len); } +/// The workspace tag's pet (the Pet setting, gui/pet.zig): in the blank +/// end of the grid's first row, past the tag's text, in the tag's colours, +/// on the window's own clock. Nothing of it reaches the core. +fn appendWorkspacePet(g: *Gui, which: pardes.config.Runtime.Pet, surface: *const pardes.Surface, builder: *OverlayBuilder) void { + const kind: pet.Kind = @fromBackingInt(@backingInt(which)); + if (kind == .off or surface.rows == 0 or surface.cols == 0) { + g.pet_state = .{}; + return; + } + var occupied: usize = 0; + for (surface.cells[0..surface.cols], 0..) |*cell, i| { + if (cellCodepoint(cell) != ' ') occupied = i + 1; + } + const band_top: f32 = @floatFromInt(taglineBandOffset(0, builder.win_h, g.cell_h, g.tagline_height)); + const region = if (occupied >= surface.cols) null else pet.lane( + occupied, + @floatFromInt(g.tagline_width), + builder.win_w, + band_top, + @floatFromInt(g.tagline_height), + ); + // ponytail: it moves when the window draws; a wake of its own for an + // idle window if a still pet ever matters. + const pose = g.pet_state.sample(kind, c.SDL_GetTicks(), region) orelse return; + const ink = overlayRgb(surface.chrome.tag_fg); + const eye = overlayRgb(surface.chrome.tag_bg); + // Behind the cursor: the overlay is over the cells, so the pixels a + // cursor covers are left out. + var boxes: [max_cursors]CursorBox = undefined; + const cursors = boxes[0..cursorBoxes(g, surface, fixedCellLayout(g), builder.win_h, &boxes)]; + for (0..pet.height) |y| for (0..pet.width) |x| { + const color = pet.pixel(kind, pose, x, y); + if (color == 0) continue; + const x0 = pose.x + @as(f32, @floatFromInt(x)) * pose.scale; + const y0 = pose.y + @as(f32, @floatFromInt(y)) * pose.scale; + const covered = for (cursors) |box| { + if (x0 < box.x1 and x0 + pose.scale > box.x0 and y0 < box.y1 and y0 + pose.scale > box.y0) break true; + } else false; + if (covered) continue; + builder.addRect(x0, y0, x0 + pose.scale, y0 + pose.scale, if (color == 2) eye else ink); + }; +} + +test "a pet stays past the workspace tag's text, and off it is gone" { + std.testing.refAllDecls(pet); + var cells: [80]pardes.Cell = @splat(.{}); + const surface: pardes.Surface = .{ .cols = 80, .rows = 1, .cells = &cells }; + for ("Workspace Commands", 0..) |ch, i| { + cells[i].text[0] = ch; + cells[i].len = 1; + cells[i].default = false; + } + var g = testGui(10, 20, 8, 16); + var vertices: [pet.max_vertices]OverlayVertex = undefined; + var builder: OverlayBuilder = .{ .vertices = &vertices, .win_w = 800, .win_h = 480 }; + appendWorkspacePet(&g, .cat, &surface, &builder); + try std.testing.expect(builder.len > 0); + for (vertices[0..builder.len]) |v| { + const x = (v.x + 1) * 400; + const y = (1 - v.y) * 240; + try std.testing.expect(x >= 20 * 8 - 0.01); + try std.testing.expect(y >= 0 and y < 20); + } + builder.len = 0; + appendWorkspacePet(&g, .off, &surface, &builder); + try std.testing.expectEqual(@as(usize, 0), builder.len); + try std.testing.expectEqual(@as(?u64, null), g.pet_state.tick); +} + +fn overlayRgb(rgb: [3]u8) OverlayColor { + return .{ + .r = @as(f32, @floatFromInt(rgb[0])) / 255.0, + .g = @as(f32, @floatFromInt(rgb[1])) / 255.0, + .b = @as(f32, @floatFromInt(rgb[2])) / 255.0, + .a = 1, + }; +} + /// G3: the focused cursor while it glides or blinks, its quad from the core /// (Chrome.cursor_quad, grid cells) in its cursor colour; a little less than /// solid, so the glyphs it passes over stay legible. @@ -8171,6 +8254,16 @@ const OverlayBuilder = struct { b.addVertex(x2, y2, color); } + fn addRect(b: *OverlayBuilder, x0_in: f32, y0_in: f32, x1_in: f32, y1_in: f32, color: OverlayColor) void { + if (b.len + 6 > b.vertices.len) return; + const x0 = std.math.clamp(@min(x0_in, x1_in), 0.0, b.win_w); + const x1 = std.math.clamp(@max(x0_in, x1_in), 0.0, b.win_w); + const y0 = std.math.clamp(@min(y0_in, y1_in), 0.0, b.win_h); + const y1 = std.math.clamp(@max(y0_in, y1_in), 0.0, b.win_h); + if (x1 <= x0 or y1 <= y0) return; + b.addTri(x0, y0, x1, y0, x1, y1, color); + b.addTri(x0, y0, x1, y1, x0, y1, color); + } }; fn envU16(env: *std.process.Environ.Map, name: []const u8) ?u16 { diff --git a/src/gui/pet.zig b/src/gui/pet.zig new file mode 100644 index 00000000..7a13ca91 --- /dev/null +++ b/src/gui/pet.zig @@ -0,0 +1,215 @@ +//! Original code-native pixel companions. Pure, bounded geometry and a 10 Hz +//! animation clock: no textures, allocation, input handling or editor state. +const std = @import("std"); + +pub const Kind = enum { off, cat, frog }; +pub const width = 12; +pub const height = 9; +pub const max_vertices = width * height * 6; +pub const Lane = struct { left: f32, right: f32, top: f32, scale: f32 }; +pub const Pose = struct { x: f32, y: f32, scale: f32, left: bool, walking: bool, frame: u8 }; +/// The frog's hop: sits two ticks, takes off, peaks for two, comes down, lands. +const hop_ticks = 8; + +/// Only the trailing blank part of the global tag is eligible. The caller +/// supplies its last occupied cell, including any wide-grapheme continuation. +pub fn lane(occupied: usize, cell_w: f32, screen_w: f32, top: f32, band_h: f32) ?Lane { + if (cell_w <= 0 or band_h < height + 2) return null; + const scale = @min(3, @floor((band_h - 2) / height)); + const left = @as(f32, @floatFromInt(occupied + 2)) * cell_w; + const right = screen_w - 2 * cell_w - width * scale; + if (right - left < 4 * scale) return null; + return .{ .left = left, .right = right, .top = top + band_h - height * scale - 1, .scale = scale }; +} + +pub const State = struct { + kind: Kind = .off, + tick: ?u64 = null, + x: f32 = 0, + target: f32 = 0, + left: bool = false, + idle: u8 = 12, + frame: u8 = 0, + rng: std.Random.DefaultPrng = undefined, + + /// No lane (the tag's text has filled it) hides the pet but keeps + /// where it was and what it was doing. + pub fn sample(s: *State, kind: Kind, now_ms: u64, area: ?Lane) ?Pose { + const region = area orelse return null; + if (kind == .off) { + s.* = .{}; + return null; + } + const tick = now_ms / 100; + if (s.kind != kind or s.tick == null) { + s.* = .{ .kind = kind, .tick = tick, .x = region.left, .target = region.left, .rng = .init(now_ms) }; + } + s.x = std.math.clamp(s.x, region.left, region.right); + s.target = std.math.clamp(s.target, region.left, region.right); + // Coming back from suspension never launches the pet across the tag. + const steps = @min(tick -| s.tick.?, 2); + s.tick = tick; + for (0..steps) |_| { + s.frame +%= 1; + if (s.idle > 0) { + s.idle -= 1; + if (s.idle == 0) { + // Wander to a random spot of the lane; one too close + // would be a shuffle in place, so take the far end. + s.target = region.left + s.rng.random().float(f32) * (region.right - region.left); + if (@abs(s.target - s.x) < 8 * region.scale) + s.target = if (s.x - region.left > region.right - s.x) region.left else region.right; + s.left = s.target < s.x; + s.frame = 255; // the walk starts at frame 0 + } + continue; + } + // The cat walks steadily; the frog travels only while airborne. + const phase = s.frame % hop_ticks; + if (kind == .cat or (phase >= 2 and phase < 6)) { + const step = (if (kind == .cat) @as(f32, 1.8) else 2.6) * region.scale; + s.x = if (s.left) @max(s.target, s.x - step) else @min(s.target, s.x + step); + } + if (s.x == s.target and (kind == .cat or phase == hop_ticks - 1)) + s.idle = s.rng.random().intRangeAtMost(u8, 20, 60); + } + return .{ .x = @floor(s.x), .y = region.top, .scale = region.scale, .left = s.left, .walking = s.idle == 0, .frame = s.frame }; + } +}; + +const cat = [_][]const u8{ + " # # ", + " #####", + " ##o##", + " # ######", + " # ######## ", + " ######### ", + " ######## ", + " ## ## ", + " # # ", +}; +const frog = [_][]const u8{ + " ", + " ", + " ", + " ## ", + " #o## ", + " #########", + " ######### ", + " ##### ## ", + "###### ### ", +}; +const frog_leap = [_][]const u8{ + " ## ", + " #o## ", + " #######", + " ######## ", + " ####### ", + " ### ## ", + "## # ", + "# ", + " ", +}; + +/// 0 transparent, 1 theme ink, 2 eye cutout. Walk legs and idle blinks are +/// generated in this fixed 12x9 tile; neither can spill into an adjacent row. +pub fn pixel(kind: Kind, pose: Pose, x_in: usize, y: usize) u2 { + if (x_in >= width or y >= height or kind == .off) return 0; + const x = if (pose.left) width - 1 - x_in else x_in; + const alternate = (pose.frame / 2) % 2 == 1; + // An idle companion still looks alive during its first pause: a slow + // two-pixel tail-tip flick, or the frog's throat gently inflating. These + // remain inside the tile and mirror with the rest of the animal. + const breathing = (pose.frame / 4) % 2 == 1; + if (!pose.walking and breathing) { + if (kind == .cat and y == 3 and x <= 1) + return @intFromBool(x == 0); + if (kind == .frog and ((y == 6 and x == 11) or (y == 7 and x == 10))) return 1; + } + if (kind == .cat and pose.walking and alternate and y >= 7) + return @intFromBool((y == 7 and (x == 3 or x == 7)) or (y == 8 and (x == 2 or x == 8))); + // A hopping frog leaps two rows low at take-off and one on the way down. + const phase = pose.frame % hop_ticks; + const leaping = kind == .frog and pose.walking and phase >= 2 and phase < 6; + const sink: usize = if (!leaping) 0 else if (phase == 2) 2 else if (phase == 5) 1 else 0; + if (y < sink) return 0; + const ch = (if (kind == .cat) cat else if (leaping) frog_leap else frog)[y - sink][x]; + return switch (ch) { + '#' => 1, + 'o' => if (!pose.walking and pose.frame % 30 >= 27) 1 else 2, + else => 0, + }; +} + +test "pet stays in trailing workspace blanks, and a lost lane hides it without a reset" { + try std.testing.expect(lane(20, 8, 200, 0, 16) == null); + try std.testing.expect(lane(3, 8, 200, 0, 10) == null); + const region = lane(3, 8, 200, 2, 20).?; + try std.testing.expect(region.left >= 5 * 8); + var state: State = .{}; + var moved = false; + var reversed = false; + for (0..2000) |i| { + const pose = state.sample(.cat, i * 100, region).?; + try std.testing.expect(pose.x >= region.left and pose.x + width * pose.scale <= 200 - 16); + try std.testing.expect(pose.y >= 2 and pose.y + height * pose.scale < 22); + moved = moved or pose.walking; + reversed = reversed or pose.left; + } + try std.testing.expect(moved and reversed); + const kept = state; + try std.testing.expect(state.sample(.cat, 300000, null) == null); + try std.testing.expectEqual(kept.x, state.x); + try std.testing.expectEqual(kept.tick, state.tick); + try std.testing.expect(state.sample(.off, 300100, region) == null); +} + +test "pet clock is fixed rate and sprites bounded in both directions" { + const region = lane(1, 8, 400, 0, 16).?; + var state: State = .{}; + const start = state.sample(.frog, 1000, region).?; + try std.testing.expectEqualDeep(start, state.sample(.frog, 1099, region).?); + for ([_]Kind{ .cat, .frog }) |kind| for ([_]bool{ false, true }) |left| { + const pose: Pose = .{ .x = 0, .y = 0, .scale = 1, .left = left, .walking = true, .frame = 3 }; + var count: usize = 0; + for (0..height) |y| for (0..width) |x| { + count += @intFromBool(pixel(kind, pose, x, y) != 0); + }; + try std.testing.expect(count > 20 and count <= width * height); + try std.testing.expectEqual(@as(u2, 0), pixel(kind, pose, width, height)); + }; +} + +test "pet rests at random spots and the frog travels only in the air" { + const region = lane(1, 8, 800, 0, 16).?; + var state: State = .{}; + var last = state.sample(.frog, 0, region).?; + var rested_midway = false; + for (1..3000) |i| { + const pose = state.sample(.frog, i * 100, region).?; + const phase = pose.frame % hop_ticks; + if (pose.x != last.x) try std.testing.expect(pose.walking and phase >= 2 and phase < 6); + rested_midway = rested_midway or (last.walking and !pose.walking and pose.x > region.left and pose.x < @floor(region.right)); + last = pose; + } + try std.testing.expect(rested_midway); +} + +test "pet initial idle visibly animates without walking or leaving its tile" { + const region = lane(1, 8, 400, 0, 16).?; + for ([_]Kind{ .cat, .frog }) |kind| { + var state: State = .{}; + const initial = state.sample(kind, 0, region).?; + var later = initial; + for (1..5) |tick| later = state.sample(kind, tick * 100, region).?; + try std.testing.expect(!initial.walking and !later.walking); + try std.testing.expectEqual(initial.x, later.x); + try std.testing.expectEqual(initial.y, later.y); + var changes: usize = 0; + for (0..height) |y| for (0..width) |x| { + changes += @intFromBool(pixel(kind, initial, x, y) != pixel(kind, later, x, y)); + }; + try std.testing.expectEqual(@as(usize, 2), changes); + try std.testing.expectEqual(@as(u2, 0), pixel(kind, later, width, height)); + } +} |
