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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/**
* 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",
"reverse_saw",
"square",
"noise",
];
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 === "shape") {
relabi.waves[index].shape = WAVE_SHAPE_NAMES[value];
}
if (type === "frequency") {
relabi.waves[index].frequency = Math.exp(value);
}
if (type === "weight") {
relabi.waves[index].weight = value;
}
if (type === "settings") {
relabi.settings[index] = index === "speed" ? Math.exp(value) : value;
}
},
[relabi]
);
if (!relabi) {
return null;
}
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}
tipFormatter={(value) => -value}
defaultValue={bound.level}
color={bound.color}
reverse
/>
))}
</SliderRow>
</ControlBox>
<ControlBox>
<Label>Wave frequencies</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)}
tipFormatter={(value) => Math.exp(value).toFixed(1)}
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={WAVE_SHAPE_NAMES.length - 1}
step={1}
defaultValue={WAVE_SHAPE_NAMES.indexOf(wave.shape)}
color={"#aa8"}
tipFormatter={(value) => WAVE_SHAPE_NAMES[value]}
/>
))}
</SliderRow>
</ControlBox>
<ControlBox>
<Label>Wave weights</Label>
<SliderRow>
{relabi?.waves.map((wave, index) => (
<ControlSlider
rel={`weight_${index}`}
type="weight"
index={index}
onChange={onChange}
min={0}
max={2}
step={0.01}
defaultValue={wave.weight}
color={"#99b"}
/>
))}
</SliderRow>
</ControlBox>
<ControlBox>
<Label>Speed</Label>
<SliderRow>
<ControlSlider
type="settings"
index="speed"
onChange={onChange}
min={-3}
max={3}
step={0.01}
defaultValue={Math.log(relabi.settings.speed)}
tipFormatter={(value) => Math.exp(value).toFixed(1)}
color={"#a88"}
/>
</SliderRow>
</ControlBox>
<ControlBox>
<Label>Chaos</Label>
<SliderRow>
<ControlSlider
type="settings"
index="chaos"
onChange={onChange}
min={0}
max={0.5}
step={0.01}
defaultValue={relabi.settings.chaos}
color={"#a88"}
/>
</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>
);
|