Skip to content

Commit cc5133a

Browse files
authored
feat: add steering trace to input widget (#54)
1 parent aa0252c commit cc5133a

File tree

12 files changed

+233
-54
lines changed

12 files changed

+233
-54
lines changed

src/app/storage/defaultDashboard.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export const defaultDashboard: DashboardLayout = {
4545
enabled: true,
4646
unit: 'auto',
4747
},
48+
steer: {
49+
enabled: true,
50+
},
4851
},
4952
},
5053
{

src/frontend/components/Input/InputBar/InputBar.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@ const INPUT_CONFIG = [
88
{ key: 'throttle', color: getColor('green') }
99
] as const;
1010

11-
export interface InputTraceProps {
11+
export interface InputBarProps {
1212
brake?: number;
1313
throttle?: number;
1414
clutch?: number;
15-
settings: {
15+
settings?: {
1616
includeClutch: boolean;
1717
includeBrake: boolean;
1818
includeThrottle: boolean;
1919
};
2020
}
2121

22-
export const InputBar = ({ brake, throttle, clutch, settings }: InputTraceProps) => {
22+
export const InputBar = ({
23+
brake,
24+
throttle,
25+
clutch,
26+
settings = {
27+
includeClutch: true,
28+
includeBrake: true,
29+
includeThrottle: true,
30+
},
31+
}: InputBarProps) => {
2332
const svgRef = useRef<SVGSVGElement>(null);
2433

2534
useEffect(() => {

src/frontend/components/Input/InputContainer/InputContainer.stories.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const RandomTraces = () => {
1515
const [clutch, setClutch] = useState(0);
1616
const [gear] = useState(2);
1717
const [speed] = useState(122);
18+
const [steer, setSteer] = useState(0);
1819

1920
useEffect(() => {
2021
const interval = setInterval(() => {
@@ -29,6 +30,13 @@ const RandomTraces = () => {
2930
setClutch((value) =>
3031
Math.max(0, Math.min(1, value + Math.random() * 0.1 - 0.05))
3132
);
33+
34+
setSteer((value) =>
35+
Math.max(
36+
-Math.PI,
37+
Math.min(Math.PI, value + Math.random() * 0.2 - 0.1),
38+
)
39+
);
3240
}, 1000 / 60);
3341
return () => clearInterval(interval);
3442
}, []);
@@ -39,6 +47,7 @@ const RandomTraces = () => {
3947
clutch={clutch}
4048
gear={gear}
4149
speed={speed}
50+
steer={steer}
4251
settings={{
4352
trace: {
4453
enabled: true,
@@ -55,6 +64,9 @@ const RandomTraces = () => {
5564
enabled: true,
5665
unit: 'auto',
5766
},
67+
steer: {
68+
enabled: true,
69+
},
5870
}}
5971
/>
6072
);
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { InputWidgetSettings } from '../../Settings/types';
12
import { InputBar } from '../InputBar/InputBar';
23
import { InputGear } from '../InputGear/InputGear';
4+
import { InputSteer } from '../InputSteer/InputSteer';
35
import { InputTrace } from '../InputTrace/InputTrace';
46

57
export interface InputProps {
@@ -9,25 +11,8 @@ export interface InputProps {
911
gear?: number;
1012
speed?: number;
1113
unit?: number;
12-
settings?: InputSettings;
13-
}
14-
15-
export interface InputSettings {
16-
trace: {
17-
enabled: boolean;
18-
includeThrottle: boolean;
19-
includeBrake: boolean;
20-
};
21-
bar: {
22-
enabled: boolean;
23-
includeClutch: boolean;
24-
includeBrake: boolean;
25-
includeThrottle: boolean;
26-
};
27-
gear: {
28-
enabled: boolean;
29-
unit: 'mph' | 'km/h' | 'auto';
30-
};
14+
steer?: number;
15+
settings?: InputWidgetSettings['config'];
3116
}
3217

3318
export const InputContainer = ({
@@ -36,15 +21,32 @@ export const InputContainer = ({
3621
clutch,
3722
gear,
3823
speed,
24+
steer,
3925
unit,
4026
settings,
4127
}: InputProps) => {
4228
return (
4329
<div className="w-full h-full inline-flex gap-1 p-2 flex-row bg-slate-800/50">
44-
{settings?.trace.enabled && <InputTrace input={{ brake, throttle }} settings={settings.trace} />}
45-
{settings?.bar.enabled && <InputBar brake={brake} throttle={throttle} clutch={clutch} settings={settings.bar} />}
46-
{settings?.gear.enabled && <InputGear gear={gear} speedMs={speed} unit={unit} settings={settings.gear} />}
47-
{/* <InputSteer /> */} {/* WIP */}
30+
{(settings?.trace?.enabled ?? true) && (
31+
<InputTrace input={{ brake, throttle }} settings={settings?.trace} />
32+
)}
33+
{(settings?.bar?.enabled ?? true) && (
34+
<InputBar
35+
brake={brake}
36+
throttle={throttle}
37+
clutch={clutch}
38+
settings={settings?.bar}
39+
/>
40+
)}
41+
{(settings?.gear?.enabled ?? true) && (
42+
<InputGear
43+
gear={gear}
44+
speedMs={speed}
45+
unit={unit}
46+
settings={settings?.gear}
47+
/>
48+
)}
49+
{(settings?.steer?.enabled ?? true) && <InputSteer angleRad={steer} />}
4850
</div>
4951
);
5052
};

src/frontend/components/Input/InputGear/InputGear.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ export interface InputGearProps {
22
gear?: number;
33
speedMs?: number;
44
unit?: number;
5-
settings: {
5+
settings?: {
66
unit: 'mph' | 'km/h' | 'auto';
77
};
88
}
99

10-
export const InputGear = ({ gear, speedMs, unit, settings }: InputGearProps) => {
11-
const isMetric = (unit === 1 && settings.unit === 'auto') || settings.unit === 'km/h';
10+
export const InputGear = ({
11+
gear,
12+
speedMs,
13+
unit,
14+
settings = { unit: 'auto' },
15+
}: InputGearProps) => {
16+
const isMetric =
17+
(unit === 1 && settings.unit === 'auto') || settings.unit === 'km/h';
1218
const speed = (speedMs ?? 0) * (isMetric ? 3.6 : 2.23694);
1319
const displayUnit = isMetric ? 'km/h' : 'mph';
1420
let gearText = '';

src/frontend/components/Input/InputSteer/InputSteer.stories.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ import { InputSteer } from './InputSteer';
33

44
export default {
55
component: InputSteer,
6+
argTypes: {
7+
angleRad: {
8+
control: {
9+
type: 'range',
10+
min: -3.14,
11+
max: 3.14,
12+
step: 0.01,
13+
},
14+
},
15+
},
616
} as Meta;
717

818
type Story = StoryObj<typeof InputSteer>;
919

1020
export const Primary: Story = {
11-
args: {},
21+
args: {
22+
angleRad: 0,
23+
},
1224
};
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
import { SteeringWheelIcon } from '@phosphor-icons/react';
1+
export interface InputSteerProps {
2+
angleRad?: number;
3+
}
24

3-
export type InputSteerProps = object; // TODO
4-
5-
export const InputSteer = () => {
5+
export const InputSteer = ({ angleRad = 0 }: InputSteerProps) => {
66
return (
7-
<div className="w-[120px]">
8-
<SteeringWheelIcon width="100%" height="100%" />
7+
<div className="w-[120px] fill-white">
8+
<svg
9+
xmlns="http://www.w3.org/2000/svg"
10+
xmlSpace="preserve"
11+
width="100%"
12+
height="100%"
13+
viewBox="-66 33 145 145"
14+
>
15+
<g
16+
style={{
17+
transform: `rotate(${angleRad * -1}rad)`,
18+
transformBox: 'fill-box',
19+
transformOrigin: 'center',
20+
}}
21+
>
22+
<path d="M6.033 32.908a72.196 72.196 0 0 0-72.196 72.196A72.196 72.196 0 0 0 6.033 177.3a72.196 72.196 0 0 0 72.196-72.196A72.196 72.196 0 0 0 6.033 32.908Zm0 12.657A59.538 59.538 0 0 1 64.298 93.54C48.864 89.147 33.41 84.76 6.42 84.51v-.013c-.133 0-.256.005-.388.006-.133 0-.255-.005-.388-.006v.013c-26.99.25-42.444 4.637-57.877 9.03A59.538 59.538 0 0 1 6.033 45.565Zm-28.908 58.126c9.141 2.38 16.78 12.14 22.875 29.501-.808 17.98-1.985 28.342-3.55 30.653a59.538 59.538 0 0 1-49.561-53.73c8.89-2.973 16.97-6.514 30.236-6.424zm57.816 0c13.345-.09 21.44 3.494 30.393 6.477a59.538 59.538 0 0 1-49.708 53.695c-1.57-2.281-2.75-12.651-3.56-30.671 6.093-17.36 13.733-27.122 22.875-29.501z" />
23+
<path
24+
d="M 0 33.25 A 72 72 0 0 1 12 33.25 L 12 45.8 A 60 60 0 0 0 0 45.8 Z"
25+
className='fill-yellow-500'
26+
/>
27+
</g>
28+
</svg>
929
</div>
1030
);
1131
};

src/frontend/components/Input/InputTrace/InputTrace.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import * as d3 from 'd3';
22
import { useEffect, useRef, useState } from 'react';
3-
import tailwindColors from 'tailwindcss/colors';
3+
import { getColor } from '@irdashies/utils/colors';
44

5-
const BRAKE_COLOR = tailwindColors.red['500'];
6-
const THROTTLE_COLOR = tailwindColors.green['500'];
5+
const BRAKE_COLOR = getColor('red');
6+
const THROTTLE_COLOR = getColor('green');
77

88
export interface InputTraceProps {
99
input: {
1010
brake?: number;
1111
throttle?: number;
1212
};
13-
settings: {
13+
settings?: {
1414
includeThrottle?: boolean;
1515
includeBrake?: boolean;
1616
};
1717
}
1818

19-
export const InputTrace = ({ input, settings }: InputTraceProps) => {
19+
export const InputTrace = ({
20+
input,
21+
settings = { includeThrottle: true, includeBrake: true },
22+
}: InputTraceProps) => {
2023
const { includeThrottle, includeBrake } = settings;
2124
const svgRef = useRef<SVGSVGElement>(null);
2225
const { width, height } = { width: 400, height: 100 };

src/frontend/components/Input/hooks/useInputSettings.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useDashboard } from '@irdashies/context'
2-
import { InputSettings } from '../InputContainer/InputContainer';
2+
import { InputWidgetSettings } from '../../Settings/types';
33

44
export const useInputSettings = () => {
55
const { currentDashboard } = useDashboard();
@@ -18,7 +18,7 @@ export const useInputSettings = () => {
1818
typeof inputSettings.bar === 'object' &&
1919
typeof inputSettings.gear === 'object'
2020
) {
21-
return inputSettings as unknown as InputSettings;
21+
return inputSettings as unknown as InputWidgetSettings['config'];
2222
}
2323

2424
return undefined;

src/frontend/components/Input/hooks/useInputs.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const useInputs = () => {
77
const gear = useTelemetryValue('Gear');
88
const speed = useTelemetryValue('Speed');
99
const unit = useTelemetryValue('DisplayUnits');
10+
const steer = useTelemetryValue('SteeringWheelAngle');
1011

11-
return { brake, throttle, clutch, gear, speed, unit };
12+
return { brake, throttle, clutch, gear, speed, unit, steer };
1213
};

0 commit comments

Comments
 (0)