Skip to content

Commit

Permalink
Simplify highlighting assets further (#136)
Browse files Browse the repository at this point in the history
* Simplify highlighting assets further

* Get highlighting working
  • Loading branch information
aevyrie authored Nov 13, 2022
1 parent 3860a40 commit 7aaa424
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
Binary file removed docs/demo_vid.webp
Binary file not shown.
41 changes: 13 additions & 28 deletions src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,24 @@ pub struct Highlighting<T: Asset> {
/// with the [`Highlighting`] component.
#[derive(Clone, Debug, Resource)]
pub struct DefaultHighlighting<T: Highlightable + ?Sized> {
pub hovered: Handle<T::HighlightAsset>,
pub pressed: Handle<T::HighlightAsset>,
pub selected: Handle<T::HighlightAsset>,
pub hovered: Handle<T>,
pub pressed: Handle<T>,
pub selected: Handle<T>,
}

/// This trait makes it possible for highlighting to be generic over any type of asset.
pub trait Highlightable: Default {
pub trait Highlightable: Default + Asset {
/// 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>> {
fn highlight_defaults(materials: Mut<Assets<Self>>) -> DefaultHighlighting<Self>;
fn materials(world: &mut World) -> Mut<Assets<Self>> {
world
.get_resource_mut::<Assets<Self::HighlightAsset>>()
.get_resource_mut::<Assets<Self>>()
.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> {
impl Highlightable for StandardMaterial {
fn highlight_defaults(mut materials: Mut<Assets<Self>>) -> 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()),
Expand All @@ -56,14 +47,8 @@ impl Highlightable for StandardMaterialHighlight {
}
}

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

fn highlight_defaults(
mut materials: Mut<Assets<Self::HighlightAsset>>,
) -> DefaultHighlighting<Self> {
impl Highlightable for ColorMaterial {
fn highlight_defaults(mut materials: Mut<Assets<Self>>) -> 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()),
Expand Down Expand Up @@ -107,9 +92,9 @@ pub fn mesh_highlighting<T: 'static + Highlightable + Send + Sync>(
mut interaction_query: Query<
(
&Interaction,
&mut Handle<T::HighlightAsset>,
&mut Handle<T>,
Option<&Selection>,
&Highlighting<T::HighlightAsset>,
&Highlighting<T>,
),
Or<(Changed<Interaction>, Changed<Selection>)>,
>,
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ pub mod highlight;
pub mod mouse;
pub mod selection;

use std::marker::PhantomData;

pub use crate::{
events::{event_debug_system, mesh_events_system, HoverEvent, PickingEvent, SelectionEvent},
focus::{mesh_focus, pause_for_picking_blockers, Hover, PickingBlocker},
highlight::{
mesh_highlighting, DefaultHighlighting, Highlightable, Highlighting,
StandardMaterialHighlight,
},
highlight::{mesh_highlighting, DefaultHighlighting, Highlightable, Highlighting},
mouse::update_pick_source_positions,
selection::{mesh_selection, NoDeselect, Selection},
};
pub use bevy_mod_raycast::{Primitive3d, RaycastMesh, RaycastSource};

use bevy::{app::PluginGroupBuilder, ecs::schedule::ShouldRun, prelude::*, ui::FocusPolicy};
use highlight::{get_initial_mesh_highlight_asset, ColorMaterialHighlight, Highlight};
use highlight::{get_initial_mesh_highlight_asset, Highlight};

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
pub enum PickingSystem {
Expand Down Expand Up @@ -95,8 +94,8 @@ impl PluginGroup for DefaultPickingPlugins {
PluginGroupBuilder::start::<Self>()
.add(PickingPlugin)
.add(InteractablePickingPlugin)
.add(CustomHighlightPlugin(StandardMaterialHighlight))
.add(CustomHighlightPlugin(ColorMaterialHighlight))
.add(CustomHighlightPlugin::<StandardMaterial>::default())
.add(CustomHighlightPlugin::<ColorMaterial>::default())
}
}

Expand Down Expand Up @@ -171,7 +170,7 @@ impl Plugin for InteractablePickingPlugin {
/// A highlighting plugin, generic over any asset that might be used for rendering the different
/// highlighting states.
#[derive(Default)]
pub struct CustomHighlightPlugin<T: 'static + Highlightable + Sync + Send>(pub T);
pub struct CustomHighlightPlugin<T: 'static + Highlightable + Sync + Send>(PhantomData<T>);

impl<T> Plugin for CustomHighlightPlugin<T>
where
Expand All @@ -186,7 +185,7 @@ where
simple_criteria(state.enable_highlighting)
})
.with_system(
get_initial_mesh_highlight_asset::<T::HighlightAsset>
get_initial_mesh_highlight_asset::<T>
.after(PickingSystem::UpdateIntersections)
.before(PickingSystem::Highlighting),
)
Expand Down

0 comments on commit 7aaa424

Please sign in to comment.