Add volume adjustment, lower default volume

Former-commit-id: 1b581ef16ceb4a7a26772352d24df9c84d846c88
This commit is contained in:
Imbris 2019-05-19 15:31:32 -04:00
parent 13e916d485
commit 888bd65036
6 changed files with 56 additions and 17 deletions

View File

@ -76,4 +76,8 @@ impl AudioFrontend {
self.play_music(music[i]) self.play_music(music[i])
} }
} }
pub fn set_volume(&mut self, volume: f32) {
self.stream.set_volume(volume.min(1.0))
}
} }

View File

@ -91,6 +91,7 @@ font_ids! {
pub enum Event { pub enum Event {
SendMessage(String), SendMessage(String),
AdjustViewDistance(u32), AdjustViewDistance(u32),
AdjustVolume(f32),
Logout, Logout,
Quit, Quit,
} }
@ -210,6 +211,7 @@ pub struct Hud {
force_ungrab: bool, force_ungrab: bool,
// TODO: move to settings // TODO: move to settings
current_vd: u32, current_vd: u32,
current_volume: f32,
} }
impl Hud { impl Hud {
@ -247,6 +249,7 @@ impl Hud {
settings, settings,
force_ungrab: false, force_ungrab: false,
current_vd: 5, current_vd: 5,
current_volume: 0.5,
} }
} }
@ -376,8 +379,14 @@ impl Hud {
// Settings // Settings
if let Windows::Settings = self.show.open_windows { if let Windows::Settings = self.show.open_windows {
for event in SettingsWindow::new(&self.show, &self.imgs, &self.fonts, self.current_vd) for event in SettingsWindow::new(
.set(self.ids.settings_window, ui_widgets) &self.show,
&self.imgs,
&self.fonts,
self.current_vd,
self.current_volume,
)
.set(self.ids.settings_window, ui_widgets)
{ {
match event { match event {
settings_window::Event::ToggleHelp => self.show.toggle_help(), settings_window::Event::ToggleHelp => self.show.toggle_help(),
@ -390,6 +399,10 @@ impl Hud {
self.current_vd = view_distance; self.current_vd = view_distance;
events.push(Event::AdjustViewDistance(view_distance)); events.push(Event::AdjustViewDistance(view_distance));
} }
settings_window::Event::AdjustVolume(volume) => {
self.current_volume = volume;
events.push(Event::AdjustVolume(volume));
}
} }
} }
} }

View File

