@driftengine/ui2d 3.63.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ const STRIDE = 6;
2
+ export function createDirtyTracker(capacity) {
3
+ return {
4
+ nodes: Array.from({ length: capacity }, () => null),
5
+ state: new Float64Array(capacity * STRIDE),
6
+ count: 0,
7
+ x: 0,
8
+ y: 0,
9
+ w: 0,
10
+ h: 0,
11
+ any: false,
12
+ };
13
+ }
14
+ export function clearDirty(tracker) {
15
+ tracker.any = false;
16
+ tracker.x = 0;
17
+ tracker.y = 0;
18
+ tracker.w = 0;
19
+ tracker.h = 0;
20
+ }
21
+ /** Union `rect` into what must be redrawn. A zero-area rectangle still counts as a change. */
22
+ export function markDirty(tracker, rect) {
23
+ if (!tracker.any) {
24
+ tracker.x = rect.x;
25
+ tracker.y = rect.y;
26
+ tracker.w = rect.w;
27
+ tracker.h = rect.h;
28
+ tracker.any = true;
29
+ return;
30
+ }
31
+ const left = Math.min(tracker.x, rect.x);
32
+ const top = Math.min(tracker.y, rect.y);
33
+ const right = Math.max(tracker.x + tracker.w, rect.x + rect.w);
34
+ const bottom = Math.max(tracker.y + tracker.h, rect.y + rect.h);
35
+ tracker.x = left;
36
+ tracker.y = top;
37
+ tracker.w = right - left;
38
+ tracker.h = bottom - top;
39
+ }
40
+ /** The union of everything marked. Returns false when nothing was. */
41
+ export function dirtyBounds(tracker, out) {
42
+ out.x = tracker.x;
43
+ out.y = tracker.y;
44
+ out.w = tracker.w;
45
+ out.h = tracker.h;
46
+ return tracker.any;
47
+ }
48
+ /** Visual state that changes pixels, packed so one comparison covers all of it. */
49
+ function visualOf(node) {
50
+ return ((node.hidden ? 1 : 0) |
51
+ (node.hovered ? 2 : 0) |
52
+ (node.pressed ? 4 : 0) |
53
+ ((node.texture + 1) * 8));
54
+ }
55
+ const SCRATCH = { x: 0, y: 0, w: 0, h: 0 };
56
+ function visit(tracker, node, at) {
57
+ const base = at * STRIDE;
58
+ const previous = tracker.nodes[at] ?? null;
59
+ const visual = visualOf(node);
60
+ const textHash = node.text.length;
61
+ const moved = previous !== node ||
62
+ tracker.state[base] !== node.rect.x ||
63
+ tracker.state[base + 1] !== node.rect.y ||
64
+ tracker.state[base + 2] !== node.rect.w ||
65
+ tracker.state[base + 3] !== node.rect.h ||
66
+ tracker.state[base + 4] !== visual ||
67
+ tracker.state[base + 5] !== textHash;
68
+ if (moved) {
69
+ /* Where it was, so a node that moved or vanished does not leave its pixels behind. */
70
+ if (previous !== null) {
71
+ SCRATCH.x = tracker.state[base] ?? 0;
72
+ SCRATCH.y = tracker.state[base + 1] ?? 0;
73
+ SCRATCH.w = tracker.state[base + 2] ?? 0;
74
+ SCRATCH.h = tracker.state[base + 3] ?? 0;
75
+ markDirty(tracker, SCRATCH);
76
+ }
77
+ if (!node.hidden)
78
+ markDirty(tracker, node.rect);
79
+ }
80
+ tracker.nodes[at] = node;
81
+ tracker.state[base] = node.rect.x;
82
+ tracker.state[base + 1] = node.rect.y;
83
+ tracker.state[base + 2] = node.rect.w;
84
+ tracker.state[base + 3] = node.rect.h;
85
+ tracker.state[base + 4] = visual;
86
+ tracker.state[base + 5] = textHash;
87
+ let next = at + 1;
88
+ for (const child of node.children)
89
+ next = visit(tracker, child, next);
90
+ return next;
91
+ }
92
+ /**
93
+ * Compare the tree against last frame and mark what changed.
94
+ *
95
+ * Walks hidden nodes too, because a node that just became hidden is exactly the one whose previous
96
+ * rectangle has to be marked.
97
+ */
98
+ export function diffTree(tracker, root) {
99
+ clearDirty(tracker);
100
+ const count = visit(tracker, root, 0);
101
+ /* A node that existed last frame and is gone now: its rectangle must be repainted. */
102
+ for (let at = count; at < tracker.count; at += 1) {
103
+ const previous = tracker.nodes[at];
104
+ if (previous === null || previous === undefined)
105
+ continue;
106
+ const base = at * STRIDE;
107
+ SCRATCH.x = tracker.state[base] ?? 0;
108
+ SCRATCH.y = tracker.state[base + 1] ?? 0;
109
+ SCRATCH.w = tracker.state[base + 2] ?? 0;
110
+ SCRATCH.h = tracker.state[base + 3] ?? 0;
111
+ markDirty(tracker, SCRATCH);
112
+ tracker.nodes[at] = null;
113
+ }
114
+ tracker.count = count;
115
+ return tracker.any;
116
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Draw order when tree order is not enough.
3
+ *
4
+ * A dropdown is owned by the control that opens it and must cover the panel that control sits in;
5
+ * a drag preview is owned by the thing being dragged and must cover everything. Tree order cannot
6
+ * express either, because ownership and covering point in opposite directions.
7
+ *
8
+ * **A layer is inherited.** A popup's own children have to ride with it, or a menu draws above the
9
+ * panel and its labels draw below it — which is the first bug any other arrangement produces.
10
+ *
11
+ * **A stable sort, and the stability is a requirement rather than a nicety.** Two nodes on one
12
+ * layer that swap between frames produce a flicker nobody can reproduce on demand. Sorting the
13
+ * traversal indices rather than the nodes makes the tiebreaker the traversal order by
14
+ * construction, so it cannot be lost to whatever the sort implementation guarantees.
15
+ */
16
+ import type { UiNode } from './uiNode.ts';
17
+ /** Fill `out` with the tree in draw order and return how many entries were written. */
18
+ export declare function layerOrder(root: UiNode, out: UiNode[]): number;
@@ -0,0 +1,30 @@
1
+ /* Reused across calls: this runs per frame and these are its only allocations. */
2
+ const FLAT = [];
3
+ const LAYERS = [];
4
+ const ORDER = [];
5
+ function gather(node, inherited, at) {
6
+ if (node.hidden)
7
+ return at;
8
+ const layer = node.layer === 0 ? inherited : node.layer;
9
+ FLAT[at] = node;
10
+ LAYERS[at] = layer;
11
+ let next = at + 1;
12
+ for (const child of node.children)
13
+ next = gather(child, layer, next);
14
+ return next;
15
+ }
16
+ /** Fill `out` with the tree in draw order and return how many entries were written. */
17
+ export function layerOrder(root, out) {
18
+ const count = gather(root, 0, 0);
19
+ ORDER.length = 0;
20
+ for (let i = 0; i < count; i += 1)
21
+ ORDER.push(i);
22
+ ORDER.sort((a, b) => {
23
+ const byLayer = (LAYERS[a] ?? 0) - (LAYERS[b] ?? 0);
24
+ return byLayer !== 0 ? byLayer : a - b;
25
+ });
26
+ for (let i = 0; i < count; i += 1)
27
+ out[i] = FLAT[ORDER[i] ?? 0];
28
+ out.length = count;
29
+ return count;
30
+ }
package/dist/uiLayout.js CHANGED
@@ -66,8 +66,14 @@ function place(node, x, y, w, h) {
66
66
  node.rect.w = w;
67
67
  node.rect.h = h;
68
68
  const row = node.direction === 'row';
69
- const contentX = x + node.paddingLeft;
70
- const contentY = y + node.paddingTop;
69
+ /*
70
+ * Scrolling is subtracted here, at placement, rather than applied as a transform at draw.
71
+ * Applied at draw, hit testing would need the inverse and every consumer would eventually get
72
+ * it wrong in one direction; applied here, a scrolled child's rectangle simply *is* where it is,
73
+ * and hit testing, focus and clipping all keep working without knowing scrolling exists.
74
+ */
75
+ const contentX = x + node.paddingLeft - node.scrollX;
76
+ const contentY = y + node.paddingTop - node.scrollY;
71
77
  const contentW = Math.max(0, w - node.paddingLeft - node.paddingRight);
72
78
  const contentH = Math.max(0, h - node.paddingTop - node.paddingBottom);
73
79
  const contentAlong = row ? contentW : contentH;
@@ -129,6 +135,7 @@ function place(node, x, y, w, h) {
129
135
  }
130
136
  cursor += sizeAlong + node.gap + between;
131
137
  }
138
+ measureSpan(node, contentX, contentY);
132
139
  }
