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>
14namespace WidgetFactory
18static 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>();
27static 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>();
36static 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 std::array<double, 2> to01(
const T& slider, ossia::vec2f val)
noexcept
81 std::array<double, 2> res;
82 const auto min = getMin<ossia::vec2f>(slider);
83 const auto max = getMax<ossia::vec2f>(slider);
84 res[0] = to01(min[0],
double(max[0]) -
double(min[0]), val[0]);
85 res[1] = to01(min[1],
double(max[1]) -
double(min[1]), val[1]);
90 static ossia::vec2f from01(
const T& slider, std::array<double, 2> val)
noexcept
93 const auto min = getMin<ossia::vec2f>(slider);
94 const auto max = getMax<ossia::vec2f>(slider);
95 res[0] = from01(min[0],
double(max[0]) -
double(min[0]), val[0]);
96 res[1] = from01(min[1],
double(max[1]) -
double(min[1]), val[1]);
100 template <
typename T>
101 static std::array<double, 3> to01(
const T& slider, ossia::vec3f val)
noexcept
103 std::array<double, 3> res;
104 const auto min = getMin<ossia::vec3f>(slider);
105 const auto max = getMax<ossia::vec3f>(slider);
106 res[0] = to01(min[0],
double(max[0]) -
double(min[0]), val[0]);
107 res[1] = to01(min[1],
double(max[1]) -
double(min[1]), val[1]);
108 res[2] = to01(min[2],
double(max[2]) -
double(min[2]), val[2]);
112 template <
typename T>
113 static ossia::vec3f from01(
const T& slider, std::array<double, 3> val)
noexcept
116 const auto min = getMin<ossia::vec3f>(slider);
117 const auto max = getMax<ossia::vec3f>(slider);
118 res[0] = from01(min[0],
double(max[0]) -
double(min[0]), val[0]);
119 res[1] = from01(min[1],
double(max[1]) -
double(min[1]), val[1]);
120 res[2] = from01(min[2],
double(max[2]) -
double(min[2]), val[2]);
124 template <
typename T>
125 static std::array<double, 4> to01(
const T& slider, ossia::vec4f val)
noexcept
127 std::array<double, 4> res;
128 const auto min = getMin<ossia::vec4f>(slider);
129 const auto max = getMax<ossia::vec4f>(slider);
130 res[0] = to01(min[0],
double(max[0]) -
double(min[0]), val[0]);
131 res[1] = to01(min[1],
double(max[1]) -
double(min[1]), val[1]);
132 res[2] = to01(min[2],
double(max[2]) -
double(min[2]), val[2]);
133 res[3] = to01(min[3],
double(max[3]) -
double(min[3]), val[3]);
137 template <
typename T>
138 static ossia::vec4f from01(
const T& slider, std::array<double, 4> val)
noexcept
141 const auto min = getMin<ossia::vec4f>(slider);
142 const auto max = getMax<ossia::vec4f>(slider);
143 res[0] = from01(min[0],
double(max[0]) -
double(min[0]), val[0]);
144 res[1] = from01(min[1],
double(max[1]) -
double(min[1]), val[1]);
145 res[2] = from01(min[2],
double(max[2]) -
double(min[2]), val[2]);
146 res[3] = from01(min[3],
double(max[3]) -
double(min[3]), val[3]);
150 template <
typename T, std::
size_t N>
151 static std::array<float, N> from01(
const T& slider, std::array<double, N> val)
noexcept
153 std::array<float, N> res;
154 const auto min = getMin<std::array<double, N>>(slider);
155 const auto max = getMax<std::array<double, N>>(slider);
156 for(std::size_t i = 0; i < N; i++)
157 res[i] = from01(min[i],
double(max[i]) - double(min[i]), val[i]);
164 static double to01(
double min,
double range,
double val)
noexcept
166 return ossia::log_to_normalized(min,
range, val);
169 static double from01(
double min,
double range,
double val)
noexcept
171 return ossia::normalized_to_log(min,
range, val);
174 template <
typename T>
175 static double to01(
const T& slider,
double val)
noexcept
177 auto min = getMin<double>(slider);
178 auto max = getMax<double>(slider);
181 return to01(min, max - min, val);
184 template <
typename T>
185 static double from01(
const T& slider,
double val)
noexcept
187 auto min = getMin<double>(slider);
188 auto max = getMax<double>(slider);
191 return from01(min, max - min, val);
198template <
typename Mapper>
201 static double to01(
double min,
double range,
double val)
noexcept
203 return Mapper::map(ossia::clamp((val - min) /
range, 0., 1.));
206 static double from01(
double min,
double range,
double val)
noexcept
208 return min + Mapper::unmap(ossia::clamp(val, 0., 1.)) *
range;
211 template <
typename T>
212 static double to01(
const T& slider,
double val)
noexcept
214 auto min = getMin<double>(slider);
215 auto max = getMax<double>(slider);
218 return to01(min, max - min, val);
221 template <
typename T>
222 static double from01(
const T& slider,
double val)
noexcept
224 auto min = getMin<double>(slider);
225 auto max = getMax<double>(slider);
228 return from01(min, max - min, val);
232template <
typename Norm_T>
237 template <
typename T>
240 min = getMin<double>(slider);
241 max = getMax<double>(slider);
246 constexpr double to01(
double val)
const noexcept
248 return Norm_T::to01(min, max - min, val);
251 constexpr double from01(
double val)
const noexcept
253 return Norm_T::from01(min, max - min, val);
257template <
typename Norm_T,
typename Sl
ider_T>
260 const Slider_T& slider;
267 constexpr double to01(
double val)
const noexcept {
return Norm_T::to01(slider, val); }
269 constexpr double from01(
double val)
const noexcept
271 return Norm_T::from01(slider, val);
275template <
typename Control_T,
typename W
idget_T>
276static void initWidgetProperties(Control_T& inlet, Widget_T& widget)
278 if constexpr(
requires { widget.setNoValueChangeOnMove(
true); })
279 widget.setNoValueChangeOnMove(inlet.noValueChangeOnMove);
282template <
typename W
idget_T>
283static void setRange(Widget_T& widget,
auto&& min,
auto&& max,
auto&& init)
285 widget.setRange(min, max, init);
288static inline void setRange(QSpinBox& widget,
auto&& min,
auto&& max,
auto&& init)
290 widget.setRange(min, max);
293static inline void setRange(QDoubleSpinBox& widget,
auto&& min,
auto&& max,
auto&& init)
295 widget.setRange(min, max);
298template <
typename T,
typename Control_T,
typename W
idget_T>
299static void bindFloatDomain(
const T& slider, Control_T& inlet, Widget_T& widget)
301 auto min = getMin<float>(slider);
302 auto max = getMax<float>(slider);
306 setRange(widget, min, max, getInit<float>(slider));
308 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
310 SCORE_ASSERT(&slider == &inlet);
311 QObject::connect(&inlet, &Control_T::domainChanged, &widget, [&slider, &widget] {
312 auto min = getMin<float>(slider);
313 auto max = getMax<float>(slider);
317 setRange(widget, min, max, getInit<float>(slider));
322template <
typename T,
typename Control_T,
typename W
idget_T>
323static void bindIntDomain(
const T& slider, Control_T& inlet, Widget_T& widget)
325 auto min = getMin<int>(slider);
326 auto max = getMax<int>(slider);
330 setRange(widget, min, max, getInit<int>(slider));
332 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
334 SCORE_ASSERT(&slider == &inlet);
335 QObject::connect(&inlet, &Control_T::domainChanged, &widget, [&slider, &widget] {
336 auto min = getMin<int>(slider);
337 auto max = getMax<int>(slider);
341 setRange(widget, min, max, getInit<int>(slider));
346template <
typename T,
typename Control_T,
typename W
idget_T>
347static void bindVec2Domain(
const T& slider, Control_T& inlet, Widget_T& widget)
349 auto update_range = [&widget, &inlet, &slider] {
350 auto min = ossia::get_min(inlet.domain());
351 auto max = ossia::get_max(inlet.domain());
352 auto min_float = min.template target<float>();
353 auto max_float = max.template target<float>();
354 if(min_float && max_float)
356 if(*max_float - *min_float == 0)
357 *max_float = *min_float + 1;
359 {*min_float, *min_float}, {*max_float, *max_float}, {*min_float, *min_float});
363 auto min_vec2 = min.template target<ossia::vec2f>();
364 auto max_vec2 = max.template target<ossia::vec2f>();
365 if(min_vec2 && max_vec2)
367 auto& min = *min_vec2;
368 auto& max = *max_vec2;
369 for(std::size_t i = 0; i < min.size(); i++)
371 if(max[i] - min[i] == 0)
375 auto init_vec2 = getInit<ossia::vec2f>(slider);
376 widget.setRange(min, max, init_vec2);
383 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
385 SCORE_ASSERT(&slider == &inlet);
386 QObject::connect(&inlet, &Control_T::domainChanged, &widget, update_range);
390template <
typename T,
typename Control_T,
typename W
idget_T>
391static void bindVec3Domain(
const T& slider, Control_T& inlet, Widget_T& widget)
393 auto update_range = [&widget, &inlet, &slider] {
394 auto min = ossia::get_min(inlet.domain());
395 auto max = ossia::get_max(inlet.domain());
396 auto min_float = min.template target<float>();
397 auto max_float = max.template target<float>();
398 if(min_float && max_float)
400 if(*max_float - *min_float == 0)
401 *max_float = *min_float + 1;
403 {*min_float, *min_float, *min_float}, {*max_float, *max_float, *max_float});
407 auto min_vec3 = min.template target<ossia::vec3f>();
408 auto max_vec3 = max.template target<ossia::vec3f>();
409 if(min_vec3 && max_vec3)
411 auto& min = *min_vec3;
412 auto& max = *max_vec3;
413 for(std::size_t i = 0; i < min.size(); i++)
415 if(max[i] - min[i] == 0)
419 auto init_vec3 = getInit<ossia::vec3f>(slider);
420 widget.setRange(min, max, init_vec3);
427 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
429 SCORE_ASSERT(&slider == &inlet);
430 QObject::connect(&inlet, &Control_T::domainChanged, &widget, update_range);
Definition MIDISync.hpp:149