-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathInputSchemeSelection.tsx
More file actions
194 lines (180 loc) · 8.01 KB
/
Copy pathInputSchemeSelection.tsx
File metadata and controls
194 lines (180 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { Box, Divider, FormControl, InputLabel, MenuItem, Stack, Tooltip } from "@mui/material"
import { type ReactElement, useCallback, useEffect, useReducer, useState } from "react"
import EventSystem from "@/systems/EventSystem.ts"
import InputSchemeManager from "@/systems/input/InputSchemeManager"
import InputSystem from "@/systems/input/InputSystem"
import { type InputScheme, type InputSchemeAvailability, InputSchemeUseType } from "@/systems/input/InputTypes"
import { DriveType } from "@/systems/simulation/behavior/Behavior"
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain"
import Label from "@/ui/components/Label"
import { PositiveButton, SynthesisIcons, Select } from "@/ui/components/StyledComponents"
import { useStateContext } from "@/ui/helpers/StateProviderHelpers"
import { useIsTouchDevice } from "@/ui/helpers/useIsMobile"
interface SchemeSelectorProps {
scheme: InputScheme
panelId?: string
brainIndex: number
style?: React.CSSProperties
message: string
disabled?: boolean
onSelect?: () => void
}
const SchemeSelector: React.FC<SchemeSelectorProps> = ({
scheme,
panelId,
brainIndex,
style,
message,
disabled = false,
onSelect,
}): ReactElement | null => {
const isTouch = useIsTouchDevice()
if (scheme.usesTouchControls && !isTouch) return null
return (
<Tooltip title={message} key={scheme.schemeName} placement={"left"}>
<Stack
direction="row"
justifyContent={"space-between"}
alignItems={"center"}
gap={"1rem"}
key={scheme.schemeName}
>
<Label size="sm">
{`${scheme.schemeName} | ${scheme.customized ? "Custom" : scheme.descriptiveName}`}
</Label>
<Stack direction="row-reverse" gap="0.25rem" justifyContent={"center"} alignItems={"center"}>
{/** Select button */}
<Box sx={style}>
<PositiveButton
disabled={disabled}
onClick={() => {
InputSystem.setBrainIndexSchemeMapping(brainIndex, scheme)
// Ensure touch controls are shown when a touch scheme is selected
if (scheme.usesTouchControls) {
EventSystem.dispatch("SetTouchControlsVisibilityEvent", true)
}
EventSystem.dispatch("InputSchemeChanged", { panelId })
onSelect?.()
}}
>
<SynthesisIcons.SELECT_LARGE />
</PositiveButton>
</Box>
</Stack>
</Stack>
</Tooltip>
)
}
interface InputSchemeSelectionProps {
brainIndex: number
onSelect?: () => void
panelId?: string
}
export default function InputSchemeSelection({ brainIndex, onSelect, panelId }: InputSchemeSelectionProps) {
const { setSelectedScheme } = useStateContext()
const [_, update] = useReducer(x => !x, false)
const [robotDriveType, setRobotDriveType] = useState<DriveType>(
SynthesisBrain.brainIndexMap.get(brainIndex)?.driveType ?? DriveType.ARCADE
)
const [availableSchemes, setAvailableSchemes] = useState<InputSchemeAvailability[]>()
const refreshAvailableSchemes = useCallback(() => {
const schemes = [...InputSchemeManager.availableInputSchemesByType(robotDriveType)]
if (matchMedia("(hover: none)").matches) {
// showing input schemes that support touch controls first (on mobile devices)
schemes.sort((a, b) => (b.scheme.usesTouchControls ? 1 : 0) - (a.scheme.usesTouchControls ? 1 : 0))
}
setAvailableSchemes(schemes)
}, [robotDriveType])
useEffect(() => {
// Initial load and when robotDriveType changes
refreshAvailableSchemes()
return EventSystem.listen("InputSchemeChanged", () => refreshAvailableSchemes())
}, [refreshAvailableSchemes])
const onSchemeSelected = useCallback(() => {
onSelect?.()
update()
}, [onSelect])
return (
// A scroll view with buttons to select default and custom input schemes
<>
{/** The label and divider at the top of the scroll view */}
<Divider />
<FormControl fullWidth>
<InputLabel id="input-scheme-drivetrain-type-label">Drivetrain Type</InputLabel>
<Select
label="Drivetrain Type"
value={robotDriveType}
onChange={e => {
const newDriveType = e.target.value as DriveType
const brain = SynthesisBrain.brainIndexMap.get(brainIndex)
const appliedDriveType = brain?.configureDriveBehavior(newDriveType) ?? newDriveType
setRobotDriveType(appliedDriveType)
const scheme = InputSchemeManager.applyCompatibleScheme(brainIndex)
if (scheme) setSelectedScheme(scheme)
EventSystem.dispatch("InputSchemeChanged", { panelId })
}}
>
{[DriveType.TANK, DriveType.ARCADE, DriveType.SWERVE].map(dt => (
<MenuItem key={dt} value={dt}>
{dt}
</MenuItem>
))}
</Select>
</FormControl>
<Divider />
<Label size="md" className="text-center mt-[4pt] mb-[2pt] mx-[5%]">
{`${availableSchemes?.length} Input Schemes`}
</Label>
<Divider />
{/** Creates list items with buttons */}
{availableSchemes
?.filter(scheme => scheme.status == InputSchemeUseType.AVAILABLE)
.map(scheme => {
return (
<SchemeSelector
key={`available-${scheme.scheme.schemeName}`}
scheme={scheme.scheme}
panelId={panelId}
brainIndex={brainIndex}
message="Available"
onSelect={onSchemeSelected}
/>
)
})}
{availableSchemes
?.filter(scheme => scheme.status == InputSchemeUseType.CONFLICT)
.map((scheme, i) => {
return (
<div key={`conflict-${scheme.scheme.schemeName}`}>
{i == 0 && <Divider />}
<SchemeSelector
scheme={scheme.scheme}
panelId={panelId}
brainIndex={brainIndex}
style={{ filter: "brightness(60%)" }}
message={"Conflicts with " + scheme.conflictingSchemeNames}
onSelect={onSchemeSelected}
/>
</div>
)
})}
{availableSchemes
?.filter(scheme => scheme.status == InputSchemeUseType.IN_USE)
.map((scheme, i) => {
return (
<div key={`in-use-${scheme.scheme.schemeName}`}>
{i == 0 && <Divider />}
<SchemeSelector
scheme={scheme.scheme}
panelId={panelId}
brainIndex={brainIndex}
message="In Use"
disabled={true}
onSelect={onSchemeSelected}
/>
</div>
)
})}
</>
)
}