-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Volume and GlobalVolume to own file (#16838)
# Objective - Prework for reviving #9582. ## Solution - Move the two types to volume.rs and made it compile. - Also `#[reflect(Debug)]` on `Volume` while I'm here. ## Testing - Ran example locally. - Rely on CI.
- Loading branch information
Showing
3 changed files
with
51 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use bevy_derive::Deref; | ||
use bevy_ecs::prelude::*; | ||
use bevy_reflect::prelude::*; | ||
|
||
/// Use this [`Resource`] to control the global volume of all audio. | ||
/// | ||
/// Note: changing this value will not affect already playing audio. | ||
#[derive(Resource, Default, Clone, Copy, Reflect)] | ||
#[reflect(Resource, Default)] | ||
pub struct GlobalVolume { | ||
/// The global volume of all audio. | ||
pub volume: Volume, | ||
} | ||
|
||
impl GlobalVolume { | ||
/// Create a new [`GlobalVolume`] with the given volume. | ||
pub fn new(volume: f32) -> Self { | ||
Self { | ||
volume: Volume::new(volume), | ||
} | ||
} | ||
} | ||
|
||
/// A volume level equivalent to a non-negative float. | ||
#[derive(Clone, Copy, Deref, Debug, Reflect)] | ||
#[reflect(Debug)] | ||
pub struct Volume(pub(crate) f32); | ||
|
||
impl Default for Volume { | ||
fn default() -> Self { | ||
Self(1.0) | ||
} | ||
} | ||
|
||
impl Volume { | ||
/// Create a new volume level. | ||
pub fn new(volume: f32) -> Self { | ||
debug_assert!(volume >= 0.0); | ||
Self(f32::max(volume, 0.)) | ||
} | ||
/// Get the value of the volume level. | ||
pub fn get(&self) -> f32 { | ||
self.0 | ||
} | ||
|
||
/// Zero (silent) volume level | ||
pub const ZERO: Self = Volume(0.0); | ||
} |