-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement Tooltip by attaching handlers to Tiles
- Loading branch information
Showing
11 changed files
with
398 additions
and
71 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,70 +1,35 @@ | ||
import { formatTooltipVal } from '@h5web/shared'; | ||
import { Suspense } from 'react'; | ||
import { Vector2 } from 'three'; | ||
|
||
import type { Size } from '../models'; | ||
import { useAxisSystemContext } from '../shared/AxisSystemProvider'; | ||
import TooltipMesh from '../shared/TooltipMesh'; | ||
import type { TilesApi } from './api'; | ||
import { useLayerScales } from './hooks'; | ||
import { scaleFromLayerToTile } from './utils'; | ||
import { useTooltipStore } from './store'; | ||
|
||
interface Props { | ||
layer: number; | ||
api: TilesApi; | ||
meshSize: Size; | ||
size: Size; | ||
} | ||
|
||
function TiledTooltipMesh(props: Props) { | ||
const { layer, api, meshSize } = props; | ||
|
||
const layerSize = api.layerSizes[layer]; | ||
|
||
const { dataToWorld } = useAxisSystemContext(); | ||
const { xScale, yScale } = useLayerScales(layerSize, meshSize); | ||
|
||
function dataToLayer(v: Vector2) { | ||
const worldVec = dataToWorld(v); | ||
|
||
return new Vector2( | ||
Math.floor(xScale(worldVec.x)), | ||
Math.floor(yScale(worldVec.y)) | ||
); | ||
} | ||
const { size } = props; | ||
const val = useTooltipStore((state) => state.val); | ||
|
||
return ( | ||
<TooltipMesh | ||
renderTooltip={(x, y) => { | ||
size={size} | ||
renderTooltip={() => { | ||
if (!val) { | ||
return undefined; | ||
} | ||
|
||
const { x, y, v } = val; | ||
return ( | ||
<div style={{ display: 'flex', flexDirection: 'column' }}> | ||
{`x=${formatTooltipVal(x)}, y=${formatTooltipVal(y)}`} | ||
<strong> | ||
<Suspense fallback={<>Loading...</>}> | ||
<TooltipValue | ||
api={api} | ||
layerVec={dataToLayer(new Vector2(x, y))} | ||
layer={layer} | ||
/> | ||
</Suspense> | ||
</strong> | ||
<strong>{formatTooltipVal(v)}</strong> | ||
</div> | ||
); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
function TooltipValue(props: { | ||
layerVec: Vector2; | ||
api: TilesApi; | ||
layer: number; | ||
}) { | ||
const { layerVec, api, layer } = props; | ||
|
||
const { offset, x, y } = scaleFromLayerToTile(layerVec, api.tileSize); | ||
const dataArray = api.get(layer, offset); | ||
|
||
return <>{formatTooltipVal(dataArray.get(y, x))}</>; | ||
} | ||
|
||
export default TiledTooltipMesh; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import create from 'zustand'; | ||
|
||
interface TooltipStore { | ||
val: { x: number; y: number; v: number } | undefined; | ||
setTooltipValue: (x: number, y: number, v: number) => void; | ||
} | ||
|
||
export const useTooltipStore = create<TooltipStore>((set) => ({ | ||
val: undefined, | ||
setTooltipValue: (x: number, y: number, v: number) => | ||
set(() => ({ | ||
val: { x, y, v }, | ||
})), | ||
})); |
Oops, something went wrong.