recompose_core/
lib.rs

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
use bevy_app::{App, Plugin, PreUpdate};
use bevy_ecs::{
    component::{Component, ComponentHooks, ComponentId, StorageType},
    entity::Entity,
    query::Added,
    schedule::IntoSystemConfigs,
    system::{Commands, Query, SystemState},
    world::{DeferredWorld, World},
};
use bevy_hierarchy::{BuildChildren, Parent};
use bevy_reflect::Reflect;
use dyn_compose::DynCompose;
use paste::paste;
use scope::{Scope, ScopeId};
use spawn::update_spawn_composables;
use state::{SetState, StateChanged, StateSetter, StateSetterAction};
use std::{
    collections::{HashMap, HashSet, VecDeque},
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
};

pub mod bundle_extension;
pub mod dyn_compose;
pub mod keyed;
pub mod modify;
pub mod scope;
pub mod spawn;
pub mod state;

pub struct RecomposePlugin;

impl Plugin for RecomposePlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<StateSetter>()
            .register_type::<ChildOrder>()
            .add_systems(
                PreUpdate,
                (
                    initial_compose,
                    run_queued_systems,
                    drop_decomposed_scopes,
                    set_states,
                    recompose,
                    update_spawn_composables,
                    order_children,
                    decompose,
                )
                    .chain(),
            );
    }
}

// ===
// UniqueId
// ===

static UNIQUE_ID: AtomicUsize = AtomicUsize::new(0);

fn unique_id() -> usize {
    UNIQUE_ID.fetch_add(1, Ordering::Relaxed)
}

/// A trait that defines how a scope should be composed and decomposed. This trait is used to define the structure of a
/// scope. The `compose` function is called when the scope is composed or recomposed, and the `decompose` function is
/// called when the scope is decomposed.
pub trait Compose: Send + Sync {
    /// Compose the scope. This function is run when the composable is first initiated. It is then recomposed whenever
    /// it's parent scope is recomposed, or any of the states used inside the composable are changed.
    fn compose<'a>(&self, cx: &mut Scope) -> impl Compose + 'a;

    /// Decomposes the scope. This function is run when the composable is removed from the parent scope.
    fn decompose(&self, cx: &mut Scope) {
        let _ = cx;
    }

    /// Whether the children returned by the `compose` function should be ignored. This is useful when the composable
    /// has custom logic for handling children. This is mostly used internally.
    fn ignore_children(&self) -> bool {
        false
    }

    /// Returns the name of the composable. This is mostly intended for debugging purposes.
    fn name(&self) -> String {
        String::from("AnonymousCompose")
    }
}

impl Compose for () {
    fn compose<'a>(&self, _: &mut Scope) -> impl Compose + 'a {}

    fn ignore_children(&self) -> bool {
        true
    }

    fn name(&self) -> String {
        String::from("EmptyCompose")
    }
}

impl<C: Compose + Clone + 'static> Compose for Option<C> {
    fn compose<'a>(&self, _: &mut Scope) -> impl Compose + 'a {
        match self {
            Some(inner) => DynCompose::new(inner.clone()),
            None => DynCompose::new(()),
        }
    }

    fn name(&self) -> String {
        match self {
            Some(inner) => format!("Some({})", inner.name()),
            None => String::from("None"),
        }
    }
}

impl<C: Compose + 'static, F: (Fn(&mut Scope) -> C) + Send + Sync> Compose for F {
    fn compose<'a>(&self, cx: &mut Scope) -> impl Compose + 'a {
        self(cx)
    }

    fn name(&self) -> String {
        String::from("ClosureCompose")
    }
}

impl<K: Compose + Key + Clone + 'static> Compose for Vec<K> {
    fn compose<'a>(&self, cx: &mut Scope) -> impl Compose + 'a {
        let scope_ids = cx.use_state(HashMap::<usize, ScopeId>::new());

        let mut modified_scope_ids = (*scope_ids).clone();

        let keys = self.iter().map(|k| k.key()).collect::<Vec<_>>();

        let mut unique_keys = HashSet::new();
        let duplicate_key = keys.iter().find(|key| !unique_keys.insert(*key));

        if let Some(duplicate_key) = duplicate_key {
            panic!("Duplicate key found: {:?}", duplicate_key);
        }

        for (index, key_compose) in self.iter().enumerate() {
            let key = key_compose.key();
            let scope_id = scope_ids.get(&key);
            let scope =
                scope_id.and_then(|scope_id| cx.children.iter_mut().find(|s| s.id == *scope_id));

            if let Some(scope) = scope {
                scope.index = index;
                scope.composer = Arc::new(key_compose.clone());
                scope.composer.clone().recompose_scope(scope);
                continue;
            }

            let compose_name = key_compose.name();
            let compose = Arc::new(key_compose.clone());
            let mut scope = Scope::new(compose, index, compose_name);
            key_compose.recompose_scope(&mut scope);

            modified_scope_ids.insert(key, scope.id);
            cx.children.push(scope);
        }

        for (key, scope_id) in modified_scope_ids.clone().iter() {
            if keys.contains(key) {
                continue;
            }

            modified_scope_ids.remove(key);

            let Some(scope) = cx.children.iter_mut().find(|scope| scope.id == *scope_id) else {
                continue;
            };

            scope.will_decompose = true;
        }

        cx.set_state(&scope_ids, modified_scope_ids);
    }

    fn ignore_children(&self) -> bool {
        true
    }

    fn name(&self) -> String {
        String::from("VecCompose")
    }
}

