All files / src/components/music/sidebar InstrumentDutyEditor.tsx

0% Statements 0/51
0% Branches 0/52
0% Functions 0/11
0% Lines 0/50

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                                                                                                                                                                                                                                                                                                                                     
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import trackerDocumentActions from "store/features/trackerDocument/trackerDocumentActions";
import { DutyInstrument } from "shared/lib/uge/types";
import { FormDivider, FormField, FormRow } from "ui/form/layout/FormLayout";
import l10n from "shared/lib/lang/l10n";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { Alert, AlertItem } from "ui/alerts/Alert";
import { InstrumentEnvelopeEditor } from "components/music/sidebar/InstrumentEnvelopeEditor";
import { InstrumentEnvelopePreview } from "components/music/sidebar/InstrumentEnvelopePreview";
import { Slider } from "ui/form/Slider";
import { FlexGrow } from "ui/spacing/Spacing";
import { DutyCycleSelect } from "components/music/form/DutyCycleSelect";
import { SweepTimeSelect } from "components/music/form/SweepTimeSelect";
 
interface InstrumentDutyEditorProps {
  id: string;
  instrument?: DutyInstrument;
}
 
export const InstrumentDutyEditor = ({
  instrument,
}: InstrumentDutyEditorProps) => {
  const dispatch = useAppDispatch();
  const selectedChannel = useAppSelector(
    (state) => state.tracker.selectedChannel,
  );
 
  const instrumentId = instrument?.index;
 
  const lastSweepTimeRef = useRef(instrument?.frequencySweepTime || 4);
  useEffect(() => {
    const newSweepTime = instrument?.frequencySweepTime;
    Iif (typeof newSweepTime === "number" && newSweepTime !== 0) {
      lastSweepTimeRef.current = newSweepTime;
    }
  }, [instrument?.frequencySweepTime]);
 
  const onChangeField = useCallback(
    <T extends keyof DutyInstrument>(key: T) =>
      (editValue: DutyInstrument[T]) => {
        Iif (instrumentId === undefined) {
          return;
        }
        dispatch(
          trackerDocumentActions.editDutyInstrument({
            instrumentId,
            changes: {
              [key]: editValue,
            },
          }),
        );
      },
    [dispatch, instrumentId],
  );
 
  const onChangeEnvelopeLength = useMemo(
    () => onChangeField("length"),
    [onChangeField],
  );
 
  const onChangeEnvelopeVolume = useMemo(
    () => onChangeField("initialVolume"),
    [onChangeField],
  );
 
  const onChangeEnvelopeSweep = useMemo(
    () => onChangeField("volumeSweepChange"),
    [onChangeField],
  );
 
  const onChangeDutyCycle = useMemo(
    () => onChangeField("dutyCycle"),
    [onChangeField],
  );
 
  const onChangeSweepShift = useCallback(
    (value: number) => {
      if (value === 0) {
        onChangeField("frequencySweepTime")(0);
      } else Iif (Number(instrument?.frequencySweepTime) === 0) {
        onChangeField("frequencySweepTime")(lastSweepTimeRef.current);
      }
      onChangeField("frequencySweepShift")(value);
    },
    [instrument?.frequencySweepTime, onChangeField],
  );
 
  const onChangeSweepTime = useCallback(
    (value: number) => {
      if (value !== 0 && Number(instrument?.frequencySweepShift) === 0) {
        onChangeField("frequencySweepShift")(7);
      } else Iif (value === 0) {
        onChangeField("frequencySweepShift")(0);
      }
      onChangeField("frequencySweepTime")(value);
    },
    [instrument?.frequencySweepShift, onChangeField],
  );
 
  Iif (!instrument) {
    return null;
  }
 
  return (
    <>
      <InstrumentEnvelopeEditor
        volume={instrument.initialVolume}
        sweep={instrument.volumeSweepChange}
        length={instrument.length}
        onChangeVolume={onChangeEnvelopeVolume}
        onChangeSweep={onChangeEnvelopeSweep}
        onChangeLength={onChangeEnvelopeLength}
      />
      <FormRow>
        <InstrumentEnvelopePreview
          volume={instrument.initialVolume}
          sweep={instrument.volumeSweepChange}
          length={instrument.length}
        />
      </FormRow>
      <FormDivider />
      <FormRow>
        <FormField name="dutyCycle" label={l10n("FIELD_DUTY_CYCLE")}>
          <DutyCycleSelect
            name="dutyCycle"
            value={instrument.dutyCycle}
            onChange={onChangeDutyCycle}
            menuPlacement="top"
          />
        </FormField>
      </FormRow>
 
      <FormDivider />
      <FormRow>
        <FormField name="frequencySweepTime" label={l10n("FIELD_SWEEP_SHIFT")}>
          <Slider
            value={instrument.frequencySweepShift || 0}
            min={-7}
            max={7}
            onChange={onChangeSweepShift}
          />
        </FormField>
        <FormField name="frequencySweepTime" label={l10n("FIELD_SWEEP_TIME")}>
          <SweepTimeSelect
            name={"frequencySweepTime"}
            value={instrument.frequencySweepTime}
            onChange={onChangeSweepTime}
            menuPlacement="top"
          />
        </FormField>
      </FormRow>
      {Number(instrument.frequencySweepTime) !== 0 && selectedChannel === 1 && (
        <FormRow>
          <Alert variant="info">
            <AlertItem>{l10n("MESSAGE_SWEEP_ONLY_DUTY1")}</AlertItem>
          </Alert>
        </FormRow>
      )}
      <FlexGrow />
    </>
  );
};