mittens-engine 0.7.0

A Vulkan and OpenXR scene engine with ECS, reactive signals, and Meow Meow scripting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::collections::HashMap;

use crate::engine::ecs::{ComponentId, World};

use super::{
    EventSignal, IntentSignal, Signal, SignalEmitter, SignalHandler, SignalKind, SignalWhen,
};

use super::signal_pipeline::SignalPipeline;

enum Handler {
    Fn {
        handler: SignalHandler,
        name: Option<String>,
    },
    Closure {
        handler:
            Box<dyn FnMut(&mut World, &mut dyn SignalEmitter, &Signal) + Send + Sync + 'static>,
        name: Option<String>,
    },
}

impl Handler {
    fn name(&self) -> Option<&str> {
        match self {
            Handler::Fn { name, .. } => name.as_deref(),
            Handler::Closure { name, .. } => name.as_deref(),
        }
    }
}

struct Emitter {
    intents: *mut Vec<Signal>,
    pending_intents: *mut Vec<Signal>,
    events_out: *mut Vec<Signal>,
}

impl SignalEmitter for Emitter {
    fn push_event(&mut self, scope: ComponentId, event: EventSignal) {
        // SAFETY: pointers refer to `RxWorld` storage for the duration of dispatch.
        unsafe {
            (*self.events_out).push(Signal::event(scope, event));
        }
    }

    fn push_intent(&mut self, scope: ComponentId, intent: IntentSignal) {
        // SAFETY: pointers refer to `RxWorld` storage for the duration of dispatch.
        unsafe {
            match intent.when {
                SignalWhen::Now => (*self.intents).push(Signal::intent(scope, intent)),
                SignalWhen::AtBeat(_) => {
                    (*self.pending_intents).push(Signal::intent(scope, intent));
                    sort_pending_intents_by_beat(&mut *self.pending_intents);
                }
            }
        }
    }
}

impl SignalEmitter for RxWorld {
    fn push_event(&mut self, scope: ComponentId, event: EventSignal) {
        RxWorld::push_event(self, scope, event);
    }

    fn push_intent(&mut self, scope: ComponentId, intent: IntentSignal) {
        RxWorld::push_intent(self, scope, intent);
    }
}

/// Stores a unified signal stream and dispatches signals to scope-rooted handlers.
///
/// Handlers are keyed by (signal kind, scope root). When dispatching a signal with `scope=S`,
/// handlers attached to `S` or any ancestor of `S` are invoked.
#[derive(Default)]
pub struct RxWorld {
    /// Ready event signals for this tick.
    ready_events: Vec<Signal>,

    /// Event signals produced while processing (handlers/executor). These are deferred until the
    /// next tick.
    deferred_events: Vec<Signal>,

    /// Ready intent signals for this tick.
    ready_intents: Vec<Signal>,

    /// Timed holding-pen for intent signals that should not run until a target transport beat.
    ///
    /// Invariant: this list is kept sorted by target beat ascending.
    pending_intents: Vec<Signal>,

    /// Scoped handlers keyed by (kind, scope_root).
    scoped_handlers: HashMap<SignalKind, HashMap<ComponentId, Vec<Handler>>>,

    /// Optional global handlers keyed by signal kind.
    ///
    /// These are useful for system-level observers (gesture/editor) that need to see events
    /// regardless of scope.
    global_handlers: HashMap<SignalKind, Vec<Handler>>,

    /// Component-targeted intent routing pipelines.
    ///
    /// Keyed by the component id the pipeline applies to.
    pipelines_by_component: HashMap<ComponentId, Vec<SignalPipeline>>,

    /// Reverse index for efficient removal: operator component id -> pipeline owner component id.
    pipeline_owner_by_operator: HashMap<ComponentId, ComponentId>,
}

