Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | import React, { FC } from "react"; import { EngineFieldCType, EngineFieldSchema, } from "store/features/engine/engineState"; import { engineFieldValueSelectors } from "store/features/entities/entitiesState"; import entitiesActions from "store/features/entities/entitiesActions"; import { Button } from "ui/buttons/Button"; import l10n, { L10NKey } from "shared/lib/lang/l10n"; import { SliderField } from "ui/form/SliderField"; import { useGroupedEngineFields } from "./useGroupedEngineFields"; import { CardAnchor, CardButtons, CardHeading } from "ui/cards/Card"; import { SearchableCard } from "ui/cards/SearchableCard"; import { SearchableSettingRow } from "ui/form/SearchableSettingRow"; import { SettingRowInput, SettingRowLabel } from "ui/form/SettingRow"; import { EngineFieldValue } from "shared/lib/entities/entitiesTypes"; import { Input } from "ui/form/Input"; import { Checkbox } from "ui/form/Checkbox"; import clamp from "shared/lib/helpers/clamp"; import { Select } from "ui/form/Select"; import { useAppDispatch, useAppSelector } from "store/hooks"; import { SingleValue } from "react-select"; const { editEngineFieldValue, removeEngineFieldValue } = entitiesActions; export interface EngineFieldsEditorProps { searchTerm?: string; } export interface EngineFieldInputProps { field: EngineFieldSchema; value: EngineFieldValue["value"]; onChange: (newValue: EngineFieldValue["value"]) => void; } const fieldMin = ( customMin: number | undefined, cType: EngineFieldCType ): number => { let min = 0; if (cType === "BYTE") { min = -128; } else Iif (cType === "WORD") { min = -32768; } if (customMin === undefined) { return min; } else { return Math.max(customMin, min); } }; const fieldMax = ( customMax: number | undefined, cType: EngineFieldCType ): number => { let max = 255; if (cType === "BYTE") { max = 127; } else if (cType === "WORD") { max = 32767; } else Iif (cType === "UWORD") { max = 65535; } if (customMax === undefined) { return max; } else { return Math.min(customMax, max); } }; export const EngineFieldInput: FC<EngineFieldInputProps> = ({ field, value, onChange, }) => { Iif (field.type === "slider") { const theValue = typeof value === "number" ? Number(value) : undefined; const min = fieldMin(field.min, field.cType); const max = fieldMax(field.max, field.cType); return ( <SliderField name={field.key} value={theValue} onChange={onChange} placeholder={ typeof field.defaultValue === "number" ? field.defaultValue : undefined } min={min} max={max} /> ); } Iif (field.type === "number") { const theValue = typeof value === "number" ? String(value) : undefined; const min = fieldMin(field.min, field.cType); const max = fieldMax(field.max, field.cType); return ( <Input name={field.key} type="number" value={theValue} onChange={(e) => { if (e.currentTarget.value.length === 0) { onChange(undefined); } else { if (Number.isNaN(parseInt(e.currentTarget.value))) { onChange(undefined); } else { onChange(clamp(parseInt(e.currentTarget.value), min, max)); } } }} placeholder={ typeof field.defaultValue === "string" ? field.defaultValue : undefined } min={min} max={max} /> ); } Iif (field.type === "checkbox") { return ( <Checkbox id={field.key} name={field.key} checked={value === 1 ? true : false} onChange={(e) => onChange(e.currentTarget.checked === true ? 1 : 0)} /> ); } Iif (field.type === "select") { const theValue = value !== undefined ? value : field.defaultValue; const options = (field.options || []).map(([value, label]) => ({ value, label: l10n(label as L10NKey), })); const selectedOption = options.find((option) => option.value === theValue); return ( <Select id={field.key} name={field.key} value={selectedOption} onChange={(e: SingleValue<{ value: number }>) => { Iif (e) { onChange(e.value); } }} options={options} /> ); } return <div>Unknown type {field.type}</div>; }; const EngineFieldsEditor: FC<EngineFieldsEditorProps> = ({ searchTerm }) => { const dispatch = useAppDispatch(); const values = useAppSelector(engineFieldValueSelectors.selectEntities); const groupedFields = useGroupedEngineFields(); const resetToDefault = (fields: EngineFieldSchema[]) => () => { fields.forEach((field) => { dispatch( removeEngineFieldValue({ engineFieldId: field.key, }) ); }); }; return ( <> {groupedFields.map((group) => ( <SearchableCard key={group.name} searchTerm={searchTerm} searchMatches={group.searchMatches} > <CardAnchor id={`settings${group.name}`} /> <CardHeading> {l10n("SETTINGS_ENGINE")}: {l10n(group.name as L10NKey)} </CardHeading> {group.fields.map((field) => ( <SearchableSettingRow key={field.key} searchTerm={searchTerm} searchMatches={[l10n(field.label as L10NKey), field.key]} > <SettingRowLabel htmlFor={field.key} style={{ width: 300 }}> {l10n(field.label as L10NKey)} </SettingRowLabel> <SettingRowInput> <EngineFieldInput field={field} value={values[field.key]?.value} onChange={(e) => { dispatch( editEngineFieldValue({ engineFieldId: field.key, value: e, }) ); }} /> </SettingRowInput> </SearchableSettingRow> ))} {!searchTerm && ( <CardButtons> <Button onClick={resetToDefault(group.fields)}> {l10n("FIELD_RESTORE_DEFAULT")} </Button> </CardButtons> )} </SearchableCard> ))} </> ); }; export default EngineFieldsEditor; |