summaryrefslogtreecommitdiff
path: root/src/ui/Controls.jsx
blob: f999e4fc7946e8c813b13e60037fa7ef864393be (plain)
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
164
165
166
167
168
169
170
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 (
    <ControlRow>
      <ControlBox>
        <Label>Bounds</Label>
        <SliderRow>
          {relabi?.bounds.map((bound, index) => (
            <ControlSlider
              rel={`bound_${index}`}
              type="bound"
              index={index}
              onChange={onChange}
              min={-1}
              max={1}
              step={0.01}
              defaultValue={bound.level}
              color={bound.color}
              reverse
            />
          ))}
        </SliderRow>
      </ControlBox>

      <ControlBox>
        <Label>Wave speeds</Label>
        <SliderRow>
          {relabi?.waves.map((wave, index) => (
            <ControlSlider
              rel={`frequency_${index}`}
              type="frequency"
              index={index}
              onChange={onChange}
              min={-2}
              max={3}
              step={0.001}
              defaultValue={Math.log(wave.frequency)}
              color={"#8a8"}
            />
          ))}
        </SliderRow>
      </ControlBox>

      <ControlBox>
        <Label>Wave shapes</Label>
        <SliderRow>
          {relabi?.waves.map((wave, index) => (
            <ControlSlider
              rel={`shape_${index}`}
              type="shape"
              index={index}
              onChange={onChange}
              min={0}
              max={3}
              step={1}
              defaultValue={WAVE_SHAPE_NAMES.indexOf(wave.shape)}
              color={"#998"}
              tipFormatter={(value) => WAVE_SHAPE_NAMES[value]}
            />
          ))}
        </SliderRow>
      </ControlBox>
    </ControlRow>
  );
}

/**
 * UI Elements
 */
const ControlRow = ({ children }) => (
  <div
    style={{
      display: "flex",
      flexDirection: "row",
      marginTop: "1rem",
    }}
  >
    {children}
  </div>
);

const ControlBox = ({ children }) => (
  <div
    style={{
      marginLeft: "1rem",
    }}
  >
    {children}
  </div>
);

const SliderRow = ({ children }) => (
  <div
    style={{
      display: "flex",
      flexDireciton: "row",
      height: "10rem",
      marginTop: "0.5rem",
    }}
  >
    {children}
  </div>
);

const Label = ({ children }) => (
  <div style={{ fontSize: "0.75rem" }}>{children}</div>
);

const ControlSlider = ({
  type,
  index,
  min,
  max,
  step,
  reverse,
  defaultValue,
  color,
  onChange,
  tipFormatter,
}) => (
  <div style={{ marginRight: "0.5rem" }}>
    <TooltipSlider
      vertical
      min={min}
      max={max}
      step={step}
      reverse={reverse}
      defaultValue={defaultValue}
      onChange={onChange(type, index)}
      handleStyle={{
        backgroundColor: color,
        borderColor: color,
        opacity: 1,
      }}
      trackStyle={{ backgroundColor: "#888", width: "2px" }}
      railStyle={{ backgroundColor: "#888", width: "2px" }}
      tipFormatter={tipFormatter || ((value) => value)}
    />
  </div>
);