Conversation

eli (ˈe̝ːli), vampire kitsune

Edited 7 days ago
game dev, video, sound warning
Show content

also quick vid of that data binding thing i was talking about

1
0
3

eli (ˈe̝ːli), vampire kitsune

code, not screen reader friendly re: game dev, video, sound warning
Show content

associated code. i’m really happy with how simple this is to use

#[derive(Clone, Component, Reflect)]
#[reflect(Component)]
struct StringDataSource(String);

#[derive(Clone, Component, Reflect)]
#[reflect(Component)]
struct F32DataSource(f32);

fn spawn_ui() {
    // these are the components being bound to
    let source = commands
        .spawn((
            Name::new("data source controlling the text content"),
            StringDataSource("test".to_string()),
        ))
        .id();

    let source2 = commands
        .spawn((
            Name::new("data source controlling the text size"),
            F32DataSource(64.),
        ))
        .id();

    let children = (
        Name::new("ui children"),
        Text::new(""),
        TextColor(Color::WHITE),
        TextFont::from_font_size(64.),
        // this establishes the component's relationship to the data sources
        BindTarget::from_collection([source, source2].into()),
        // these are the binding components which describes how to get and apply the changes
        Binding::new(|source: &F32DataSource, text: &mut TextFont| {
            text.font_size = source.0;
        }),
        Binding::new(|source: &StringDataSource, text: &mut Text| {
            text.0 = source.0.to_string();
        }),
    );
}
0
0
3