Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 49x 51x 49x 49x 1x 1x 49x 49x 12x 12x 49x 6x 24x 8x 5x 6x 6x 6x 6x 6x 6x 2x 2x 6x 51x 51x 515x 1x 1x 515x | import { CanvasTextMetrics, type Container, Text, TextStyle } from 'pixi.js';
import { DEFAULT_TEXT_FONT, layoutTabularRun } from '@be-music/player-skin-sdk';
import { fitTextToWidth, stopMarquee } from './marquee.ts';
import type { ChildPool } from './pools.ts';
import { textResolution } from './resolution.ts';
import { alignCapCenter } from './text-metrics.ts';
/**
* Per-frame HUD text for built-in skins. Nodes come from a {@link ChildPool} when one is given (so a 60 fps redraw
* reuses parented `Text` objects), styles are cached by value, and {@link addHUDNumber} lays numerals out with tabular
* figures.
*/
type TextWeight = '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';
export interface HUDTextOptions {
size?: number;
weight?: TextWeight;
fill?: number;
fontFamily?: string;
letterSpacing?: number;
anchorX?: number;
anchorY?: number;
/** Width to fit the text into (squeezed a little, then scrolled as a marquee; see `fitTextToWidth`). */
maxWidth?: number;
/** `false` clips an overflowing text at its start instead of scrolling it. */
marquee?: boolean;
/** Squeezes further when at most the final two characters would scroll. */
squeezeShortOverflow?: boolean;
/** Horizontal skew in radians (negative leans the glyph tops right, like italic). */
skewX?: number;
rotation?: number;
stroke?: { color: number; width: number; alignment?: number; join?: 'round' | 'bevel' | 'miter' };
dropShadow?: { color: number; alpha: number; blur: number; distance: number; angle?: number };
/** Uniform scale around the anchor, applied after the `maxWidth` squeeze (judge / combo "punch"). */
scale?: number;
}
export function addHUDText(
layer: Container,
text: string,
x: number,
y: number,
opts: HUDTextOptions = {},
pool?: ChildPool,
): Text {
const tintable = isTintable(opts);
return placeHUDText(layer, text, x, y, opts, pool, resolveTextStyle(opts, tintable), tintable);
}
function placeHUDText(
layer: Container,
text: string,
x: number,
y: number,
opts: HUDTextOptions,
pool: ChildPool | undefined,
style: TextStyle,
tintable: boolean,
): Text {
// A plain fill (no stroke or shadow to keep their own colours) rasterizes white and takes its colour from the tint, so
// a colour change — a flashing judgement, a combo turning gold — reuses the glyph texture instead of re-rendering it.
// The pool hands back the node that showed this string last frame, so it keeps its rasterized texture.
const node = pool?.acquireText(text, style) ?? new Text();
// Rasterize at the final device density so text stays crisp after the viewport magnifies the design canvas.
const resolution = textResolution();
Eif (node.resolution !== resolution) {
node.resolution = resolution;
}
node.text = text;
Eif (node.style !== style) {
node.style = style;
}
node.tint = tintable ? (opts.fill ?? 0xffffff) : 0xffffff;
node.anchor.set(opts.anchorX ?? 0, opts.anchorY ?? 0);
alignCapCenter(node, opts.fontFamily ?? DEFAULT_TEXT_FONT, opts.weight ?? '500', opts.size ?? 10);
node.position.set(x, y);
node.scale.set(1, 1);
// Pooled texts keep their previous skew / rotation, so both are always written.
node.skew.set(opts.skewX ?? 0, 0);
node.rotation = opts.rotation ?? 0;
Eif (!pool) {
layer.addChild(node);
}
Iif (opts.maxWidth !== undefined) {
// Pooled nodes live in the pool's text host; the clip box joins them there.
fitTextToWidth(node, node.parent ?? layer, {
maxWidth: opts.maxWidth,
anchorX: opts.anchorX ?? 0,
anchorY: opts.anchorY ?? 0,
scale: opts.scale,
scroll: opts.marquee,
squeezeShortOverflow: opts.squeezeShortOverflow,
});
} else {
stopMarquee(node);
Iif (opts.scale !== undefined && opts.scale !== 1) node.scale.set(opts.scale, opts.scale);
}
return node;
}
/**
* Numeric readout with tabular figures: each glyph is its own pooled text centred in a fixed-width cell (see
* `layoutTabularRun`), so a changing score or combo never shifts sideways as its digits change. Returns the glyphs'
* nodes, for a caller that fades them.
*/
export function addHUDNumber(
layer: Container,
text: string,
x: number,
y: number,
opts: HUDTextOptions = {},
pool?: ChildPool,
): Text[] {
const tintable = isTintable(opts);
const style = resolveTextStyle(opts, tintable);
const run = layoutTabularRun(text, (char) => measureGlyph(char, style));
const squeeze = opts.maxWidth !== undefined && run.width > opts.maxWidth ? opts.maxWidth / run.width : 1;
const scale = opts.scale ?? 1;
const left = x - run.width * squeeze * scale * (opts.anchorX ?? 0);
const nodes: Text[] = [];
const glyphOptions = { ...opts, anchorX: 0.5, maxWidth: undefined, scale: undefined };
for (const glyph of run.glyphs) {
if (glyph.char === ' ') continue;
const node = placeHUDText(
layer,
glyph.char,
left + (glyph.x + glyph.w / 2) * squeeze * scale,
y,
glyphOptions,
pool,
style,
tintable,
);
node.scale.set(squeeze * scale, scale);
nodes.push(node);
}
return nodes;
}
/** The width {@link addHUDNumber} lays `text` out in (before any `maxWidth` squeeze or `scale`), for a box around it. */
export function measureHUDNumber(text: string, opts: HUDTextOptions = {}): number {
const style = resolveTextStyle(opts, isTintable(opts));
return layoutTabularRun(text, (char) => measureGlyph(char, style)).width;
}
const GLYPH_WIDTH_CACHE = new WeakMap<TextStyle, Map<string, number>>();
/** The advance of `char` in `style`, cached per style, for laying glyphs out in tabular cells. */
export function measureGlyph(char: string, style: TextStyle): number {
let widths = GLYPH_WIDTH_CACHE.get(style);
if (!widths) {
widths = new Map();
GLYPH_WIDTH_CACHE.set(style, widths);
}
let width = widths.get(char);
if (width === undefined) {
// Letter spacing is part of the advance; stroke width is not, so strip it from the measured box.
width = CanvasTextMetrics.measureText(char, style).width - (style._stroke?.width ?? 0);
widths.set(char, width);
}
return width;
}
/** Whether `opts` paints a plain fill, which {@link addHUDText} applies as a tint over a white raster. */
function isTintable(opts: HUDTextOptions): boolean {
return opts.stroke === undefined && opts.dropShadow === undefined;
}
/**
* A glow or outline colour rounded to 16 levels per channel. A skin that animates the colour (Synesthesia's accent drifts
* with every beat) would otherwise give the text a new style, and so a new rasterized texture, every frame; rounded, the
* style changes only when the colour crosses a step, and a blurred glow or thin outline looks the same.
*/
export function quantizeTextEffectColor(color: number): number {
const channel = (shift: number): number => Math.round(((color >> shift) & 0xff) / 17) * 17;
return (channel(16) << 16) | (channel(8) << 8) | channel(0);
}
/** A glow's opacity rounded to steps of 0.05, for the same reason as {@link quantizeTextEffectColor}. */
export function quantizeTextEffectAlpha(alpha: number): number {
return Math.round(alpha * 20) / 20;
}
function resolveTextStyle(opts: HUDTextOptions, whiteFill = false): TextStyle {
const stroke = opts.stroke && { ...opts.stroke, color: quantizeTextEffectColor(opts.stroke.color) };
const shadow = opts.dropShadow && {
...opts.dropShadow,
color: quantizeTextEffectColor(opts.dropShadow.color),
alpha: quantizeTextEffectAlpha(opts.dropShadow.alpha),
};
const fill = whiteFill ? 0xffffff : (opts.fill ?? 0xffffff);
const key = [
fill,
opts.size ?? 10,
opts.weight ?? '500',
opts.fontFamily ?? DEFAULT_TEXT_FONT,
opts.letterSpacing ?? 0,
stroke?.color ?? '',
stroke?.width ?? '',
stroke?.alignment ?? '',
stroke?.join ?? '',
shadow?.color ?? '',
shadow?.alpha ?? '',
shadow?.blur ?? '',
shadow?.distance ?? '',
shadow?.angle ?? '',
].join('|');
let style = TEXT_STYLE_CACHE.get(key);
if (!style) {
style = new TextStyle({
fill,
fontSize: opts.size ?? 10,
fontWeight: opts.weight ?? '500',
fontFamily: opts.fontFamily ?? DEFAULT_TEXT_FONT,
letterSpacing: opts.letterSpacing ?? 0,
stroke,
// Room for the blurred glow; without it the shadow is clipped to the glyph box and shows as a hard rectangle.
padding: shadow ? Math.ceil(shadow.blur * 2 + shadow.distance) : 0,
...(shadow
? {
dropShadow: {
color: shadow.color,
alpha: shadow.alpha,
blur: shadow.blur,
distance: shadow.distance,
angle: shadow.angle ?? Math.PI / 2,
},
}
: {}),
});
rememberStyle(TEXT_STYLE_CACHE, key, style);
}
return style;
}
const TEXT_STYLE_CACHE = new Map<string, TextStyle>();
/** Styles kept per cache: plenty for every look on screen, bounded when an effect animates a style every frame. */
const STYLE_CACHE_LIMIT = 512;
/** Caches `style` under `key`, forgetting the oldest entry once the cache is full (nodes keep the styles they use). */
export function rememberStyle(cache: Map<string, TextStyle>, key: string, style: TextStyle): void {
if (cache.size >= STYLE_CACHE_LIMIT) {
const oldest = cache.keys().next().value;
Eif (oldest !== undefined) cache.delete(oldest);
}
cache.set(key, style);
}
|