@ -1,11 +1,10 @@
use super::{img_ids::Imgs, Fonts, TEXT_COLOR}; use super::{img_ids::Imgs, Fonts, Show, TEXT_COLOR};
use crate::{hud::Show, ui::ToggleButton};
use crate::{ use crate::{
render::Renderer, render::Renderer,
ui::{ ui::{
self, self,
img_ids::{ImageGraphic, VoxelGraphic}, img_ids::{ImageGraphic, VoxelGraphic},
ImageSlider, ScaleMode, Ui, ImageSlider, ScaleMode, ToggleButton, Ui,
}, },
window::Window, window::Window,
}; };
@ -65,20 +64,26 @@ pub struct SettingsWindow<'a> {
fonts: &'a Fonts, fonts: &'a Fonts,
current_vd: u32, current_vd: u32,
//current_volume: u32, current_volume: f32,
#[conrod(common_builder)] #[conrod(common_builder)]
common: widget::CommonBuilder, common: widget::CommonBuilder,
} }
impl<'a> SettingsWindow<'a> { impl<'a> SettingsWindow<'a> {
pub fn new(show: &'a Show, imgs: &'a Imgs, fonts: &'a Fonts, current_vd: u32) -> Self { pub fn new(
show: &'a Show,
imgs: &'a Imgs,
fonts: &'a Fonts,
current_vd: u32,
current_volume: f32,
) -> Self {
Self { Self {
show, show,
imgs, imgs,
fonts, fonts,
current_vd, current_vd,
//current_volume, current_volume,
common: widget::CommonBuilder::default(), common: widget::CommonBuilder::default(),
} }
} }
@ -96,6 +101,7 @@ pub enum Event {
ToggleDebug, ToggleDebug,
Close, Close,
AdjustViewDistance(u32), AdjustViewDistance(u32),
AdjustVolume(f32),
} }
impl<'a> Widget for SettingsWindow<'a> { impl<'a> Widget for SettingsWindow<'a> {
@ -316,6 +322,8 @@ impl<'a> Widget for SettingsWindow<'a> {
Toggle Help Window\n\ Toggle Help Window\n\
Toggle Interface\n\ Toggle Interface\n\
Toggle FPS and Debug Info\n\ Toggle FPS and Debug Info\n\
Take Screenshot\n\
Toggle Fullscreen\n\
\n\ \n\
\n\ \n\
Move Forward\n\ Move Forward\n\
@ -382,6 +390,8 @@ impl<'a> Widget for SettingsWindow<'a> {
F1\n\ F1\n\
F2\n\ F2\n\
F3\n\ F3\n\
F4\n\
F11\n\
\n\ \n\
\n\ \n\
W\n\ W\n\
@ -493,7 +503,7 @@ impl<'a> Widget for SettingsWindow<'a> {
.pad_track((5.0, 5.0)) .pad_track((5.0, 5.0))
.set(state.ids.vd_slider, ui) .set(state.ids.vd_slider, ui)
{ {
events.push(Event::AdjustViewDistance(new_val as u32)); events.push(Event::AdjustViewDistance(new_val));
} }
} }
// 5 Sound // 5 Sound
@ -532,11 +542,10 @@ impl<'a> Widget for SettingsWindow<'a> {
.color(TEXT_COLOR) .color(TEXT_COLOR)
.set(state.ids.audio_volume_text, ui); .set(state.ids.audio_volume_text, ui);
if let Some(new_val) = ImageSlider::discrete( if let Some(new_val) = ImageSlider::continuous(
//self.current_volume, self.current_volume,
self.current_vd, 0.0,
0, 1.0,
100,
self.imgs.slider_indicator, self.imgs.slider_indicator,
self.imgs.slider, self.imgs.slider,
) )
@ -547,7 +556,7 @@ impl<'a> Widget for SettingsWindow<'a> {
.pad_track((5.0, 5.0)) .pad_track((5.0, 5.0))
.set(state.ids.audio_volume_slider, ui) .set(state.ids.audio_volume_slider, ui)
{ {
events.push(Event::AdjustViewDistance(new_val as u32)); events.push(Event::AdjustVolume(new_val));
} }
} }

View File

@ -165,6 +165,10 @@ fn main() {
audio: AudioFrontend::new(), audio: AudioFrontend::new(),
}; };
// TODO: Remove this when the volume setting can be saved
// Lower the volume to 50%
global_state.audio.set_volume(0.5);
// Set up the initial play state. // Set up the initial play state.
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(MainMenuState::new(&mut global_state))]; let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(MainMenuState::new(&mut global_state))];
states states

View File

@ -193,6 +193,9 @@ impl PlayState for SessionState {
HudEvent::AdjustViewDistance(view_distance) => { HudEvent::AdjustViewDistance(view_distance) => {
self.client.borrow_mut().set_view_distance(view_distance) self.client.borrow_mut().set_view_distance(view_distance)
} }
HudEvent::AdjustVolume(volume) => {
global_state.audio.set_volume(volume);
}
} }
} }

View File

@ -119,7 +119,10 @@ impl<T, K> ImageSlider<T, K> {
} }
} }
impl<T> ImageSlider<T, Continuous> { impl<T> ImageSlider<T, Continuous>
where
T: Float,
{
pub fn continuous( pub fn continuous(
value: T, value: T,
min: T, min: T,
@ -131,7 +134,10 @@ impl<T> ImageSlider<T, Continuous> {
} }
} }
impl<T> ImageSlider<T, Discrete> { impl<T> ImageSlider<T, Discrete>
where
T: Integer,
{
pub fn discrete( pub fn discrete(
value: T, value: T,
min: T, min: T,