2 #include <Process/Dataflow/Port.hpp>
4 #include <score/tools/Debug.hpp>
6 #include <ossia/detail/math.hpp>
7 #include <ossia/network/domain/domain.hpp>
9 #include <QDoubleSpinBox>
12 #include <type_traits>
14 namespace WidgetFactory
18 static constexpr
auto getMin(
auto& slider) noexcept
20 if constexpr(requires { slider.getMin(); })
21 return slider.getMin();
23 return slider.domain().get().template convert_min<T>();
27 static constexpr
auto getMax(
auto& slider) noexcept
29 if constexpr(requires { slider.getMax(); })
30 return slider.getMax();
32 return slider.domain().get().template convert_max<T>();
36 static constexpr
auto getInit(
auto& slider) noexcept
38 if constexpr(requires { slider.getInit(); })
39 return slider.getInit();
40 else if constexpr(requires { slider.init(); })
41 return ossia::convert<T>(slider.init());
43 return getMin<T>(slider);
48 static constexpr
double to01(
double min,
double range,
double val) noexcept
50 return (val - min) /
range;
53 static constexpr
double from01(
double min,
double range,
double val) noexcept
55 return min + val *
range;
59 static double to01(
const T& slider,
double val) noexcept
61 auto min = getMin<double>(slider);
62 auto max = getMax<double>(slider);
65 return to01(min, max - min, val);
69 static double from01(
const T& slider,
double val) noexcept
71 auto min = getMin<double>(slider);
72 auto max = getMax<double>(slider);
75 return from01(min, max - min, val);
79 static ossia::vec2f to01(
const T& slider, ossia::vec2f val) noexcept
82 const auto min = getMin<ossia::vec2f>(slider);
83 const auto max = getMax<ossia::vec2f>(slider);
84 res[0] = to01(min[0], max[0] - min[0], val[0]);
85 res[1] = to01(min[1], max[1] - min[1], val[1]);
90 static ossia::vec2f from01(
const T& slider, ossia::vec2f val) noexcept
93 const auto min = getMin<ossia::vec2f>(slider);
94 const auto max = getMax<ossia::vec2f>(slider);
95 res[0] = from01(min[0], max[0] - min[0], val[0]);
96 res[1] = from01(min[1], max[1] - min[1], val[1]);
100 template <
typename T>
101 static ossia::vec3f to01(
const T& slider, ossia::vec3f val) noexcept
104 const auto min = getMin<ossia::vec3f>(slider);
105 const auto max = getMax<ossia::vec3f>(slider);
106 res[0] = to01(min[0], max[0] - min[0], val[0]);
107 res[1] = to01(min[1], max[1] - min[1], val[1]);
108 res[2] = to01(min[2], max[2] - min[2], val[2]);
112 template <
typename T>
113 static ossia::vec3f from01(
const T& slider, ossia::vec3f val) noexcept
116 const auto min = getMin<ossia::vec3f>(slider);
117 const auto max = getMax<ossia::vec3f>(slider);
118 res[0] = from01(min[0], max[0] - min[0], val[0]);
119 res[1] = from01(min[1], max[1] - min[1], val[1]);
120 res[2] = from01(min[2], max[2] - min[2], val[2]);
124 template <
typename T>
125 static ossia::vec4f to01(
const T& slider, ossia::vec4f val) noexcept
128 const auto min = getMin<ossia::vec4f>(slider);
129 const auto max = getMax<ossia::vec4f>(slider);
130 res[0] = to01(min[0], max[0] - min[0], val[0]);
131 res[1] = to01(min[1], max[1] - min[1], val[1]);
132 res[2] = to01(min[2], max[2] - min[2], val[2]);
133 res[3] = to01(min[3], max[3] - min[3], val[3]);
137 template <
typename T>
138 static ossia::vec4f from01(
const T& slider, ossia::vec4f val) noexcept
141 const auto min = getMin<ossia::vec4f>(slider);
142 const auto max = getMax<ossia::vec4f>(slider);
143 res[0] = from01(min[0], max[0] - min[0], val[0]);
144 res[1] = from01(min[1], max[1] - min[1], val[1]);
145 res[2] = from01(min[2], max[2] - min[2], val[2]);
146 res[3] = from01(min[3], max[3] - min[3], val[3]);
153 static double to01(
double min,
double range,
double val) noexcept
155 return ossia::log_to_normalized(min,
range, val);
158 static double from01(
double min,
double range,
double val) noexcept
160 return ossia::normalized_to_log(min,
range, val);
163 template <
typename T>
164 static double to01(
const T& slider,
double val) noexcept
166 auto min = getMin<double>(slider);
167 auto max = getMax<double>(slider);
170 return to01(min, max - min, val);
173 template <
typename T>
174 static double from01(
const T& slider,
double val) noexcept
176 auto min = getMin<double>(slider);
177 auto max = getMax<double>(slider);
180 return from01(min, max - min, val);
184 template <
typename Norm_T>
189 template <
typename T>
192 min = getMin<double>(slider);
193 max = getMax<double>(slider);
198 constexpr
double to01(
double val)
const noexcept
200 return Norm_T::to01(min, max - min, val);
203 constexpr
double from01(
double val)
const noexcept
205 return Norm_T::from01(min, max - min, val);
209 template <
typename Norm_T,
typename Sl
ider_T>
212 const Slider_T& slider;
219 constexpr
double to01(
double val)
const noexcept {
return Norm_T::to01(slider, val); }
221 constexpr
double from01(
double val)
const noexcept
223 return Norm_T::from01(slider, val);
227 template <
typename Control_T,
typename W
idget_T>
228 static void initWidgetProperties(Control_T& inlet, Widget_T& widget)
230 if constexpr(requires { widget.setNoValueChangeOnMove(
true); })
231 widget.setNoValueChangeOnMove(inlet.noValueChangeOnMove);
234 template <
typename W
idget_T>
235 static void setRange(Widget_T& widget,
auto&& min,
auto&& max,
auto&& init)
237 widget.setRange(min, max, init);
240 static inline void setRange(QSpinBox& widget,
auto&& min,
auto&& max,
auto&& init)
242 widget.setRange(min, max);
245 static inline void setRange(QDoubleSpinBox& widget,
auto&& min,
auto&& max,
auto&& init)
247 widget.setRange(min, max);
250 template <
typename T,
typename Control_T,
typename W
idget_T>
251 static void bindFloatDomain(
const T& slider, Control_T& inlet, Widget_T& widget)
253 auto min = getMin<float>(slider);
254 auto max = getMax<float>(slider);
258 setRange(widget, min, max, getInit<float>(slider));
260 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
262 SCORE_ASSERT(&slider == &inlet);
263 QObject::connect(&inlet, &Control_T::domainChanged, &widget, [&slider, &widget] {
264 auto min = getMin<float>(slider);
265 auto max = getMax<float>(slider);
269 setRange(widget, min, max, getInit<float>(slider));
274 template <
typename T,
typename Control_T,
typename W
idget_T>
275 static void bindIntDomain(
const T& slider, Control_T& inlet, Widget_T& widget)
277 auto min = getMin<int>(slider);
278 auto max = getMax<int>(slider);
282 setRange(widget, min, max, getInit<int>(slider));
284 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
286 SCORE_ASSERT(&slider == &inlet);
287 QObject::connect(&inlet, &Control_T::domainChanged, &widget, [&slider, &widget] {
288 auto min = getMin<int>(slider);
289 auto max = getMax<int>(slider);
293 setRange(widget, min, max, getInit<int>(slider));
298 template <
typename T,
typename Control_T,
typename W
idget_T>
299 static void bindVec2Domain(
const T& slider, Control_T& inlet, Widget_T& widget)
301 auto update_range = [&widget, &inlet, &slider] {
302 auto min = ossia::get_min(inlet.domain());
303 auto max = ossia::get_max(inlet.domain());
304 auto min_float = min.template target<float>();
305 auto max_float = max.template target<float>();
306 if(min_float && max_float)
308 if(*max_float - *min_float == 0)
309 *max_float = *min_float + 1;
311 {*min_float, *min_float}, {*max_float, *max_float}, {*min_float, *min_float});
315 auto min_vec2 = min.template target<ossia::vec2f>();
316 auto max_vec2 = max.template target<ossia::vec2f>();
317 if(min_vec2 && max_vec2)
319 auto& min = *min_vec2;
320 auto& max = *max_vec2;
321 for(std::size_t i = 0; i < min.size(); i++)
323 if(max[i] - min[i] == 0)
327 auto init_vec2 = getInit<ossia::vec2f>(slider);
328 widget.setRange(min, max, init_vec2);
335 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
337 SCORE_ASSERT(&slider == &inlet);
338 QObject::connect(&inlet, &Control_T::domainChanged, &widget, update_range);
342 template <
typename T,
typename Control_T,
typename W
idget_T>
343 static void bindVec3Domain(
const T& slider, Control_T& inlet, Widget_T& widget)
345 auto update_range = [&widget, &inlet, &slider] {
346 auto min = ossia::get_min(inlet.domain());
347 auto max = ossia::get_max(inlet.domain());
348 auto min_float = min.template target<float>();
349 auto max_float = max.template target<float>();
350 if(min_float && max_float)
352 if(*max_float - *min_float == 0)
353 *max_float = *min_float + 1;
355 {*min_float, *min_float, *min_float}, {*max_float, *max_float, *max_float});
359 auto min_vec3 = min.template target<ossia::vec3f>();
360 auto max_vec3 = max.template target<ossia::vec3f>();
361 if(min_vec3 && max_vec3)
363 auto& min = *min_vec3;
364 auto& max = *max_vec3;
365 for(std::size_t i = 0; i < min.size(); i++)
367 if(max[i] - min[i] == 0)
371 auto init_vec3 = getInit<ossia::vec3f>(slider);
372 widget.setRange(min, max, init_vec3);
379 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
381 SCORE_ASSERT(&slider == &inlet);
382 QObject::connect(&inlet, &Control_T::domainChanged, &widget, update_range);
Definition: LFO_v2.hpp:68