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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | /**
* @packageDocumentation
*
* Timeline behavior for ScrollableCanvas. Renders a rhythm-game-style
* vertical grid with columns for measures, time signatures, BPM changes,
* and (in the future) gameplay lanes.
*
* ## Layout
*
* - Bottom = start of song (pulse 0)
* - Top = end of song (pulse = chart size)
- Scale Y = 0.2 px/pulse at 100% zoom, multiplied by zoom level from EditorController
* - Columns are defined by the EditorController and stacked left-to-right
* - Horizontal grid lines span the entire timeline width across all columns
*
* ## Grid levels
*
* - **Measure lines** — thick, labeled with 1-based measure number
* - **Beat lines** (1/4) — medium opacity
* - **Snap lines** (1/16) — very light, only visible when zoomed in enough
*/
import type {
ScrollableCanvasBehavior,
ScrollableCanvasBehaviorFactory,
ScrollableCanvasContext,
RenderObject,
RenderHandle,
Renderer,
} from "../scrollable-canvas";
import type { EditorController, TimelineRenderSpec } from "../editor-core";
// ---------------------------------------------------------------------------
// Renderers
// ---------------------------------------------------------------------------
interface GridLineData {
color: string;
label?: string;
}
function createGridLineRenderer(): (data: unknown) => RenderHandle<GridLineData> {
return (data: unknown) => {
const d = data as GridLineData;
const el = document.createElement("div");
el.style.backgroundColor = d.color;
if (d.label) {
const labelEl = document.createElement("span");
labelEl.textContent = d.label;
labelEl.style.position = "absolute";
labelEl.style.left = "4px";
labelEl.style.top = "-7px";
labelEl.style.fontSize = "10px";
labelEl.style.color = "var(--gray-11)";
labelEl.style.fontFamily = "var(--default-font-family)";
labelEl.style.pointerEvents = "none";
el.appendChild(labelEl);
}
return {
dom: el,
update(newData: unknown) {
const nd = newData as GridLineData;
el.style.backgroundColor = nd.color;
const labelEl = el.querySelector("span");
if (labelEl && nd.label !== undefined) {
labelEl.textContent = nd.label;
}
},
};
};
}
interface ColumnBgData {
backgroundColor?: string;
showBorder: boolean;
}
function createColumnBgRenderer(): (data: unknown) => RenderHandle<ColumnBgData> {
return (data: unknown) => {
const d = data as ColumnBgData;
const el = document.createElement("div");
if (d.backgroundColor) {
el.style.backgroundColor = d.backgroundColor;
}
// Left border (skip for first column)
if (d.showBorder) {
const border = document.createElement("div");
border.style.position = "absolute";
border.style.left = "0";
border.style.top = "0";
border.style.bottom = "0";
border.style.width = "1px";
border.style.backgroundColor = "var(--gray-5)";
el.appendChild(border);
}
return {
dom: el,
update(newData: unknown) {
const nd = newData as ColumnBgData;
if (nd.backgroundColor) {
el.style.backgroundColor = nd.backgroundColor;
}
},
};
};
}
interface ColumnTitleData {
title: string;
}
function createColumnTitleRenderer(): (data: unknown) => RenderHandle<ColumnTitleData> {
return (data: unknown) => {
const d = data as ColumnTitleData;
const el = document.createElement("div");
el.textContent = d.title;
el.style.display = "flex";
el.style.alignItems = "center";
el.style.justifyContent = "center";
el.style.fontSize = "10px";
el.style.color = "var(--gray-11)";
el.style.fontFamily = "var(--default-font-family)";
el.style.pointerEvents = "none";
return {
dom: el,
update(newData: unknown) {
const nd = newData as ColumnTitleData;
el.textContent = nd.title;
},
};
};
}
function createTrailingBorderRenderer(): () => RenderHandle<{}> {
return () => {
const el = document.createElement("div");
el.style.backgroundColor = "var(--gray-5)";
return {
dom: el,
update() {},
};
};
}
interface EventMarkerData {
text: string;
backgroundColor: string;
textColor: string;
selected?: boolean;
}
function createEventMarkerRenderer(): (data: unknown) => RenderHandle<EventMarkerData> {
return (data: unknown) => {
const d = data as EventMarkerData;
const el = document.createElement("div");
el.style.backgroundColor = d.backgroundColor;
el.style.color = d.textColor;
el.style.fontSize = "10px";
el.style.fontWeight = "600";
el.style.fontFamily = "var(--default-font-family)";
el.style.display = "flex";
el.style.alignItems = "center";
el.style.justifyContent = "center";
el.style.boxShadow = "inset 1px 1px 0 #fff5, inset -1px -1px 0 #0005";
el.style.pointerEvents = "auto";
if (d.selected) {
el.style.backgroundColor = "var(--cyan-10)";
el.style.color = "#000";
}
const textEl = document.createElement("span");
textEl.textContent = d.text;
el.appendChild(textEl);
return {
dom: el,
update(newData: unknown) {
const nd = newData as EventMarkerData;
el.style.backgroundColor = nd.backgroundColor;
textEl.textContent = nd.text;
if (nd.selected) {
el.style.backgroundColor = "var(--cyan-10)";
el.style.color = "#000";
} else {
el.style.backgroundColor = nd.backgroundColor;
el.style.color = nd.textColor;
}
},
};
};
}
function createPlayheadRenderer(): () => RenderHandle<{}> {
return () => {
const el = document.createElement("div");
el.style.backgroundColor = "var(--accent-9)";
el.style.pointerEvents = "none";
el.style.zIndex = "2";
return {
dom: el,
update() {},
};
};
}
// ---------------------------------------------------------------------------
// Behavior factory
// ---------------------------------------------------------------------------
function createSelectionBoxRenderer(): () => RenderHandle<{}> {
return () => {
const el = document.createElement("div");
el.style.backgroundColor = "rgba(159, 238, 42, 0.15)";
el.style.border = "1px dashed rgba(159, 238, 42, 0.6)";
el.style.pointerEvents = "none";
return {
dom: el,
update() {},
};
};
}
const rendererMap: Record<string, Renderer> = {
"column-bg": createColumnBgRenderer(),
"column-title": createColumnTitleRenderer(),
"trailing-border": createTrailingBorderRenderer(),
"event-marker": createEventMarkerRenderer(),
playhead: createPlayheadRenderer(),
"grid-line": createGridLineRenderer(),
"selection-box": createSelectionBoxRenderer(),
};
function specToRenderObject(spec: TimelineRenderSpec): RenderObject {
const renderer = rendererMap[spec.type];
if (!renderer) {
throw new Error(`Unknown render spec type: ${spec.type}`);
}
return {
key: spec.key,
x: spec.x,
y: spec.y,
width: spec.width,
height: spec.height,
renderer,
data: spec.data,
testId: spec.testId,
layer: spec.layer,
};
}
export function createTimelineBehaviorFactory(
controller: EditorController,
): ScrollableCanvasBehaviorFactory {
return (ctx: ScrollableCanvasContext): ScrollableCanvasBehavior => {
controller.setViewportSize(ctx.viewportWidth, ctx.viewportHeight);
const unsubOutbox = controller.outbox.on("setScroll", (point) => {
ctx.setScroll(point);
});
const unsubVisible = controller.$visibleRenderObjects.subscribe(() => {
ctx.refresh();
});
return {
getContentSize() {
return {
width: controller.getTimelineWidth(),
height: controller.getContentHeight(),
};
},
onConnected() {
controller.setViewportSize(ctx.viewportWidth, ctx.viewportHeight);
controller.onConnected();
},
onScroll(scrollLeft: number, scrollTop: number) {
controller.setScroll({ x: scrollLeft, y: scrollTop });
},
onPointerEvent(event, contentX, contentY) {
const viewportX = contentX - ctx.scrollLeft;
const viewportY = contentY - ctx.scrollTop;
if (event.type === "pointermove") {
controller.handlePointerMove(viewportX, viewportY);
}
if (event.type === "pointerdown") {
controller.handlePointerDown({ x: viewportX, y: viewportY }, event.shiftKey);
}
if (event.type === "pointerup") {
controller.handlePointerUp();
}
},
getVisibleObjects(): RenderObject[] {
return controller.$visibleRenderObjects.get().map(specToRenderObject);
},
[Symbol.dispose]() {
unsubOutbox();
unsubVisible();
},
};
};
}
|