macro_rules! impl_compose_for_tuple {
    ($($c:expr),*) => {
        paste! {
            impl<$([<C$c>]: Compose + Clone + 'static),*> Compose for ($([<C$c>]),*) {
                fn compose<'a>(&self, cx: &mut Scope) -> impl Compose + 'a {
                    $(
                        if let Some(existing_scope) = cx.children.get_mut($c) {
                            existing_scope.composer = Arc::new(self.$c.clone());
                            existing_scope
                                .composer
                                .clone()
                                .recompose_scope(existing_scope);
                        } else {
                            let compose = Arc::new(self.$c.clone());
                            let compose_name = compose.name();
                            let mut scope = Scope::new(compose, $c, compose_name);
                            self.$c.recompose_scope(&mut scope);
                            cx.children.push(scope);
                        }
                    )*
                }

                fn ignore_children(&self) -> bool {
                    true
                }

                fn name(&self) -> String {
                    String::from("TupleCompose")
                }
            }
        }
    };
}

impl_compose_for_tuple!(0, 1);
impl_compose_for_tuple!(0, 1, 2);
impl_compose_for_tuple!(0, 1, 2, 3);
impl_compose_for_tuple!(0, 1, 2, 3, 4);
impl_compose_for_tuple!(0, 1, 2, 3, 4, 5);
impl_compose_for_tuple!(0, 1, 2, 3, 4, 5, 6);
impl_compose_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7);
impl_compose_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8);
impl_compose_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

// ===
// Key
// ===

pub trait Key: Send + Sync {
    fn key(&self) -> usize;
}

/// A trait that (re)composes and decomposes a scope. It is used to act as a "wrapper" for the `Compose` trait, which
/// itself is not dyn-compatible. Since this trait is dyn-compatible, it can be stored in a `Box` or `Arc`.
pub trait AnyCompose: Send + Sync {
    /// This function is similar to the `compose` function on the `Compose` trait, but rather than returning the
    /// children, it sets the children directly to the passed scope (if having children is desirable). Doing it this
    /// way allows this trait to be dyn-compatible, which allows us to store it in a `Box` or `Arc`.
    fn recompose_scope(&self, scope: &mut Scope);

    /// This function decomposes the scope. Usually this calls the `decompose` function on the `Compose` trait directly.
    fn decompose_scope(&self, scope: &mut Scope);

    /// Returns the name of the composable. This is mostly intended for debugging purposes.
    fn get_name(&self) -> String;
}

impl<C: Compose> AnyCompose for C {
    // TODO: Make this take in the new compose value and index, since we basicall always need to set it anyways
    fn recompose_scope(&self, scope: &mut Scope) {
        scope.state_index = 0;

        for state in scope.states.iter_mut() {
            if matches!(state.changed, StateChanged::Queued) {
                state.changed = StateChanged::Changed;
            }
        }

        let child = self.compose(scope);

        for state in scope.states.iter_mut() {
            if matches!(state.changed, StateChanged::Changed) {
                state.changed = StateChanged::Unchanged;
            }
        }

        if self.ignore_children() {
            return;
        }

        if let Some(child_scope) = scope.children.first_mut() {
            child_scope.composer = Arc::new(child);
            child_scope.composer.clone().recompose_scope(child_scope);
            return;
        };

        let child_compose = Arc::new(child);
        let compose_name = child_compose.name();
        let mut child_scope = Scope::new(child_compose.clone(), 0, compose_name);

        child_compose.recompose_scope(&mut child_scope);

        scope.children.push(child_scope);
    }

    fn decompose_scope(&self, scope: &mut Scope) {
        self.decompose(scope);
    }

    fn get_name(&self) -> String {
        self.name()
    }
}

// ===
// Systems
// ===

fn initial_compose(mut roots: Query<(Entity, &mut Root), Added<Root>>) {
    for (entity, mut root) in roots.iter_mut() {
        let mut scope = Scope::as_root_scope(entity, root.compose.clone(), root.compose.get_name());

        root.compose.recompose_scope(&mut scope);

        root.scope = Some(scope);
    }
}

fn run_queued_systems(world: &mut World) {
    let mut roots_system_state = SystemState::<Query<&mut Root>>::new(world);
    let mut roots = roots_system_state.get_mut(world);

    let mut queued_systems = vec![];

    for mut root in roots.iter_mut() {
        let Some(scope) = &mut root.scope else {
            continue;
        };

        let mut scopes = VecDeque::from([scope]);

        while let Some(scope) = scopes.pop_front() {
            queued_systems.append(&mut scope.queued_systems);

            for child in scope.children.iter_mut().rev() {
                scopes.push_front(child);
            }
        }
    }

    for mut system in queued_systems {
        system.initialize(world);
        system.run((), world);
        system.apply_deferred(world);
    }
}

