summaryrefslogtreecommitdiff
path: root/apps/web/src/preview/components/overlay-layer.tsx
blob: eb730f6e5576a5e31ab8b49d51fac090e5589e22 (plain) (blame)
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
"use client";

import type {
	PreviewOverlayHudAnchor,
	PreviewOverlayInstance,
	PreviewOverlayPlane,
} from "@/preview/overlays";
import { usePreviewViewport } from "./preview-viewport";

const HUD_ANCHOR_CLASS_NAMES: Record<PreviewOverlayHudAnchor, string> = {
	"top-left": "absolute top-2 left-2 flex flex-col items-start gap-1.5",
	"top-right": "absolute top-2 right-2 flex flex-col items-end gap-1.5",
	"bottom-left":
		"absolute bottom-2 left-2 flex flex-col-reverse items-start gap-1.5",
	"bottom-right":
		"absolute right-2 bottom-2 flex flex-col-reverse items-end gap-1.5",
};

function getPositionedMountStyle({
	instance,
	baseStyle,
	sceneLeft,
	sceneTop,
	sceneWidth,
	sceneHeight,
}: {
	instance: PreviewOverlayInstance;
	baseStyle: {
		pointerEvents: "none" | "auto";
		zIndex: number | undefined;
	};
	sceneLeft: number;
	sceneTop: number;
	sceneWidth: number;
	sceneHeight: number;
}) {
	if (instance.mount.kind === "hud") {
		return baseStyle;
	}

	const isSceneMount = instance.mount.kind === "scene";
	const mount = instance.mount;
	const hasExplicitBounds =
		mount.x != null ||
		mount.y != null ||
		mount.width != null ||
		mount.height != null;

	return {
		...baseStyle,
		left: (isSceneMount ? sceneLeft : 0) + (mount.x ?? 0),
		top: (isSceneMount ? sceneTop : 0) + (mount.y ?? 0),
		...(hasExplicitBounds
			? {
					...(mount.width != null ? { width: mount.width } : {}),
					...(mount.height != null ? { height: mount.height } : {}),
				}
			: {
					width: isSceneMount ? sceneWidth : "100%",
					height: isSceneMount ? sceneHeight : "100%",
				}),
	} as const;
}

export function PreviewOverlayLayer({
	instances,
	plane,
}: {
	instances: PreviewOverlayInstance[];
	plane: PreviewOverlayPlane;
}) {
	const viewport = usePreviewViewport();
	const visibleInstances = instances
		.filter((instance) => (instance.plane ?? "over-interaction") === plane)
		.sort((left, right) => (left.zIndex ?? 0) - (right.zIndex ?? 0));

	if (visibleInstances.length === 0) {
		return null;
	}

	const hudInstancesByAnchor = visibleInstances.reduce<
		Record<PreviewOverlayHudAnchor, PreviewOverlayInstance[]>
	>(
		(groups, instance) => {
			if (instance.mount.kind === "hud") {
				groups[instance.mount.anchor].push(instance);
			}
			return groups;
		},
		{
			"top-left": [],
			"top-right": [],
			"bottom-left": [],
			"bottom-right": [],
		},
	);

	return (
		<>
			{visibleInstances.map((instance) => {
				if (instance.mount.kind === "hud") {
					return null;
				}

				const pointerEvents = instance.pointerEvents ?? "none";
				const baseStyle = {
					pointerEvents,
					zIndex: instance.zIndex,
				} as const;
				const content = instance.render({
					sceneHeight: viewport.sceneHeight,
					sceneWidth: viewport.sceneWidth,
				});

				switch (instance.mount.kind) {
					case "scene":
						return (
							<div
								key={instance.id}
								className="absolute"
								style={getPositionedMountStyle({
									instance,
									baseStyle,
									sceneLeft: viewport.sceneLeft,
									sceneTop: viewport.sceneTop,
									sceneWidth: viewport.sceneWidth,
									sceneHeight: viewport.sceneHeight,
								})}
							>
								{content}
							</div>
						);

					case "viewport":
						return (
							<div
								key={instance.id}
								className="absolute"
								style={getPositionedMountStyle({
									instance,
									baseStyle,
									sceneLeft: viewport.sceneLeft,
									sceneTop: viewport.sceneTop,
									sceneWidth: viewport.sceneWidth,
									sceneHeight: viewport.sceneHeight,
								})}
							>
								{content}
							</div>
						);

					default:
						return null;
				}
			})}
			{(
				Object.entries(hudInstancesByAnchor) as Array<
					[PreviewOverlayHudAnchor, PreviewOverlayInstance[]]
				>
			).map(([anchor, anchorInstances]) => {
				if (anchorInstances.length === 0) {
					return null;
				}

				const sortedAnchorInstances = [...anchorInstances].sort(
					(left, right) =>
						(left.mount.kind === "hud" ? (left.mount.order ?? 0) : 0) -
						(right.mount.kind === "hud" ? (right.mount.order ?? 0) : 0),
				);

				return (
					<div key={anchor} className={HUD_ANCHOR_CLASS_NAMES[anchor]}>
						{sortedAnchorInstances.map((instance) => {
							const pointerEvents = instance.pointerEvents ?? "none";
							const content = instance.render({
								sceneHeight: viewport.sceneHeight,
								sceneWidth: viewport.sceneWidth,
							});

							return (
								<div
									key={instance.id}
									style={{
										pointerEvents,
										zIndex: instance.zIndex,
									}}
								>
									{content}
								</div>
							);
						})}
					</div>
				);
			})}
		</>
	);
}