Repository: bevyengine/bevy
File: crates/bevy_feathers/src/alpha_pattern.rs (Rust)

Code before:

impl UiMaterial for AlphaPatternMaterial {
    fn fragment_shader() -> ShaderRef {
        "embedded://bevy_feathers/assets/shaders/alpha_pattern.wgsl".into()
    }
}

#[derive(Resource)]
pub(crate) struct AlphaPatternResource(pub(crate) Handle<AlphaPatternMaterial>);

impl FromWorld for AlphaPatternResource {
    fn from_world(world: &mut bevy_ecs::world::World) -> Self {
        let mut ui_materials = world
            .get_resource_mut::<Assets<AlphaPatternMaterial>>()
            .unwrap();
        Self(ui_materials.add(AlphaPatternMaterial::default()))
    }
}

/// Marker that tells us we want to fill in the [`MaterialNode`] with the alpha material.
#[derive(Component, Default, Clone)]
pub(crate) struct AlphaPattern;

/// Observer to fill in the material handle (since we don't have access to the materials asset
/// in the template)
fn on_add_color_swatch(
    ev: On<Add, AlphaPattern>,
    mut q_swatch: Query<&mut MaterialNode<AlphaPatternMaterial>>,
    r_material: Res<AlphaPatternResource>,
) {
    if let Ok(mut material) = q_swatch.get_mut(ev.target()) {
        material.0 = r_material.0.clone();
    }
}

/// Plugin which registers the systems for updating the button styles.
pub struct AlphaPatternPlugin;

impl Plugin for AlphaPatternPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        app.add_observer(on_add_color_swatch);
    }
}


Review comment (suggestion):
I don't want to nitpick at all, but when I read the name of the function and then the list of parameters, I expected something like "On<Add, ColorSwatch>" because that's literally what the function is named. Instead it's AlphaPattern.

Maybe AlphaPattern should be renamed? Or this function? (Yes, I saw line 44 and the mut q_swatch, which again doesn't query a ColorSwatch, but a MaterialNode<AlphaPatternMaterial>

Code after:

impl UiMaterial for AlphaPatternMaterial {
    fn fragment_shader() -> ShaderRef {
        "embedded://bevy_feathers/assets/shaders/alpha_pattern.wgsl".into()
    }
}

#[derive(Resource)]
pub(crate) struct AlphaPatternResource(pub(crate) Handle<AlphaPatternMaterial>);

impl FromWorld for AlphaPatternResource {
    fn from_world(world: &mut bevy_ecs::world::World) -> Self {
        let mut ui_materials = world
            .get_resource_mut::<Assets<AlphaPatternMaterial>>()
            .unwrap();
        Self(ui_materials.add(AlphaPatternMaterial::default()))
    }
}

/// Marker that tells us we want to fill in the [`MaterialNode`] with the alpha material.
#[derive(Component, Default, Clone)]
pub(crate) struct AlphaPattern;

/// Observer to fill in the material handle (since we don't have access to the materials asset
/// in the template)
fn on_add_alpha_pattern(
    ev: On<Add, AlphaPattern>,
    mut q_material_node: Query<&mut MaterialNode<AlphaPatternMaterial>>,
    r_material: Res<AlphaPatternResource>,
) {
    if let Ok(mut material) = q_material_node.get_mut(ev.target()) {
        material.0 = r_material.0.clone();
    }
}

/// Plugin which registers the systems for updating the button styles.
pub struct AlphaPatternPlugin;

impl Plugin for AlphaPatternPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        app.add_observer(on_add_alpha_pattern);
    }
}