Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
feat: zoom to layer (#301)
Browse files Browse the repository at this point in the history
Co-authored-by: nina992 <nouralali992@gmail.com>
  • Loading branch information
rot1024 and nina992 committed Oct 14, 2022
1 parent 7c304f0 commit 1f5296d
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 24 deletions.
8 changes: 8 additions & 0 deletions src/components/atoms/Icon/Icons/zoomToLayer.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/components/atoms/Icon/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import Install from "./Icons/install.svg";
import CopyCode from "./Icons/copyCode.svg";
import FieldClear from "./Icons/fieldClear.svg";
import FieldLinked from "./Icons/fieldLinked.svg";
import ZoomToLayer from "./Icons/zoomToLayer.svg";

// Misc
import Logo from "./Icons/reearthLogo.svg";
Expand Down Expand Up @@ -276,4 +277,5 @@ export default {
compass: Compass,
compassFocus: CompassFocus,
house: House,
zoomToLayer: ZoomToLayer,
};
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type Props = {
onAdd?: (id?: string) => void;
onGroupCreate?: () => void;
onImport?: (file: File, format: Format) => void;
onZoomToLayer?: (selectedLayerId: string) => void;
};

const Layer: React.ForwardRefRenderFunction<HTMLDivElement, Props> = (
Expand Down Expand Up @@ -101,6 +102,7 @@ const Layer: React.ForwardRefRenderFunction<HTMLDivElement, Props> = (
onAdd,
onGroupCreate,
onImport,
onZoomToLayer,
},
ref,
) => {
Expand Down Expand Up @@ -245,6 +247,7 @@ const Layer: React.ForwardRefRenderFunction<HTMLDivElement, Props> = (
onLayerImport={onImport}
onLayerRemove={onRemove}
onLayerGroupCreate={onGroupCreate}
onZoomToLayer={onZoomToLayer}
/>
)}
</LayerActionsWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Props = {
onLayerRemove?: (id: string) => void;
onLayerGroupCreate?: () => void;
onLayerImport?: (file: File, format: Format) => void;
onZoomToLayer?: (layerId: string) => void;
};

const LayerActions: React.FC<Props> = ({
Expand All @@ -22,6 +23,7 @@ const LayerActions: React.FC<Props> = ({
onLayerRemove,
onLayerImport,
onLayerGroupCreate,
onZoomToLayer,
}) => {
const t = useT();
const importLayer = useFileInput(
Expand Down Expand Up @@ -56,20 +58,31 @@ const LayerActions: React.FC<Props> = ({
onLayerRemove?.(selectedLayerId);
}
}}>
<HelpButton descriptionTitle={t("Delete selected layer.")} balloonDirection="bottom">
<HelpButton descriptionTitle={t("Delete Selected Layer")} balloonDirection="bottom">
<StyledIcon icon="bin" size={16} disabled={!selectedLayerId} />
</HelpButton>
</Action>
<Action disabled={!rootLayerId} onClick={onLayerGroupCreate}>
<HelpButton descriptionTitle={t("Create new folder.")} balloonDirection="bottom">
<HelpButton descriptionTitle={t("Create New Folder")} balloonDirection="bottom">
<StyledIcon icon="folderAdd" size={16} />
</HelpButton>
</Action>
<Action disabled={!rootLayerId} onClick={importLayer}>
<HelpButton descriptionTitle={t("Add Layer.")} balloonDirection="bottom">
<HelpButton descriptionTitle={t("Add Layer")} balloonDirection="bottom">
<StyledIcon icon="layerAdd" size={16} />
</HelpButton>
</Action>
<Action
disabled={!selectedLayerId}
onClick={() => {
if (selectedLayerId) {
onZoomToLayer?.(selectedLayerId);
}
}}>
<HelpButton descriptionTitle={t("Zoom To Layer")} balloonDirection="bottom">
<StyledIcon icon="zoomToLayer" size={16} disabled={!selectedLayerId} />
</HelpButton>
</Action>
</ActionWrapper>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Props<T = unknown> = ItemProps<Layer<T>> & {
onAdd?: (id?: string) => void;
onGroupCreate?: () => void;
onImport?: (file: File, format: Format) => void;
onZoomToLayer?: (layerId: string) => void;
};

function LayerTreeViewItem<T = unknown>(
Expand Down Expand Up @@ -49,6 +50,7 @@ function LayerTreeViewItem<T = unknown>(
onRemove,
onGroupCreate,
onImport,
onZoomToLayer,
}: Props<T>,
ref: Ref<HTMLDivElement>,
) {
Expand Down Expand Up @@ -94,6 +96,7 @@ function LayerTreeViewItem<T = unknown>(
onGroupCreate={onGroupCreate}
onImport={onImport}
childSelected={childSelected}
onZoomToLayer={onZoomToLayer}
/>
{children && (
<Children expanded={expanded} dropType={canDrop ? dropType : undefined}>
Expand Down Expand Up @@ -137,6 +140,7 @@ export function useLayerTreeViewItem<T>({
onRemove,
onGroupCreate,
onImport,
onZoomToLayer,
}: {
rootLayerId?: string;
selectedLayerId?: string;
Expand All @@ -150,6 +154,7 @@ export function useLayerTreeViewItem<T>({
onRemove?: (selectedLayerId: string) => void;
onGroupCreate?: () => void;
onImport?: (file: File, format: Format) => void;
onZoomToLayer?: (layerId: string) => void;
} = {}) {
return useMemo(() => {
function InnerLayerTreeViewItem(props: Props<T>, ref: Ref<HTMLDivElement>) {
Expand Down Expand Up @@ -179,6 +184,7 @@ export function useLayerTreeViewItem<T>({
onAdd,
onGroupCreate,
onImport,
onZoomToLayer,
...events,
},
ref,
Expand All @@ -195,6 +201,7 @@ export function useLayerTreeViewItem<T>({
onAdd,
onGroupCreate,
onImport,
onZoomToLayer,
onVisibilityChange,
onActivationChange,
onRename,
Expand Down
3 changes: 3 additions & 0 deletions src/components/molecules/EarthEditor/OutlinePane/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default ({
onLayerGroupCreate,
onLayerImport,
handleShowWarning,
onZoomToLayer,
}: {
rootLayerId?: string;
layers?: Layer[];
Expand Down Expand Up @@ -125,6 +126,7 @@ export default ({
onDrop?: (layer: string, index: number, childrenCount: number) => any;
onLayerGroupCreate?: () => void;
handleShowWarning?: (show: boolean) => void;
onZoomToLayer?: (layerId: string) => void;
}) => {
const t = useT();
const [selected, setSelected] = useState<string[]>([]);
Expand Down Expand Up @@ -384,6 +386,7 @@ export default ({
visibilityShown: true,
selectedLayerId,
rootLayerId,
onZoomToLayer,
});

const clusterTreeViewItemOnRename = useCallback(
Expand Down
3 changes: 3 additions & 0 deletions src/components/molecules/EarthEditor/OutlinePane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type Props = {
onLayerGroupCreate?: () => void;
onLayerImport?: (file: File, format: Format) => void;
onDrop?: (layer: string, index: number, childrenCount: number) => any;
onZoomToLayer?: (layerId: string) => void;
};

const OutlinePane: React.FC<Props> = ({
Expand Down Expand Up @@ -84,6 +85,7 @@ const OutlinePane: React.FC<Props> = ({
onLayerImport,
onLayerGroupCreate,
onDrop,
onZoomToLayer,
loading,
}) => {
const t = useT();
Expand Down Expand Up @@ -131,6 +133,7 @@ const OutlinePane: React.FC<Props> = ({
onDrop,
onLayerGroupCreate,
handleShowWarning,
onZoomToLayer,
});

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Cesium from "cesium";
import { Math as CesiumMath } from "cesium";
import { useImperativeHandle, Ref, RefObject, useMemo, useRef } from "react";
import type { CesiumComponentRef } from "resium";
import { CesiumComponentRef } from "resium";

import type { Ref as EngineRef } from "..";
import type { MouseEvents, MouseEvent } from "../ref";
Expand Down Expand Up @@ -87,6 +87,19 @@ export default function useEngineRef(
options,
);
},
lookAtLayer: layerId => {
const viewer = cesium.current?.cesiumElement;
if (!viewer || viewer.isDestroyed()) return;
const e = viewer.entities.getById(layerId);
if (!e) return;
const camera = getCamera(viewer);
const offset = new Cesium.HeadingPitchRange(
camera?.heading ?? 0,
camera?.pitch ?? -90,
50000,
);
viewer.zoomTo(e, offset);
},
getViewport: () => {
const viewer = cesium.current?.cesiumElement;
if (!viewer || viewer.isDestroyed()) return;
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/Visualizer/Engine/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type EngineRef = {
getLocationFromScreenXY: (x: number, y: number) => LatLngHeight | undefined;
flyTo: (destination: FlyToDestination, options?: CameraOptions) => void;
lookAt: (destination: LookAtDestination, options?: CameraOptions) => void;
lookAtLayer: (layerId: string) => void;
zoomIn: (amount: number, options?: CameraOptions) => void;
zoomOut: (amount: number, options?: CameraOptions) => void;
orbit: (radian: number) => void;
Expand Down
8 changes: 8 additions & 0 deletions src/components/molecules/Visualizer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default ({
rootLayer,
selectedLayerId: outerSelectedLayerId,
selectedBlockId: outerSelectedBlockId,
zoomedLayerId,
camera,
clock,
sceneProperty,
Expand All @@ -53,6 +54,7 @@ export default ({
rootLayer?: Layer;
selectedLayerId?: string;
selectedBlockId?: string;
zoomedLayerId?: string;
camera?: Camera;
clock?: Clock;
sceneProperty?: SceneProperty;
Expand Down Expand Up @@ -220,6 +222,12 @@ export default ({
selectLayer(undefined);
}, [selectLayer]);

useEffect(() => {
if (zoomedLayerId) {
engineRef.current?.lookAtLayer(zoomedLayerId);
}
}, [zoomedLayerId]);

return {
engineRef,
wrapperRef,
Expand Down
3 changes: 3 additions & 0 deletions src/components/molecules/Visualizer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type Props = {
pluginProperty?: { [key: string]: any };
clusterProperty?: ClusterProperty[];
selectedLayerId?: string;
zoomedLayerId?: string;
selectedBlockId?: string;
pluginBaseUrl?: string;
isPublished?: boolean;
Expand Down Expand Up @@ -87,6 +88,7 @@ export default function Visualizer({
isPublished,
selectedLayerId: outerSelectedLayerId,
selectedBlockId: outerSelectedBlockId,
zoomedLayerId,
widgetAlignEditorActivated,
onLayerSelect,
onWidgetUpdate,
Expand Down Expand Up @@ -134,6 +136,7 @@ export default function Visualizer({
rootLayer,
selectedLayerId: outerSelectedLayerId,
selectedBlockId: outerSelectedBlockId,
zoomedLayerId,
camera: props.camera,
clock: props.clock,
sceneProperty,
Expand Down
3 changes: 3 additions & 0 deletions src/components/organisms/EarthEditor/CanvasArea/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
useSelected,
useSelectedBlock,
useWidgetAlignEditorActivated,
useZoomedLayerId,
useClock,
} from "@reearth/state";
import { valueTypeToGQL, ValueTypes, valueToGQL, LatLng } from "@reearth/util/value";
Expand All @@ -52,6 +53,7 @@ export default (isBuilt?: boolean) => {
const [selected, select] = useSelected();
const [selectedBlock, selectBlock] = useSelectedBlock();
const [widgetAlignEditorActivated] = useWidgetAlignEditorActivated();
const [zoomedLayerId] = useZoomedLayerId();

const [moveInfoboxField] = useMoveInfoboxFieldMutation();
const [removeInfoboxField] = useRemoveInfoboxFieldMutation();
Expand Down Expand Up @@ -277,6 +279,7 @@ export default (isBuilt?: boolean) => {
sceneId,
rootLayerId,
selectedLayerId,
zoomedLayerId,
selectedBlockId: selectedBlock,
sceneProperty,
pluginProperty,
Expand Down
2 changes: 2 additions & 0 deletions src/components/organisms/EarthEditor/CanvasArea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const CanvasArea: React.FC<Props> = ({ className, isBuilt }) => {
const {
rootLayerId,
selectedBlockId,
zoomedLayerId,
sceneProperty,
pluginProperty,
clusterProperty,
Expand Down Expand Up @@ -63,6 +64,7 @@ const CanvasArea: React.FC<Props> = ({ className, isBuilt }) => {
widgets={widgets}
selectedLayerId={selectedLayerId}
selectedBlockId={selectedBlockId}
zoomedLayerId={zoomedLayerId}
rootLayerId={rootLayerId}
sceneProperty={sceneProperty}
tags={tags}
Expand Down
3 changes: 3 additions & 0 deletions src/components/organisms/EarthEditor/OutlinePane/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
useSelectedBlock,
useRootLayerId,
useWidgetAlignEditorActivated,
useZoomedLayerId,
} from "@reearth/state";
import deepFind from "@reearth/util/deepFind";
import deepGet from "@reearth/util/deepGet";
Expand All @@ -53,6 +54,7 @@ export default () => {
const locale = useLang();
const [sceneId] = useSceneId();
const [selected, select] = useSelected();
const [, zoomToLayer] = useZoomedLayerId();
const [, selectBlock] = useSelectedBlock();
const [rootLayerId] = useRootLayerId();
const [, toggleWidgetAlignEditor] = useWidgetAlignEditorActivated();
Expand Down Expand Up @@ -455,6 +457,7 @@ export default () => {
selectCluster,
addCluster,
removeCluster,
zoomToLayer,
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/components/organisms/EarthEditor/OutlinePane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const OutlinePane: React.FC<Props> = ({ className }) => {
addCluster,
renameCluster,
removeCluster,
zoomToLayer,
} = useHooks();

return (
Expand Down Expand Up @@ -73,6 +74,7 @@ const OutlinePane: React.FC<Props> = ({ className }) => {
onClusterRemove={removeCluster}
onWidgetActivation={activateWidget}
onDrop={handleDrop}
onZoomToLayer={zoomToLayer}
/>
);
};
Expand Down
6 changes: 4 additions & 2 deletions src/components/pages/EarthEditor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { useEffect } from "react";

import { useAuth } from "@reearth/auth";
import { useGetSceneQuery } from "@reearth/gql";
import { useSceneId, useRootLayerId } from "@reearth/state";
import { useSceneId, useRootLayerId, useZoomedLayerId } from "@reearth/state";

export type Mode = "layer" | "widget";

export default (sceneId?: string) => {
const isAuthenticated = useAuth();
const [, setRootLayerId] = useRootLayerId();
const [, setSceneId] = useSceneId();
const [, zoomToLayer] = useZoomedLayerId();

const { loading, data } = useGetSceneQuery({
variables: { sceneId: sceneId || "" },
Expand All @@ -18,7 +19,8 @@ export default (sceneId?: string) => {

useEffect(() => {
setSceneId(sceneId);
}, [sceneId, setSceneId]);
zoomToLayer(undefined);
}, [sceneId, setSceneId, zoomToLayer]);

useEffect(() => {
setRootLayerId(
Expand Down

0 comments on commit 1f5296d

Please sign in to comment.