MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Wiring.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Agent.hpp"
4
8
9namespace MayaFlux::Core {
10class Window;
11}
12
13namespace MayaFlux::Vruta {
14class TaskScheduler;
15class EventManager;
16class Routine;
17class SoundRoutine;
18class GraphicsRoutine;
19class ComplexRoutine;
20class Event;
21}
22
23namespace MayaFlux::Nexus {
24
25class Fabric;
26
27/**
28 * @class Wiring
29 * @brief Fluent builder that wires an entity into Fabric's scheduling infrastructure.
30 *
31 * Obtained via @c Fabric::wire. Describes when and how the object's function
32 * is invoked. Call @c finalise() to apply the configuration.
33 *
34 * No coroutine is created unless a scheduling modifier is set. @c bind()
35 * performs an immediate single call with no coroutine. Any scheduling
36 * modifier (@c every, @c on, @c move_to, @c position_from, @c use) creates
37 * and registers a coroutine owned by Fabric.
38 */
39class MAYAFLUX_API Wiring {
40public:
41 using PositionFn = std::function<glm::vec3()>;
46
47 // =====================================================================
48 // Scheduling modifiers
49 // =====================================================================
50
51 /**
52 * @brief Fire the entity on a recurring interval.
53 * @param interval_seconds Period between invocations.
54 */
55 Wiring& every(double interval_seconds);
56
57 /**
58 * @brief Limit a recurring registration to a fixed duration then cancel.
59 * @param seconds Total active time. Pairs with @c every().
60 */
61 Wiring& for_duration(double seconds);
62
63 /**
64 * @brief Fire the entity on a key event from a window.
65 * @param window Source window.
66 * @param key Key to listen for.
67 */
68 Wiring& on(std::shared_ptr<Core::Window> window, IO::Keys key);
69
70 /**
71 * @brief Fire the entity on a mouse button event from a window.
72 * @param window Source window.
73 * @param button Mouse button to listen for.
74 */
75 Wiring& on(std::shared_ptr<Core::Window> window, IO::MouseButtons button);
76
77 /**
78 * @brief Fire the entity on each incoming message from a network source.
79 * @param source OSC or raw network source.
80 */
81 Wiring& on(Vruta::NetworkSource& source);
82
83 /**
84 * @brief Fire the entity on each matching window event.
85 * @param source Event stream.
86 * @param filter Optional filter criteria.
87 */
88 Wiring& on(Vruta::EventSource& source, Vruta::EventFilter filter = {});
89
90 /**
91 * @brief Choreograph a position move as an EventChain step.
92 * @param pos Target position.
93 * @param delay_seconds Delay after the previous step (0 = immediate on start).
94 */
95 Wiring& move_to(const glm::vec3& pos, double delay_seconds = 0.0);
96
97 /**
98 * @brief Drive position from a callable evaluated on each @c every() tick.
99 * @param fn Returns the new position on each invocation.
100 */
101 Wiring& position_from(PositionFn fn);
102
103 /**
104 * @brief Drive position from a named callable evaluated on each @c every() tick.
105 * @param fn_name Identifier used for state encoding.
106 * @param fn Returns the new position on each invocation.
107 */
108 Wiring& position_from(std::string fn_name, PositionFn fn);
109
110 /**
111 * @brief Repeat the configured sequence or choreography N times.
112 * @param count Number of repetitions.
113 */
114 Wiring& times(size_t count);
115
116 /**
117 * @brief Delegate coroutine creation entirely to the caller.
118 *
119 * Fabric registers the entity with the spatial index and adds the
120 * returned coroutine. All timing and entity commit logic is the
121 * caller's responsibility.
122 */
123 Wiring& use(SoundFactory factory);
124 Wiring& use(GraphicsFactory factory);
125 Wiring& use(ComplexFactory factory);
126 Wiring& use(EventFactory factory);
127
128 /** @brief Register a named factory. */
129 Wiring& use(std::string fn_name, SoundFactory factory);
130 Wiring& use(std::string fn_name, GraphicsFactory factory);
131 Wiring& use(std::string fn_name, ComplexFactory factory);
132 Wiring& use(std::string fn_name, EventFactory factory);
133
134 // =====================================================================
135 // Immediate bind — no coroutine
136 // =====================================================================
137
138 /**
139 * @brief Call the entity's influence function once immediately.
140 *
141 * No coroutine is created. If @c for_duration was set, a Timer fires
142 * the detach function after expiry.
143 */
144 Wiring& bind();
145
146 /**
147 * @brief Call a custom function once immediately instead of the entity's own.
148 * @param fn Callable to invoke in place of the entity's influence function.
149 */
150 Wiring& bind(std::function<void()> fn);
151
152 /**
153 * @brief Call attach immediately; call detach after @c for_duration expires
154 * or on explicit @c cancel().
155 * @param attach Called immediately on finalise().
156 * @param detach Called on expiry or cancellation.
157 */
158 Wiring& bind(std::function<void()> attach, std::function<void()> detach);
159
160 /** @brief Call a named custom function once immediately. */
161 Wiring& bind(std::string fn_name, std::function<void()> fn);
162
163 /** @brief Call named attach/detach functions. */
164 Wiring& bind(std::string attach_name, std::function<void()> attach,
165 std::string detach_name, std::function<void()> detach);
166
167 // =====================================================================
168 // Terminal
169 // =====================================================================
170
171 /**
172 * @brief Apply the configured wiring.
173 *
174 * Resolves the builder state into a coroutine (if any scheduling
175 * modifier was set) or an immediate call (if @c bind was used),
176 * and registers the result with Fabric.
177 */
178 void finalise();
179
180 /**
181 * @brief Cancel an active wiring and release any owned coroutine.
182 */
183 void cancel();
184
185private:
186 friend class Fabric;
187
188 explicit Wiring(Fabric& fabric, uint32_t entity_id)
189 : m_fabric(fabric)
190 , m_entity_id(entity_id)
191 {
192 }
193
194 // =====================================================================
195 // Stored configuration
196 // =====================================================================
197
198 struct MoveStep {
199 glm::vec3 position;
201 };
202
203 struct KeyTrigger {
204 std::shared_ptr<Core::Window> window;
206 };
207
209 std::shared_ptr<Core::Window> window;
211 };
212
216
221
222 using Trigger = std::variant<std::monostate, KeyTrigger, MouseTrigger, NetworkTrigger, EventTrigger>;
223 using Factory = std::variant<std::monostate, SoundFactory, GraphicsFactory, ComplexFactory>;
224 using EFactory = std::optional<EventFactory>;
225
226 std::string make_name(const char* prefix) const;
228 uint32_t m_entity_id;
229 bool m_has_scheduling {};
230
231 std::optional<double> m_interval;
232 std::optional<double> m_duration;
233 std::optional<PositionFn> m_position_fn;
235 std::vector<MoveStep> m_move_steps;
236 size_t m_times { 1 };
237
241 std::string m_factory_name;
242
243 std::optional<std::function<void()>> m_bind_attach;
244 std::optional<std::function<void()>> m_bind_detach;
247
248public:
249 // =====================================================================
250 // Move semantics
251 // =====================================================================
252 ~Wiring() = default;
253 Wiring(const Wiring&) = delete;
254 Wiring& operator=(const Wiring&) = delete;
255 Wiring(Wiring&&) noexcept = default;
256 Wiring& operator=(Wiring&&) = delete;
257
258 // =====================================================================
259 // Introspection
260 // =====================================================================
261
262 /** @brief Stable id of the wired entity. */
263 [[nodiscard]] uint32_t entity_id() const { return m_entity_id; }
264
265 /** @brief Recurring interval in seconds, if @c every was called. */
266 [[nodiscard]] std::optional<double> interval() const { return m_interval; }
267
268 /** @brief Active duration in seconds, if @c for_duration was called. */
269 [[nodiscard]] std::optional<double> duration() const { return m_duration; }
270
271 /** @brief Repetition count set by @c times, default 1. */
272 [[nodiscard]] size_t times_count() const { return m_times; }
273
274 /** @brief Choreography steps from @c move_to calls. */
275 [[nodiscard]] const std::vector<MoveStep>& move_steps() const { return m_move_steps; }
276
277 /** @brief Active trigger variant set by @c on. */
278 [[nodiscard]] const Trigger& trigger() const { return m_trigger; }
279
280 /** @brief Active factory variant set by @c use for non-Event factories. */
281 [[nodiscard]] const Factory& factory() const { return m_factory; }
282
283 /** @brief Active event-factory, if @c use(EventFactory) was called. */
284 [[nodiscard]] const EFactory& event_factory() const { return m_event_factory; }
285
286 /** @brief True if @c position_from was called. */
287 [[nodiscard]] bool has_position_fn() const { return m_position_fn.has_value(); }
288
289 /** @brief True if any @c bind overload was called. */
290 [[nodiscard]] bool has_bind() const { return m_bind_attach.has_value(); }
291
292 /** @brief True if @c bind(attach, detach) was called. */
293 [[nodiscard]] bool has_bind_detach() const { return m_bind_detach.has_value(); }
294
295 /** @brief Name of the position function, empty if anonymous. */
296 [[nodiscard]] const std::string& position_fn_name() const { return m_position_fn_name; }
297
298 /** @brief Name of the active factory, empty if anonymous or none. */
299 [[nodiscard]] const std::string& factory_name() const { return m_factory_name; }
300
301 /** @brief Name of the bind attach function, empty if anonymous or none. */
302 [[nodiscard]] const std::string& bind_attach_name() const { return m_bind_attach_name; }
303
304 /** @brief Name of the bind detach function, empty if anonymous or none. */
305 [[nodiscard]] const std::string& bind_detach_name() const { return m_bind_detach_name; }
306};
307
308} // namespace MayaFlux::Nexus
Eigen::Index count
double delay_seconds
std::optional< size_t > times
std::string fn_name
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:37
std::optional< std::function< void()> > m_bind_detach
Definition Wiring.hpp:244
bool has_position_fn() const
True if position_from was called.
Definition Wiring.hpp:287
const Trigger & trigger() const
Active trigger variant set by on.
Definition Wiring.hpp:278
std::string m_position_fn_name
Definition Wiring.hpp:234
bool has_bind_detach() const
True if bind(attach, detach) was called.
Definition Wiring.hpp:293
std::string m_bind_attach_name
Definition Wiring.hpp:245
std::variant< std::monostate, KeyTrigger, MouseTrigger, NetworkTrigger, EventTrigger > Trigger
Definition Wiring.hpp:222
const std::string & position_fn_name() const
Name of the position function, empty if anonymous.
Definition Wiring.hpp:296
const std::string & bind_attach_name() const
Name of the bind attach function, empty if anonymous or none.
Definition Wiring.hpp:302
const std::string & factory_name() const
Name of the active factory, empty if anonymous or none.
Definition Wiring.hpp:299
Wiring(Wiring &&) noexcept=default
const EFactory & event_factory() const
Active event-factory, if use(EventFactory) was called.
Definition Wiring.hpp:284
std::function< glm::vec3()> PositionFn
Definition Wiring.hpp:41
std::optional< std::function< void()> > m_bind_attach
Definition Wiring.hpp:243
const std::vector< MoveStep > & move_steps() const
Choreography steps from move_to calls.
Definition Wiring.hpp:275
std::optional< double > m_duration
Definition Wiring.hpp:232
std::function< Vruta::ComplexRoutine(Vruta::TaskScheduler &)> ComplexFactory
Definition Wiring.hpp:44
std::function< Vruta::GraphicsRoutine(Vruta::TaskScheduler &)> GraphicsFactory
Definition Wiring.hpp:43
std::optional< EventFactory > EFactory
Definition Wiring.hpp:224
Wiring & operator=(const Wiring &)=delete
std::function< Vruta::Event(Vruta::TaskScheduler &)> EventFactory
Definition Wiring.hpp:45
const std::string & bind_detach_name() const
Name of the bind detach function, empty if anonymous or none.
Definition Wiring.hpp:305
std::optional< double > m_interval
Definition Wiring.hpp:231
const Factory & factory() const
Active factory variant set by use for non-Event factories.
Definition Wiring.hpp:281
bool has_bind() const
True if any bind overload was called.
Definition Wiring.hpp:290
std::optional< double > interval() const
Recurring interval in seconds, if every was called.
Definition Wiring.hpp:266
std::optional< double > duration() const
Active duration in seconds, if for_duration was called.
Definition Wiring.hpp:269
Wiring(const Wiring &)=delete
std::string m_bind_detach_name
Definition Wiring.hpp:246
std::variant< std::monostate, SoundFactory, GraphicsFactory, ComplexFactory > Factory
Definition Wiring.hpp:223
std::vector< MoveStep > m_move_steps
Definition Wiring.hpp:235
std::string m_factory_name
Definition Wiring.hpp:241
size_t times_count() const
Repetition count set by times, default 1.
Definition Wiring.hpp:272
std::function< Vruta::SoundRoutine(Vruta::TaskScheduler &)> SoundFactory
Definition Wiring.hpp:42
std::optional< PositionFn > m_position_fn
Definition Wiring.hpp:233
Wiring(Fabric &fabric, uint32_t entity_id)
Definition Wiring.hpp:188
Fluent builder that wires an entity into Fabric's scheduling infrastructure.
Definition Wiring.hpp:39
Multi-domain coroutine that can handle multiple processing rates.
Definition Routine.hpp:665
Awaitable event stream for window events.
Coroutine type for event-driven suspension.
Definition Event.hpp:26
A C++20 coroutine-based graphics processing task with frame-accurate timing.
Definition Routine.hpp:513
Awaitable broadcast message stream for a network endpoint.
A C++20 coroutine-based audio processing task with sample-accurate timing.
Definition Routine.hpp:316
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
MouseButtons
Enumeration for mouse buttons.
Definition Keys.hpp:147
std::shared_ptr< Core::Window > window
Definition Wiring.hpp:204
std::shared_ptr< Core::Window > window
Definition Wiring.hpp:209
Filter criteria for window events.