This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change layer indicators from preset list (#245)
* indicator * impl indicator * impl indicator * change indicators * show image * reslove comments * Fix syntax * Update icons.ts * Update src/components/molecules/Visualizer/Engine/Cesium/core/Indicator.tsx Co-authored-by: KaWaite <34051327+KaWaite@users.noreply.github.com> * Update src/components/molecules/Visualizer/Engine/Cesium/hooks.ts Co-authored-by: KaWaite <34051327+KaWaite@users.noreply.github.com> * Update src/components/molecules/Visualizer/Engine/Cesium/core/Indicator.tsx Co-authored-by: KaWaite <34051327+KaWaite@users.noreply.github.com> * Update src/components/molecules/Visualizer/Engine/Cesium/core/Indicator.tsx Co-authored-by: KaWaite <34051327+KaWaite@users.noreply.github.com> * pr-commets * pr-fixes3 * remove commented code * zooming - dragging * pr1 Co-authored-by: rot1024 <aayhrot@gmail.com> Co-authored-by: nina992 <nouralali992@gmail.com> Co-authored-by: KaWaite <34051327+KaWaite@users.noreply.github.com>
- Loading branch information
1 parent
845e2a3
commit db185ef
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/components/molecules/Visualizer/Engine/Cesium/core/Indicator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import useTransition, { TransitionStatus } from "@rot1024/use-transition"; | ||
import { BoundingSphere, Cartesian3, SceneTransforms, Cartesian2 } from "cesium"; | ||
import { useEffect, useState } from "react"; | ||
import { useCesium } from "resium"; | ||
|
||
import Icon from "@reearth/components/atoms/Icon"; | ||
import { styled } from "@reearth/theme"; | ||
|
||
import { SceneProperty } from "../../ref"; | ||
import { useIcon } from "../common"; | ||
|
||
export type Props = { | ||
className?: string; | ||
property?: SceneProperty; | ||
}; | ||
|
||
export default function Indicator({ className, property }: Props): JSX.Element | null { | ||
const { viewer } = useCesium(); | ||
const [isVisible, setIsVisible] = useState(true); | ||
const [pos, setPos] = useState<Cartesian2>(); | ||
|
||
const transiton = useTransition(!!pos && isVisible, 500, { | ||
mountOnEnter: true, | ||
unmountOnExit: true, | ||
}); | ||
const { indicator_type, indicator_image, indicator_image_scale } = property?.indicator ?? {}; | ||
const [img, w, h] = useIcon({ image: indicator_image, imageSize: indicator_image_scale }); | ||
|
||
useEffect(() => { | ||
if (!viewer) return; | ||
const handleTick = () => { | ||
if (viewer.isDestroyed()) return; | ||
const selected = viewer.selectedEntity; | ||
if ( | ||
!selected || | ||
!selected.isShowing || | ||
!selected.isAvailable(viewer.clock.currentTime) || | ||
!selected.position | ||
) { | ||
setIsVisible(false); | ||
return; | ||
} | ||
|
||
// https://github.com/CesiumGS/cesium/blob/1.94/Source/Widgets/Viewer/Viewer.js#L1839 | ||
let position: Cartesian3 | undefined = undefined; | ||
const boundingSphere = new BoundingSphere(); | ||
const state = (viewer.dataSourceDisplay as any).getBoundingSphere( | ||
selected, | ||
true, | ||
boundingSphere, | ||
); | ||
// https://github.com/CesiumGS/cesium/blob/main/Source/DataSources/BoundingSphereState.js#L24 | ||
if (state !== 2 /* BoundingSphereState.FAILED */) { | ||
position = boundingSphere.center; | ||
} else if (selected.position) { | ||
position = selected.position.getValue(viewer.clock.currentTime, position); | ||
} | ||
|
||
if (position) { | ||
const pos = SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, position); | ||
setPos(pos); | ||
setIsVisible(true); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
}; | ||
|
||
viewer.clock.onTick.addEventListener(handleTick); | ||
return () => { | ||
if (viewer.isDestroyed()) return; | ||
viewer.clock.onTick.removeEventListener(handleTick); | ||
}; | ||
}, [viewer]); | ||
|
||
return transiton !== "unmounted" && pos ? ( | ||
indicator_type === "crosshair" ? ( | ||
<StyledIcon | ||
icon="crosshair" | ||
size="48px" | ||
className={className} | ||
transition={transiton} | ||
style={{ left: pos.x + "px", top: pos.y + "px" }} | ||
/> | ||
) : indicator_type === "custom" ? ( | ||
<Image | ||
src={img} | ||
width={w} | ||
height={h} | ||
transition={transiton} | ||
style={{ left: pos.x + "px", top: pos.y + "px" }} | ||
/> | ||
) : ( | ||
<StyledIndicator transition={transiton} style={{ left: pos.x + "px", top: pos.y + "px" }} /> | ||
) | ||
) : null; | ||
} | ||
|
||
const StyledIndicator = styled.div<{ transition: TransitionStatus }>` | ||
position: absolute; | ||
transform: translate(-50%, -50%); | ||
transition: ${({ transition }) => | ||
transition === "entering" || transition === "exiting" ? "all 0.5s ease" : ""}; | ||
opacity: ${({ transition }) => (transition === "entering" || transition === "entered" ? 1 : 0)}; | ||
pointer-events: none; | ||
`; | ||
|
||
const StyledIcon = styled(Icon)<{ transition: TransitionStatus }>` | ||
position: absolute; | ||
transform: translate(-50%, -50%); | ||
transition: ${({ transition }) => | ||
transition === "entering" || transition === "exiting" ? "all 0.5s ease" : ""}; | ||
opacity: ${({ transition }) => (transition === "entering" || transition === "entered" ? 1 : 0)}; | ||
pointer-events: none; | ||
`; | ||
|
||
const Image = styled.img<{ transition: TransitionStatus }>` | ||
position: absolute; | ||
transform: translate(-50%, -50%); | ||
transition: ${({ transition }) => | ||
transition === "entering" || transition === "exiting" ? "all 0.5s ease" : ""}; | ||
opacity: ${({ transition }) => (transition === "entering" || transition === "entered" ? 1 : 0)}; | ||
pointer-events: none; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters