2024-07-22 01:41:17 +02:00
|
|
|
"use client";
|
2024-08-07 23:34:13 +02:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonGroup,
|
|
|
|
Grid,
|
|
|
|
Input,
|
|
|
|
Slider,
|
|
|
|
Stack,
|
|
|
|
TextField,
|
|
|
|
} from "@mui/material";
|
2024-07-22 01:41:17 +02:00
|
|
|
import React from "react";
|
|
|
|
import { StripBusOutput, StripBusOutputEvent } from "./StripBusOutput";
|
2024-08-07 21:39:58 +02:00
|
|
|
import { EventCounter } from "@/utils/EventCounter";
|
2024-07-22 01:41:17 +02:00
|
|
|
|
2024-08-06 02:51:59 +02:00
|
|
|
export interface BusProps {
|
2024-08-07 23:34:13 +02:00
|
|
|
values?: Partial<Omit<BusState, StateDefaultValueException>>;
|
|
|
|
showInputdB?: boolean;
|
|
|
|
name: string;
|
2024-07-30 02:39:44 +02:00
|
|
|
onChange: (event: BusEvent) => any;
|
2024-07-22 01:41:17 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 23:34:13 +02:00
|
|
|
type StateDefaultValueException = "showInputdB";
|
|
|
|
|
2024-08-06 02:51:59 +02:00
|
|
|
export interface BusState {
|
2024-07-22 01:41:17 +02:00
|
|
|
gain: number;
|
|
|
|
muted: boolean;
|
2024-08-06 02:51:59 +02:00
|
|
|
selected: boolean;
|
2024-08-07 23:34:13 +02:00
|
|
|
showInputdB: boolean;
|
2024-07-22 01:41:17 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 02:51:59 +02:00
|
|
|
export type BusEvent = BusMuted | BusGainChanged | BusSelectToggle;
|
2024-07-22 01:41:17 +02:00
|
|
|
|
2024-08-06 02:51:59 +02:00
|
|
|
export interface BusGainChanged {
|
2024-07-22 01:41:17 +02:00
|
|
|
type: "StripGainChanged";
|
|
|
|
gain: number;
|
|
|
|
}
|
2024-08-06 02:51:59 +02:00
|
|
|
export interface BusMuted {
|
2024-07-22 01:41:17 +02:00
|
|
|
type: "StripMuted";
|
|
|
|
muted: boolean;
|
|
|
|
}
|
2024-08-06 02:51:59 +02:00
|
|
|
export interface BusSelectToggle {
|
|
|
|
type: "BusSelectToggle";
|
|
|
|
selected: boolean;
|
|
|
|
}
|
2024-07-22 01:41:17 +02:00
|
|
|
|
2024-08-06 02:51:59 +02:00
|
|
|
export class Bus extends React.Component<BusProps, BusState> {
|
2024-08-07 21:39:58 +02:00
|
|
|
private eventCounter = new EventCounter<"onSliderResetDefaults">();
|
2024-08-07 23:34:13 +02:00
|
|
|
private defaultValues: Omit<BusState, StateDefaultValueException> = {
|
2024-08-07 21:39:58 +02:00
|
|
|
gain: 0,
|
|
|
|
selected: false,
|
|
|
|
muted: false,
|
|
|
|
};
|
2024-08-06 02:51:59 +02:00
|
|
|
constructor(props: BusProps) {
|
2024-07-22 01:41:17 +02:00
|
|
|
super(props);
|
2024-08-07 23:34:13 +02:00
|
|
|
let { values: initialValues } = props;
|
2024-08-07 21:39:58 +02:00
|
|
|
const getValue = (name: keyof typeof this.defaultValues) => {
|
|
|
|
if (initialValues === undefined) return this.defaultValues[name];
|
2024-08-06 02:51:59 +02:00
|
|
|
return initialValues[name] !== undefined
|
2024-08-07 21:39:58 +02:00
|
|
|
? this.defaultValues[name]
|
2024-08-06 02:51:59 +02:00
|
|
|
: initialValues[name];
|
2024-07-22 01:41:17 +02:00
|
|
|
};
|
2024-08-07 23:34:13 +02:00
|
|
|
this.state = {
|
|
|
|
...Object.fromEntries(
|
|
|
|
Object.entries(this.defaultValues).map(([name]) => [
|
|
|
|
name,
|
|
|
|
getValue(name as keyof typeof this.defaultValues) as any,
|
|
|
|
])
|
|
|
|
),
|
|
|
|
showInputdB: props.showInputdB !== undefined ? props.showInputdB : true,
|
|
|
|
} as BusState;
|
2024-08-07 21:39:58 +02:00
|
|
|
this.eventCounter.on({
|
|
|
|
callback: this.onResetDefaults.bind(this),
|
|
|
|
count: 2,
|
|
|
|
name: "onSliderResetDefaults",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onResetDefaults() {
|
|
|
|
this.setState({ gain: this.defaultValues.gain });
|
2024-07-22 01:41:17 +02:00
|
|
|
}
|
|
|
|
onKeyDown(event: React.KeyboardEvent) {
|
2024-07-30 02:39:44 +02:00
|
|
|
console.log(event);
|
|
|
|
|
2024-07-22 01:41:17 +02:00
|
|
|
if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2024-08-07 21:39:58 +02:00
|
|
|
onGainChange(event: Event | React.ChangeEvent<HTMLInputElement>) {
|
2024-08-07 23:34:13 +02:00
|
|
|
let changedGain = parseFloat((event.target as HTMLInputElement).value);
|
|
|
|
if (changedGain > 12) changedGain = 12;
|
|
|
|
else if (changedGain < -60) changedGain = -60;
|
2024-07-22 01:41:17 +02:00
|
|
|
this.setState({
|
|
|
|
gain: changedGain,
|
|
|
|
});
|
|
|
|
this.props.onChange({
|
|
|
|
type: "StripGainChanged",
|
|
|
|
gain: changedGain,
|
|
|
|
});
|
|
|
|
}
|
2024-07-30 02:39:44 +02:00
|
|
|
onStripWheel(event: React.WheelEvent) {
|
|
|
|
console.log(event);
|
|
|
|
}
|
2024-08-07 21:39:58 +02:00
|
|
|
onSliderClick(event: React.MouseEvent) {
|
|
|
|
this.stopPropagation(event);
|
|
|
|
this.eventCounter.emit("onSliderResetDefaults");
|
|
|
|
}
|
|
|
|
onMuteToggle(event: React.MouseEvent) {
|
|
|
|
this.stopPropagation(event);
|
2024-07-22 01:41:17 +02:00
|
|
|
this.setState(
|
|
|
|
(state) => ({ muted: !state.muted }),
|
|
|
|
() => {
|
|
|
|
this.props.onChange({ type: "StripMuted", muted: this.state.muted });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-08-07 21:39:58 +02:00
|
|
|
onSelectToggle(event: React.MouseEvent) {
|
|
|
|
this.stopPropagation(event);
|
2024-08-06 02:51:59 +02:00
|
|
|
this.setState(
|
|
|
|
(state) => ({ selected: !state.selected }),
|
|
|
|
() => {
|
|
|
|
this.props.onChange({
|
|
|
|
type: "BusSelectToggle",
|
|
|
|
selected: this.state.selected,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-08-07 21:39:58 +02:00
|
|
|
stopPropagation(event: Event | React.MouseEvent) {
|
|
|
|
if (event instanceof Event) event.stopImmediatePropagation();
|
|
|
|
else event.nativeEvent.stopImmediatePropagation();
|
|
|
|
}
|
2024-07-22 01:41:17 +02:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<>
|
2024-08-07 23:34:13 +02:00
|
|
|
<Stack
|
|
|
|
alignItems={"center"}
|
|
|
|
direction={"row"}
|
|
|
|
spacing={0}
|
|
|
|
sx={{ width: "inherit" }}
|
|
|
|
>
|
|
|
|
{this.state.showInputdB && (
|
|
|
|
<TextField
|
|
|
|
// inputProps={{
|
|
|
|
// "aria-labelledby": "input-slider",
|
|
|
|
// itemType: "number",
|
|
|
|
// style:{padding: 0}
|
|
|
|
// }}
|
|
|
|
value={this.state.gain}
|
|
|
|
onWheel={(e) => this.onStripWheel(e)}
|
|
|
|
onClick={(e) => this.stopPropagation(e)}
|
|
|
|
onChange={(e) => this.onGainChange(e as unknown as Event)}
|
|
|
|
size="small"
|
|
|
|
label={this.props.name}
|
|
|
|
type="number"
|
|
|
|
variant="outlined"
|
|
|
|
sx={{ minWidth: "100px", maxWidth: "100px" }}
|
|
|
|
/>
|
|
|
|
)}
|
2024-08-06 02:51:59 +02:00
|
|
|
<Slider
|
|
|
|
sx={{
|
|
|
|
margin: "5px",
|
|
|
|
marginInline: "15px",
|
|
|
|
}}
|
|
|
|
// orientation="vertical"
|
2024-08-07 21:39:58 +02:00
|
|
|
color={
|
|
|
|
(this.state.gain > 0 && this.state.gain < 12 && "warning") ||
|
|
|
|
(this.state.gain >= 12 && "error") ||
|
|
|
|
(this.state.gain < 0 && "info") ||
|
|
|
|
"primary"
|
|
|
|
}
|
2024-08-06 02:51:59 +02:00
|
|
|
defaultValue={0.0}
|
|
|
|
step={0.1}
|
|
|
|
min={-60}
|
|
|
|
max={12}
|
|
|
|
value={this.state.gain}
|
2024-08-07 21:39:58 +02:00
|
|
|
onChange={(e) => this.onGainChange(e)}
|
|
|
|
onClick={(e) => this.onSliderClick(e)}
|
2024-08-06 02:51:59 +02:00
|
|
|
/>
|
2024-07-22 01:41:17 +02:00
|
|
|
|
2024-08-06 02:51:59 +02:00
|
|
|
<ButtonGroup>
|
|
|
|
<Button
|
2024-08-07 21:39:58 +02:00
|
|
|
onClick={(e) => this.onMuteToggle(e)}
|
2024-08-06 02:51:59 +02:00
|
|
|
variant={this.state.muted ? "contained" : "outlined"}
|
|
|
|
color={"error"}
|
|
|
|
>
|
|
|
|
Mute
|
|
|
|
</Button>
|
|
|
|
<Button
|
2024-08-07 21:39:58 +02:00
|
|
|
onClick={(e) => this.onSelectToggle(e)}
|
2024-08-06 02:51:59 +02:00
|
|
|
sx={{}}
|
|
|
|
variant={this.state.selected ? "contained" : "outlined"}
|
|
|
|
color={"warning"}
|
|
|
|
>
|
|
|
|
S
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
</Stack>
|
|
|
|
{/* <Stack>
|
2024-07-22 01:41:17 +02:00
|
|
|
<ButtonGroup orientation="vertical">
|
|
|
|
{[
|
|
|
|
...Array(this.props.physicalBuses + this.props.virtualBuses),
|
|
|
|
].map((_v, i) => (
|
|
|
|
<StripBusOutput
|
|
|
|
key={`${i < this.props.physicalBuses ? "a" : "b"}${
|
|
|
|
i < this.props.physicalBuses
|
|
|
|
? i
|
|
|
|
: i - this.props.physicalBuses
|
|
|
|
}`}
|
|
|
|
id={
|
|
|
|
i < this.props.physicalBuses
|
|
|
|
? i
|
|
|
|
: i - this.props.physicalBuses
|
|
|
|
}
|
|
|
|
isVirtual={i < this.props.physicalBuses ? false : true}
|
|
|
|
onChange={(ev) => this.onBusChange(ev)}
|
|
|
|
default={{ enabled: this.state.buses[i] }}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ButtonGroup>
|
2024-07-30 02:39:44 +02:00
|
|
|
</Stack> */}
|
2024-07-22 01:41:17 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|