From d13f2873d53f615b2d497ab1e6d0fc12159a804a Mon Sep 17 00:00:00 2001 From: julian laplace Date: Tue, 9 May 2023 19:26:47 +0200 Subject: add controls --- src/ui/App.jsx | 10 +-- src/ui/Controls.jsx | 171 +++++++++++++++++++++++++++++++++++++++++++++++ src/ui/TooltipSlider.tsx | 95 ++++++++++++++++++++++++++ 3 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 src/ui/Controls.jsx create mode 100644 src/ui/TooltipSlider.tsx (limited to 'src/ui') diff --git a/src/ui/App.jsx b/src/ui/App.jsx index eaca9a4..fbbbd35 100644 --- a/src/ui/App.jsx +++ b/src/ui/App.jsx @@ -7,6 +7,7 @@ import * as React from "react"; import { useState, useEffect, useRef } from "react"; import Relabi from "../relabi"; import { Kalimba, Drums } from "../lib/instruments"; +import Controls from "./Controls.jsx"; export default function App() { const [relabi, setRelabi] = useState(); @@ -19,10 +20,10 @@ export default function App() { if (!relabi) { const relabiGenerator = new Relabi({ waves: [ - { type: "sine", frequency: 0.75 }, - { type: "sine", frequency: 1.0 }, - { type: "sine", frequency: 1.617 }, - { type: "sine", frequency: 3.141 }, + { shape: "sine", frequency: 0.75 }, + { shape: "sine", frequency: 1.0 }, + { shape: "sine", frequency: 1.617 }, + { shape: "sine", frequency: 3.141 }, ], bounds: [ { @@ -86,6 +87,7 @@ export default function App() { }} >
+
); } diff --git a/src/ui/Controls.jsx b/src/ui/Controls.jsx new file mode 100644 index 0000000..f999e4f --- /dev/null +++ b/src/ui/Controls.jsx @@ -0,0 +1,171 @@ +/** + * Relabi controls + * @module src/ui/Controls.jsx; + */ + +import * as React from "react"; +import { useCallback, useRef } from "react"; + +import Slider from "rc-slider"; +import TooltipSlider, { handleRender } from "./TooltipSlider.tsx"; +import "rc-slider/assets/index.css"; + +const WAVE_SHAPE_NAMES = ["sine", "triangle", "saw", "square"]; + +export default function Controls({ relabi }) { + /** + * Handle updating a slider + */ + const onChange = useCallback( + (type, index) => (value) => { + if (type === "bound") { + relabi.bounds[index].level = value; + } + if (type === "frequency") { + relabi.waves[index].frequency = Math.exp(value); + } + if (type === "shape") { + relabi.waves[index].shape = WAVE_SHAPE_NAMES[value]; + } + }, + [relabi] + ); + + return ( + + + + + {relabi?.bounds.map((bound, index) => ( + + ))} + + + + + + + {relabi?.waves.map((wave, index) => ( + + ))} + + + + + + + {relabi?.waves.map((wave, index) => ( + WAVE_SHAPE_NAMES[value]} + /> + ))} + + + + ); +} + +/** + * UI Elements + */ +const ControlRow = ({ children }) => ( +
+ {children} +
+); + +const ControlBox = ({ children }) => ( +
+ {children} +
+); + +const SliderRow = ({ children }) => ( +
+ {children} +
+); + +const Label = ({ children }) => ( +
{children}
+); + +const ControlSlider = ({ + type, + index, + min, + max, + step, + reverse, + defaultValue, + color, + onChange, + tipFormatter, +}) => ( +
+ value)} + /> +
+); diff --git a/src/ui/TooltipSlider.tsx b/src/ui/TooltipSlider.tsx new file mode 100644 index 0000000..0bb8350 --- /dev/null +++ b/src/ui/TooltipSlider.tsx @@ -0,0 +1,95 @@ +/** + * rc-tooltip tooltip slider + */ + +import * as React from "react"; +import "rc-tooltip/assets/bootstrap.css"; +import Slider from "rc-slider"; +import type { SliderProps } from "rc-slider"; +import raf from "rc-util/lib/raf"; +import Tooltip from "rc-tooltip"; + +const HandleTooltip = (props: { + value: number; + children: React.ReactElement; + visible: boolean; + tipFormatter?: (value: number) => React.ReactNode; +}) => { + const { + value, + children, + visible, + tipFormatter = (val) => `${val} %`, + ...restProps + } = props; + + const tooltipRef = React.useRef(); + const rafRef = React.useRef(null); + + function cancelKeepAlign() { + raf.cancel(rafRef.current!); + } + + function keepAlign() { + rafRef.current = raf(() => { + // tooltipRef.current?.forcePopupAlign(); + }); + } + + React.useEffect(() => { + if (visible) { + keepAlign(); + } else { + cancelKeepAlign(); + } + + return cancelKeepAlign; + }, [value, visible]); + + return ( + + {children} + + ); +}; + +export const handleRender: SliderProps["handleRender"] = (node, props) => { + return ( + + {node} + + ); +}; + +const TooltipSlider = ({ + tipFormatter, + tipProps, + ...props +}: SliderProps & { + tipFormatter?: (value: number) => React.ReactNode; + tipProps: any; +}) => { + const tipHandleRender: SliderProps["handleRender"] = (node, handleProps) => { + return ( + + {node} + + ); + }; + + return ; +}; + +export default TooltipSlider; -- cgit v1.2.3-70-g09d2