impl std::fmt::Debug for RxWorld {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RxWorld")
            .field("ready_events_len", &self.ready_events.len())
            .field("deferred_events_len", &self.deferred_events.len())
            .field("ready_intents_len", &self.ready_intents.len())
            .field("pending_intents_len", &self.pending_intents.len())
            .field("global_kinds", &self.global_handlers.len())
            .field("scoped_kinds", &self.scoped_handlers.len())
            .field(
                "pipeline_owner_by_operator_len",
                &self.pipeline_owner_by_operator.len(),
            )
            .field(
                "pipelines_by_component_len",
                &self.pipelines_by_component.len(),
            )
            .finish()
    }
}

impl RxWorld {
    pub(crate) fn pipelines_for_component(&self, component: ComponentId) -> &[SignalPipeline] {
        static EMPTY: [SignalPipeline; 0] = [];
        self.pipelines_by_component
            .get(&component)
            .map(|v| v.as_slice())
            .unwrap_or(&EMPTY)
    }

    pub(crate) fn register_pipeline(&mut self, owner: ComponentId, pipeline: SignalPipeline) {
        self.remove_pipelines_from_operator(pipeline.source_operator);

        self.pipeline_owner_by_operator
            .insert(pipeline.source_operator, owner);
        self.pipelines_by_component
            .entry(owner)
            .or_default()
            .push(pipeline);
    }

    pub(crate) fn remove_pipelines_from_operator(&mut self, operator: ComponentId) {
        let Some(owner) = self.pipeline_owner_by_operator.remove(&operator) else {
            return;
        };

        let Some(list) = self.pipelines_by_component.get_mut(&owner) else {
            return;
        };

        list.retain(|p| p.source_operator != operator);
        if list.is_empty() {
            self.pipelines_by_component.remove(&owner);
        }
    }

    pub fn push_event(&mut self, scope: ComponentId, event: EventSignal) {
        self.ready_events.push(Signal::event(scope, event));
    }

    pub fn push_intent(&mut self, scope: ComponentId, intent: IntentSignal) {
        match intent.when {
            SignalWhen::Now => self.ready_intents.push(Signal::intent(scope, intent)),
            SignalWhen::AtBeat(_) => {
                self.pending_intents.push(Signal::intent(scope, intent));
                sort_pending_intents_by_beat(&mut self.pending_intents);
            }
        }
    }

    /// Move any pending timed signals whose target beat is now due into the per-frame queue.
    ///
    /// Returns the number of promoted signals.
    pub fn promote_due_intents(&mut self, now_beat: f64) -> usize {
        if self.pending_intents.is_empty() {
            return 0;
        }

        let eps = 1e-9;
        let mut end = 0usize;
        while end < self.pending_intents.len() {
            let Some(intent) = self.pending_intents[end].intent.as_ref() else {
                end += 1;
                continue;
            };
            let SignalWhen::AtBeat(b) = intent.when else {
                end += 1;
                continue;
            };
            if b <= now_beat + eps {
                end += 1;
            } else {
                break;
            }
        }

        if end == 0 {
            return 0;
        }

        let due: Vec<Signal> = self.pending_intents.drain(..end).collect();
        let promoted = due.len();
        self.ready_intents.extend(due);
        promoted
    }

    /// Reset drain-point dispatch state for a new frame.
    ///
    /// In the current architecture, signals are typically drained once per frame in
    /// `SystemWorld::process_commands`, which also implicitly resets this cursor.
    pub fn begin_frame(&mut self) {
        if !self.deferred_events.is_empty() {
            self.ready_events
                .extend(std::mem::take(&mut self.deferred_events));
        }
    }

    /// Returns the current queued signals for this frame.
    ///
    /// This is intentionally read-only: signals are drained and dispatched later in
    /// `SystemWorld::process_commands`.
    pub fn drain_ready_events(&mut self) -> Vec<Signal> {
        std::mem::take(&mut self.ready_events)
    }

