mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
1efc762924
# Objective
- Introduce a stable alternative to
[`std::any::type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html).
- Rewrite of #5805 with heavy inspiration in design.
- On the path to #5830.
- Part of solving #3327.
## Solution
- Add a `TypePath` trait for static stable type path/name information.
- Add a `TypePath` derive macro.
- Add a `impl_type_path` macro for implementing internal and foreign
types in `bevy_reflect`.
---
## Changelog
- Added `TypePath` trait.
- Added `DynamicTypePath` trait and `get_type_path` method to `Reflect`.
- Added a `TypePath` derive macro.
- Added a `bevy_reflect::impl_type_path` for implementing `TypePath` on
internal and foreign types in `bevy_reflect`.
- Changed `bevy_reflect::utility::(Non)GenericTypeInfoCell` to
`(Non)GenericTypedCell<T>` which allows us to be generic over both
`TypeInfo` and `TypePath`.
- `TypePath` is now a supertrait of `Asset`, `Material` and
`Material2d`.
- `impl_reflect_struct` needs a `#[type_path = "..."]` attribute to be
specified.
- `impl_reflect_value` needs to either specify path starting with a
double colon (`::core::option::Option`) or an `in my_crate::foo`
declaration.
- Added `bevy_reflect_derive::ReflectTypePath`.
- Most uses of `Ident` in `bevy_reflect_derive` changed to use
`ReflectTypePath`.
## Migration Guide
- Implementors of `Asset`, `Material` and `Material2d` now also need to
derive `TypePath`.
- Manual implementors of `Reflect` will need to implement the new
`get_type_path` method.
## Open Questions
- [x] ~This PR currently does not migrate any usages of
`std::any::type_name` to use `bevy_reflect::TypePath` to ease the review
process. Should it?~ Migration will be left to a follow-up PR.
- [ ] This PR adds a lot of `#[derive(TypePath)]` and `T: TypePath` to
satisfy new bounds, mostly when deriving `TypeUuid`. Should we make
`TypePath` a supertrait of `TypeUuid`? [Should we remove `TypeUuid` in
favour of
`TypePath`?](2afbd85532 (r961067892)
)
102 lines
3 KiB
Rust
102 lines
3 KiB
Rust
//! Shows how to create a custom [`Decodable`] type by implementing a Sine wave.
|
|
use bevy::audio::AddAudioSource;
|
|
use bevy::audio::AudioPlugin;
|
|
use bevy::audio::Source;
|
|
use bevy::prelude::*;
|
|
use bevy::reflect::{TypePath, TypeUuid};
|
|
use bevy::utils::Duration;
|
|
|
|
// This struct usually contains the data for the audio being played.
|
|
// This is where data read from an audio file would be stored, for example.
|
|
// Implementing `TypeUuid` will automatically implement `Asset`.
|
|
// This allows the type to be registered as an asset.
|
|
#[derive(TypePath, TypeUuid)]
|
|
#[uuid = "c2090c23-78fd-44f1-8508-c89b1f3cec29"]
|
|
struct SineAudio {
|
|
frequency: f32,
|
|
}
|
|
// This decoder is responsible for playing the audio,
|
|
// and so stores data about the audio being played.
|
|
struct SineDecoder {
|
|
// how far along one period the wave is (between 0 and 1)
|
|
current_progress: f32,
|
|
// how much we move along the period every frame
|
|
progress_per_frame: f32,
|
|
// how long a period is
|
|
period: f32,
|
|
sample_rate: u32,
|
|
}
|
|
|
|
impl SineDecoder {
|
|
fn new(frequency: f32) -> Self {
|
|
// standard sample rate for most recordings
|
|
let sample_rate = 44_100;
|
|
SineDecoder {
|
|
current_progress: 0.,
|
|
progress_per_frame: frequency / sample_rate as f32,
|
|
period: std::f32::consts::PI * 2.,
|
|
sample_rate,
|
|
}
|
|
}
|
|
}
|
|
|
|
// The decoder must implement iterator so that it can implement `Decodable`.
|
|
impl Iterator for SineDecoder {
|
|
type Item = f32;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.current_progress += self.progress_per_frame;
|
|
// we loop back round to 0 to avoid floating point inaccuracies
|
|
self.current_progress %= 1.;
|
|
Some(f32::sin(self.period * self.current_progress))
|
|
}
|
|
}
|
|
// `Source` is what allows the audio source to be played by bevy.
|
|
// This trait provides information on the audio.
|
|
impl Source for SineDecoder {
|
|
fn current_frame_len(&self) -> Option<usize> {
|
|
None
|
|
}
|
|
|
|
fn channels(&self) -> u16 {
|
|
1
|
|
}
|
|
|
|
fn sample_rate(&self) -> u32 {
|
|
self.sample_rate
|
|
}
|
|
|
|
fn total_duration(&self) -> Option<Duration> {
|
|
None
|
|
}
|
|
}
|
|
|
|
// Finally `Decodable` can be implemented for our `SineAudio`.
|
|
impl Decodable for SineAudio {
|
|
type Decoder = SineDecoder;
|
|
|
|
type DecoderItem = <SineDecoder as Iterator>::Item;
|
|
|
|
fn decoder(&self) -> Self::Decoder {
|
|
SineDecoder::new(self.frequency)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let mut app = App::new();
|
|
// register the audio source so that it can be used
|
|
app.add_plugins(DefaultPlugins.set(AudioPlugin {
|
|
global_volume: GlobalVolume::new(0.2),
|
|
}))
|
|
.add_audio_source::<SineAudio>()
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut assets: ResMut<Assets<SineAudio>>, audio: Res<Audio<SineAudio>>) {
|
|
// add a `SineAudio` to the asset server so that it can be played
|
|
let audio_handle = assets.add(SineAudio {
|
|
frequency: 440., //this is the frequency of A4
|
|
});
|
|
audio.play(audio_handle);
|
|
}
|