-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui-trackoccupancydiagram: tracks layer
- create drawTrack - create drawTracks - create drawTick - add track background - use Space time context Co-authored-by: Yohh <durandyohan@zaclys.net> Signed-off-by: Uriel Sautron <uriel.sautron@gmail.com>
- Loading branch information
1 parent
2c1f5c6
commit b5586e5
Showing
12 changed files
with
495 additions
and
48 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
39 changes: 9 additions & 30 deletions
39
ui-trackoccupancydiagram/src/components/TrackOccupancyCanvas.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 |
---|---|---|
@@ -1,35 +1,14 @@ | ||
import React, { useState } from 'react'; | ||
import React from 'react'; | ||
|
||
import OccupancyZonesLayer from './layers/OccupancyZonesLayer'; | ||
import TracksLayer from './layers/TracksLayer'; | ||
import type { Store, TrackOccupancyCanvasProps } from './types'; | ||
|
||
const TrackOccupancyCanvas = ({ | ||
tracks, | ||
zones, | ||
selectedTrain, | ||
timeOrigin, | ||
timeScale, | ||
}: TrackOccupancyCanvasProps) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [store, setStore] = useState<Store>({ | ||
tracks, | ||
zones, | ||
selectedTrain, | ||
timeOrigin, | ||
timeScale, | ||
ratio: 0, | ||
offsetX: 0, | ||
clientX: 0, | ||
clientY: 0, | ||
}); | ||
|
||
return ( | ||
<div id="track-occupancy-canvas" className="bg-white-100"> | ||
<TracksLayer /> | ||
<OccupancyZonesLayer /> | ||
</div> | ||
); | ||
}; | ||
import { type TrackOccupancyCanvasProps } from './types'; | ||
|
||
const TrackOccupancyCanvas = ({ useDraw, setCanvasesRoot }: TrackOccupancyCanvasProps) => ( | ||
<div id="track-occupancy-canvas" className="bg-white-100" ref={setCanvasesRoot}> | ||
<TracksLayer useDraw={useDraw} /> | ||
<OccupancyZonesLayer /> | ||
</div> | ||
); | ||
|
||
export default TrackOccupancyCanvas; |
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 @@ | ||
export const TRACK_HEIGHT_CONTAINER = 73; |
111 changes: 111 additions & 0 deletions
111
ui-trackoccupancydiagram/src/components/helpers/drawElements/drawTrack.ts
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,111 @@ | ||
import { sum } from 'lodash'; | ||
|
||
import { timeScaleSample } from '../../../sample/timeScale'; | ||
import { TRACK_HEIGHT_CONTAINER } from '../../consts'; | ||
type DrawTrackProps = { | ||
ctx: CanvasRenderingContext2D; | ||
width: number; | ||
getTimePixel: (time: number) => number; | ||
}; | ||
|
||
const TICKS_PATTERN = { | ||
MINUTE: [2, 9, 2], | ||
FIVE_MINUTES: [6, 9, 6], | ||
QUARTER_HOUR: [2, 2, 6, 9, 6, 2, 2], | ||
HALF_HOUR: [2, 2, 2, 2, 6, 9, 6, 2, 2, 2, 2], | ||
HOUR: [16, 9, 16], | ||
}; | ||
|
||
const drawRails = ({ | ||
xStart, | ||
yStart, | ||
width, | ||
stroke = '#D3D1CF', | ||
ctx, | ||
}: { | ||
xStart: number; | ||
yStart: number; | ||
width: number; | ||
stroke?: string; | ||
ctx: CanvasRenderingContext2D; | ||
}) => { | ||
ctx.clearRect(xStart, yStart, width, 9); | ||
ctx.beginPath(); | ||
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)'; | ||
ctx.strokeStyle = stroke; | ||
ctx.lineWidth = 1; | ||
ctx.rect(xStart, yStart, width, 8); | ||
ctx.fill(); | ||
ctx.stroke(); | ||
}; | ||
|
||
const drawTick = ({ | ||
ctx, | ||
xStart, | ||
yStart, | ||
ticks, | ||
stroke, | ||
}: { | ||
ctx: CanvasRenderingContext2D; | ||
xStart: number; | ||
yStart: number; | ||
ticks: number[]; | ||
stroke: string; | ||
}) => { | ||
ctx.beginPath(); | ||
ctx.setLineDash(ticks); | ||
ctx.strokeStyle = stroke; | ||
ctx.lineWidth = 1; | ||
ctx.moveTo(xStart, yStart - sum(ticks) / 2); | ||
ctx.lineTo(xStart, yStart + sum(ticks) / 2); | ||
ctx.stroke(); | ||
}; | ||
|
||
export const drawTrack = ({ ctx, width, getTimePixel }: DrawTrackProps) => { | ||
ctx.save(); | ||
|
||
drawRails({ xStart: -1, yStart: TRACK_HEIGHT_CONTAINER / 2 - 4, width: width + 1, ctx }); | ||
|
||
timeScaleSample.forEach((time) => { | ||
const date = new Date(+time); | ||
const minutes = date.getMinutes().toString().padStart(2, '0'); | ||
|
||
type TickPattern = keyof typeof TICKS_PATTERN; | ||
let tickPattern: TickPattern = 'MINUTE'; | ||
switch (minutes) { | ||
case '00': | ||
tickPattern = 'HOUR'; | ||
break; | ||
case '30': | ||
tickPattern = 'HALF_HOUR'; | ||
break; | ||
case '15': | ||
case '45': | ||
tickPattern = 'QUARTER_HOUR'; | ||
break; | ||
case '05': | ||
case '10': | ||
case '20': | ||
case '25': | ||
case '35': | ||
case '40': | ||
case '50': | ||
case '55': | ||
tickPattern = 'FIVE_MINUTES'; | ||
break; | ||
default: | ||
tickPattern = 'MINUTE'; | ||
break; | ||
} | ||
|
||
drawTick({ | ||
ctx, | ||
xStart: getTimePixel(+time), | ||
yStart: TRACK_HEIGHT_CONTAINER / 2, | ||
ticks: TICKS_PATTERN[tickPattern], | ||
stroke: '#2170B9', | ||
}); | ||
}); | ||
|
||
ctx.restore(); | ||
}; |
64 changes: 64 additions & 0 deletions
64
ui-trackoccupancydiagram/src/components/helpers/drawElements/drawTracks.ts
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,64 @@ | ||
import { drawTrack } from './drawTrack'; | ||
import { timeScaleSample } from '../../../sample/timeScale'; | ||
import { type Track } from '../../types'; | ||
|
||
export function getTimeToPixel( | ||
timeOrigin: number, | ||
pixelOffset: number, | ||
timeScale: number | ||
): (time: number) => number { | ||
return (time: number) => pixelOffset + (time - timeOrigin) / timeScale; | ||
} | ||
|
||
const drawBackground = ({ | ||
ctx, | ||
xStart, | ||
width, | ||
height, | ||
switchBackground, | ||
}: { | ||
ctx: CanvasRenderingContext2D; | ||
xStart: number; | ||
width: number; | ||
height: number; | ||
switchBackground: boolean; | ||
}) => { | ||
if (xStart >= 0) { | ||
ctx.clearRect(xStart, 0, width, height); | ||
ctx.fillStyle = switchBackground ? 'rgba(243, 248, 253, 0.5)' : 'rgb(255, 255, 255)'; | ||
ctx.fillRect(xStart, 0, width, height); | ||
} | ||
}; | ||
|
||
type DrawTracksProps = { | ||
ctx: CanvasRenderingContext2D; | ||
width: number; | ||
height: number; | ||
tracks: Track[] | undefined; | ||
getTimePixel: (time: number) => number; | ||
}; | ||
|
||
export const drawTracks = ({ ctx, width, height, tracks, getTimePixel }: DrawTracksProps) => { | ||
ctx.clearRect(0, 0, width, height); | ||
|
||
let switchBackground = false; | ||
timeScaleSample.forEach((time) => { | ||
const date = new Date(+time); | ||
const minutes = date.getMinutes().toString().padStart(2, '0'); | ||
|
||
switch (minutes) { | ||
case '00': | ||
switchBackground = !switchBackground; | ||
drawBackground({ ctx, xStart: getTimePixel(+time), width, height, switchBackground }); | ||
break; | ||
default: | ||
return; | ||
} | ||
}); | ||
|
||
tracks?.forEach((_, index) => { | ||
const trackTranslate = index === 0 ? 8 : 73; | ||
ctx.translate(0, trackTranslate); | ||
drawTrack({ ctx, width, getTimePixel }); | ||
}); | ||
}; |
30 changes: 30 additions & 0 deletions
30
ui-trackoccupancydiagram/src/components/hooks/useCanvas.ts
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,30 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import type { DrawFunctionParams, Store } from '../types'; | ||
|
||
type DrawFunction = (params: DrawFunctionParams) => void; | ||
|
||
type UseCanvasParams = { | ||
width: number; | ||
height: number; | ||
store: Store; | ||
setStore?: React.Dispatch<React.SetStateAction<Store>>; | ||
}; | ||
|
||
type UseCanvas = ( | ||
draw: DrawFunction, | ||
params: UseCanvasParams | ||
) => React.RefObject<HTMLCanvasElement>; | ||
|
||
export const useCanvas: UseCanvas = (draw, { width, height, store, setStore }) => { | ||
const canvas = useRef<HTMLCanvasElement>(null); | ||
|
||
useEffect(() => { | ||
const currentCanvas = canvas.current as HTMLCanvasElement; | ||
const ctx = currentCanvas.getContext('2d') as CanvasRenderingContext2D; | ||
draw({ ctx, width, height, store, setStore }); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [width, height, store]); | ||
|
||
return canvas; | ||
}; |
29 changes: 27 additions & 2 deletions
29
ui-trackoccupancydiagram/src/components/layers/TracksLayer.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 |
---|---|---|
@@ -1,5 +1,30 @@ | ||
import React from 'react'; | ||
import { useCallback } from 'react'; | ||
|
||
const TracksLayer = () => <canvas id="tracks-layer" />; | ||
import { | ||
type LayerType, | ||
type DrawingFunction, | ||
} from '@osrd-project/ui-spacetimechart/dist/lib/types'; | ||
Check warning on line 6 in ui-trackoccupancydiagram/src/components/layers/TracksLayer.tsx GitHub Actions / build
|
||
|
||
import { drawTracks } from '../helpers/drawElements/drawTracks'; | ||
|
||
const TracksLayer = ({ useDraw }: { useDraw: (layer: LayerType, fn: DrawingFunction) => void }) => { | ||
const drawingFunction = useCallback<DrawingFunction>( | ||
(ctx, { getTimePixel, tracks, trackOccupancyWidth, trackOccupancyHeight }) => { | ||
if (trackOccupancyHeight && trackOccupancyWidth) | ||
drawTracks({ | ||
ctx, | ||
width: trackOccupancyWidth, | ||
height: trackOccupancyHeight, | ||
tracks, | ||
getTimePixel, | ||
}); | ||
}, | ||
[] | ||
); | ||
|
||
useDraw('background', drawingFunction); | ||
|
||
return null; | ||
}; | ||
|
||
export default TracksLayer; |
Oops, something went wrong.