MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Wiring.cpp
Go to the documentation of this file.
1#include "Wiring.hpp"
2
3#include "Fabric.hpp"
4
6
13
17
18namespace MayaFlux::Nexus {
19
20// =============================================================================
21// Scheduling modifiers
22// =============================================================================
23
24Wiring& Wiring::every(double interval_seconds, Vruta::ProcessingToken token)
25{
26 m_interval = interval_seconds;
27 m_metro_token = token;
28 return *this;
29}
30
32{
33 m_duration = seconds;
34 m_duration_token = token;
35 return *this;
36}
37
38Wiring& Wiring::on(std::shared_ptr<Core::Window> window, IO::Keys key)
39{
40 m_trigger = KeyTrigger { .window = std::move(window), .key = key };
41 return *this;
42}
43
44Wiring& Wiring::on(std::shared_ptr<Core::Window> window, IO::Keys key, bool held,
45 std::function<void()> on_release)
46{
48 .window = std::move(window),
49 .key = key,
50 .on_release = on_release ? std::optional<std::function<void()>>(std::move(on_release)) : std::nullopt,
51 .held = held
52 };
53 return *this;
54}
55
56Wiring& Wiring::on(std::shared_ptr<Core::Window> window, IO::MouseButtons button)
57{
58 m_trigger = MouseTrigger { .window = std::move(window), .button = button };
59 return *this;
60}
61
62Wiring& Wiring::on(std::shared_ptr<Core::Window> window, IO::MouseButtons button, bool held, std::function<void(double, double)> on_release)
63{
65 .window = std::move(window),
66 .button = button,
67 .on_release = on_release ? std::optional<std::function<void(double, double)>>(std::move(on_release)) : std::nullopt,
68 .held = held
69 };
70
71 return *this;
72}
73
74Wiring& Wiring::on(std::shared_ptr<Core::Window> window, IO::MouseButtons button,
75 std::function<void(double, double)> on_release)
76{
78 .window = std::move(window),
79 .button = button,
80 .on_release = std::move(on_release)
81 };
82 return *this;
83}
84
86{
87 m_trigger = NetworkTrigger { .source = &source };
88 return *this;
89}
90
92{
93 m_trigger = WindowEventTrigger { .source = &source, .filter = std::move(filter) };
94 return *this;
95}
96
97Wiring& Wiring::move_to(const glm::vec3& pos, double delay_seconds)
98{
99 m_move_steps.push_back({ .position = pos, .delay_seconds = delay_seconds });
100 return *this;
101}
102
104{
105 m_position_fn = std::move(fn);
106 return *this;
107}
108
110{
111 m_times = count;
112 return *this;
113}
114
116{
117 m_factory = std::move(factory);
118 return *this;
119}
120
122{
123 m_factory = std::move(factory);
124 return *this;
125}
126
128{
129 m_factory = std::move(factory);
130 return *this;
131}
132
134{
135 m_event_factory = std::move(factory);
136 return *this;
137}
138
139Wiring& Wiring::position_from(std::string fn_name, PositionFn fn)
140{
141 m_position_fn_name = std::move(fn_name);
142 m_position_fn = std::move(fn);
143 return *this;
144}
145
146Wiring& Wiring::use(std::string fn_name, SoundFactory factory)
147{
148 m_factory_name = std::move(fn_name);
149 m_factory = std::move(factory);
150 return *this;
151}
152
153Wiring& Wiring::use(std::string fn_name, GraphicsFactory factory)
154{
155 m_factory_name = std::move(fn_name);
156 m_factory = std::move(factory);
157 return *this;
158}
159
160Wiring& Wiring::use(std::string fn_name, CrossFactory factory)
161{
162 m_factory_name = std::move(fn_name);
163 m_factory = std::move(factory);
164 return *this;
165}
166
167Wiring& Wiring::use(std::string fn_name, EventFactory factory)
168{
169 m_factory_name = std::move(fn_name);
170 m_event_factory = std::move(factory);
171 return *this;
172}
173
174// =============================================================================
175// Immediate bind
176// =============================================================================
177
179{
180 m_bind_attach = [this]() { m_fabric.fire(m_entity_id); };
181 return *this;
182}
183
184Wiring& Wiring::bind(std::function<void()> fn)
185{
186 m_bind_attach = std::move(fn);
187 return *this;
188}
189
190Wiring& Wiring::bind(std::function<void()> attach, std::function<void()> detach)
191{
192 m_bind_attach = std::move(attach);
193 m_bind_detach = std::move(detach);
194 return *this;
195}
196
197Wiring& Wiring::bind(std::string fn_name, std::function<void()> fn)
198{
199 m_bind_attach_name = std::move(fn_name);
200 m_bind_attach = std::move(fn);
201 return *this;
202}
203
205 std::string attach_name, std::function<void()> attach,
206 std::string detach_name, std::function<void()> detach)
207{
208 m_bind_attach_name = std::move(attach_name);
209 m_bind_attach = std::move(attach);
210 m_bind_detach_name = std::move(detach_name);
211 m_bind_detach = std::move(detach);
212 return *this;
213}
214
215// =============================================================================
216// make_name
217// =============================================================================
218
219std::string Wiring::make_name(const char* prefix) const
220{
221 return std::string(prefix) + "_" + std::to_string(m_entity_id) + "_" + std::to_string(m_fabric.m_scheduler.get_next_task_id());
222}
223
224namespace {
225
226 // =============================================================================
227 // Network coroutine
228 // =============================================================================
229
230 Vruta::SoundRoutine network_loop(
231 Vruta::NetworkSource& source,
232 Fabric& fabric,
233 uint32_t id)
234 {
235 auto& promise = co_await Kriya::GetAudioPromise {};
236 while (true) {
237 if (promise.should_terminate) {
238 break;
239 }
240 co_await source.next_message();
241 fabric.fire(id);
242 }
243 }
244}
245
246// =============================================================================
247// Terminal
248// =============================================================================
249
251{
252 auto& scheduler = m_fabric.m_scheduler;
253 auto& ev_manager = m_fabric.m_event_manager;
254 const uint32_t id = m_entity_id;
255 auto& reg = m_fabric.m_registrations[id];
256
257 bool has_any_path = !std::holds_alternative<std::monostate>(m_factory)
258 || m_event_factory.has_value()
259 || m_bind_attach.has_value()
260 || !m_move_steps.empty()
261 || !std::holds_alternative<std::monostate>(m_trigger)
262 || m_interval.has_value();
263
264 reg.commit_driven = !has_any_path;
265
266 // ------------------------------------------------------------------
267 // Register named callables before any early return.
268 // ------------------------------------------------------------------
269 std::visit([&](const auto& ptr) {
270 using T = std::decay_t<decltype(*ptr)>;
271 if constexpr (std::is_same_v<T, Emitter>) {
272 if (!ptr->fn_name().empty()) {
273 m_fabric.m_influence_fns.try_emplace(
274 ptr->fn_name(), std::make_shared<Emitter::InfluenceFn>(ptr->fn()));
275 }
276 } else if constexpr (std::is_same_v<T, Sensor>) {
277 if (!ptr->fn_name().empty()) {
278 m_fabric.m_perception_fns.try_emplace(
279 ptr->fn_name(), std::make_shared<Sensor::PerceptionFn>(ptr->fn()));
280 }
281 } else if constexpr (std::is_same_v<T, Agent>) {
282 if (!ptr->influence_fn_name().empty()) {
283 m_fabric.m_influence_fns.try_emplace(
284 ptr->influence_fn_name(), std::make_shared<Emitter::InfluenceFn>(ptr->influence_fn()));
285 }
286 if (!ptr->perception_fn_name().empty()) {
287 m_fabric.m_perception_fns.try_emplace(
288 ptr->perception_fn_name(), std::make_shared<Sensor::PerceptionFn>(ptr->perception_fn()));
289 }
290 }
291 },
292 reg.member);
293
294 // ------------------------------------------------------------------
295 // Routine factory path
296 // ------------------------------------------------------------------
297 if (!std::holds_alternative<std::monostate>(m_factory)) {
298 auto name = make_name("nexus_task");
299 reg.task_name = name;
300 std::visit([&](auto& factory) {
301 using F = std::decay_t<decltype(factory)>;
302 if constexpr (!std::is_same_v<F, std::monostate>) {
303 scheduler.add_task(
304 std::make_shared<std::invoke_result_t<F>>(factory()),
305 name, false);
306 }
307 },
308 m_factory);
309 m_fabric.m_registrations[m_entity_id].wiring.emplace(std::move(*this));
310 return;
311 }
312
313 // ------------------------------------------------------------------
314 // Event factory path
315 // ------------------------------------------------------------------
316 if (m_event_factory.has_value()) {
317 auto name = make_name("nexus_event");
318 reg.event_name = name;
319 ev_manager.add_event(
320 std::make_shared<Vruta::Event>((*m_event_factory)(scheduler)),
321 name);
322 m_fabric.m_registrations[m_entity_id].wiring.emplace(std::move(*this));
323 return;
324 }
325
326 // ------------------------------------------------------------------
327 // Immediate bind path
328 // ------------------------------------------------------------------
329 if (m_bind_attach.has_value()) {
330 (*m_bind_attach)();
331
332 if (m_duration.has_value() && m_bind_detach.has_value()) {
333 auto timer = std::make_shared<Kriya::Timer>(scheduler, m_duration_token);
334 auto detach = *m_bind_detach;
335 timer->schedule(*m_duration, [timer, detach]() {
336 detach();
337 });
338 }
339 m_fabric.m_registrations[m_entity_id].wiring.emplace(std::move(*this));
340 return;
341 }
342
343 // ------------------------------------------------------------------
344 // Choreographed position path
345 // ------------------------------------------------------------------
346 if (!m_move_steps.empty()) {
347 auto name = make_name("nexus_chain");
348 reg.chain_name = name;
349
350 Kriya::EventChain chain(scheduler, name);
351 auto& fab = m_fabric;
352 for (auto& step : m_move_steps) {
353 glm::vec3 pos = step.position;
354 chain.then([&fab, pos, id]() {
355 std::visit([&pos](const auto& ptr) {
356 ptr->set_position(pos);
357 },
358 fab.m_registrations[id].member);
359 fab.fire(id);
360 },
361 step.delay_seconds);
362 }
363 if (m_times > 1) {
364 chain.times(m_times);
365 }
366 chain.start();
367 m_fabric.m_registrations[m_entity_id].wiring.emplace(std::move(*this));
368 return;
369 }
370
371 // ------------------------------------------------------------------
372 // Trigger path
373 // ------------------------------------------------------------------
374 if (!std::holds_alternative<std::monostate>(m_trigger)) {
375 std::visit([&](auto& trig) {
376 using T = std::decay_t<decltype(trig)>;
377
378 if constexpr (std::is_same_v<T, KeyTrigger>) {
379 auto name = make_name("nexus_event");
380 reg.event_name = name;
381 auto& fab = m_fabric;
382 ev_manager.add_event(
383 std::make_shared<Vruta::Event>(
384 trig.held
385 ? Kriya::key_held(trig.window, trig.key,
386 [&fab, id]() { fab.fire(id); })
387 : Kriya::key_pressed(trig.window, trig.key,
388 [&fab, id]() { fab.fire(id); })),
389 name);
390
391 if (trig.on_release) {
392 auto release_name = make_name("nexus_event_release");
393 reg.chain_name = release_name;
394 ev_manager.add_event(
395 std::make_shared<Vruta::Event>(
396 Kriya::key_released(trig.window, trig.key, *trig.on_release)),
397 release_name);
398 }
399
400 } else if constexpr (std::is_same_v<T, MouseTrigger>) {
401 auto name = make_name("nexus_event");
402 reg.event_name = name;
403 Fabric& fab = m_fabric;
404 uint32_t eid = m_entity_id;
405 ev_manager.add_event(
406 std::make_shared<Vruta::Event>(
407 trig.held
408 ? Kriya::mouse_dragged(trig.window, trig.button,
409 [&fab, eid](double px, double py) {
410 fab.m_registrations[eid].pending_cursor = glm::vec2(px, py);
411 fab.fire(eid);
412 })
413 : Kriya::mouse_pressed(trig.window, trig.button,
414 [&fab, eid](double px, double py) {
415 fab.m_registrations[eid].pending_cursor = glm::vec2(px, py);
416 fab.fire(eid);
417 })),
418 name);
419 if (trig.on_release) {
420 auto release_name = make_name("nexus_event_release");
421 reg.chain_name = release_name;
422 ev_manager.add_event(
423 std::make_shared<Vruta::Event>(
424 Kriya::mouse_released(trig.window, trig.button, *trig.on_release)),
425 release_name);
426 }
427
428 } else if constexpr (std::is_same_v<T, NetworkTrigger>) {
429 auto name = make_name("nexus_task");
430 reg.task_name = name;
431 scheduler.add_task(
432 std::make_shared<Vruta::SoundRoutine>(
433 network_loop(*trig.source, m_fabric, id)),
434 name, false);
435
436 } else if constexpr (std::is_same_v<T, WindowEventTrigger>) {
437 auto name = make_name("nexus_event");
438 reg.event_name = name;
439 Fabric& fab = m_fabric;
440 ev_manager.add_event(
441 std::make_shared<Vruta::Event>(
442 window_event_source_loop(*trig.source, trig.filter, fab, id)),
443 name);
444 }
445 },
446 m_trigger);
447 m_fabric.m_registrations[m_entity_id].wiring.emplace(std::move(*this));
448 return;
449 }
450
451 // ------------------------------------------------------------------
452 // Metro path
453 // ------------------------------------------------------------------
454 if (m_interval.has_value()) {
455 auto pos_fn = m_position_fn;
456 auto name = make_name("nexus_metro");
457 reg.task_name = name;
458 auto& fab = m_fabric;
459
460 scheduler.add_task(
461 Kriya::metro(*m_interval, [&fab, id, pos_fn]() mutable {
462 if (pos_fn.has_value()) {
463 glm::vec3 p = (*pos_fn)();
464 std::visit([&p](const auto& ptr) {
465 ptr->set_position(p);
466 },
467 fab.m_registrations[id].member);
468 }
469 fab.fire(id); }, m_metro_token), name, false);
470
471 if (m_duration.has_value()) {
472 auto cancel_name = make_name("nexus_metro_cancel");
473 auto timer = std::make_shared<Kriya::Timer>(scheduler);
474 timer->schedule(*m_duration, [name, timer, &scheduler]() {
475 scheduler.cancel_task(name);
476 });
477 }
478 }
479
480 m_fabric.m_registrations[m_entity_id].wiring.emplace(std::move(*this));
481}
482
483// =============================================================================
484// Cancel
485// =============================================================================
486
487void Wiring::cancel()
488{
489 auto& reg = m_fabric.m_registrations[m_entity_id];
490
491 if (!reg.task_name.empty()) {
492 m_fabric.m_scheduler.cancel_task(reg.task_name);
493 reg.task_name.clear();
494 }
495 if (!reg.chain_name.empty()) {
496 m_fabric.m_scheduler.cancel_task(reg.chain_name);
497 reg.chain_name.clear();
498 }
499 if (!reg.event_name.empty()) {
500 m_fabric.m_event_manager.cancel_event(reg.event_name);
501 reg.event_name.clear();
502 }
503
504 if (m_bind_detach.has_value()) {
505 (*m_bind_detach)();
506 }
507}
508
509// =============================================================================
510// Internal
511// =============================================================================
512
513Vruta::Event Wiring::window_event_source_loop(
516 Fabric& fabric,
517 uint32_t id)
518{
519 auto& promise = co_await Kriya::GetEventPromise {};
520 while (true) {
521 if (promise.should_terminate)
522 break;
523 auto event = co_await Kriya::WindowEventAwaiter(source, filter);
524 if (auto* pos = std::get_if<Core::WindowEvent::MousePosData>(&event.data))
525 fabric.m_registrations[id].pending_cursor = glm::vec2(pos->x, pos->y);
526 fabric.fire(id);
527 }
528}
529
530} // namespace MayaFlux::Nexus
size_t count
const uint8_t * ptr
A sequential chain of timed events with precise temporal control.
Definition Chain.hpp:43
Awaiter for suspending on GLFW window input events with optional filtering.
void fire(uint32_t id)
Fire a single object against the current snapshot without republishing.
Definition Fabric.cpp:204
std::unordered_map< std::string, std::shared_ptr< Sensor::PerceptionFn > > m_perception_fns
Definition Fabric.hpp:300
Vruta::TaskScheduler & m_scheduler
Definition Fabric.hpp:288
std::unordered_map< uint32_t, Registration > m_registrations
Definition Fabric.hpp:292
std::unordered_map< std::string, std::shared_ptr< Emitter::InfluenceFn > > m_influence_fns
Definition Fabric.hpp:299
Vruta::EventManager & m_event_manager
Definition Fabric.hpp:289
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:38
std::optional< std::function< void()> > m_bind_detach
Definition Wiring.hpp:294
Wiring & position_from(PositionFn fn)
Drive position from a callable evaluated on each every() tick.
Definition Wiring.cpp:103
std::string m_position_fn_name
Definition Wiring.hpp:282
Vruta::Event window_event_source_loop(Vruta::WindowEventSource &source, Vruta::WindowEventFilter filter, Fabric &fabric, uint32_t id)
Definition Wiring.cpp:513
std::string make_name(const char *prefix) const
Definition Wiring.cpp:219
Wiring & use(SoundFactory factory)
Delegate coroutine creation entirely to the caller.
Definition Wiring.cpp:115
std::function< Vruta::CrossRoutine()> CrossFactory
Definition Wiring.hpp:46
std::string m_bind_attach_name
Definition Wiring.hpp:295
Wiring & on(std::shared_ptr< Core::Window > window, IO::Keys key)
Fire the entity on a key event from a window.
Definition Wiring.cpp:38
std::function< glm::vec3()> PositionFn
Definition Wiring.hpp:43
std::optional< std::function< void()> > m_bind_attach
Definition Wiring.hpp:293
std::optional< double > m_duration
Definition Wiring.hpp:280
Vruta::ProcessingToken m_metro_token
Definition Wiring.hpp:285
Wiring & every(double interval_seconds, Vruta::ProcessingToken token=Vruta::ProcessingToken::SAMPLE_ACCURATE)
Fire the entity on a recurring interval.
Definition Wiring.cpp:24
Wiring & times(size_t count)
Repeat the configured sequence or choreography N times.
Definition Wiring.cpp:109
Wiring & move_to(const glm::vec3 &pos, double delay_seconds=0.0)
Choreograph a position move as an EventChain step.
Definition Wiring.cpp:97
std::function< Vruta::GraphicsRoutine()> GraphicsFactory
Definition Wiring.hpp:45
std::function< Vruta::Event(Vruta::TaskScheduler &)> EventFactory
Definition Wiring.hpp:47
Vruta::ProcessingToken m_duration_token
Definition Wiring.hpp:286
std::optional< double > m_interval
Definition Wiring.hpp:279
const Factory & factory() const
Active factory variant set by use for non-Event factories.
Definition Wiring.hpp:333
std::string m_bind_detach_name
Definition Wiring.hpp:296
Wiring & for_duration(double seconds, Vruta::ProcessingToken token=Vruta::ProcessingToken::SAMPLE_ACCURATE)
Limit a recurring registration to a fixed duration then cancel.
Definition Wiring.cpp:31
std::vector< MoveStep > m_move_steps
Definition Wiring.hpp:283
std::string m_factory_name
Definition Wiring.hpp:291
std::optional< PositionFn > m_position_fn
Definition Wiring.hpp:281
void finalise()
Apply the configured wiring.
Definition Wiring.cpp:250
Wiring & bind()
Call the entity's influence function once immediately.
Definition Wiring.cpp:178
std::function< Vruta::SoundRoutine()> SoundFactory
Definition Wiring.hpp:44
Fluent builder that wires an entity into Fabric's scheduling infrastructure.
Definition Wiring.hpp:41
Coroutine type for event-driven suspension.
Definition Event.hpp:26
Awaitable broadcast message stream for a network endpoint.
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:316
uint64_t get_next_task_id() const
Generates a unique task ID for new tasks.
Awaitable stream of GLFW window input events.
MouseButtons
Enumeration for mouse buttons.
Definition Keys.hpp:147
Vruta::Event key_released(std::shared_ptr< Core::Window > window, IO::Keys key, std::function< void()> callback)
Creates an Event coroutine that triggers on specific key release.
Vruta::Event key_pressed(std::shared_ptr< Core::Window > window, IO::Keys key, std::function< void()> callback)
Creates an Event coroutine that triggers on specific key press.
Vruta::Event key_held(std::shared_ptr< Core::Window > window, IO::Keys key, std::function< void()> callback)
Creates an Event coroutine that triggers on key press and repeats while held.
Vruta::Event mouse_released(std::shared_ptr< Core::Window > window, IO::MouseButtons button, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on specific mouse button release.
Vruta::Event mouse_pressed(std::shared_ptr< Core::Window > window, IO::MouseButtons button, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on specific mouse button press.
Vruta::Event mouse_dragged(std::shared_ptr< Core::Window > window, IO::MouseButtons button, std::function< void(double, double)> callback)
Creates an Event coroutine that triggers on mouse drag with specific button.
std::shared_ptr< Vruta::Routine > metro(double interval_seconds, std::function< void()> callback, Vruta::ProcessingToken token)
Creates a periodic event generator that executes a callback at regular intervals.
Definition Tasks.cpp:12
Event-domain promise accessor with optional NetworkSource ownership transfer.
Templated awaitable for accessing a coroutine's promise object.
std::shared_ptr< Core::Window > window
Definition Wiring.hpp:243
std::shared_ptr< Core::Window > window
Definition Wiring.hpp:250
Vruta::WindowEventSource * source
Definition Wiring.hpp:266
Filter criteria for GLFW window input events.