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 | 1x 51x 51x 33x 33x 33x 33x 33x 33x 33x 33x 1087x 47x 47x 28x 28x 28x 27x 27x 33x 10x 10x 10x 10x 10x 30x 30x 6x 4x 14x 14x 14x 14x 14x 99x 99x 13x 13x 13x 13x 13x 13x 13x 17x 37x 37x 4x 4x 15x 4x 4x 4x 4x 4x 4x 4x 33x 4x 4x 3x 3x 3x 4x 29x 29x 23x 6x 6x 1x 5x 6x 17x 6x 6x 6x 6x 5x 12x 4x 12x 12x 3x 3x | import { atom } from "nanostores";
import { Slice } from "../slice";
import type { EditorContext } from "../editor-context";
import { ProjectSlice } from "./project-slice";
import { ViewportSlice } from "./viewport-slice";
import { ColumnsSlice } from "./columns-slice";
import { CursorSlice } from "./cursor-slice";
import { SelectionSlice } from "./selection-slice";
import { BoxSelectionSlice } from "./box-selection-slice";
import { ToolSlice } from "./tool-slice";
import { SnapSlice } from "./snap-slice";
import { RenderSlice } from "./render-slice";
import { HistorySlice } from "./history-slice";
import { TimingSlice } from "./timing-slice";
import { EVENT } from "../components";
import { Point, Rect } from "../../geometry";
import { EraseUserAction, PlaceEntityUserAction } from "../user-actions";
export class PointerInteractionSlice extends Slice {
static readonly sliceKey = "pointer-interaction";
$lastPlacedEntityInfo = atom<{ entityId: string; columnId: string } | null>(null);
constructor(ctx: EditorContext) {
super(ctx);
}
hitTest(point: Point): string | null {
const scroll = this.ctx.get(ViewportSlice).$scroll.get();
const contentX = point.x + scroll.x;
const contentY = point.y + scroll.y;
const specs = this.ctx.get(RenderSlice).$visibleRenderObjects.get();
const HIT_TOLERANCE = 4;
let bestId: string | null = null;
let bestDistance = Infinity;
for (const spec of specs) {
if (!spec.entityId) continue;
const hitRect = Rect.expand(
{ x: spec.x, y: spec.y, width: spec.width, height: spec.height },
HIT_TOLERANCE,
);
if (!Rect.contains(hitRect, { x: contentX, y: contentY })) continue;
const center = Rect.center({ x: spec.x, y: spec.y, width: spec.width, height: spec.height });
const distance = Point.distance({ x: contentX, y: contentY }, center);
if (distance < bestDistance) {
bestDistance = distance;
bestId = spec.entityId;
}
}
return bestId;
}
getColumnIndexFromViewportX(viewportX: number): number {
const contentX = viewportX + this.ctx.get(ViewportSlice).$scroll.get().x;
const columns = this.ctx.get(ColumnsSlice).$columns.get();
Iif (columns.length === 0) return 0;
Iif (contentX < columns[0]!.x) return 0;
for (let i = 0; i < columns.length; i++) {
const col = columns[i]!;
if (contentX >= col.x && contentX < col.x + col.width) {
return i;
}
}
return columns.length - 1;
}
computePulseFromViewportY(viewportY: number): number {
const scrollTop = this.ctx.get(ViewportSlice).$scroll.get().y;
const contentY = viewportY + scrollTop;
const trackHeight = this.ctx.get(ViewportSlice).getTrackHeight();
const scaleY = this.ctx.get(ViewportSlice).getScaleY();
return (trackHeight - contentY) / scaleY;
}
recomputeCursorPulse(): void {
const viewportY = this.ctx.get(CursorSlice).$cursorViewportPos.get().y;
if (viewportY < 0) return;
const scrollTop = this.ctx.get(ViewportSlice).$scroll.get().y;
const contentY = viewportY + scrollTop;
const trackHeight = this.ctx.get(ViewportSlice).getTrackHeight();
const scaleY = this.ctx.get(ViewportSlice).getScaleY();
const rawPulse = (trackHeight - contentY) / scaleY;
const snappedPulse = this.snapToGrid(rawPulse);
this.ctx.get(CursorSlice).$cursorPulse.set(snappedPulse);
}
private snapToGrid(pulse: number): number {
return this.ctx
.get(TimingSlice)
.getTimingEngine()
.snapPulse(pulse, this.ctx.get(SnapSlice).$snap.get());
}
handlePointerDown(point: Point, shiftKey: boolean = false): void {
const activeTool = this.ctx.get(ToolSlice).$activeTool.get();
if (activeTool === "pencil") {
const contentX = point.x + this.ctx.get(ViewportSlice).$scroll.get().x;
const columns = this.ctx.get(ColumnsSlice).$columns.get();
const column = columns.find((c) => contentX >= c.x && contentX < c.x + c.width);
Iif (!column?.placementHandler) return;
const pulse = this.snapToGrid(this.computePulseFromViewportY(point.y));
const entity = column.placementHandler(pulse);
Iif (!entity) return;
const previousSelection = new Set(this.ctx.get(SelectionSlice).$selection.get());
this.ctx
.get(HistorySlice)
.applyAction(
new PlaceEntityUserAction(
this.ctx,
entity,
column.id,
previousSelection,
this.$lastPlacedEntityInfo,
),
);
return;
}
if (activeTool === "erase") {
const hit = this.hitTest(point);
if (hit) {
const entity = this.ctx.get(ProjectSlice).entityManager.get(hit);
Eif (entity) {
this.ctx
.get(HistorySlice)
.applyAction(new EraseUserAction(this.ctx, hit, structuredClone(entity)));
}
}
return;
}
const hit = this.hitTest(point);
if (hit) {
if (shiftKey) {
const next = new Set(this.ctx.get(SelectionSlice).$selection.get());
if (next.has(hit)) {
next.delete(hit);
} else {
next.add(hit);
}
this.ctx.get(SelectionSlice).$selection.set(next);
} else {
this.ctx.get(SelectionSlice).$selection.set(new Set([hit]));
}
} else {
const colIndex = this.getColumnIndexFromViewportX(point.x);
const pulse = this.computePulseFromViewportY(point.y);
this.ctx.get(BoxSelectionSlice).start(colIndex, pulse);
if (!shiftKey) {
this.ctx.get(SelectionSlice).$selection.set(new Set());
}
}
}
handlePointerMove(viewportX: number, viewportY: number): void {
if (this.ctx.get(BoxSelectionSlice).isActive()) {
this.ctx
.get(BoxSelectionSlice)
.update(
this.getColumnIndexFromViewportX(viewportX),
this.computePulseFromViewportY(viewportY),
);
}
this.ctx.get(CursorSlice).$cursorViewportPos.set({ x: viewportX, y: viewportY });
this.recomputeCursorPulse();
}
handlePointerUp(): void {
Iif (!this.ctx.get(BoxSelectionSlice).isActive()) return;
this.ctx
.get(BoxSelectionSlice)
.finalize(this.ctx.get(ProjectSlice).entityManager.entitiesWithComponent(EVENT));
}
}
|