Skip to content

Commit 1587948

Browse files
author
mige
committed
Merge remote-tracking branch 'origin/master'
2 parents ff74db3 + 7717534 commit 1587948

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

src/Globe/Globe.stories.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ import { Globe } from './Globe';
55
const meta = {
66
component: Globe,
77
title: 'Components/Scenes/Globe',
8+
argTypes: {
9+
atmosphereEnabled: {
10+
description: 'Enable or disable atmosphere rendering',
11+
control: {
12+
type: 'boolean'
13+
},
14+
table: {
15+
defaultValue: { summary: 'true' }
16+
}
17+
},
18+
sunActive: {
19+
description: 'Enable or disable sun lighting',
20+
control: {
21+
type: 'boolean'
22+
},
23+
table: {
24+
defaultValue: { summary: 'true' }
25+
}
26+
},
27+
viewExtent: {
28+
description: 'Set initial view extent as [west, south, east, north] in degrees',
29+
control: {
30+
type: 'object'
31+
},
32+
table: {
33+
type: { summary: 'NumberArray4 | Extent' },
34+
defaultValue: { summary: 'undefined' }
35+
}
36+
}
37+
}
838
} satisfies Meta<typeof Globe>;
939

1040
export default meta;
@@ -16,8 +46,16 @@ export const Default: Story = {
1646
atmosphereEnabled: false
1747
}
1848
};
49+
1950
export const AtmosphereEnabled: Story = {
2051
args: {
2152
atmosphereEnabled: true
2253
}
54+
};
55+
56+
export const ViewExtentEurope: Story = {
57+
args: {
58+
atmosphereEnabled: true,
59+
viewExtent: [-10, 35, 40, 70] // Europe
60+
}
2361
};

src/Globe/Globe.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {Globe as GlobusGlobe, GlobusRgbTerrain, OpenStreetMap, Bing} from "@open
77
import {EventCallback} from "@openglobus/og/lib/Events";
88
import {IGlobeParams} from "@openglobus/og/lib/Globe";
99
import {Layer, Vector} from "@/layer";
10+
import {Extent, LonLat} from "@openglobus/og";
11+
import type {NumberArray4} from "@openglobus/og/lib/math/Vec4";
1012

1113
type LayerChildren = React.ReactElement<{ type: typeof Layer | typeof Vector }>;
1214

@@ -15,16 +17,33 @@ export interface GlobusProps extends IGlobeParams {
1517
atmosphereEnabled?: boolean,
1618
sunActive?: boolean,
1719
onDraw?: EventCallback
20+
viewExtent?: Extent | NumberArray4;
1821
}
1922

2023
const Globe: React.FC<GlobusProps> = ({children, onDraw, ...rest}) => {
2124
const targetRef = useRef<HTMLDivElement | null>(null);
2225
const {setGlobe} = useGlobeContext();
2326
const [options, setOptions] = useState<IGlobeParams>(rest);
2427
const gRef = useRef<GlobusGlobe | null>(null);
28+
29+
useEffect(() => {
30+
if (gRef && gRef.current && rest.viewExtent !== undefined) {
31+
const extent = rest.viewExtent instanceof Extent
32+
? rest.viewExtent
33+
: new Extent(
34+
new LonLat(rest.viewExtent[0], rest.viewExtent[1]),
35+
new LonLat(rest.viewExtent[2], rest.viewExtent[3])
36+
);
37+
gRef.current.planet.viewExtent(extent);
38+
}
39+
}, [rest.viewExtent]);
40+
2541
useEffect(() => {
26-
if (gRef && gRef.current && rest.atmosphereEnabled !== undefined) gRef.current.planet.atmosphereEnabled = rest.atmosphereEnabled;
42+
if (gRef && gRef.current && rest.atmosphereEnabled !== undefined) {
43+
gRef.current.planet.atmosphereEnabled = rest.atmosphereEnabled;
44+
}
2745
}, [rest.atmosphereEnabled]);
46+
2847
useEffect(() => {
2948
if (gRef && gRef.current && rest.sunActive !== undefined) {
3049
if (rest.sunActive) {
@@ -34,29 +53,17 @@ const Globe: React.FC<GlobusProps> = ({children, onDraw, ...rest}) => {
3453
}
3554
}
3655
}, [rest.sunActive]);
56+
3757
useEffect(() => {
3858
if (!gRef.current) {
3959
const osm = new OpenStreetMap("OSM");
40-
41-
function toQuadKey(x: number, y: number, z: number): string {
42-
var index = '';
43-
for (var i = z; i > 0; i--) {
44-
var b = 0;
45-
var mask = 1 << (i - 1);
46-
if ((x & mask) !== 0) b++;
47-
if ((y & mask) !== 0) b += 2;
48-
index += b.toString();
49-
}
50-
return index;
51-
}
52-
5360
const sat = new Bing("Microsoft Bing");
5461

5562
gRef.current = new GlobusGlobe({
5663
target: targetRef.current!,
5764
name: 'Earth',
5865
terrain: new GlobusRgbTerrain(),
59-
layers: [osm, sat],
66+
layers: [osm, sat],
6067
autoActivate: true,
6168
atmosphereEnabled: true,
6269
...options
@@ -70,10 +77,13 @@ const Globe: React.FC<GlobusProps> = ({children, onDraw, ...rest}) => {
7077

7178
setGlobe(gRef.current);
7279
return () => {
73-
if (onDraw)
74-
gRef.current?.planet.events.off('draw', onDraw)
75-
gRef.current?.destroy();
76-
gRef.current = null;
80+
if (gRef.current) {
81+
if (onDraw) {
82+
gRef.current.planet.events.off('draw', onDraw)
83+
}
84+
gRef.current.destroy();
85+
gRef.current = null;
86+
}
7787
};
7888
}, [options]);
7989

0 commit comments

Comments
 (0)