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 | import React, { useContext, useEffect, useRef } from "react"; import l10n from "shared/lib/lang/l10n"; import { ThemeContext } from "styled-components"; import { FormRow } from "ui/form/layout/FormLayout"; import { SliderField } from "ui/form/SliderField"; type EditableInstrument = { initial_volume: number; volume_sweep_change: number; }; interface InstrumentVolumeEditorProps { initialVolume: number; volumeSweepChange: number; length: number | null; onChange: <T extends keyof EditableInstrument>( key: T ) => (editValue: EditableInstrument[T]) => void; } export const InstrumentVolumeEditor = ({ initialVolume, volumeSweepChange, length, onChange, }: InstrumentVolumeEditorProps) => { const canvasRef = useRef<HTMLCanvasElement>(null); const themeContext = useContext(ThemeContext); useEffect(() => { const canvas = canvasRef.current; Iif (!canvas) { return; } Iif (!themeContext) { return; } const drawWidth = canvas.width - 10; const drawHeight = canvas.height - 10; const ctx = canvas.getContext("2d"); const normalisedVolume = initialVolume / 15; const secLength = length === null ? 1 : length / 256; const defaultColor = themeContext.colors.highlight; // eslint-disable-next-line no-self-assign canvas.width = canvas.width; // eslint-disable-next-line no-self-assign canvas.height = canvas.height; Iif (ctx) { ctx.strokeStyle = defaultColor; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(5, canvas.height - 5 - normalisedVolume * drawHeight); let localVolumeSweepChange; if (volumeSweepChange < 0) { //fade down localVolumeSweepChange = volumeSweepChange + 8; const envLength = ((localVolumeSweepChange / 64) * initialVolume) / 2; ctx.lineTo( 5 + Math.min(envLength, secLength) * drawWidth, drawHeight + 5 - (1 - Math.min(secLength / envLength, 1)) * normalisedVolume * drawHeight ); ctx.lineTo(5 + secLength * drawWidth, canvas.height - 5); } else if (volumeSweepChange > 0) { //fade up localVolumeSweepChange = 8 - volumeSweepChange; const envLength = ((volumeSweepChange / 64) * (15 - initialVolume)) / 2; ctx.lineTo( 5 + Math.min(envLength, secLength) * drawWidth, (1 - Math.min(secLength / envLength, 1)) * normalisedVolume * drawHeight + 5 ); ctx.lineTo( 5 + secLength * drawWidth, (1 - Math.min(secLength / envLength, 1)) * normalisedVolume * drawHeight + 5 ); } else { //no fade ctx.lineTo( 5 + secLength * drawWidth, canvas.height - 5 - normalisedVolume * drawHeight ); } Iif (secLength !== 1) { ctx.lineTo(5 + secLength * drawWidth, canvas.height - 5); Iif (secLength < 1) { ctx.lineTo(5 + drawWidth, canvas.height - 5); } } ctx.stroke(); } }, [initialVolume, length, themeContext, volumeSweepChange]); return ( <> <FormRow> <SliderField name="initial_volume" label={l10n("FIELD_INITIAL_VOLUME")} value={initialVolume || 0} min={0} max={15} onChange={(value) => { onChange("initial_volume")(value || 0); }} /> </FormRow> <FormRow> <SliderField name="volume_sweep_change" label={l10n("FIELD_VOLUME_SWEEP_CHANGE")} value={volumeSweepChange || 0} min={-7} max={7} onChange={(value) => { onChange("volume_sweep_change")(value || 0); }} /> </FormRow> <FormRow> <canvas ref={canvasRef} style={{ width: "100%", height: "100px", backgroundColor: "#000", borderRadius: 4, }} height={100} /> </FormRow> </> ); }; |