    pub fn drain_ready_intents(&mut self) -> Vec<Signal> {
        std::mem::take(&mut self.ready_intents)
    }

    pub fn has_ready_events(&self) -> bool {
        !self.ready_events.is_empty()
    }

    pub fn has_ready_intents(&self) -> bool {
        !self.ready_intents.is_empty()
    }

    pub fn has_global_handlers(&self, kind: SignalKind) -> bool {
        self.global_handlers
            .get(&kind)
            .is_some_and(|handlers| !handlers.is_empty())
    }

    pub fn requeue_ready_events(&mut self, mut events: Vec<Signal>) {
        if events.is_empty() {
            return;
        }
        self.ready_events.append(&mut events);
    }

    pub fn requeue_ready_intents(&mut self, mut intents: Vec<Signal>) {
        if intents.is_empty() {
            return;
        }
        self.ready_intents.append(&mut intents);
    }

    /// Add a scoped handler rooted at `scope_root`.
    ///
    /// Note: this is a function pointer (no captures). Use `add_handler_closure` internally
    /// when you need stateful handlers.
    pub fn add_handler(
        &mut self,
        kind: SignalKind,
        scope_root: ComponentId,
        handler: SignalHandler,
    ) {
        self.add_handler_named(kind, scope_root, None, handler);
    }

    /// Add a named scoped handler rooted at `scope_root`.
    pub fn add_handler_named(
        &mut self,
        kind: SignalKind,
        scope_root: ComponentId,
        name: Option<String>,
        handler: SignalHandler,
    ) {
        let list = self
            .scoped_handlers
            .entry(kind)
            .or_default()
            .entry(scope_root)
            .or_default();

        // Idempotent registration for function-pointer handlers.
        // This avoids duplicate dispatches if a system registers handlers multiple times
        // for the same (kind, scope_root).
        let handler_usize = handler as usize;
        if list.iter().any(|h| match h {
            Handler::Fn {
                handler: fp,
                name: existing_name,
            } => *fp as usize == handler_usize && *existing_name == name,
            _ => false,
        }) {
            return;
        }

        list.push(Handler::Fn { handler, name });
    }

    /// Add a scoped handler closure rooted at `scope_root`.
    pub fn add_handler_closure(
        &mut self,
        kind: SignalKind,
        scope_root: ComponentId,
        handler: impl FnMut(&mut World, &mut dyn SignalEmitter, &Signal) + Send + Sync + 'static,
    ) {
        self.add_handler_closure_named(kind, scope_root, None, handler);
    }

    /// Add a named scoped handler closure rooted at `scope_root`.
    pub fn add_handler_closure_named(
        &mut self,
        kind: SignalKind,
        scope_root: ComponentId,
        name: Option<String>,
        handler: impl FnMut(&mut World, &mut dyn SignalEmitter, &Signal) + Send + Sync + 'static,
    ) {
        self.scoped_handlers
            .entry(kind)
            .or_default()
            .entry(scope_root)
            .or_default()
            .push(Handler::Closure {
                handler: Box::new(handler),
                name,
            });
    }

    /// Add a global handler rooted at no scope.
    ///
    /// Note: this is a function pointer (no captures). Use `add_global_handler_closure`
    /// when you need stateful handlers.
    pub fn add_global_handler(&mut self, kind: SignalKind, handler: SignalHandler) {
        self.add_global_handler_named(kind, None, handler);
    }

    /// Add a named global handler rooted at no scope.
    pub fn add_global_handler_named(
        &mut self,
        kind: SignalKind,
        name: Option<String>,
        handler: SignalHandler,
    ) {
        let list = self.global_handlers.entry(kind).or_default();

        let handler_usize = handler as usize;
        if list.iter().any(|h| match h {
            Handler::Fn {
                handler: fp,
                name: existing_name,
            } => *fp as usize == handler_usize && *existing_name == name,
            _ => false,
        }) {
            return;
        }

        list.push(Handler::Fn { handler, name });
    }