133
140
  /**
134
141
  * A child taken out of the flow: at its own offset inside the parent's content box.
@@ -137,6 +144,26 @@ function place(node, x, y, w, h) {
137
144
  * where the panel's contents start — which is where a caller reading the padding expects it, and
138
145
  * the only reading under which `x: 0` means the same thing for an absolute child as for a flow one.
139
146
  */
147
+ /**
148
+ * How far the placed children reach past the content origin.
149
+ *
150
+ * Measured after placement and relative to `contentX`/`contentY`, which the scroll has already
151
+ * moved — so the offset cancels and the answer is the same whatever the node is scrolled to.
152
+ * `uiScroll.ts` clamps against this and would be wrong clamping against anything derived from the
153
+ * live scroll value.
154
+ */
155
+ function measureSpan(node, contentX, contentY) {
156
+ let spanX = 0;
157
+ let spanY = 0;
158
+ for (const child of node.children) {
159
+ if (child.hidden)
160
+ continue;
161
+ spanX = Math.max(spanX, child.rect.x + child.rect.w - contentX);
162
+ spanY = Math.max(spanY, child.rect.y + child.rect.h - contentY);
163
+ }
164
+ node.contentSpanX = spanX;
165
+ node.contentSpanY = spanY;
166
+ }
140
167
  function placeAbsolute(child, contentX, contentY, contentW, contentH) {
141
168
  const w = child.width === 'grow' ? contentW : child.measuredWidth;
142
169
  const h = child.height === 'grow' ? contentH : child.measuredHeight;
package/dist/uiNode.d.ts CHANGED
@@ -39,6 +39,18 @@ export interface UiNodeOptions {
39
39
  readonly contentWidth?: number;
40
40
  readonly contentHeight?: number;
41
41
  readonly hidden?: boolean;
42
+ /**
43
+ * Whether this node cuts its descendants to its own rectangle.
44
+ *
45
+ * Off by default, because most nodes do not clip and one that does costs a scissor change in
46
+ * the pass. A scroll container sets it and a panel sets it; a row of buttons does not.
47
+ */
48
+ readonly clip?: boolean;
49
+ /** How far this node's contents are moved under it. Only meaningful with `clip`. */
50
+ readonly scrollX?: number;
51
+ readonly scrollY?: number;
52
+ /** Draw order beyond tree order. Inherited by descendants that do not set their own. */
53
+ readonly layer?: number;
42
54
  readonly background?: ArrayLike<number> | null;
43
55
  /** The sprite slot this node draws from, or `-1` for none. */
44
56
  readonly texture?: number;
@@ -78,6 +90,23 @@ export interface UiNode {
78
90
  contentWidth: number;
79
91
  contentHeight: number;
80
92
  hidden: boolean;
93
+ /** Whether this node cuts its descendants to its own rectangle. See `uiClip.ts`. */
94
+ clip: boolean;
95
+ /** How far this node's contents are moved under it. See `uiScroll.ts`. */
96
+ scrollX: number;
97
+ scrollY: number;
98
+ /**
99
+ * How far the placed children reach past this node's content origin, along each axis.
100
+ *
101
+ * Written by `layoutUiTree` and **independent of the current scroll**, because it is measured
102
+ * relative to the content origin that the scroll already moved — the offset cancels. That
103
+ * independence is the point: an extent derived from placed positions plus the live scroll value
104
+ * is wrong the moment the two disagree, which is exactly when a caller is scrolling.
105
+ */
106
+ contentSpanX: number;
107
+ contentSpanY: number;
108
+ /** Draw order beyond tree order. See `uiLayer.ts`. */
109
+ layer: number;
81
110
  /** Where this node ended up. Meaningless until `layoutUiTree` has run over its root. */
82
111
  readonly rect: UiRect;
83
112
  /** What it measured to along each axis, before the parent distributed anything. */
package/dist/uiNode.js CHANGED
@@ -30,6 +30,12 @@ export function createUiNode(options = {}) {
30
30
  contentWidth: options.contentWidth ?? 0,
31
31
  contentHeight: options.contentHeight ?? 0,
32
32
  hidden: options.hidden ?? false,
33
+ clip: options.clip ?? false,
34
+ scrollX: options.scrollX ?? 0,
35
+ scrollY: options.scrollY ?? 0,
36
+ contentSpanX: 0,
37
+ contentSpanY: 0,
38
+ layer: options.layer ?? 0,
33
39
  rect: { x: 0, y: 0, w: 0, h: 0 },
34
40
  measuredWidth: 0,
35
41
  measuredHeight: 0,
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Who the pointer belongs to, and what a drag is carrying.
3
+ *
4
+ * **Capture is the half without which every slider in the interface is wrong in the same way.** A
5
+ * control being dragged has to keep receiving motion after the pointer leaves its rectangle — that
6
+ * is what dragging *is* — and a framework that routes purely by hit test cannot express it.
7
+ *
8
+ * **A drag records its target as the pointer moves**, rather than hit-testing once at release,
9
+ * because the release position is the only thing a drop handler could otherwise use and a pointer
10
+ * that leaves the window never produces one.
11
+ */
12
+ import type { UiNode } from './uiNode.ts';
13
+ export interface PointerState {
14
+ /** The node holding the pointer, or null. */
15
+ captured: UiNode | null;
16
+ /** Where a drag began, or null when none is in progress. */
17
+ dragSource: UiNode | null;
18
+ /** The last node the drag was over, excluding the source itself. */
19
+ dragTarget: UiNode | null;
20
+ /** What the drag is carrying. Opaque to this module. */
21
+ dragPayload: string;
22
+ }
23
+ export declare function createPointerState(): PointerState;
24
+ export declare function capturePointer(state: PointerState, node: UiNode): void;
25
+ export declare function releasePointer(state: PointerState): void;
26
+ /**
27
+ * Who should receive pointer input at this position.
28
+ *
29
+ * A captured node receives it wherever the pointer is, including outside the window. Otherwise it
30
+ * is an ordinary hit test — and while a drag is in progress, the result is remembered as the
31
+ * prospective drop target.
32
+ */
33
+ export declare function pointerTarget(state: PointerState, root: UiNode, x: number, y: number): UiNode | null;
34
+ export declare function beginDrag(state: PointerState, source: UiNode, payload: string): void;
35
+ /** The node a drop would land on, or null. */
36
+ export declare function dropTarget(state: PointerState): UiNode | null;
37
+ /**
38
+ * Finish a drag and report where it landed. Null when no drag was in progress.
39
+ *
40
+ * A null `target` is an ordinary outcome — a drop on nothing — and is reported rather than
41
+ * suppressed, because a caller often wants to know a drag ended even where it achieved nothing.
42
+ */
43
+ export declare function endDrag(state: PointerState): {
44
+ source: UiNode;
45
+ target: UiNode | null;
46
+ payload: string;
47
+ } | null;
@@ -0,0 +1,50 @@
1
+ import { uiHitTest } from './uiFocus.js';
2
+ export function createPointerState() {
3
+ return { captured: null, dragSource: null, dragTarget: null, dragPayload: '' };
4
+ }
5
+ export function capturePointer(state, node) {
6
+ state.captured = node;
7
+ }
8
+ export function releasePointer(state) {
9
+ state.captured = null;
10
+ }
11
+ /**
12
+ * Who should receive pointer input at this position.
13
+ *
14
+ * A captured node receives it wherever the pointer is, including outside the window. Otherwise it
15
+ * is an ordinary hit test — and while a drag is in progress, the result is remembered as the
16
+ * prospective drop target.
17
+ */
18
+ export function pointerTarget(state, root, x, y) {
19
+ if (state.captured !== null)
20
+ return state.captured;
21
+ const hit = uiHitTest(root, x, y);
22
+ if (state.dragSource !== null)
23
+ state.dragTarget = hit === state.dragSource ? null : hit;
24
+ return hit;
25
+ }
26
+ export function beginDrag(state, source, payload) {
27
+ state.dragSource = source;
28
+ state.dragTarget = null;
29
+ state.dragPayload = payload;
30
+ }
31
+ /** The node a drop would land on, or null. */
32
+ export function dropTarget(state) {
33
+ return state.dragTarget;
34
+ }
35
+ /**
36
+ * Finish a drag and report where it landed. Null when no drag was in progress.
37
+ *
38
+ * A null `target` is an ordinary outcome — a drop on nothing — and is reported rather than
39
+ * suppressed, because a caller often wants to know a drag ended even where it achieved nothing.
40
+ */
41
+ export function endDrag(state) {
42
+ const source = state.dragSource;
43
+ if (source === null)
44
+ return null;
45
+ const result = { source, target: state.dragTarget, payload: state.dragPayload };
46
+ state.dragSource = null;
47
+ state.dragTarget = null;
48
+ state.dragPayload = '';
49
+ return result;
50
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Moving a clipping node's contents underneath it.
3
+ *
4
+ * **Scrolling is an offset applied at placement, not a transform applied at draw.** `uiLayout.ts`
5
+ * subtracts it from the content origin, so a scrolled child's rectangle simply is where it is —
6
+ * and hit testing, focus and clipping keep working without any of them knowing that scrolling
7
+ * exists. Applied at draw, each of those would need the inverse and one of them would eventually
8
+ * get it wrong in one direction only.
9
+ *
10
+ * **A scroll that moves nothing says so.** Nested containers are the case: a wheel over an inner
11
+ * list that has reached its end should scroll the outer one rather than stopping dead, and a
12
+ * caller that cannot tell "consumed" from "already at the end" cannot implement that. The boolean
13
+ * is the whole mechanism.
14
+ */
15
+ import type { UiNode } from './uiNode.ts';
16
+ /**
17
+ * How far this node's contents extend past its own box, along each axis.
18
+ *
19
+ * Read from the span `layoutUiTree` measured, which is relative to the content origin and so does
20
+ * not change as the node scrolls. **Deriving it from placed rectangles plus the live scroll value
21
+ * is wrong**, and wrong in the one situation it is asked about: after a caller has moved the
22
+ * scroll, the rectangles still reflect the previous value and the two no longer agree.
23
+ */
24
+ export declare function scrollExtent(node: UiNode): {
25
+ x: number;
26
+ y: number;
27
+ };
28
+ export declare function clampScroll(node: UiNode): void;
29
+ /** Scroll, clamped. Returns whether anything actually moved — see the header for why. */
30
+ export declare function scrollBy(node: UiNode, dx: number, dy: number): boolean;
31
+ /**
32
+ * Give a wheel to the innermost container under the pointer that can use it, then outwards.
33
+ *
34
+ * Innermost first is the order a person expects and the opposite of the order the tree makes
35
+ * convenient, so the chain is collected on the way down and walked back up. Returns the node that
36
+ * consumed it, or null when nothing did — which is a caller's signal to let the page have it.
37
+ */
38
+ export declare function routeScrollWheel(root: UiNode, x: number, y: number, dx: number, dy: number): UiNode | null;
@@ -0,0 +1,65 @@
1
+ import { uiRectHolds } from './uiNode.js';
2
+ /**
3
+ * How far this node's contents extend past its own box, along each axis.
4
+ *
5
+ * Read from the span `layoutUiTree` measured, which is relative to the content origin and so does
6
+ * not change as the node scrolls. **Deriving it from placed rectangles plus the live scroll value
7
+ * is wrong**, and wrong in the one situation it is asked about: after a caller has moved the
8
+ * scroll, the rectangles still reflect the previous value and the two no longer agree.
9
+ */
10
+ export function scrollExtent(node) {
11
+ const contentW = Math.max(0, node.rect.w - node.paddingLeft - node.paddingRight);
12
+ const contentH = Math.max(0, node.rect.h - node.paddingTop - node.paddingBottom);
13
+ return {
14
+ x: Math.max(0, node.contentSpanX - contentW),
15
+ y: Math.max(0, node.contentSpanY - contentH),
16
+ };
17
+ }
18
+ export function clampScroll(node) {
19
+ const extent = scrollExtent(node);
20
+ node.scrollX = Math.min(Math.max(0, node.scrollX), extent.x);
21
+ node.scrollY = Math.min(Math.max(0, node.scrollY), extent.y);
22
+ }
23
+ /** Scroll, clamped. Returns whether anything actually moved — see the header for why. */
24
+ export function scrollBy(node, dx, dy) {
25
+ const beforeX = node.scrollX;
26
+ const beforeY = node.scrollY;
27
+ node.scrollX += dx;
28
+ node.scrollY += dy;
29
+ clampScroll(node);
30
+ return node.scrollX !== beforeX || node.scrollY !== beforeY;
31
+ }
32
+ /* Reused across calls: this runs per wheel event and the chain is its only allocation. */
33
+ const CHAIN = [];
34
+ /**
35
+ * Give a wheel to the innermost container under the pointer that can use it, then outwards.
36
+ *
37
+ * Innermost first is the order a person expects and the opposite of the order the tree makes
38
+ * convenient, so the chain is collected on the way down and walked back up. Returns the node that
39
+ * consumed it, or null when nothing did — which is a caller's signal to let the page have it.
40
+ */
41
+ export function routeScrollWheel(root, x, y, dx, dy) {
42
+ let depth = 0;
43
+ let walk = root;
44
+ while (walk !== null) {
45
+ CHAIN[depth] = walk;
46
+ depth += 1;
47
+ let next = null;
48
+ for (let i = walk.children.length - 1; i >= 0; i -= 1) {
49
+ const child = walk.children[i];
50
+ if (!child.hidden && uiRectHolds(child, x, y)) {
51
+ next = child;
52
+ break;
53
+ }
54
+ }
55
+ walk = next;
56
+ }
57
+ for (let i = depth - 1; i >= 0; i -= 1) {
58
+ const node = CHAIN[i];
59
+ if (!node.clip)
60
+ continue;
61
+ if (scrollBy(node, dx, dy))
62
+ return node;
63
+ }
64
+ return null;
65
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * A panel background that stretches without stretching its corners.
3
+ *
4
+ * Nine cells: the four corners keep their source size, the four edges stretch along one axis, and
5
+ * the middle stretches along both. Written into a caller's array as
6
+ * `x, y, w, h, u0, v0, u1, v1` per cell, in row-major order — top-left, top, top-right, left,
7
+ * middle, right, bottom-left, bottom, bottom-right — so the middle is always index 4.
8
+ *
9
+ * **A box narrower than its own insets scales them down rather than letting them overlap.**
10
+ * Overlapping corners draw the same alpha twice, which on a translucent panel is a visible seam
11
+ * exactly where the design was trying to be careful.
12
+ *
13
+ * **The source size is a parameter because `SpriteFrame` carries only texture coordinates.** The
14
+ * insets are in texels, so splitting the frame needs to know what the frame's texels span.
15
+ */
16
+ import type { SpriteFrame } from './spriteSheet.ts';
17
+ /** Floats per placement: x, y, w, h, u0, v0, u1, v1. */
18
+ export declare const SLICE_STRIDE = 8;
19
+ export interface SliceInset {
20
+ readonly left: number;
21
+ readonly top: number;
22
+ readonly right: number;
23
+ readonly bottom: number;
24
+ }
25
+ export declare function sliceInto(out: Float32Array, frame: SpriteFrame, sourceWidth: number, sourceHeight: number, inset: SliceInset, x: number, y: number, w: number, h: number): number;
@@ -0,0 +1,51 @@
1
+ /** Floats per placement: x, y, w, h, u0, v0, u1, v1. */
2
+ export const SLICE_STRIDE = 8;
3
+ export function sliceInto(out, frame, sourceWidth, sourceHeight, inset, x, y, w, h) {
4
+ /*
5
+ * Scaled down together when the box cannot hold both, so the two corners meet exactly at the
6
+ * midpoint instead of overlapping. The texture coordinates are deliberately *not* scaled with
7
+ * them: the corner draws a smaller piece of the box out of the same piece of the source, which
8
+ * is squashing rather than seaming and is the better of the two wrong pictures available.
9
+ */
10
+ const sideX = inset.left + inset.right;
11
+ const kx = sideX > w && sideX > 0 ? w / sideX : 1;
12
+ const left = inset.left * kx;
13
+ const right = inset.right * kx;
14
+ const sideY = inset.top + inset.bottom;
15
+ const ky = sideY > h && sideY > 0 ? h / sideY : 1;
16
+ const top = inset.top * ky;
17
+ const bottom = inset.bottom * ky;
18
+ const colX = [x, x + left, x + w - right];
19
+ const colW = [left, Math.max(0, w - left - right), right];
20
+ const rowY = [y, y + top, y + h - bottom];
21
+ const rowH = [top, Math.max(0, h - top - bottom), bottom];
22
+ const du = frame.u1 - frame.u0;
23
+ const dv = frame.v1 - frame.v0;
24
+ const uAt = [
25
+ frame.u0,
26
+ frame.u0 + (inset.left / sourceWidth) * du,
27
+ frame.u1 - (inset.right / sourceWidth) * du,
28
+ frame.u1,
29
+ ];
30
+ const vAt = [
31
+ frame.v0,
32
+ frame.v0 + (inset.top / sourceHeight) * dv,
33
+ frame.v1 - (inset.bottom / sourceHeight) * dv,
34
+ frame.v1,
35
+ ];
36
+ let at = 0;
37
+ for (let r = 0; r < 3; r += 1) {
38
+ for (let c = 0; c < 3; c += 1) {
39
+ out[at] = colX[c];
40
+ out[at + 1] = rowY[r];
41
+ out[at + 2] = colW[c];
42
+ out[at + 3] = rowH[r];
43
+ out[at + 4] = uAt[c];
44
+ out[at + 5] = vAt[r];
45
+ out[at + 6] = uAt[c + 1];
46
+ out[at + 7] = vAt[r + 1];
47
+ at += SLICE_STRIDE;
48
+ }
49
+ }
50
+ return 9;
51
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Which rows a scroll offset makes visible, so that a list costs a screenful rather than a list.
3
+ *
4
+ * An asset browser with ten thousand entries has to cost what a screenful costs. Without this the
5
+ * scene tree is unusable the first time somebody opens a real project, which is the moment nobody
6
+ * wants to discover it.
7
+ *
8
+ * **Overscan is applied before clamping, not after.** Applied after, a window near either end
9
+ * runs past the row count and a caller indexes rows that do not exist.
10
+ */
11
+ export interface RowWindow {
12
+ first: number;
13
+ count: number;
14
+ }
15
+ /** Uniform row heights, which is arithmetic. */
16
+ export declare function visibleRange(scrollY: number, viewHeight: number, rowHeight: number, rowCount: number, overscan: number): RowWindow;
17
+ /**
18
+ * Variable row heights, through a prefix-sum table of row tops the caller owns.
19
+ *
20
+ * **The caller owns the table** because only the caller knows when a row's height changed, and
21
+ * recomputing it here every frame would be the cost this module exists to avoid. `offsets` holds
22
+ * `rowCount + 1` entries: the top of each row, then the bottom of the last.
23
+ */
24
+ export declare function visibleRangeVariable(scrollY: number, viewHeight: number, offsets: Float64Array, rowCount: number, overscan: number): RowWindow;
@@ -0,0 +1,49 @@
1
+ function clampWindow(first, lastExclusive, rowCount) {
2
+ const lo = Math.max(0, Math.min(rowCount, first));
3
+ const hi = Math.max(lo, Math.min(rowCount, lastExclusive));
4
+ return { first: lo, count: hi - lo };
5
+ }
6
+ /** Uniform row heights, which is arithmetic. */
7
+ export function visibleRange(scrollY, viewHeight, rowHeight, rowCount, overscan) {
8
+ if (rowHeight <= 0 || rowCount <= 0)
9
+ return { first: 0, count: 0 };
10
+ const top = Math.max(0, scrollY);
11
+ const first = Math.floor(top / rowHeight) - overscan;
12
+ const lastExclusive = Math.ceil((top + viewHeight) / rowHeight) + overscan;
13
+ return clampWindow(first, lastExclusive, rowCount);
14
+ }
15
+ /**
16
+ * Variable row heights, through a prefix-sum table of row tops the caller owns.
17
+ *
18
+ * **The caller owns the table** because only the caller knows when a row's height changed, and
19
+ * recomputing it here every frame would be the cost this module exists to avoid. `offsets` holds
20
+ * `rowCount + 1` entries: the top of each row, then the bottom of the last.
21
+ */
22
+ export function visibleRangeVariable(scrollY, viewHeight, offsets, rowCount, overscan) {
23
+ if (rowCount <= 0)
24
+ return { first: 0, count: 0 };
25
+ const top = Math.max(0, scrollY);
26
+ const bottom = top + viewHeight;
27
+ /* Largest index whose top is at or before the window's top. */
28
+ let lo = 0;
29
+ let hi = rowCount;
30
+ while (lo < hi) {
31
+ const mid = (lo + hi + 1) >> 1;
32
+ if ((offsets[mid] ?? 0) <= top)
33
+ lo = mid;
34
+ else
35
+ hi = mid - 1;
36
+ }
37
+ const first = lo - overscan;
38
+ /* Smallest index whose top is at or after the window's bottom. */
39
+ let a = 0;
40
+ let b = rowCount;
41
+ while (a < b) {
42
+ const mid = (a + b) >> 1;
43
+ if ((offsets[mid] ?? 0) >= bottom)
44
+ b = mid;
45
+ else
46
+ a = mid + 1;
47
+ }
48
+ return clampWindow(first, a + overscan, rowCount);
49
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@driftengine/ui2d",
3
- "version": "3.63.0",
3
+ "version": "4.0.0",
4
4
  "description": "The 2D layer: batched sprites, sheets, tilemaps, and a retained interface tree over them",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "sideEffects": false,
29
29
  "peerDependencies": {
30
- "@driftengine/core": "3.63.0"
30
+ "@driftengine/core": "4.0.0"
31
31
  },
32
32
  "author": "Drift Technologies",
33
33
  "repository": {