Files
stemengine_old/frontend/src/components/visualizers/RendererControlsPanel.tsx
T
2026-04-28 17:51:01 +09:00

121 lines
4.2 KiB
TypeScript

import type { VisualizerIframeCapabilities, VisualizerParamValue } from '../../hooks/useVisualizerIframeTransport'
interface RendererControlsPanelProps {
capabilities: VisualizerIframeCapabilities | null
values: Record<string, VisualizerParamValue>
onValueChange: (name: string, value: VisualizerParamValue) => void
onReset?: () => void
className?: string
}
function readValue(name: string, values: Record<string, VisualizerParamValue>, fallback: VisualizerParamValue): VisualizerParamValue {
if (Object.prototype.hasOwnProperty.call(values, name)) return values[name]
return fallback
}
export default function RendererControlsPanel({
capabilities,
values,
onValueChange,
onReset,
className,
}: RendererControlsPanelProps) {
if (!capabilities) {
return (
<div className={className}>
<p className="text-[11px] text-gray-400">Loading renderer controls...</p>
</div>
)
}
const params = capabilities.params || []
if (!params.length) {
return (
<div className={className}>
<p className="text-[11px] text-gray-400">No renderer-specific controls reported.</p>
</div>
)
}
return (
<div className={className}>
<div className="space-y-2">
{params.map((param) => {
const value = readValue(param.name, values, param.default)
const key = `renderer-param-${param.name}`
if (param.type === 'boolean') {
return (
<label key={param.name} className="flex items-center justify-between gap-2 text-[11px] text-gray-200" htmlFor={key}>
<span>{param.label}</span>
<input
id={key}
type="checkbox"
checked={Boolean(value)}
onChange={(event) => onValueChange(param.name, event.target.checked)}
className="h-3.5 w-3.5"
/>
</label>
)
}
if (param.type === 'enum') {
const options = param.options && param.options.length > 0 ? param.options : [String(param.default)]
return (
<label key={param.name} className="flex flex-col gap-1 text-[11px] text-gray-300" htmlFor={key}>
<span>{param.label}</span>
<select
id={key}
value={String(value)}
onChange={(event) => onValueChange(param.name, event.target.value)}
className="rounded border border-white/10 bg-black/30 px-2 py-1 text-[11px] text-gray-100"
>
{options.map((option) => (
<option key={`${param.name}-${option}`} value={option}>{option}</option>
))}
</select>
</label>
)
}
const min = Number.isFinite(param.min) ? Number(param.min) : 0
const max = Number.isFinite(param.max) ? Number(param.max) : 1
const step = Number.isFinite(param.step) ? Number(param.step) : 0.01
const numericValue = Number.isFinite(Number(value)) ? Number(value) : Number(param.default)
return (
<label key={param.name} className="flex flex-col gap-1 text-[11px] text-gray-300" htmlFor={key}>
<span className="flex items-center justify-between gap-2">
<span>{param.label}</span>
<span className="font-mono text-[10px] text-gray-500">{numericValue.toFixed(2)}</span>
</span>
<input
id={key}
type="range"
min={min}
max={max}
step={step}
value={numericValue}
onChange={(event) => onValueChange(param.name, Number(event.target.value))}
className="w-full"
/>
</label>
)
})}
</div>
{onReset ? (
<div className="mt-2">
<button
type="button"
onClick={onReset}
className="rounded border border-white/10 bg-white/5 px-2 py-1 text-[10px] text-gray-200 hover:bg-white/10"
>
Reset renderer controls
</button>
</div>
) : null}
</div>
)
}