fn drop_decomposed_scopes(mut roots: Query<&mut Root>) {
    for mut root in roots.iter_mut() {
        let Some(scope) = &mut root.scope else {
            continue;
        };

        let mut scopes = VecDeque::from([scope]);

        while let Some(scope) = scopes.pop_front() {
            scope.children.retain(|child| !child.will_decompose);
            for child in scope.children.iter_mut().rev() {
                scopes.push_front(child);
            }
        }
    }
}

fn set_states(mut setter: SetState, mut roots: Query<&mut Root>) {
    for mut root in roots.iter_mut() {
        let Some(scope) = &mut root.scope else {
            continue;
        };

        let mut scopes = VecDeque::from([scope]);

        while let Some(scope) = scopes.pop_front() {
            for state in scope.states.iter_mut() {
                let Some(state_setter_action) = setter.setter.queued.remove(&state.id) else {
                    continue;
                };

                if !matches!(state_setter_action, StateSetterAction::SetUnchanged(_)) {
                    state.changed = StateChanged::Queued;
                }

                state.value = match state_setter_action {
                    StateSetterAction::Set(value) => value,
                    StateSetterAction::SetUnchanged(value) => value,
                    StateSetterAction::Modify(f) => f(state.value.clone()),
                };
            }

            for child in scope.children.iter_mut().rev() {
                scopes.push_front(child);
            }
        }
    }
}

fn recompose(mut roots: Query<&mut Root>) {
    for mut root in roots.iter_mut() {
        let Some(scope) = &mut root.scope else {
            continue;
        };

        let mut scopes = VecDeque::from([scope]);

        while let Some(scope) = scopes.pop_front() {
            if scope
                .states
                .iter()
                .any(|state| matches!(state.changed, StateChanged::Queued))
                && !scope.will_decompose
            {
                let composer = scope.composer.clone();

                composer.recompose_scope(scope);
                continue;
            }

            for child in scope.children.iter_mut().rev() {
                scopes.push_front(child);
            }
        }
    }
}

#[derive(Component, Clone, Copy, PartialEq, PartialOrd, Reflect)]
pub(crate) struct ChildOrder(pub f64);

fn order_children(mut commands: Commands, parents: Query<(Entity, &Parent, &ChildOrder)>) {
    let mut parent_children = HashMap::<Entity, Vec<(Entity, ChildOrder)>>::new();

    for (entity, parent, order) in parents.iter() {
        let parent_entity = parent.get();
        let entry = parent_children.get_mut(&parent_entity);

        if let Some(entry) = entry {
            entry.push((entity, *order));
        } else {
            parent_children.insert(parent_entity, vec![(entity, *order)]);
        }
    }

    for (parent_entity, children) in parent_children.iter_mut() {
        // We never expect to have NaN values, so we can safely unwrap here
        children.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());

        for (entity, _) in children.iter() {
            let Some(mut ec) = commands.get_entity(*entity) else {
                continue;
            };

            ec.set_parent(*parent_entity);
        }
    }
}

fn decompose(mut roots: Query<&mut Root>) {
    for mut root in roots.iter_mut() {
        let Some(scope) = &mut root.scope else {
            continue;
        };

        let mut scopes = VecDeque::from([scope]);

        while let Some(scope) = scopes.pop_front() {
            if scope.will_decompose {
                let composer = scope.composer.clone();
                composer.decompose_scope(scope);

                for child in scope.children.iter_mut() {
                    child.will_decompose = true;
                }
            }

            for child in scope.children.iter_mut().rev() {
                scopes.push_front(child);
            }
        }
    }
}

pub struct Root {
    compose: Arc<dyn AnyCompose>,
    scope: Option<Scope<'static>>,
}

impl Component for Root {
    const STORAGE_TYPE: StorageType = StorageType::Table;

    fn register_component_hooks(hooks: &mut ComponentHooks) {
        fn decompose_root(mut world: DeferredWorld, entity: Entity, _: ComponentId) {
            let Some(mut roots) = world.get_mut::<Root>(entity) else {
                return;
            };

            let Some(ref mut scope) = roots.scope else {
                return;
            };

            let mut scopes = VecDeque::from([scope]);

            while let Some(scope) = scopes.pop_front() {
                let composer = scope.composer.clone();
                composer.decompose_scope(scope);
                for child in scope.children.iter_mut().rev() {
                    scopes.push_front(child);
                }
            }
        }

        hooks.on_replace(decompose_root);
        hooks.on_remove(decompose_root);
    }
}

impl Root {
    pub fn new<C: Compose + 'static>(composer: C) -> Self {
        Self {
            compose: Arc::new(composer),
            scope: None,
        }
    }
}