mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Move to using only global_state for settings menu
Former-commit-id: bb86c851f51c02af1a25d57f2be95d1947b330ad
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
use crate::settings::AudioSettings;
|
||||||
use common::assets;
|
use common::assets;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use rodio::{Decoder, Device, Source, SpatialSink};
|
use rodio::{Decoder, Device, Source, SpatialSink};
|
||||||
@ -21,11 +22,15 @@ pub struct AudioFrontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AudioFrontend {
|
impl AudioFrontend {
|
||||||
pub fn new() -> Self {
|
pub fn new(settings: &AudioSettings) -> Self {
|
||||||
let mut device = AudioFrontend::get_devices_raw()[0].clone();
|
let mut device = rodio::output_devices()
|
||||||
|
.find(|x| x.name() == settings.audio_device)
|
||||||
|
.or_else(rodio::default_output_device)
|
||||||
|
.expect("No Audio devices found");
|
||||||
|
|
||||||
let mut sink =
|
let mut sink =
|
||||||
rodio::SpatialSink::new(&device, [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]);
|
rodio::SpatialSink::new(&device, [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [-1.0, 0.0, 0.0]);
|
||||||
|
sink.set_volume(settings.music_volume);
|
||||||
|
|
||||||
AudioFrontend {
|
AudioFrontend {
|
||||||
device,
|
device,
|
||||||
@ -66,20 +71,20 @@ impl AudioFrontend {
|
|||||||
self.stream.set_volume(volume.min(1.0).max(0.0))
|
self.stream.set_volume(volume.min(1.0).max(0.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Internal method for working with rodio. Filters out the jack audio server.
|
|
||||||
fn get_devices_raw() -> Vec<Device> {
|
|
||||||
rodio::output_devices()
|
|
||||||
.filter(|x| !x.name().contains("jack"))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a vec of the audio devices available.
|
/// Returns a vec of the audio devices available.
|
||||||
/// Does not return rodio Device struct in case our audio backend changes.
|
/// Does not return rodio Device struct in case our audio backend changes.
|
||||||
// TODO: Decide if this should be an associated function
|
pub fn get_devices() -> Vec<String> {
|
||||||
pub fn get_devices(&self) -> Vec<String> {
|
|
||||||
rodio::output_devices().map(|x| x.name()).collect()
|
rodio::output_devices().map(|x| x.name()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the default audio device.
|
||||||
|
/// Does not return rodio Device struct in case our audio backend changes.
|
||||||
|
pub fn get_default_device() -> String {
|
||||||
|
rodio::default_output_device()
|
||||||
|
.expect("No audio output devices detected.")
|
||||||
|
.name()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the name of the current audio device.
|
/// Returns the name of the current audio device.
|
||||||
/// Does not return rodio Device struct in case our audio backend changes.
|
/// Does not return rodio Device struct in case our audio backend changes.
|
||||||
pub fn get_device(&self) -> String {
|
pub fn get_device(&self) -> String {
|
||||||
|
@ -208,12 +208,11 @@ pub struct Hud {
|
|||||||
inventory_space: u32,
|
inventory_space: u32,
|
||||||
show: Show,
|
show: Show,
|
||||||
to_focus: Option<Option<widget::Id>>,
|
to_focus: Option<Option<widget::Id>>,
|
||||||
settings: Settings,
|
|
||||||
force_ungrab: bool,
|
force_ungrab: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hud {
|
impl Hud {
|
||||||
pub fn new(window: &mut Window, settings: Settings) -> Self {
|
pub fn new(window: &mut Window) -> Self {
|
||||||
let mut ui = Ui::new(window).unwrap();
|
let mut ui = Ui::new(window).unwrap();
|
||||||
// TODO: Adjust/remove this, right now it is used to demonstrate window scaling functionality.
|
// TODO: Adjust/remove this, right now it is used to demonstrate window scaling functionality.
|
||||||
ui.scaling_mode(ScaleMode::RelativeToWindow([1920.0, 1080.0].into()));
|
ui.scaling_mode(ScaleMode::RelativeToWindow([1920.0, 1080.0].into()));
|
||||||
@ -244,12 +243,11 @@ impl Hud {
|
|||||||
want_grab: true,
|
want_grab: true,
|
||||||
},
|
},
|
||||||
to_focus: None,
|
to_focus: None,
|
||||||
settings,
|
|
||||||
force_ungrab: false,
|
force_ungrab: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_layout(&mut self, tps: f64) -> Vec<Event> {
|
fn update_layout(&mut self, tps: f64, global_state: &GlobalState) -> Vec<Event> {
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
let ref mut ui_widgets = self.ui.set_widgets();
|
let ref mut ui_widgets = self.ui.set_widgets();
|
||||||
let version = env!("CARGO_PKG_VERSION");
|
let version = env!("CARGO_PKG_VERSION");
|
||||||
@ -298,7 +296,7 @@ impl Hud {
|
|||||||
.top_left_with_margins_on(ui_widgets.window, 3.0, 3.0)
|
.top_left_with_margins_on(ui_widgets.window, 3.0, 3.0)
|
||||||
.w_h(300.0, 190.0)
|
.w_h(300.0, 190.0)
|
||||||
.set(self.ids.help_bg, ui_widgets);
|
.set(self.ids.help_bg, ui_widgets);
|
||||||
Text::new(get_help_text(&self.settings.controls).as_str())
|
Text::new(get_help_text(&global_state.settings.controls).as_str())
|
||||||
.color(TEXT_COLOR)
|
.color(TEXT_COLOR)
|
||||||
.top_left_with_margins_on(self.ids.help_bg, 20.0, 20.0)
|
.top_left_with_margins_on(self.ids.help_bg, 20.0, 20.0)
|
||||||
.font_id(self.fonts.opensans)
|
.font_id(self.fonts.opensans)
|
||||||
@ -375,7 +373,7 @@ 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.settings)
|
for event in SettingsWindow::new(&self.show, &self.imgs, &self.fonts, &global_state)
|
||||||
.set(self.ids.settings_window, ui_widgets)
|
.set(self.ids.settings_window, ui_widgets)
|
||||||
{
|
{
|
||||||
match event {
|
match event {
|
||||||
@ -386,15 +384,12 @@ impl Hud {
|
|||||||
settings_window::Event::ToggleDebug => self.show.debug = !self.show.debug,
|
settings_window::Event::ToggleDebug => self.show.debug = !self.show.debug,
|
||||||
settings_window::Event::Close => self.show.open_windows = Windows::None,
|
settings_window::Event::Close => self.show.open_windows = Windows::None,
|
||||||
settings_window::Event::AdjustViewDistance(view_distance) => {
|
settings_window::Event::AdjustViewDistance(view_distance) => {
|
||||||
self.settings.graphics.view_distance = view_distance;
|
|
||||||
events.push(Event::AdjustViewDistance(view_distance));
|
events.push(Event::AdjustViewDistance(view_distance));
|
||||||
}
|
}
|
||||||
settings_window::Event::AdjustVolume(volume) => {
|
settings_window::Event::AdjustVolume(volume) => {
|
||||||
self.settings.audio.music_volume = volume;
|
|
||||||
events.push(Event::AdjustVolume(volume));
|
events.push(Event::AdjustVolume(volume));
|
||||||
}
|
}
|
||||||
settings_window::Event::ChangeAudioDevice(name) => {
|
settings_window::Event::ChangeAudioDevice(name) => {
|
||||||
self.settings.audio.audio_device = name.clone();
|
|
||||||
events.push(Event::ChangeAudioDevice(name));
|
events.push(Event::ChangeAudioDevice(name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -477,10 +472,6 @@ impl Hud {
|
|||||||
pub fn handle_event(&mut self, event: WinEvent, global_state: &mut GlobalState) -> bool {
|
pub fn handle_event(&mut self, event: WinEvent, global_state: &mut GlobalState) -> bool {
|
||||||
let cursor_grabbed = global_state.window.is_cursor_grabbed();
|
let cursor_grabbed = global_state.window.is_cursor_grabbed();
|
||||||
let handled = match event {
|
let handled = match event {
|
||||||
WinEvent::SettingsChanged => {
|
|
||||||
self.settings = global_state.settings.clone();
|
|
||||||
false
|
|
||||||
}
|
|
||||||
WinEvent::Ui(event) => {
|
WinEvent::Ui(event) => {
|
||||||
if (self.typing() && event.is_keyboard() && self.show.ui)
|
if (self.typing() && event.is_keyboard() && self.show.ui)
|
||||||
|| !(cursor_grabbed && event.is_keyboard_or_mouse())
|
|| !(cursor_grabbed && event.is_keyboard_or_mouse())
|
||||||
@ -575,12 +566,12 @@ impl Hud {
|
|||||||
handled
|
handled
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn maintain(&mut self, renderer: &mut Renderer, tps: f64) -> Vec<Event> {
|
pub fn maintain(&mut self, global_state: &mut GlobalState, tps: f64) -> Vec<Event> {
|
||||||
if let Some(maybe_id) = self.to_focus.take() {
|
if let Some(maybe_id) = self.to_focus.take() {
|
||||||
self.ui.focus_widget(maybe_id);
|
self.ui.focus_widget(maybe_id);
|
||||||
}
|
}
|
||||||
let events = self.update_layout(tps);
|
let events = self.update_layout(tps, &global_state);
|
||||||
self.ui.maintain(renderer);
|
self.ui.maintain(&mut global_state.window.renderer_mut());
|
||||||
events
|
events
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use crate::{
|
|||||||
ImageSlider, ScaleMode, ToggleButton, Ui,
|
ImageSlider, ScaleMode, ToggleButton, Ui,
|
||||||
},
|
},
|
||||||
window::Window,
|
window::Window,
|
||||||
Settings,
|
GlobalState,
|
||||||
};
|
};
|
||||||
use conrod_core::{
|
use conrod_core::{
|
||||||
color,
|
color,
|
||||||
@ -66,19 +66,24 @@ pub struct SettingsWindow<'a> {
|
|||||||
imgs: &'a Imgs,
|
imgs: &'a Imgs,
|
||||||
fonts: &'a Fonts,
|
fonts: &'a Fonts,
|
||||||
|
|
||||||
settings: &'a Settings,
|
global_state: &'a GlobalState,
|
||||||
|
|
||||||
#[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, settings: &'a Settings) -> Self {
|
pub fn new(
|
||||||
|
show: &'a Show,
|
||||||
|
imgs: &'a Imgs,
|
||||||
|
fonts: &'a Fonts,
|
||||||
|
global_state: &'a GlobalState,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
show,
|
show,
|
||||||
imgs,
|
imgs,
|
||||||
fonts,
|
fonts,
|
||||||
settings,
|
global_state,
|
||||||
common: widget::CommonBuilder::default(),
|
common: widget::CommonBuilder::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -486,7 +491,7 @@ impl<'a> Widget for SettingsWindow<'a> {
|
|||||||
.set(state.ids.vd_slider_text, ui);
|
.set(state.ids.vd_slider_text, ui);
|
||||||
|
|
||||||
if let Some(new_val) = ImageSlider::discrete(
|
if let Some(new_val) = ImageSlider::discrete(
|
||||||
self.settings.graphics.view_distance,
|
self.global_state.settings.graphics.view_distance,
|
||||||
1,
|
1,
|
||||||
25,
|
25,
|
||||||
self.imgs.slider_indicator,
|
self.imgs.slider_indicator,
|
||||||
@ -540,7 +545,7 @@ impl<'a> Widget for SettingsWindow<'a> {
|
|||||||
.set(state.ids.audio_volume_text, ui);
|
.set(state.ids.audio_volume_text, ui);
|
||||||
|
|
||||||
if let Some(new_val) = ImageSlider::continuous(
|
if let Some(new_val) = ImageSlider::continuous(
|
||||||
self.settings.audio.music_volume,
|
self.global_state.settings.audio.music_volume,
|
||||||
0.0,
|
0.0,
|
||||||
1.0,
|
1.0,
|
||||||
self.imgs.slider_indicator,
|
self.imgs.slider_indicator,
|
||||||
@ -566,19 +571,21 @@ impl<'a> Widget for SettingsWindow<'a> {
|
|||||||
|
|
||||||
// Get which device is currently selected
|
// Get which device is currently selected
|
||||||
let selected = self
|
let selected = self
|
||||||
|
.global_state
|
||||||
.settings
|
.settings
|
||||||
.audio
|
.audio
|
||||||
.audio_devices
|
.audio_devices
|
||||||
.iter()
|
.iter()
|
||||||
.position(|x| x.contains(&self.settings.audio.audio_device));
|
.position(|x| x.contains(&self.global_state.settings.audio.audio_device));
|
||||||
|
|
||||||
if let Some(clicked) = DropDownList::new(&self.settings.audio.audio_devices, selected)
|
if let Some(clicked) =
|
||||||
.w_h(400.0, 22.0)
|
DropDownList::new(&self.global_state.settings.audio.audio_devices, selected)
|
||||||
.down_from(state.ids.audio_device_text, 10.0)
|
.w_h(400.0, 22.0)
|
||||||
.label_font_id(self.fonts.opensans)
|
.down_from(state.ids.audio_device_text, 10.0)
|
||||||
.set(state.ids.audio_device_list, ui)
|
.label_font_id(self.fonts.opensans)
|
||||||
|
.set(state.ids.audio_device_list, ui)
|
||||||
{
|
{
|
||||||
let new_val = self.settings.audio.audio_devices[clicked].clone();
|
let new_val = self.global_state.settings.audio.audio_devices[clicked].clone();
|
||||||
events.push(Event::ChangeAudioDevice(new_val));
|
events.push(Event::ChangeAudioDevice(new_val));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ pub trait PlayState {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Set up the global state.
|
// Set up the global state.
|
||||||
let settings = Settings::load();
|
let mut settings = Settings::load();
|
||||||
let window = Window::new(&settings).expect("Failed to create window!");
|
let window = Window::new(&settings).expect("Failed to create window!");
|
||||||
|
|
||||||
// Initialize logging.
|
// Initialize logging.
|
||||||
@ -159,30 +159,16 @@ fn main() {
|
|||||||
default_hook(panic_info);
|
default_hook(panic_info);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let mut global_state = GlobalState {
|
if settings.audio.audio_device == "" {
|
||||||
settings,
|
settings.audio.audio_device = AudioFrontend::get_default_device();
|
||||||
window,
|
|
||||||
audio: AudioFrontend::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Load volume from audio file
|
|
||||||
global_state
|
|
||||||
.audio
|
|
||||||
.set_volume(global_state.settings.audio.music_volume);
|
|
||||||
|
|
||||||
global_state.settings.audio.audio_devices = global_state.audio.get_devices();
|
|
||||||
|
|
||||||
// Load last used audio device, or set the current audio device as the last
|
|
||||||
// used if there is no last used
|
|
||||||
if global_state.settings.audio.audio_device != "" {
|
|
||||||
global_state
|
|
||||||
.audio
|
|
||||||
.set_device(global_state.settings.audio.audio_device.clone());
|
|
||||||
} else {
|
|
||||||
global_state.settings.audio.audio_device = global_state.audio.get_device();
|
|
||||||
global_state.settings.save_to_file();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut global_state = GlobalState {
|
||||||
|
audio: AudioFrontend::new(&settings.audio),
|
||||||
|
window,
|
||||||
|
settings,
|
||||||
|
};
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -32,7 +32,7 @@ impl SessionState {
|
|||||||
scene,
|
scene,
|
||||||
client,
|
client,
|
||||||
key_state: KeyState::new(),
|
key_state: KeyState::new(),
|
||||||
hud: Hud::new(window, settings),
|
hud: Hud::new(window),
|
||||||
input_events: Vec::new(),
|
input_events: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -173,14 +173,11 @@ impl PlayState for SessionState {
|
|||||||
// Maintain the scene.
|
// Maintain the scene.
|
||||||
self.scene.maintain(
|
self.scene.maintain(
|
||||||
global_state.window.renderer_mut(),
|
global_state.window.renderer_mut(),
|
||||||
&mut self.client.borrow_mut(),
|
&self.client.borrow_mut(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Maintain the UI.
|
// Maintain the UI.
|
||||||
for event in self
|
for event in self.hud.maintain(&mut global_state, clock.get_tps()) {
|
||||||
.hud
|
|
||||||
.maintain(global_state.window.renderer_mut(), clock.get_tps())
|
|
||||||
{
|
|
||||||
match event {
|
match event {
|
||||||
HudEvent::SendMessage(msg) => {
|
HudEvent::SendMessage(msg) => {
|
||||||
// TODO: Handle result
|
// TODO: Handle result
|
||||||
|
Reference in New Issue
Block a user