    /// Add a global handler closure rooted at no scope.
    pub fn add_global_handler_closure(
        &mut self,
        kind: SignalKind,
        handler: impl FnMut(&mut World, &mut dyn SignalEmitter, &Signal) + Send + Sync + 'static,
    ) {
        self.add_global_handler_closure_named(kind, None, handler);
    }

    /// Add a named global handler closure rooted at no scope.
    pub fn add_global_handler_closure_named(
        &mut self,
        kind: SignalKind,
        name: Option<String>,
        handler: impl FnMut(&mut World, &mut dyn SignalEmitter, &Signal) + Send + Sync + 'static,
    ) {
        self.global_handlers
            .entry(kind)
            .or_default()
            .push(Handler::Closure {
                handler: Box::new(handler),
                name,
            });
    }

    pub fn remove_handler(
        &mut self,
        kind: SignalKind,
        scope_root: ComponentId,
        handler: SignalHandler,
    ) -> bool {
        let Some(by_scope) = self.scoped_handlers.get_mut(&kind) else {
            return false;
        };
        let Some(list) = by_scope.get_mut(&scope_root) else {
            return false;
        };

        let before = list.len();
        list.retain(|h| match h {
            Handler::Fn {
                handler: fp,
                name: _,
            } => *fp as usize != handler as usize,
            Handler::Closure { .. } => true,
        });
        let removed = list.len() != before;

        if list.is_empty() {
            by_scope.remove(&scope_root);
        }
        if by_scope.is_empty() {
            self.scoped_handlers.remove(&kind);
        }

        removed
    }

    /// Remove all *scoped* handlers rooted at `scope_root`.
    ///
    /// This is intended for component lifecycle cleanup: when a component (or subtree) is removed
    /// from the `World`, any handlers rooted at those component ids should be removed to avoid
    /// unbounded growth of the handler maps.
    pub fn remove_all_scoped_handlers_for_scope(&mut self, scope_root: ComponentId) -> usize {
        if self.scoped_handlers.is_empty() {
            return 0;
        }

        let mut removed = 0usize;

        // Clone keys to avoid borrowing issues while mutating the map.
        let kinds: Vec<SignalKind> = self.scoped_handlers.keys().copied().collect();
        for kind in kinds {
            let Some(by_scope) = self.scoped_handlers.get_mut(&kind) else {
                continue;
            };

            if let Some(list) = by_scope.remove(&scope_root) {
                removed += list.len();
            }

            if by_scope.is_empty() {
                self.scoped_handlers.remove(&kind);
            }
        }

        removed
    }

    pub fn remove_all_scoped_handlers_for_scopes(
        &mut self,
        scopes: impl IntoIterator<Item = ComponentId>,
    ) -> usize {
        let mut removed = 0usize;
        for scope in scopes {
            removed += self.remove_all_scoped_handlers_for_scope(scope);
        }
        removed
    }

    pub fn dispatch_event_handlers(&mut self, world: &mut World, env: &Signal) {
        let Some(event) = env.event.as_ref() else {
            return;
        };
        let kind = event.kind();

        let mut emitter = Emitter {
            intents: &mut self.ready_intents as *mut Vec<Signal>,
            pending_intents: &mut self.pending_intents as *mut Vec<Signal>,
            events_out: &mut self.deferred_events as *mut Vec<Signal>,
        };

        dispatch_global_kind(self, world, &mut emitter, SignalKind::Any, env);
        dispatch_global_kind(self, world, &mut emitter, kind, env);

        let scope_chain = compute_scope_chain(world, env.scope);
        for scope in scope_chain {
            dispatch_scoped_kind(self, world, &mut emitter, SignalKind::Any, scope, env);
            dispatch_scoped_kind(self, world, &mut emitter, kind, scope, env);
        }
    }
}

