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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 30x 30x 30x 30x 30x 30x 53x 52x 52x 30x 22x 22x 22x 22x 21x 21x 21x 30x 52x 52x 27x 39x 4x 30x 53x 53x 28x 28x 37x 36x 36x 32x 32x 37x 37x 17x 11x 11x 11x 37x 37x 37x 37x 3x 9x 31x 31x 31x 14x 14x 12x 12x 2x 2x 41x 23x 30x 46x 38x 36x 19x 19x 36x 12x 24x 24x 2x 22x 22x 22x 22x 22x 38x 16x 16x 36x 36x 36x 36x | /**
* Touch play for the play views: which lane a touch point lands in, how fingers pressing, holding, sliding and lifting
* turn into lane presses and releases, and the `lane` intents ({@link PlayIntent}) a pointer event makes
* ({@link TouchLaneFingers}). Kept free of DOM and Pixi so the rules can be tested on their own; a play view feeds it
* its pointer events (`touch-lane-input.ts`) and sends the intents to the session, which presses the lanes through the
* engine's input.
*/
import type { PlayIntent, PlayfieldStage } from '../types.ts';
import { resolveScaledViewport } from '../../stage/viewport.ts';
/** A lane column as the gameplay layout places it, in design pixels (`bottom` is the judgement line). */
export interface TouchLaneRect {
readonly channel: string;
readonly x: number;
readonly w: number;
readonly top: number;
readonly bottom: number;
}
/** The area a finger presses a lane through, in design pixels. `right` and `bottom` are exclusive. */
export interface TouchLaneZone {
readonly channel: string;
readonly left: number;
readonly right: number;
readonly top: number;
readonly bottom: number;
}
export interface TouchLaneZoneOptions {
/** Height of the stage; zones run from their top down to it, past the judgement line. */
readonly stageHeight: number;
/** Share of the lane above the judgement line a zone covers. Defaults to 0.45. */
readonly heightRatio?: number;
/** Smallest height above the judgement line a zone covers, for short lanes. Defaults to 80. */
readonly minHeight?: number;
/** How far a zone reaches past an outer lane edge (one with no neighbour beside it). Defaults to 16. */
readonly edgePadding?: number;
}
const DEFAULT_HEIGHT_RATIO = 0.45;
const DEFAULT_MIN_HEIGHT = 80;
const DEFAULT_EDGE_PADDING = 16;
/**
* Touch zones for the lanes: each covers its lane column from the lower part of the lane (`heightRatio` of it, at least
* `minHeight`) down past the judgement line to the bottom of the stage. Neighbouring lanes split the gap (or overlap)
* between them at its midpoint, so a finger landing on a lane divider still presses one of the two; lanes further apart
* than either lane's width — the two sides of a DP layout — are not joined, and every unjoined edge reaches out by
* `edgePadding` (at most half the lane's width). Zones come back ordered left to right; lanes without width are skipped.
*/
export function resolveTouchLaneZones(lanes: Iterable<TouchLaneRect>, options: TouchLaneZoneOptions): TouchLaneZone[] {
const heightRatio = options.heightRatio ?? DEFAULT_HEIGHT_RATIO;
const minHeight = options.minHeight ?? DEFAULT_MIN_HEIGHT;
const edgePadding = options.edgePadding ?? DEFAULT_EDGE_PADDING;
const sorted = [...lanes].filter((lane) => lane.w > 0).sort((a, b) => a.x - b.x);
const lefts = sorted.map((lane) => lane.x - Math.min(edgePadding, lane.w / 2));
const rights = sorted.map((lane) => lane.x + lane.w + Math.min(edgePadding, lane.w / 2));
for (let index = 0; index + 1 < sorted.length; index += 1) {
const current = sorted[index]!;
const next = sorted[index + 1]!;
const gap = next.x - (current.x + current.w);
if (gap > Math.max(current.w, next.w)) continue;
const split = (current.x + current.w + next.x) / 2;
rights[index] = split;
lefts[index + 1] = split;
}
return sorted.map((lane, index) => {
const reach = Math.max(minHeight, (lane.bottom - lane.top) * heightRatio);
return {
channel: lane.channel,
left: lefts[index]!,
right: rights[index]!,
top: Math.max(lane.top, lane.bottom - reach),
bottom: Math.max(options.stageHeight, lane.bottom),
};
});
}
/** The channel of the zone containing (`x`, `y`), or `undefined` when the point is in none. */
export function hitTestTouchLane(zones: readonly TouchLaneZone[], x: number, y: number): string | undefined {
for (const zone of zones) {
if (x >= zone.left && x < zone.right && y >= zone.top && y < zone.bottom) return zone.channel;
}
return undefined;
}
/** Lanes that went down and came up because of one finger event. */
export interface TouchLaneChange {
readonly pressed: readonly string[];
readonly released: readonly string[];
}
const NO_CHANGE: TouchLaneChange = { pressed: [], released: [] };
/**
* Tracks which lane each finger is on. A lane is held while at least one finger is on it: the first finger to reach it
* presses it, and the last to leave (lift off, or slide into another lane or out of every lane) releases it. A finger
* that touches down outside every lane is still tracked, so sliding it into a lane presses that lane.
*/
export class TouchLaneTracker {
private readonly laneByPointer = new Map<number, string | undefined>();
private readonly fingersByLane = new Map<string, number>();
/** A finger touched down at `channel` (`undefined` outside every lane). A pointer already down is treated as a move. */
public down(pointerId: number, channel: string | undefined): TouchLaneChange {
Eif (!this.laneByPointer.has(pointerId)) this.laneByPointer.set(pointerId, undefined);
return this.move(pointerId, channel);
}
/** A finger that is down moved to `channel`. Ignored for pointers that are not down. */
public move(pointerId: number, channel: string | undefined): TouchLaneChange {
if (!this.laneByPointer.has(pointerId)) return NO_CHANGE;
const previous = this.laneByPointer.get(pointerId);
if (previous === channel) return NO_CHANGE;
this.laneByPointer.set(pointerId, channel);
const released = previous !== undefined && this.leave(previous) ? [previous] : [];
const pressed = channel !== undefined && this.enter(channel) ? [channel] : [];
return { pressed, released };
}
/** A finger lifted (or the browser cancelled it). Ignored for pointers that are not down. */
public up(pointerId: number): TouchLaneChange {
if (!this.laneByPointer.has(pointerId)) return NO_CHANGE;
const previous = this.laneByPointer.get(pointerId);
this.laneByPointer.delete(pointerId);
return { pressed: [], released: previous !== undefined && this.leave(previous) ? [previous] : [] };
}
/** Lifts every finger, releasing every held lane (used when play pauses, loses focus, or ends). */
public upAll(): TouchLaneChange {
const released = [...this.fingersByLane.keys()];
this.laneByPointer.clear();
this.fingersByLane.clear();
return { pressed: [], released };
}
/** Lanes held by at least one finger. */
public heldLanes(): string[] {
return [...this.fingersByLane.keys()];
}
/** Whether `pointerId` is a finger this tracker follows. */
public isTracking(pointerId: number): boolean {
return this.laneByPointer.has(pointerId);
}
/** Adds a finger to `channel`; true when it is the lane's first. */
private enter(channel: string): boolean {
const count = this.fingersByLane.get(channel) ?? 0;
this.fingersByLane.set(channel, count + 1);
return count === 0;
}
/** Removes a finger from `channel`; true when it was the lane's last. */
private leave(channel: string): boolean {
const count = this.fingersByLane.get(channel) ?? 0;
if (count <= 1) {
this.fingersByLane.delete(channel);
return count === 1;
}
this.fingersByLane.set(channel, count - 1);
return false;
}
}
/** Pointer kinds that play lanes. A mouse keeps its desktop behaviour (clicking only focuses the canvas). */
export function isTouchPlayPointer(pointerType: string): boolean {
return pointerType === 'touch' || pointerType === 'pen';
}
/**
* Converts a point on the canvas (CSS pixels, e.g. `PointerEvent.offsetX/Y`) to design pixels under the letterbox fit
* the scenes apply (`resolveScaledViewport`).
*/
export function canvasPointToDesign(
canvasX: number,
canvasY: number,
viewport: {
readonly x: number;
readonly y: number;
readonly scale: number;
readonly scaleX?: number;
readonly scaleY?: number;
},
): { x: number; y: number } {
return {
x: (canvasX - viewport.x) / (viewport.scaleX ?? viewport.scale),
y: (canvasY - viewport.y) / (viewport.scaleY ?? viewport.scale),
};
}
/** A touch lane's `lane` intent: a lane went down or up. */
export type TouchLaneIntent = Extract<PlayIntent, { readonly type: 'lane' }>;
/** A pointer event as the touch lanes read it (`PointerEvent`'s members of these names). */
export interface TouchLanePointer {
/** `pointerdown`, `pointermove`, `pointerup` or `pointercancel`. */
readonly type: string;
readonly pointerId: number;
readonly pointerType: string;
/** Where on the canvas, in CSS pixels. */
readonly offsetX: number;
readonly offsetY: number;
/** When, on the host clock (`performance.now()`). */
readonly timeStamp: number;
}
/** Where the touch lanes are: the design stage, the canvas it is fitted into (CSS pixels) and the lanes on the stage. */
export interface TouchLaneLayout {
readonly stage: PlayfieldStage;
readonly screen: { readonly width: number; readonly height: number };
readonly lanes: Iterable<TouchLaneRect>;
}
/** What a pointer event did: the `lane` intents it makes, and whether the browser's default for it should be stopped. */
export interface TouchLanePointerResult {
readonly intents: readonly TouchLaneIntent[];
/** A finger touching down: stopped, so the browser makes no mouse events or gestures of it. */
readonly preventDefault: boolean;
}
const NO_POINTER_RESULT: TouchLanePointerResult = { intents: [], preventDefault: false };
/**
* The fingers on a play's touch lanes: turns each pointer event into the `lane` intents it makes. A finger touching
* down or moving presses the lane under it, or — when it slid out of the lane it held — releases that lane and presses
* the new one; lifting it releases its lane. Mouse pointers are left alone (a click only focuses the canvas).
*
* The play lifts every finger itself when it pauses (it releases the lanes they held), counting each time
* (`liftCount`): the fingers down before the latest lift are forgotten, so they press nothing until they touch down
* again.
*/
export class TouchLaneFingers {
private readonly tracker = new TouchLaneTracker();
private lifts: number | undefined;
/**
* The intents `pointer` makes. `layout` is asked only for a finger touching down or moving; `liftCount` is how many
* times the play has lifted every finger so far.
*/
public handle(pointer: TouchLanePointer, layout: () => TouchLaneLayout, liftCount: number): TouchLanePointerResult {
if (!isTouchPlayPointer(pointer.pointerType)) return NO_POINTER_RESULT;
if (this.lifts !== liftCount) {
this.tracker.upAll();
this.lifts = liftCount;
}
if (pointer.type === 'pointerup' || pointer.type === 'pointercancel') {
return {
intents: touchLaneIntents(this.tracker.up(pointer.pointerId), pointer.timeStamp),
preventDefault: false,
};
}
const down = pointer.type === 'pointerdown';
if (!down && (pointer.type !== 'pointermove' || !this.tracker.isTracking(pointer.pointerId))) {
return NO_POINTER_RESULT;
}
const { stage, screen, lanes } = layout();
const viewport = resolveScaledViewport(
screen.width,
screen.height,
stage.width,
stage.height,
stage.displayAspectRatio,
);
const point = canvasPointToDesign(pointer.offsetX, pointer.offsetY, viewport);
const channel = hitTestTouchLane(resolveTouchLaneZones(lanes, { stageHeight: stage.height }), point.x, point.y);
const change = down ? this.tracker.down(pointer.pointerId, channel) : this.tracker.move(pointer.pointerId, channel);
return { intents: touchLaneIntents(change, pointer.timeStamp), preventDefault: down };
}
/** Forgets every finger, pressing nothing and releasing nothing (the view is leaving the stage). */
public reset(): void {
this.tracker.upAll();
this.lifts = undefined;
}
}
/** The `lane` intents of `change` at `atMs`: its releases first, then its presses. */
export function touchLaneIntents(change: TouchLaneChange, atMs: number): TouchLaneIntent[] {
const intents: TouchLaneIntent[] = [];
for (const channel of change.released) intents.push({ type: 'lane', channel, pressed: false, atMs });
for (const channel of change.pressed) intents.push({ type: 'lane', channel, pressed: true, atMs });
return intents;
}
|