Skip to content

Components #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {Preview} from "@storybook/react";

const preview: Preview = {
tags: ['autodocs'],
parameters: {
controls: {
matchers: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openglobus/openglobus-react",
"version": "0.1.0",
"version": "0.1.1",
"description": "Openglobus React Components",
"main": "./dist/index.umd.cjs",
"private": false,
Expand Down Expand Up @@ -28,7 +28,7 @@
"author": "Pavel Sukhodolski",
"license": "MIT",
"dependencies": {
"@openglobus/og": "^0.20.2",
"@openglobus/og": "^0.20.3",
"react": "^18.2.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/Globe/Globe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {IGlobeParams} from "@openglobus/og/lib/js/Globe";
import "@openglobus/og/css/og.css";
import {Layer, Vector} from "@/layer";

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

export interface GlobusProps extends IGlobeParams {
children?: LayerChildren | LayerChildren[]
atmosphereEnabled?: boolean,
Expand Down Expand Up @@ -68,7 +69,8 @@ const Globe: React.FC<GlobusProps> = ({children, onDraw, ...rest}) => {
nightTextureCoefficient: 2.7,
urlRewrite: function (s: any, u: string) {
// @ts-ignore
return utils.stringTemplate(u, {s: this._getSubdomain(), quad: toQuadKey(s.tileX, s.tileY, s.tileZoom)});},
return utils.stringTemplate(u, {s: this._getSubdomain(), quad: toQuadKey(s.tileX, s.tileY, s.tileZoom)});
},
});

gRef.current = new GlobusGlobe({
Expand Down
38 changes: 38 additions & 0 deletions src/camera/PlanetCamera/PlanetCamera.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import type {Meta, StoryObj} from '@storybook/react';

import {Globe, GlobeContextProvider} from "@/Globe";
import {PlanetCamera, PlanetCameraParams} from "@/camera/PlanetCamera/PlanetCamera";

/**
* This story about Billboard component. Billboard is a component that represents a 2d object which always faced to camera.
*/
const meta = {
component: PlanetCamera,
title: 'Components/Camera/PlanetCamera',
} satisfies Meta<typeof PlanetCamera>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: {
args: PlanetCameraParams
render: (args: PlanetCameraParams) => React.JSX.Element
} = {

args: {
lon: 0,
lat: 0,
alt: 100000,
lookLon: 10,
lookLat: 10,
lookAlt: 100000,
viewAngle:47
},
render: (args: PlanetCameraParams) => <GlobeContextProvider>
<Globe>
<PlanetCamera {...args} />
</Globe>
</GlobeContextProvider>
};
46 changes: 46 additions & 0 deletions src/camera/PlanetCamera/PlanetCamera.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from "react";
import {useEffect} from "react";
import {LonLat} from "@openglobus/og";
import {IPlanetCameraParams} from "@openglobus/og/lib/js/camera/PlanetCamera";
import {useGlobeContext} from "@/Globe";

export interface PlanetCameraParams extends IPlanetCameraParams {
lon?: number;
lat?: number;
alt?: number;
yaw?: number;
viewAngle?: number;
lookLon?: number;
lookLat?: number;
lookAlt?: number;
}

const PlanetCamera: React.FC<PlanetCameraParams> = ({
lon,
lat,
alt,
lookLon,
lookLat,
lookAlt,
viewAngle,
...params
}) => {
const {globe} = useGlobeContext();


useEffect(() => {
if (globe && typeof lon === "number" && typeof lat === "number" && typeof alt === "number") {
globe.planet.flyLonLat(new LonLat(lon, lat, alt), new LonLat(lookLon, lookLat, lookAlt))
}
}, [lon, lat, alt, lookLon, lookLat, lookAlt, globe]);

useEffect(() => {
if (globe && typeof viewAngle === "number") {
globe.planet.camera.setViewAngle(viewAngle)
}
}, [viewAngle, globe]);

return null;
};

export {PlanetCamera};
2 changes: 1 addition & 1 deletion src/entity/Entity/Entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const Entity: React.FC<EntityParams> = ({visibility, lon, lat, alt, lonlat, name
useEffect(() => {
if (globe) {
entityRef.current = new GlobusEntity({
lonlat, name, ...rest
lonlat: lonlat? lonlat : new LonLat(lon,lat,alt), name, ...rest
});
addEntity(entityRef.current);

Expand Down
76 changes: 76 additions & 0 deletions src/layer/Layer/Layer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type {Meta, StoryObj} from '@storybook/react';

import {Billboard, Entity} from "@/entity";
import {Globe, GlobeContextProvider} from "@/Globe";
import {action} from '@storybook/addon-actions';
import React from "react";
import {Layer, Vector, VectorProps} from "@/layer";

const meta = {
title: 'Components/Layer/Layer',
component: Layer,
} satisfies Meta<typeof Layer>;

export default meta;

type Story = StoryObj<typeof meta>;


export const Default: Story = {
parameters: {
actions: {depth: 1, maxDepth: 1},
},
argTypes: {
onVisibilityChange: {table: {disable: true}},
onAdd: {table: {disable: true}},
onRemove: {table: {disable: true}},
onMouseMove: {table: {disable: true}},
onMouseEnter: {table: {disable: true}},
onMouseLeave: {table: {disable: true}},
onLclick: {table: {disable: true}},
onRclick: {table: {disable: true}},
onMclick: {table: {disable: true}},
onLdblclick: {table: {disable: true}},
onRdblclick: {table: {disable: true}},
onMdblclick: {table: {disable: true}},
onLup: {table: {disable: true}},
onRup: {table: {disable: true}},
onMup: {table: {disable: true}},
onLdown: {table: {disable: true}},
onRdown: {table: {disable: true}},
onMdown: {table: {disable: true}},
onLhold: {table: {disable: true}},
onRhold: {table: {disable: true}},
onMhold: {table: {disable: true}},
onMouseWheel: {table: {disable: true}},
onTouchMove: {table: {disable: true}},
onTouchStart: {table: {disable: true}},
onTouchEnd: {table: {disable: true}},
onDoubleTouch: {table: {disable: true}},
onTouchLeave: {table: {disable: true}},
onTouchEnter: {table: {disable: true}},
},
args: {
name: 'test',
onMouseEnter: action('onMouseEnter', {depth: 1, maxDepth: 1}),
onLclick: action('onLclick', {depth: 1, maxDepth: 1}),
onMouseLeave: action('onMouseLeave', {depth: 1, maxDepth: 1}),
onMouseMove: action('onMouseMove', {depth: 1, maxDepth: 1}),
onLdblclick: action('onLdblclick', {depth: 1, maxDepth: 1}),
// onDraw: action('onDraw', {depth: 1, maxDepth: 1}),
},
render: (args: VectorProps) => <GlobeContextProvider>
<Globe>
<Vector {...args}>
<Entity name="Custom Entity" lon={0} lat={0} alt={0}>
<Billboard
size={[96, 96]}
color={"#ff5959"}
src={'https://openglobus.org/examples/examples/billboards/carrot.png'}
/>
</Entity>
</Vector>
</Globe>
</GlobeContextProvider>
};

98 changes: 84 additions & 14 deletions src/layer/Layer/Layer.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,93 @@
import * as React from 'react';
import {useEffect} from 'react';
import {useGlobeContext} from '@/Globe';
import {ILayerParams, Layer as GlobusLayer} from '@openglobus/og';
import {EventCallback} from '@openglobus/og/lib/js/Events';

const Layer = ({props, name}: { props: ILayerParams, name: string }) => {
const {globe} = useGlobeContext();
export interface LayerProps extends ILayerParams {
name: string
layerRef?: React.MutableRefObject<GlobusLayer | null>,
children?: React.ReactNode;
onVisibilityChange?: EventCallback;
onAdd?: EventCallback;
onRemove?: EventCallback;
onMouseMove?: EventCallback;
onMouseEnter?: EventCallback;
onMouseLeave?: EventCallback;
onLclick?: EventCallback;
onRclick?: EventCallback;
onMclick?: EventCallback;
onLdblclick?: EventCallback;
onRdblclick?: EventCallback;
onMdblclick?: EventCallback;
onLup?: EventCallback;
onRup?: EventCallback;
onMup?: EventCallback;
onLdown?: EventCallback;
onRdown?: EventCallback;
onMdown?: EventCallback;
onLhold?: EventCallback;
onRhold?: EventCallback;
onMhold?: EventCallback;
onMouseWheel?: EventCallback;
onTouchMove?: EventCallback;
onTouchStart?: EventCallback;
onTouchEnd?: EventCallback;
onDoubleTouch?: EventCallback;
onTouchLeave?: EventCallback;
onTouchEnter?: EventCallback;
}

const Layer: React.FC<LayerProps> = ({opacity, children, name, layerRef, ...params}) => {
const [refPassed, setRefPassed] = React.useState(false);
useEffect(() => {
if (globe) {
const newLayer = new GlobusLayer(name, props);
globe.planet.addLayer(newLayer);

return () => {
globe.planet.removeLayer(newLayer);
};
if (layerRef) {
if (layerRef.current && typeof opacity === 'number' && refPassed) {
layerRef.current.opacity = opacity;
}
}
}, [globe]);

return null;
}, [opacity]);
useEffect(() => {
if (refPassed && layerRef && layerRef.current) {
if (params.onVisibilityChange) layerRef.current?.events.on("visibilitychange", params.onVisibilityChange)
if (params.onAdd) layerRef.current?.events.on("add", params.onAdd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need ? here Already checked layerRef.current above

if (params.onRemove) layerRef.current?.events.on("remove", params.onRemove)
if (params.onMouseMove) layerRef.current?.events.on("mousemove", params.onMouseMove)
if (params.onMouseEnter) layerRef.current?.events.on("mouseenter", params.onMouseEnter)
if (params.onMouseLeave) layerRef.current?.events.on("mouseleave", params.onMouseLeave)
if (params.onLclick) layerRef.current?.events.on("lclick", params.onLclick)
if (params.onRclick) layerRef.current?.events.on("rclick", params.onRclick)
if (params.onMclick) layerRef.current?.events.on("mclick", params.onMclick)
if (params.onLdblclick) layerRef.current?.events.on("ldblclick", params.onLdblclick)
if (params.onRdblclick) layerRef.current?.events.on("rdblclick", params.onRdblclick)
if (params.onMdblclick) layerRef.current?.events.on("mdblclick", params.onMdblclick)
if (params.onLup) layerRef.current?.events.on("lup", params.onLup)
if (params.onRup) layerRef.current?.events.on("rup", params.onRup)
if (params.onMup) layerRef.current?.events.on("mup", params.onMup)
if (params.onLdown) layerRef.current?.events.on("ldown", params.onLdown)
if (params.onRdown) layerRef.current?.events.on("rdown", params.onRdown)
if (params.onMdown) layerRef.current?.events.on("mdown", params.onMdown)
if (params.onLhold) layerRef.current?.events.on("lhold", params.onLhold)
if (params.onRhold) layerRef.current?.events.on("rhold", params.onRhold)
if (params.onMhold) layerRef.current?.events.on("mhold", params.onMhold)
if (params.onMouseWheel) layerRef.current?.events.on("mousewheel", params.onMouseWheel)
if (params.onTouchMove) layerRef.current?.events.on("touchmove", params.onTouchMove)
if (params.onTouchStart) layerRef.current?.events.on("touchstart", params.onTouchStart)
if (params.onTouchEnd) layerRef.current?.events.on("touchend", params.onTouchEnd)
if (params.onDoubleTouch) layerRef.current?.events.on("doubletouch", params.onDoubleTouch)
}
return () => {
if (params.onLclick) layerRef?.current?.events.off('lclick', params.onLclick)
setRefPassed(false)
}
}, [refPassed]);
if (layerRef?.current && !refPassed) {
setRefPassed(true)
}
return (
<>
{children}
</>
);
};

export {Layer};

7 changes: 5 additions & 2 deletions src/layer/Vector/Vector.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {Meta, StoryObj} from '@storybook/react';

import {Vector, VectorProps} from './Vector';
import {Default as LayerDefault} from '../Layer/Layer.stories';
import {Billboard, Entity} from "@/entity";
import {Globe, GlobeContextProvider} from "@/Globe";
import {action} from '@storybook/addon-actions';
Expand All @@ -20,10 +21,12 @@ export const Default: Story = {
parameters: {
actions: {depth: 1, maxDepth: 1},
},
argTypes:{
...LayerDefault.argTypes
},
args: {
name: 'test',
onMouseEnter: action('onMouseEnter', {depth: 1, maxDepth: 1}),
// onDraw: action('onDraw', {depth: 1, maxDepth: 1}),
opacity: 1
},
render: (args: VectorProps) => <GlobeContextProvider>
<Globe>
Expand Down
10 changes: 7 additions & 3 deletions src/layer/Vector/Vector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import {createContext, useCallback, useEffect, useRef, useState} from "react";
import {useGlobeContext} from '../../index';
import {Layer, LayerProps, useGlobeContext} from '../../index';
import type {Billboard, Entity, Geometry, GeoObject, Label, Polyline, Strip} from '@openglobus/og';
import {Vector as GlobusVector} from '@openglobus/og';
import {IVectorParams} from '@openglobus/og/lib/js/layer/Vector';
Expand Down Expand Up @@ -54,13 +54,15 @@ const VectorContext = createContext<{

type EntityElement = React.ReactElement<{ type: typeof Entity }>;

export interface VectorProps extends IVectorParams {
export interface VectorPropsBase extends IVectorParams {
children?: EntityElement | EntityElement[]
name: string
onMouseEnter?: EventCallback
onDraw?: EventCallback
}

export type VectorProps = VectorPropsBase & LayerProps;

const Vector: React.FC<VectorProps> = ({visibility, children, name, ...rest}) => {
const {globe} = useGlobeContext();
const vectorRef = useRef<GlobusVector | null>(null);
Expand Down Expand Up @@ -179,7 +181,9 @@ const Vector: React.FC<VectorProps> = ({visibility, children, name, ...rest}) =>
addStrip,
removeStrip
}}>
{children}
<Layer layerRef={vectorRef} name={name} {...rest}>
{children}
</Layer>
</VectorContext.Provider>
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/layer/XYZ/XYZ.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const OpenStreetMap: Story = {

args: {
name: 'osm',
opacity: 1,
url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
},
render: (args: XYZProps) => <GlobeContextProvider>
Expand All @@ -43,6 +44,7 @@ export const OpenStreetMap: Story = {
export const Satellites: Story = {
args: {
name: 'sat',
opacity: 1,
url: 'https://ecn.{s}.tiles.virtualearth.net/tiles/a{quad}.jpeg?n=z&g=7146',
subdomains: ['t0', 't1', 't2', 't3'],
urlRewrite: function (s: any, u: string) {
Expand Down
Loading