fn dispatch_global_kind(
    rx: &mut RxWorld,
    world: &mut World,
    emit: &mut dyn SignalEmitter,
    kind: SignalKind,
    env: &Signal,
) {
    let Some(list) = rx.global_handlers.get_mut(&kind) else {
        return;
    };

    for handler in list {
        match handler {
            Handler::Fn { handler: fp, .. } => fp(world, emit, env),
            Handler::Closure { handler: c, .. } => c(world, emit, env),
        }
    }
}

fn compute_scope_chain(world: &World, start: ComponentId) -> Vec<ComponentId> {
    let mut chain = Vec::new();
    let mut cursor = Some(start);
    while let Some(node) = cursor {
        chain.push(node);
        cursor = world.parent_of(node);
    }
    chain
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::ecs::IntentValue;
    use slotmap::KeyData;

    fn cid(ffi: u64) -> ComponentId {
        KeyData::from_ffi(ffi).into()
    }

    #[test]
    fn timed_signals_are_held_until_due() {
        let mut rx = RxWorld::default();

        rx.push_intent(cid(1), IntentSignal::at_beat(10.0, IntentValue::Noop));
        assert_eq!(rx.ready_intents.len(), 0);

        rx.push_intent_now(
            cid(1),
            IntentValue::Print {
                message: "hi".to_string(),
            },
        );
        assert_eq!(rx.ready_intents.len(), 1);

        assert_eq!(rx.promote_due_intents(0.0), 0);
        assert_eq!(rx.ready_intents.len(), 1);

        assert_eq!(rx.promote_due_intents(10.0), 1);
        assert_eq!(rx.ready_intents.len(), 2);

        // Drain should clear only ready intents.
        let drained = rx.drain_ready_intents();
        assert_eq!(drained.len(), 2);
        assert_eq!(rx.ready_intents.len(), 0);
    }
}

fn sort_pending_intents_by_beat(pending: &mut Vec<Signal>) {
    pending.sort_by(|a, b| {
        let ba = a
            .intent
            .as_ref()
            .and_then(|i| i.when.beat())
            .unwrap_or(f64::NEG_INFINITY);
        let bb = b
            .intent
            .as_ref()
            .and_then(|i| i.when.beat())
            .unwrap_or(f64::NEG_INFINITY);
        ba.partial_cmp(&bb).unwrap_or(std::cmp::Ordering::Equal)
    });
}

fn dispatch_scoped_kind(
    rx: &mut RxWorld,
    world: &mut World,
    emitter: &mut dyn SignalEmitter,
    kind: SignalKind,
    scope: ComponentId,
    env: &Signal,
) {
    let Some(by_scope) = rx.scoped_handlers.get_mut(&kind) else {
        return;
    };
    let Some(handlers) = by_scope.get_mut(&scope) else {
        return;
    };

    let router = world.children_of(scope).iter().find_map(|&ch| {
        world
            .get_component_by_id_as::<crate::engine::ecs::component::SignalObserverRouterComponent>(
                ch,
            )
    });
    let (blacklist, whitelist) = if let Some(router) = router {
        (
            Some(router.blacklist.clone()),
            Some(router.whitelist.clone()),
        )
    } else {
        (None, None)
    };

    for idx in 0..handlers.len() {
        let handler_ptr: *mut Handler = &mut handlers[idx];
        unsafe {
            if let Some(name) = (*handler_ptr).name() {
                if let Some(blacklist) = blacklist.as_ref()
                    && blacklist.iter().any(|b| b == name)
                {
                    continue;
                }
                if let Some(whitelist) = whitelist.as_ref()
                    && !whitelist.is_empty()
                    && !whitelist.iter().any(|w| w == name)
                {
                    continue;
                }
            }

            match &mut *handler_ptr {
                Handler::Fn { handler: fp, .. } => fp(world, emitter, env),
                Handler::Closure { handler: f, .. } => f(world, emitter, env),
            }
        }
    }
}