Merge branch 'audio-volume-slider' into 'master'

Audio volume slider

See merge request veloren/veloren!154

Former-commit-id: e65ea111e788f5b4896ec6c256a702073020d1ab
This commit is contained in:
Joshua Barretto 2019-05-19 21:55:17 +00:00
commit 914ed1242d
6 changed files with 79 additions and 9 deletions

View File

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

View File

@ -91,6 +91,7 @@ font_ids! {
pub enum Event {
SendMessage(String),
AdjustViewDistance(u32),
AdjustVolume(f32),
Logout,
Quit,
}
@ -210,6 +211,7 @@ pub struct Hud {
force_ungrab: bool,
// TODO: move to settings
current_vd: u32,
current_volume: f32,
}
impl Hud {
@ -247,6 +249,7 @@ impl Hud {
settings,
force_ungrab: false,
current_vd: 5,
current_volume: 0.5,
}
}
@ -376,8 +379,14 @@ impl Hud {
// Settings
if let Windows::Settings = self.show.open_windows {
for event in SettingsWindow::new(&self.show, &self.imgs, &self.fonts, self.current_vd)
.set(self.ids.settings_window, ui_widgets)
for event in SettingsWindow::new(
&self.show,
&self.imgs,
&self.fonts,
self.current_vd,
self.current_volume,
)
.set(self.ids.settings_window, ui_widgets)
{
match event {
settings_window::Event::ToggleHelp => self.show.toggle_help(),
@ -390,6 +399,10 @@ impl Hud {
self.current_vd = 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 crate::{hud::Show, ui::ToggleButton};
use super::{img_ids::Imgs, Fonts, Show, TEXT_COLOR};
use crate::{
render::Renderer,
ui::{
self,
img_ids::{ImageGraphic, VoxelGraphic},
ImageSlider, ScaleMode, Ui,
ImageSlider, ScaleMode, ToggleButton, Ui,
},
window::Window,
};
@ -44,6 +43,8 @@ widget_ids! {
video,
vd_slider,
vd_slider_text,
audio_volume_slider,
audio_volume_text,
}
}
@ -63,18 +64,26 @@ pub struct SettingsWindow<'a> {
fonts: &'a Fonts,
current_vd: u32,
current_volume: f32,
#[conrod(common_builder)]
common: widget::CommonBuilder,
}
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 {
show,
imgs,
fonts,
current_vd,
current_volume,
common: widget::CommonBuilder::default(),
}
}
@ -92,6 +101,7 @@ pub enum Event {
ToggleDebug,
Close,
AdjustViewDistance(u32),
AdjustVolume(f32),
}
impl<'a> Widget for SettingsWindow<'a> {
@ -312,6 +322,8 @@ impl<'a> Widget for SettingsWindow<'a> {
Toggle Help Window\n\
Toggle Interface\n\
Toggle FPS and Debug Info\n\
Take Screenshot\n\
Toggle Fullscreen\n\
\n\
\n\
Move Forward\n\
@ -378,6 +390,8 @@ impl<'a> Widget for SettingsWindow<'a> {
F1\n\
F2\n\
F3\n\
F4\n\
F11\n\
\n\
\n\
W\n\
@ -489,7 +503,7 @@ impl<'a> Widget for SettingsWindow<'a> {
.pad_track((5.0, 5.0))
.set(state.ids.vd_slider, ui)
{
events.push(Event::AdjustViewDistance(new_val as u32));
events.push(Event::AdjustViewDistance(new_val));
}
}
// 5 Sound
@ -519,6 +533,32 @@ impl<'a> Widget for SettingsWindow<'a> {
{
state.update(|s| s.settings_tab = SettingsTab::Sound);
}
// Contents
if let SettingsTab::Sound = state.settings_tab {
Text::new("Volume")
.top_left_with_margins_on(state.ids.settings_content, 10.0, 10.0)
.font_size(14)
.font_id(self.fonts.opensans)
.color(TEXT_COLOR)
.set(state.ids.audio_volume_text, ui);
if let Some(new_val) = ImageSlider::continuous(
self.current_volume,
0.0,
1.0,
self.imgs.slider_indicator,
self.imgs.slider,
)
.w_h(104.0, 22.0)
.down_from(state.ids.audio_volume_text, 10.0)
.track_breadth(12.0)
.slider_length(10.0)
.pad_track((5.0, 5.0))
.set(state.ids.audio_volume_slider, ui)
{
events.push(Event::AdjustVolume(new_val));
}
}
events
}

View File

@ -165,6 +165,10 @@ fn main() {
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.
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(MainMenuState::new(&mut global_state))];
states

View File

@ -193,6 +193,9 @@ impl PlayState for SessionState {
HudEvent::AdjustViewDistance(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(
value: 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(
value: T,
min: T,