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
use super::selection::*;
use crate::PausedForBlockers;
use bevy::{asset::Asset, prelude::*, render::color::Color};

/// Marker component to flag an entity as highlightable
#[derive(Component, Clone, Debug, Default)]
pub struct Highlight;

/// Component used to track the initial asset of a highlightable object, as well as for overriding
/// the default highlight materials.
#[derive(Component, Clone, Debug)]
pub struct Highlighting<T: Asset> {
    pub initial: Handle<T>,
    pub hovered: Option<Handle<T>>,
    pub pressed: Option<Handle<T>>,
    pub selected: Option<Handle<T>>,
}

/// Resource that defines the default highlighting assets to use. This can be overridden per-entity
/// with the [`Highlighting`] component.
pub struct DefaultHighlighting<T: Highlightable + ?Sized> {
    pub hovered: Handle<T::HighlightAsset>,
    pub pressed: Handle<T::HighlightAsset>,
    pub selected: Handle<T::HighlightAsset>,
}

/// This trait makes it possible for highlighting to be generic over any type of asset.
pub trait Highlightable: Default {
    /// The asset used to highlight the picked object. For a 3D mesh, this would probably be [`StandardMaterial`].
    type HighlightAsset: Asset;
    fn highlight_defaults(
        materials: Mut<Assets<Self::HighlightAsset>>,
    ) -> DefaultHighlighting<Self>;
    fn materials(world: &mut World) -> Mut<Assets<Self::HighlightAsset>> {
        world
            .get_resource_mut::<Assets<Self::HighlightAsset>>()
            .expect("Failed to get resource")
    }
}

#[derive(Default)]
pub struct StandardMaterialHighlight;
impl Highlightable for StandardMaterialHighlight {
    type HighlightAsset = StandardMaterial;

    fn highlight_defaults(
        mut materials: Mut<Assets<Self::HighlightAsset>>,
    ) -> DefaultHighlighting<Self> {
        DefaultHighlighting {
            hovered: materials.add(Color::rgb(0.35, 0.35, 0.35).into()),
            pressed: materials.add(Color::rgb(0.35, 0.75, 0.35).into()),
            selected: materials.add(Color::rgb(0.35, 0.35, 0.75).into()),
        }
    }
}

#[derive(Default)]
pub struct ColorMaterialHighlight;
impl Highlightable for ColorMaterialHighlight {
    type HighlightAsset = ColorMaterial;

    fn highlight_defaults(
        mut materials: Mut<Assets<Self::HighlightAsset>>,
    ) -> DefaultHighlighting<Self> {
        DefaultHighlighting {
            hovered: materials.add(Color::rgb(0.35, 0.35, 0.35).into()),
            pressed: materials.add(Color::rgb(0.35, 0.75, 0.35).into()),
            selected: materials.add(Color::rgb(0.35, 0.35, 0.75).into()),
        }
    }
}

impl<T: Highlightable> FromWorld for DefaultHighlighting<T> {
    fn from_world(world: &mut World) -> Self {
        T::highlight_defaults(T::materials(world))
    }
}

#[allow(clippy::type_complexity)]
pub fn get_initial_mesh_highlight_asset<T: Asset>(
    mut commands: Commands,
    entity_asset_query: Query<(Entity, &Handle<T>), Added<Highlight>>,
    mut highlighting_query: Query<Option<&mut Highlighting<T>>>,
) {
    for (entity, material) in entity_asset_query.iter() {
        match highlighting_query.get_mut(entity) {
            Ok(Some(mut highlighting)) => highlighting.initial = material.to_owned(),
            _ => {
                let init_component = Highlighting {
                    initial: material.to_owned(),
                    hovered: None,
                    pressed: None,
                    selected: None,
                };
                commands.entity(entity).insert(init_component);
            }
        }
    }
}

#[allow(clippy::type_complexity)]
pub fn mesh_highlighting<T: 'static + Highlightable + Send + Sync>(
    paused: Option<Res<PausedForBlockers>>,
    global_default_highlight: Res<DefaultHighlighting<T>>,
    mut interaction_query: Query<
        (
            &Interaction,
            &mut Handle<T::HighlightAsset>,
            Option<&Selection>,
            &Highlighting<T::HighlightAsset>,
        ),
        Or<(Changed<Interaction>, Changed<Selection>)>,
    >,
) {
    // Set non-hovered material when picking is paused (e.g. while hovering a picking blocker).
    if let Some(paused) = paused {
        if paused.is_paused() {
            for (_, mut material, selection, highlight) in interaction_query.iter_mut() {
                *material = if selection.filter(|s| s.selected()).is_some() {
                    if let Some(highlight_asset) = &highlight.selected {
                        highlight_asset
                    } else {
                        &global_default_highlight.selected
                    }
                } else {
                    &highlight.initial
                }
                .to_owned();
            }
            return;
        }
    }
    for (interaction, mut material, selection, highlight) in interaction_query.iter_mut() {
        *material = match *interaction {
            Interaction::Clicked => {
                if let Some(highlight_asset) = &highlight.pressed {
                    highlight_asset
                } else {
                    &global_default_highlight.pressed
                }
            }
            Interaction::Hovered => {
                if let Some(highlight_asset) = &highlight.hovered {
                    highlight_asset
                } else {
                    &global_default_highlight.hovered
                }
            }
            Interaction::None => {
                if selection.filter(|s| s.selected()).is_some() {
                    if let Some(highlight_asset) = &highlight.selected {
                        highlight_asset
                    } else {
                        &global_default_highlight.selected
                    }
                } else {
                    &highlight.initial
                }
            }
        }
        .to_owned();
    }
}