Loading...
Searching...
No Matches
ControlViews.hpp
1#pragma once
2#include <Process/Commands/SetControlValue.hpp>
3#include <Process/Dataflow/Port.hpp>
4
5#include <score/model/Skin.hpp>
6
7#include <ossia/network/domain/domain.hpp>
8
9#include <Crousti/Painter.hpp>
10#include <Crousti/UiEdits.hpp>
11#include <avnd/binding/ossia/from_value.hpp>
12#include <avnd/binding/ossia/to_value.hpp>
13
14#include <QGraphicsItem>
15#include <QObject>
16#include <QPainter>
17
18#include <algorithm>
19#include <optional>
20#include <tuple>
21#include <utility>
22#include <vector>
23
24namespace oscr
25{
27class ValueBarItem final
28 : public QObject
29 , public QGraphicsItem
30{
31public:
32 explicit ValueBarItem(QGraphicsItem* parent = nullptr)
33 : QGraphicsItem{parent}
34 {
35 }
36
37 void setValue(double normalized)
38 {
39 // Values stream in at the control rate: repaint only on a change
40 normalized = std::clamp(normalized, 0., 1.);
41 if(normalized == m_value)
42 return;
43 m_value = normalized;
44 update();
45 }
46
47 QRectF boundingRect() const override { return {0., 0., 52., 4.}; }
48
49 void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) override
50 {
51 auto& skin = score::Skin::instance();
52 const QRectF r = boundingRect();
53 painter->fillRect(r, skin.Background2.darker.brush);
54 painter->fillRect(QRectF{r.x(), r.y(), r.width() * m_value, r.height()}, skin.Base4);
55 }
56
57private:
58 double m_value{};
59};
60
64inline double scalarValue(const ossia::value& v)
65{
66 if(auto t = v.target<ossia::vec2f>())
67 return (*t)[1] != 0.f ? (*t)[0] * 2. : (*t)[0]; // a whole note: 2 s
68 return ossia::convert<double>(v);
69}
70
72inline double normalizedValue(const Process::ControlInlet& port, const ossia::value& v)
73{
74 const auto& dom = port.domain().get();
75 const auto lo = ossia::get_min(dom);
76 const auto hi = ossia::get_max(dom);
77 if(!lo.valid() || !hi.valid())
78 return std::clamp(scalarValue(v), 0., 1.);
79 const double l = scalarValue(lo), h = scalarValue(hi);
80 // A time chooser's domain is {min, 0} .. {max, 1}: its x is the range
81 if(auto a = lo.target<ossia::vec2f>())
82 if(auto b = hi.target<ossia::vec2f>())
83 {
84 const double tl = (*a)[0], th = (*b)[0];
85 return th == tl ? 0. : std::clamp((scalarValue(v) - tl) / (th - tl), 0., 1.);
86 }
87 return h == l ? 0. : std::clamp((scalarValue(v) - l) / (h - l), 0., 1.);
88}
89
90template <typename T>
91inline constexpr bool is_value_tuple = false;
92template <typename... T>
93inline constexpr bool is_value_tuple<std::tuple<T...>> = true;
94
101template <typename Item>
103 : public QObject
104 , public CustomItem<Item>
105{
106public:
110 Item item_init, std::vector<Process::ControlInlet*> ports, UiEdits& edits)
111 : CustomItem<Item>{item_init}
112 , m_ports{std::move(ports)}
113 , m_edits{edits}
114 {
115 using values_type = std::decay_t<decltype(this->impl.values)>;
116 if constexpr(is_value_tuple<values_type>)
117 {
118 setupRaw(std::make_index_sequence<std::tuple_size_v<values_type>>{});
119 }
120 else
121 {
122 setupNormalized();
123 }
124 }
125
126 ~CustomMultiControl()
127 {
128 this->finishWheel();
129 // Destroyed mid-gesture (UI rebuilt on inlet change): what it did stays
130 if(m_active)
131 m_edits.endGesture();
132 }
133
134private:
135 template <std::size_t... I>
136 void setupRaw(std::index_sequence<I...>)
137 {
138 (setupRawPort<I>(), ...);
139 }
140
141 template <std::size_t I>
142 void setupRawPort()
143 {
144 using transactions_type = std::decay_t<decltype(this->impl.transactions)>;
145 if(I >= m_ports.size())
146 {
147 // No control to edit: the item's calls do nothing
148 if constexpr(I < std::tuple_size_v<transactions_type>)
149 {
150 auto& tr = std::get<I>(this->impl.transactions);
151 tr.start = [] { };
152 tr.update = [](const auto&) { };
153 tr.commit = [] { };
154 tr.rollback = [] { };
155 }
156 return;
157 }
158 auto* port = m_ports[I];
159 using value_type = std::decay_t<decltype(std::get<I>(this->impl.values))>;
160 auto show = [this](const ossia::value& v) {
161 value_type val{};
162 oscr::from_ossia_value(v, val);
163 std::get<I>(this->impl.values) = std::move(val);
164 this->update();
165 };
166 show(port->value());
167 QObject::connect(port, &Process::ControlInlet::valueChanged, this, show);
168
169 if constexpr(requires {
170 std::get<I>(this->impl.execution_values) = std::optional<value_type>{};
171 })
172 {
173 followExecution(
174 port, [this] { return &std::get<I>(this->impl.execution_values); },
175 [](const ossia::value& v) {
176 value_type val{};
177 oscr::from_ossia_value(v, val);
178 return val;
179 });
180 }
181
182 if constexpr(I < std::tuple_size_v<transactions_type>)
183 setupRawTransaction<I>(port);
184 }
185
186 template <std::size_t I>
187 void setupRawTransaction(Process::ControlInlet* port)
188 {
189 using value_type = std::decay_t<decltype(std::get<I>(this->impl.values))>;
190 auto& tr = std::get<I>(this->impl.transactions);
191 tr.start = [this] { start(); };
192 tr.update = [this, port](const value_type& val) {
193 std::get<I>(this->impl.values) = val;
194 m_edits.set(*port, oscr::to_ossia_value(val));
195 this->update();
196 };
197 tr.commit = [this] { commit(); };
198 tr.rollback = [this] { rollback(); };
199 }
200
201 void setupNormalized()
202 {
203 const std::size_t n = std::min(m_ports.size(), std::size(this->impl.values));
204 for(std::size_t i = 0; i < n; i++)
205 {
206 auto* port = m_ports[i];
207 auto show = [this, i, port](const ossia::value& v) {
208 const auto n = normalize(*port, v);
209 if(n == this->impl.values[i])
210 return;
211 this->impl.values[i] = n;
212 this->update();
213 };
214 show(port->value());
215 QObject::connect(port, &Process::ControlInlet::valueChanged, this, show);
216
217 // Looked up at each value: the item may resize its execution_values
218 if constexpr(requires { this->impl.execution_values[i] = std::optional<double>{}; })
219 followExecution(
220 port,
221 [this, i]() -> std::optional<double>* {
222 auto& shown = this->impl.execution_values;
223 return i < std::size(shown) ? &shown[i] : nullptr;
224 },
225 [port](const ossia::value& v) { return normalize(*port, v); });
226 }
227
228 this->impl.transaction.start = [this] { start(); };
229 this->impl.transaction.update = [this, n](int i, double normalized) {
230 if(i < 0 || std::size_t(i) >= n)
231 return;
232 auto& port = *m_ports[i];
233 m_edits.set(port, denormalize(port, normalized));
234 };
235 this->impl.transaction.commit = [this] { commit(); };
236 this->impl.transaction.rollback = [this] { rollback(); };
237 }
238
245 void followExecution(Process::ControlInlet* port, auto shown, auto convert)
246 {
247 auto show = [this, shown](auto v) {
248 auto* dst = shown();
249 if(!dst)
250 return;
251 // Values stream in at the control rate: repaint only on a change
252 if constexpr(requires { *dst == v; })
253 if(*dst == v)
254 return;
255 *dst = std::move(v);
256 this->update();
257 };
258 QObject::connect(
259 port, &Process::ControlInlet::executionValueChanged, this,
260 [show, convert](const ossia::value& v) {
261 show(std::make_optional(convert(v)));
262 });
263 using shown_type = std::decay_t<decltype(*shown())>;
264 QObject::connect(
265 port, &Process::ControlInlet::executionReset, this, [show] { show(shown_type{}); });
266 }
267
269 void start()
270 {
271 if(!m_active)
272 m_edits.beginGesture();
273 m_active = true;
274 }
275 void commit()
276 {
277 if(m_active)
278 m_edits.endGesture();
279 m_active = false;
280 }
281 void rollback()
282 {
283 if(m_active)
284 m_edits.rollback();
285 m_active = false;
286 }
287
288 static std::pair<double, double> range(const Process::ControlInlet& port)
289 {
290 const auto& dom = port.domain().get();
291 const auto lo = ossia::get_min(dom);
292 const auto hi = ossia::get_max(dom);
293 if(!lo.valid() || !hi.valid())
294 return {0., 1.};
295 // A time chooser's domain is {min, 0} .. {max, 1}
296 if(auto a = lo.target<ossia::vec2f>())
297 if(auto b = hi.target<ossia::vec2f>())
298 return {(*a)[0], (*b)[0]};
299 return {ossia::convert<double>(lo), ossia::convert<double>(hi)};
300 }
301
302 static double normalize(const Process::ControlInlet& port, const ossia::value& v)
303 {
304 return normalizedValue(port, v);
305 }
306
307 static ossia::value denormalize(const Process::ControlInlet& port, double n)
308 {
309 const auto [lo, hi] = range(port);
310 const double v = lo + std::clamp(n, 0., 1.) * (hi - lo);
311 // Keep the control's own type
312 if(port.value().get_type() == ossia::val_type::INT)
313 return int(std::lround(v));
314 // A time chooser: dragged on a graph, a length in seconds (a synced
315 // stage becomes free there)
316 if(port.value().get_type() == ossia::val_type::VEC2F)
317 return ossia::vec2f{float(v), 0.f};
318 return float(v);
319 }
320
321 std::vector<Process::ControlInlet*> m_ports;
322 UiEdits& m_edits;
323 bool m_active{};
324};
325}
Definition Port.hpp:247
Definition Painter.hpp:425
void finishWheel()
Derived controls call this before their transaction bindings go away.
Definition Painter.hpp:573
Definition ControlViews.hpp:105
CustomMultiControl(Item item_init, std::vector< Process::ControlInlet * > ports, UiEdits &edits)
Definition ControlViews.hpp:109
The edits a custom ui makes to its process's controls.
Definition UiEdits.hpp:76
void rollback()
Takes back the pending edits, without an undo step.
Definition UiEdits.cpp:333
void beginGesture()
A transaction: its edits are pushed at endGesture, not before.
Definition UiEdits.cpp:207
halp::display_style::bar
Definition ControlViews.hpp:30
Definition Controls.hpp:31
double normalizedValue(const Process::ControlInlet &port, const ossia::value &v)
A control's value, normalized over its range.
Definition ControlViews.hpp:72
double scalarValue(const ossia::value &v)
Definition ControlViews.hpp:64
Definition MIDISync.hpp:149