| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import state, { useSelector } from 'state'
- import styled from 'styles'
- import {
- ControlType,
- NumberCodeControl,
- TextCodeControl,
- VectorCodeControl,
- } from 'types'
-
- export default function Control({ id }: { id: string }): JSX.Element {
- const control = useSelector((s) => s.data.codeControls[id])
-
- if (!control) return null
-
- return (
- <>
- <label>{control.label}</label>
- {(() => {
- switch (control.type) {
- case ControlType.Number:
- return <NumberControl {...control} />
- case ControlType.Vector:
- return <VectorControl {...control} />
- case ControlType.Text:
- return <TextControl {...control} />
- }
- })()}
- </>
- )
- }
-
- function NumberControl({ id, min, max, step, value }: NumberCodeControl) {
- return (
- <Inputs>
- <input
- type="range"
- min={min}
- max={max}
- step={step}
- value={value}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: Number(e.currentTarget.value),
- })
- }
- />
- <input
- type="number"
- min={min}
- max={max}
- step={step}
- value={value}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: Number(e.currentTarget.value),
- })
- }
- />
- </Inputs>
- )
- }
-
- function VectorControl({
- id,
- value,
- min = -Infinity,
- max = Infinity,
- step = 0.01,
- isNormalized = false,
- }: VectorCodeControl) {
- return (
- <Inputs>
- <input
- type="range"
- min={isNormalized ? -1 : min}
- max={isNormalized ? 1 : max}
- step={step}
- value={value[0]}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: [Number(e.currentTarget.value), value[1]],
- })
- }
- />
- <input
- type="number"
- min={isNormalized ? -1 : min}
- max={isNormalized ? 1 : max}
- step={step}
- value={value[0]}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: [Number(e.currentTarget.value), value[1]],
- })
- }
- />
- <input
- type="range"
- min={isNormalized ? -1 : min}
- max={isNormalized ? 1 : max}
- step={step}
- value={value[1]}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: [value[0], Number(e.currentTarget.value)],
- })
- }
- />
- <input
- type="number"
- min={isNormalized ? -1 : min}
- max={isNormalized ? 1 : max}
- step={step}
- value={value[1]}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: [value[0], Number(e.currentTarget.value)],
- })
- }
- />
- </Inputs>
- )
- }
-
- function TextControl({ id, value }: TextCodeControl) {
- return (
- <Inputs>
- <input
- type="text"
- value={value}
- onChange={(e) =>
- state.send('CHANGED_CODE_CONTROL', {
- [id]: e.currentTarget.value,
- })
- }
- />
- </Inputs>
- )
- }
-
- const Inputs = styled('div', {
- display: 'flex',
- gap: '8px',
- height: '100%',
-
- '& input': {
- font: '$ui',
- width: '64px',
- fontSize: '$1',
- border: '1px solid $inputBorder',
- backgroundColor: '$input',
- color: '$text',
- height: '100%',
- padding: '0px 6px',
- },
- "& input[type='range']": {
- padding: 0,
- flexGrow: 2,
- },
- "& input[type='text']": {
- minWidth: 200,
- padding: 4,
- flexGrow: 2,
- },
- })
|