diff options
| -rw-r--r-- | core/jj.zig | 4 | ||||
| -rw-r--r-- | core/surface.zig | 10 | ||||
| -rw-r--r-- | dev/glide.zig | 51 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 135 |
4 files changed, 165 insertions, 35 deletions
diff --git a/core/jj.zig b/core/jj.zig index a0dfec10..e35da662 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -335,8 +335,8 @@ pub const Line = struct { nmarks: u16 = 0, }; -/// The lanes a row shows at most: the ruled gutter fits three. -pub const levels = 3; +/// The lanes a row shows at most (a window's strips the same). +pub const levels = pardes.Lane.levels; pub const PinIndex = enum(u16) { none = std.math.maxInt(u16), diff --git a/core/surface.zig b/core/surface.zig index 3ab9a933..3452bc77 100644 --- a/core/surface.zig +++ b/core/surface.zig @@ -150,7 +150,15 @@ pub const max_rail_ticks = 4096; /// A pinned change's lane on a body row (jj.paint): a pixel shell draws /// it as a strip in gutter cell `x`, at its `level`'s place (sample A of /// the ruled design: each a third of a cell wide, half that of air between). -pub const Lane = struct { x: u16, y: u16, level: u8, rgb: [3]u8 }; +pub const Lane = struct { + x: u16, + y: u16, + level: u8, + rgb: [3]u8, + + /// The levels a row shows at most: the ruled gutter fits three. + pub const levels = 3; +}; /// Churn's strip on a rail (jj.railHeat): a pixel shell draws its row of /// pane `owner`'s rail, beside the track, in `rgb`. pub const Heat = struct { owner: u8, row: u16, rgb: [3]u8 }; diff --git a/dev/glide.zig b/dev/glide.zig index 711acb22..62e44bb7 100644 --- a/dev/glide.zig +++ b/dev/glide.zig @@ -39,9 +39,13 @@ pub const Glide = struct { dir: i8 = 0, before: []pardes.Cell = &.{}, /// The rows past the body's top (`above`) and bottom (`below`), nearest - /// first, `bw` cells each: what the gap shows while the picture lags. + /// first, `bw` cells each, and a change's lanes on each (Surface.lanes, + /// a pixel shell's strips in the gutter, not cells): what the gap shows + /// while the picture lags. above: []pardes.Cell = &.{}, below: []pardes.Cell = &.{}, + above_lanes: []RowLanes = &.{}, + below_lanes: []RowLanes = &.{}, nabove: u16 = 0, nbelow: u16 = 0, /// The core's lines a scroll moved, not yet measured; and what the @@ -64,8 +68,10 @@ pub const Glide = struct { /// not move, so a move is measured, and the rows that left are kept, /// below them. held: [pardes.MAX_PANES]u16 = @splat(0), - /// The last frame's cells, which a move is measured against. + /// The last frame's cells, which a move is measured against, and its + /// lanes, which the rows that left take with them. last: []pardes.Cell = &.{}, + last_lanes: std.ArrayList(pardes.Lane) = .empty, last_cols: u16 = 0, last_rows: u16 = 0, @@ -97,7 +103,10 @@ pub const Glide = struct { gpa.free(glide.before); gpa.free(glide.above); gpa.free(glide.below); + gpa.free(glide.above_lanes); + gpa.free(glide.below_lanes); gpa.free(glide.last); + glide.last_lanes.deinit(gpa); } pub fn bw(glide: *const Glide) u16 { @@ -188,6 +197,8 @@ pub const Glide = struct { glide.last_cols = surface.cols; glide.last_rows = surface.rows; glide.caret_row = if (surface.cursor) |c| c.y else null; + glide.last_lanes.clearRetainingCapacity(); + glide.last_lanes.appendSlice(gpa, surface.lanes.items) catch {}; } /// Finds how far the pane's body rows moved, `dir` rows at a time, by @@ -261,28 +272,56 @@ pub const Glide = struct { const src_row: usize = top + if (glide.dir > 0) shift - 1 - i else h - shift + i; @memcpy(keep.*[i * w ..][0..w], glide.before[src_row * w ..][0..w]); } + // Their lanes go with them, from the frame they left. + const keep_lanes = if (glide.dir > 0) glide.above_lanes else glide.below_lanes; + std.mem.copyBackwards(RowLanes, keep_lanes[shift..][0..kept], keep_lanes[0..kept]); + @memset(keep_lanes[0..shift], .{}); + for (glide.last_lanes.items) |lane| { + if (lane.x < x0 or lane.x >= x0 + w or lane.y < glide.body_y + top or lane.y >= glide.body_y + glide.body_h) continue; + const row = lane.y - glide.body_y - top; + if (glide.dir > 0 and row < shift) keep_lanes[shift - 1 - row].add(lane); + if (glide.dir < 0 and row >= h - shift) keep_lanes[row - (h - shift)].add(lane); + } count.* = kept + shift; // What came into view is no longer past the other edge. const other = if (glide.dir > 0) &glide.below else &glide.above; + const other_lanes = if (glide.dir > 0) glide.below_lanes else glide.above_lanes; const other_n = if (glide.dir > 0) &glide.nbelow else &glide.nabove; // ...and after a jump, the rows past it are another place's. const dropped = if (far) other_n.* else @min(other_n.*, shift); std.mem.copyForwards(pardes.Cell, other.*[0 .. @as(usize, other_n.* - dropped) * w], other.*[@as(usize, dropped) * w ..][0 .. @as(usize, other_n.* - dropped) * w]); + std.mem.copyForwards(RowLanes, other_lanes[0 .. other_n.* - dropped], other_lanes[dropped..][0 .. other_n.* - dropped]); other_n.* -= dropped; // An input: the core's rows moved by as many (down positive). glide.follow.kick(@as(f32, @floatFromInt(shift)) * @as(f32, @floatFromInt(glide.dir)), glide.now_ns, glide.pace()); } pub fn ensure(glide: *Glide, gpa: std.mem.Allocator) bool { - const n = @as(usize, glide.bw()) * glide.body_h * reach; - inline for (.{ &glide.above, &glide.below }) |buffer| if (buffer.len < n) { - const grown = gpa.realloc(buffer.*, n) catch return false; - buffer.* = grown; + const rows = @as(usize, glide.body_h) * reach; + inline for (.{ .{ &glide.above, rows * glide.bw() }, .{ &glide.below, rows * glide.bw() }, .{ &glide.above_lanes, rows }, .{ &glide.below_lanes, rows } }) |kept| if (kept[0].len < kept[1]) { + kept[0].* = gpa.realloc(kept[0].*, kept[1]) catch return false; }; return true; } }; +/// A kept row's lanes, as the frame it left drew them: a row has a level +/// each at most. +pub const RowLanes = struct { + n: u8 = 0, + lane: [pardes.Lane.levels]pardes.Lane = undefined, + + pub fn add(row: *RowLanes, lane: pardes.Lane) void { + if (row.n == row.lane.len) return; + row.lane[row.n] = lane; + row.n += 1; + } + + pub fn items(row: *const RowLanes) []const pardes.Lane { + return row.lane[0..row.n]; + } +}; + pub fn sameRow(a: []const pardes.Cell, b: []const pardes.Cell) bool { for (a, b) |x, y| { if (x.default != y.default) return false; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 6113c9e9..4f574eff 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -455,6 +455,7 @@ const Gui = struct { scroll_lag: f32 = 0, // picture position - core position, in rows scroll_edge: []pardes.Cell = &.{}, // the row that just left the pane scroll_edge_len: u16 = 0, + scroll_edge_lanes: pardes.glide.RowLanes = .{}, // and a change's lanes on it /// Loop steps since the last fractional turn: past `settle_after`, a /// resting `scroll_lag` eases onto the core's row (stepScroll). scroll_idle: u8 = 0, @@ -3562,6 +3563,8 @@ fn scrollRows(g: *Gui, in: *Input, gpa: std.mem.Allocator) bool { if (g.scroll_edge.len >= bw) { @memcpy(g.scroll_edge[0..bw], s.cells[@as(usize, srow) * s.cols + r.x + config.GUTTER ..][0..bw]); g.scroll_edge_len = bw; + g.scroll_edge_lanes = .{}; + for (s.lanes.items) |lane| if (lane.y == srow and lane.x >= r.x + config.GUTTER and lane.x < r.x + r.w) g.scroll_edge_lanes.add(lane); } } } @@ -3570,22 +3573,63 @@ fn scrollRows(g: *Gui, in: *Input, gpa: std.mem.Allocator) bool { return moved; } +/// The body a scroll moves, as its rows are drawn: a glide's at its +/// offset, else a touchpad's fraction (scroll_lag); its pane's rect (the +/// rail and the body, Glide.rect's), its body's first row and its rows. +/// Null when nothing lags, or when it was resized under us. +const ScrollBody = struct { + pane: usize, + gliding: bool, + lag: f32, + rect: pardes.Rect, + y0: u16, + bh: u16, + + /// The grid row of the `i`th row that left it, nearest first: over its + /// moving rows (under the `held` ones) while the picture lags a move + /// down, under the body for one up. + fn leftRow(sb: ScrollBody, held: u16, i: usize) i32 { + return if (sb.lag < 0) @as(i32, sb.y0 + held) - 1 - @as(i32, @intCast(i)) else @as(i32, sb.y0) + sb.bh + @as(i32, @intCast(i)); + } + + /// A glide's rows that left it drawn: as many as the lag opens. + fn glideLeft(sb: ScrollBody, glide: *const Glide) u16 { + const gap: u16 = @intFromFloat(@min(@ceil(@abs(sb.lag)), @as(f32, @floatFromInt(@as(u32, sb.bh) * Glide.reach)))); + return @min(gap, if (sb.lag < 0) glide.nabove else glide.nbelow); + } +}; + +fn scrollBody(g: *const Gui, surface: *const pardes.Surface) ?ScrollBody { + const glide = &g.glide; + const gliding = glide.follow.offset != 0; + const sb: ScrollBody = .{ + .pane = if (gliding) glide.pane else g.scroll_pane orelse return null, + .gliding = gliding, + .lag = if (gliding) glide.follow.offset else g.scroll_lag, + .rect = if (gliding) glide.rect else g.scroll_rect, + .y0 = if (gliding) glide.body_y else g.scroll_body_y, + .bh = if (gliding) glide.body_h else g.scroll_body_h, + }; + const bw = sb.rect.w -| config.GUTTER; + if (sb.lag == 0 or bw == 0 or sb.bh == 0 or sb.rect.x + config.GUTTER + bw > surface.cols or sb.y0 + sb.bh > surface.rows) return null; + return sb; +} + /// A scrolling body's picture: its rows moved by the lag, a glide's or a /// touchpad's fraction of a row, and in the gap that opens the rows that /// left. fn emitScrollRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *const pardes.Surface, layout: CellLayout, win_w: f32, win_h: f32, page: [3]u8, held: u16) u32 { g.scroll_held_px = 0; const glide = &g.glide; - const gliding = glide.follow.offset != 0; - const pane = if (gliding) glide.pane else g.scroll_pane orelse return 0; - const lag = if (gliding) glide.follow.offset else g.scroll_lag; - if (lag == 0) return 0; - const r = if (gliding) glide.rect else g.scroll_rect; - const bh = if (gliding) glide.body_h else g.scroll_body_h; - const y0 = if (gliding) glide.body_y else g.scroll_body_y; - const bw = r.w -| config.GUTTER; + const sb = scrollBody(g, surface) orelse return 0; + const gliding = sb.gliding; + const pane = sb.pane; + const lag = sb.lag; + const r = sb.rect; + const bh = sb.bh; + const y0 = sb.y0; + const bw = r.w - config.GUTTER; const x0 = r.x + config.GUTTER; - if (bw == 0 or bh == 0 or x0 + bw > surface.cols or y0 + bh > surface.rows) return 0; // resized under us // The scissor the shifted rows are drawn in is the scroll's. g.scroll_rect = r; g.scroll_body_y = y0; @@ -3630,7 +3674,7 @@ fn emitScrollRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *cons // A touchpad's fraction opens one row, the one that left: under the // held rows, where the moving rows start. if (g.scroll_edge_len >= bw) { - const erow: u16 = if (lag > 0) y0 + bh else y0 + kept.rows - 1; + const erow: u16 = @intCast(sb.leftRow(kept.rows, 0)); var i: u16 = 0; while (i < bw) : (i += 1) { emitInstance(g, instances, base + n, x0 + i, erow, shifted, win_w, win_h, cellFontRole(&g.scroll_edge[i]), g.scroll_edge[0..bw], i, null, false, page); @@ -3641,14 +3685,10 @@ fn emitScrollRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *cons } // A glide's: rows past the top while the picture lags a move down, past // the bottom for one up; as many as it opens. - const gap: u16 = @intFromFloat(@min(@ceil(@abs(lag)), @as(f32, @floatFromInt(@as(u32, bh) * Glide.reach)))); - const nkept = if (lag < 0) glide.nabove else glide.nbelow; const rows = if (lag < 0) glide.above else glide.below; - for (0..@min(gap, nkept)) |i| { + for (0..sb.glideLeft(glide)) |i| { const cells = rows[i * bw ..][0..bw]; - // Row -1 - i above the moving rows (under the held ones), or bh + i - // below the body, in grid rows. - const at_row: i32 = if (lag < 0) @as(i32, y0 + kept.rows) - 1 - @as(i32, @intCast(i)) else @as(i32, y0) + bh + @as(i32, @intCast(i)); + const at_row = sb.leftRow(kept.rows, i); var at = shifted; // emitInstance takes an unsigned row: move the layout instead. at.y_off += @as(f32, @floatFromInt(at_row - @as(i32, y0))) * layout.h; @@ -4919,16 +4959,7 @@ fn buildDecor(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface, g } else false; if (grid_bottom < win_h and tag_last) try addDecor(g, gpa, 0, grid_bottom, win_w, win_h, chrome.tag_bg, 1, win_w, win_h); try paneDecor(g, gpa, regions, surface.rail_ticks[0..surface.nrail_ticks], surface.heat.items, chrome, layout, surface.cols, surface.rows, win_w, win_h); - // The changes' lanes (jj.paint): a strip a level in the gutter's - // lane cells, each a third of a cell wide with half that of air - // before it, so three take one and a half cells (the ruled - // sample A); whole pixels, so every row's strip lines up. - for (surface.lanes.items) |lane| { - const w = @max(1, @round(layout.w / 3)); - const x0 = @round(@as(f32, @floatFromInt(lane.x)) * layout.w + w / 2 + @as(f32, @floatFromInt(lane.level)) * (w + w / 2)); - const y0 = @as(f32, @floatFromInt(lane.y)) * layout.h; - try addDecor(g, gpa, x0, y0, x0 + w, y0 + layout.h, lane.rgb, 1, win_w, win_h); - } + try laneDecor(g, gpa, surface, layout, win_w, win_h); // A single spine runs down each column, through every tag and body. for (regions) |column| { if (column.kind != .column) continue; @@ -4958,6 +4989,58 @@ fn buildDecor(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface, g } } +/// The changes' lanes (jj.paint): a strip a level in the gutter's lane +/// cells, each a third of a cell wide with half that of air before it, so +/// three take one and a half cells (the ruled sample A); whole pixels, so +/// every row's strip lines up. A scrolling body's ride its rows as +/// emitScrollRows draws them: the lag less, cut to the body under its held +/// rows (its scissor), and the rows that left keep theirs in the gap. +fn laneDecor(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface, layout: CellLayout, win_w: f32, win_h: f32) !void { + const w = @max(1, @round(layout.w / 3)); + const b = scrollBody(g, surface) orelse { + for (surface.lanes.items) |lane| try laneStrip(g, gpa, lane, lane.y, null, w, layout, win_w, win_h); + return; + }; + const kept = holdContext(g, surface, b.pane, if (surface.facts.pane(b.pane)) |pane| pane.context_rows else 0, b.bh, layout); + const ride: LaneRide = .{ + .x0 = b.rect.x + config.GUTTER, + .x1 = b.rect.x + b.rect.w, + .y0 = b.y0 + kept.rows, + .y1 = b.y0 + b.bh, + .dy = b.lag * layout.h - kept.lift, + .top = @as(f32, @floatFromInt(b.y0 + kept.rows)) * layout.h + kept.lift, + .bottom = @as(f32, @floatFromInt(b.y0 + b.bh)) * layout.h, + }; + for (surface.lanes.items) |lane| { + const rides = lane.x >= ride.x0 and lane.x < ride.x1 and lane.y >= ride.y0 and lane.y < ride.y1; + try laneStrip(g, gpa, lane, lane.y, if (rides) &ride else null, w, layout, win_w, win_h); + } + if (b.gliding) { + const rows = if (b.lag < 0) g.glide.above_lanes else g.glide.below_lanes; + for (rows[0..b.glideLeft(&g.glide)], 0..) |*row, i| for (row.items()) |lane| try laneStrip(g, gpa, lane, b.leftRow(kept.rows, i), &ride, w, layout, win_w, win_h); + } else if (g.scroll_edge_len >= b.rect.w - config.GUTTER) { + for (g.scroll_edge_lanes.items()) |lane| try laneStrip(g, gpa, lane, b.leftRow(kept.rows, 0), &ride, w, layout, win_w, win_h); + } +} + +/// A scrolling body's moving rows (laneDecor): their columns and rows, how +/// far up they are drawn, and the pixels they are cut to. +const LaneRide = struct { x0: u16, x1: u16, y0: u16, y1: u16, dy: f32, top: f32, bottom: f32 }; + +/// `lane`'s strip, `w` pixels wide, on grid row `row`, riding a scroll's +/// rows when given one. +fn laneStrip(g: *Gui, gpa: std.mem.Allocator, lane: pardes.Lane, row: i32, ride: ?*const LaneRide, w: f32, layout: CellLayout, win_w: f32, win_h: f32) !void { + const x0 = @round(@as(f32, @floatFromInt(lane.x)) * layout.w + w / 2 + @as(f32, @floatFromInt(lane.level)) * (w + w / 2)); + var y0 = @as(f32, @floatFromInt(row)) * layout.h; + var y1 = y0 + layout.h; + if (ride) |r| { + y0 = @max(y0 - r.dy, r.top); + y1 = @min(y1 - r.dy, r.bottom); + if (y1 <= y0) return; + } + try addDecor(g, gpa, x0, y0, x0 + w, y1, lane.rgb, 1, win_w, win_h); +} + /// Each pane's rule between its tag and body, and its scroll rail: a page /// coloured gutter with a thin track and its thumb, running down through the /// pixels past the last whole row. |
