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
//! Common components used by the `arcs` CAD library.

/// A macro which, coupled with [`crate::components::ComponentVtable`], allows
/// the CAD engine to do things like component discovery (for
/// [`crate::components::register()`]) and undo/redo.
#[macro_export]
macro_rules! decl_component {
    ($type:ty) => {
        inventory::submit! {
            $crate::components::ComponentVtable::for_type::<$type>()
        }
    };
}

mod bounding_box;
mod dimension;
mod drawing_object;
mod layer;
mod name;
mod styles;
mod viewport;
mod vtable;

pub use bounding_box::BoundingBox;
pub use dimension::Dimension;
pub use drawing_object::{DrawingObject, Geometry};
pub use layer::Layer;
pub use name::{Name, NameTable};
pub use styles::{LineStyle, PointStyle, WindowStyle};
pub use viewport::Viewport;
pub use vtable::ComponentVtable;

use specs::{Entity, World};
use std::any::Any;

/// Register all [`specs::Component`]s.
pub fn register(world: &mut World) {
    for vtable in inventory::iter::<ComponentVtable> {
        vtable.register(world);
    }
}

/// Looks up all [`specs::Component`]s associated with this [`Entity`], yielding
/// copies of their current value.
///
/// # Note
///
/// This requires the [`decl_component!()`] macro to have been invoked on
/// the [`specs::Component`].
pub fn attached_to_entity(
    world: &World,
    entity: Entity,
) -> impl Iterator<Item = (&'static ComponentVtable, Box<dyn Any>)> + '_ {
    inventory::iter::<ComponentVtable>
        .into_iter()
        .filter_map(move |vtable| match vtable.get_cloned(world, entity) {
            Some(got) => Some((vtable, got)),
            None => None,
        })
}