-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathmain.tsx
151 lines (133 loc) · 4.57 KB
/
main.tsx
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
import type { Scene } from '@antv/l7-scene';
import { Cascader, ConfigProvider } from 'antd';
import 'antd/dist/reset.css';
import type { Controller } from 'lil-gui';
import GUI from 'lil-gui';
import React, { useEffect, useRef, useState } from 'react';
import { DEFAULT_GUI_OPTIONS, MAP_TYPES, SEARCH_PARAMS_KEYS } from './constants';
import { TestCases } from './demos';
import type { GUIOptions } from './types';
const DEMO_LIST = Array.from(TestCases).map(([namespace, cases]) => {
return {
label: namespace,
value: namespace,
children: cases.map(([name]) => ({ label: name, value: name })),
};
});
const inintGuiOptions = getParamsFromUrlPath();
const isSnapshotMode = inintGuiOptions.snapshot;
export const Main = () => {
const guiRef = useRef<GUI>();
const mapContainer = useRef<HTMLDivElement>(null);
const [guiOptions, setGuiOptions] = useState<GUIOptions>(inintGuiOptions);
const [viewDemo, setViewDemo] = useState<[string, string]>(() => {
const { namespace = 'point', name = 'fill' } = inintGuiOptions;
return [namespace, name];
});
// GUI
useEffect(() => {
guiRef.current = new GUI({ title: 'Scene', autoPlace: !isSnapshotMode });
const onChange = (v: Partial<GUIOptions>) => {
setGuiOptions((pre) => ({ ...pre, ...v }));
};
guiRef.current
.add(inintGuiOptions, 'map', MAP_TYPES)
.onChange((map: GUIOptions['map']) => onChange({ map }));
guiRef.current
.add(inintGuiOptions, 'renderer', ['regl', 'device'])
.onChange((renderer: GUIOptions['renderer']) => onChange({ renderer }));
guiRef.current
.add(inintGuiOptions, 'enableWebGPU')
.onChange((enableWebGPU: GUIOptions['enableWebGPU']) => onChange({ enableWebGPU }));
guiRef.current
.add(inintGuiOptions, 'animate')
.onChange((animate: GUIOptions['animate']) => onChange({ animate }));
return () => {
guiRef.current?.destroy();
};
}, []);
// change TestCase
useEffect(() => {
const [namespace, name] = viewDemo;
const TestCase = getDemoFromName(namespace, name);
if (!TestCase) return;
let scene: Scene;
let extendGUI: Controller[] | GUI[] = [];
const asyncFun = async () => {
scene = await TestCase({ ...guiOptions, id: mapContainer.current! });
if (TestCase.extendGUI) {
extendGUI = TestCase.extendGUI?.(guiRef.current!);
}
// @ts-ignore
if (isSnapshotMode && window.screenshot) {
// @ts-ignore
await window.screenshot();
}
};
asyncFun();
syncParamsToURLPath({ ...guiOptions, namespace, name });
return () => {
scene?.destroy();
while (mapContainer.current?.firstChild) {
mapContainer.current.removeChild(mapContainer.current.lastChild!);
}
try {
extendGUI.forEach((d) => d.destroy());
} catch (error) {
console.warn('error: ', error);
}
};
}, [viewDemo, guiOptions]);
const onDemoViewChange = (value: string[]) => {
setViewDemo(value as [string, string]);
};
return (
<>
{!isSnapshotMode && (
<ConfigProvider theme={{ components: { Cascader: { dropdownHeight: 400 } } }}>
<Cascader
style={{ position: 'absolute', left: '20px', zIndex: 10, top: '20px' }}
size="large"
defaultValue={viewDemo}
options={DEMO_LIST}
onChange={onDemoViewChange}
/>
</ConfigProvider>
)}
<div ref={mapContainer} style={{ width: '100%', height: '100%' }} />
</>
);
};
/**
* get guiOptions(SEARCH_PARAMS_KEYS) from URL path
* @returns
*/
function getParamsFromUrlPath() {
const guiOptions = {
...DEFAULT_GUI_OPTIONS,
};
const searchParams = new URLSearchParams(window.location.search);
SEARCH_PARAMS_KEYS.forEach((key) => {
const value = searchParams.get(key);
if (!value) return;
if (['animate', 'snapshot', 'enableWebGPU'].includes(key)) guiOptions[key] = value === 'true';
else guiOptions[key] = value;
});
return guiOptions;
}
/**
* sync guiOptions(SEARCH_PARAMS_KEYS) to URL path
*/
function syncParamsToURLPath(guiOptions: GUIOptions) {
const searchParams = new URLSearchParams(window.location.search);
Object.entries(guiOptions).forEach(([key, value]) => {
if (SEARCH_PARAMS_KEYS.includes(key as (typeof SEARCH_PARAMS_KEYS)[number])) {
searchParams.set(key, value.toString());
}
});
window.history.replaceState(null, '', `?${searchParams.toString()}`);
}
function getDemoFromName(namespace: string, name: string) {
const demo = TestCases.get(namespace)?.find(([_name]) => _name === name)?.[1];
return demo;
}