Make sound parameter feature dependent

This commit is contained in:
hodasemi 2023-01-17 07:07:19 +01:00
parent 5b1ec043a0
commit 29d6e53b62
5 changed files with 74 additions and 13 deletions

View file

@ -23,13 +23,21 @@ pub struct GuiBuilder {
impl GuiBuilder { impl GuiBuilder {
pub fn new(gui_handler: &Arc<GuiHandler>, path: &AssetPath) -> Result<Arc<Self>> { pub fn new(gui_handler: &Arc<GuiHandler>, path: &AssetPath) -> Result<Arc<Self>> {
let validator = Validator::new(gui_handler, path)?; let validator = Validator::new(
#[cfg(feature = "audio")]
gui_handler,
path,
)?;
Self::_new(gui_handler, validator) Self::_new(gui_handler, validator)
} }
pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Arc<Self>> { pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Arc<Self>> {
let validator = Validator::from_str(gui_handler, s)?; let validator = Validator::from_str(
#[cfg(feature = "audio")]
gui_handler,
s,
)?;
Self::_new(gui_handler, validator) Self::_new(gui_handler, validator)
} }

View file

@ -18,7 +18,11 @@ pub struct GuiSnippet {
impl GuiSnippet { impl GuiSnippet {
pub fn new(gui_handler: Arc<GuiHandler>, path: &AssetPath) -> Result<Arc<Self>> { pub fn new(gui_handler: Arc<GuiHandler>, path: &AssetPath) -> Result<Arc<Self>> {
let validator = Validator::new(&gui_handler, path)?; let validator = Validator::new(
#[cfg(feature = "audio")]
&gui_handler,
path,
)?;
let root = validator.root(); let root = validator.root();

View file

@ -2,6 +2,7 @@ use crate::prelude::*;
use anyhow::Result; use anyhow::Result;
use utilities::prelude::*; use utilities::prelude::*;
#[cfg(feature = "audio")]
use assetpath::AssetPath; use assetpath::AssetPath;
use super::gridinfo::GridInfo; use super::gridinfo::GridInfo;
@ -12,10 +13,12 @@ use std::str::from_utf8;
use std::sync::{Arc, RwLock, Weak}; use std::sync::{Arc, RwLock, Weak};
use super::validator::{ use super::validator::{
cow_to_button_select_mode, cow_to_fill_type, cow_to_path, cow_to_str, cow_to_text_alignment, cow_to_button_select_mode, cow_to_fill_type, cow_to_str, cow_to_text_alignment, str_into,
str_into,
}; };
#[cfg(feature = "audio")]
use super::validator::cow_to_path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum NeighbourDirection { pub enum NeighbourDirection {
East, East,
@ -45,7 +48,10 @@ pub struct ButtonInfo {
pub normal: Option<FillTypeInfo>, pub normal: Option<FillTypeInfo>,
pub selected: Option<FillTypeInfo>, pub selected: Option<FillTypeInfo>,
#[cfg(feature = "audio")]
pub click_sound: AssetPath, pub click_sound: AssetPath,
#[cfg(feature = "audio")]
pub hover_sound: AssetPath, pub hover_sound: AssetPath,
pub text: RwLock<String>, pub text: RwLock<String>,
@ -68,7 +74,7 @@ pub struct ButtonInfo {
impl ButtonInfo { impl ButtonInfo {
pub fn new<'a>( pub fn new<'a>(
gui_handler: &Arc<GuiHandler>, #[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
attributes: quick_xml::events::attributes::Attributes<'a>, attributes: quick_xml::events::attributes::Attributes<'a>,
grid: &Arc<GridInfo>, grid: &Arc<GridInfo>,
) -> Result<ButtonInfo> { ) -> Result<ButtonInfo> {
@ -84,11 +90,13 @@ impl ButtonInfo {
normal: Self::find_menu_button(grid), normal: Self::find_menu_button(grid),
selected: Self::find_menu_button_selected(grid), selected: Self::find_menu_button_selected(grid),
#[cfg(feature = "audio")]
click_sound: match Self::find_click_sound(grid) { click_sound: match Self::find_click_sound(grid) {
Some(sound) => sound, Some(sound) => sound,
None => gui_handler.click_sound().clone(), None => gui_handler.click_sound().clone(),
}, },
#[cfg(feature = "audio")]
hover_sound: match Self::find_hover_sound(grid) { hover_sound: match Self::find_hover_sound(grid) {
Some(sound) => sound, Some(sound) => sound,
None => gui_handler.hover_sound().clone(), None => gui_handler.hover_sound().clone(),
@ -122,8 +130,13 @@ impl ButtonInfo {
b"y_size" => button_info.y_dim = str_into(attribute.value)?, b"y_size" => button_info.y_dim = str_into(attribute.value)?,
b"normal" => button_info.normal = Some(cow_to_fill_type(attribute.value)), b"normal" => button_info.normal = Some(cow_to_fill_type(attribute.value)),
b"selected" => button_info.selected = Some(cow_to_fill_type(attribute.value)), b"selected" => button_info.selected = Some(cow_to_fill_type(attribute.value)),
#[cfg(feature = "audio")]
b"click_sound" => button_info.click_sound = cow_to_path(attribute.value), b"click_sound" => button_info.click_sound = cow_to_path(attribute.value),
#[cfg(feature = "audio")]
b"hover_sound" => button_info.hover_sound = cow_to_path(attribute.value), b"hover_sound" => button_info.hover_sound = cow_to_path(attribute.value),
b"select_mode" => { b"select_mode" => {
button_info.select_mode = cow_to_button_select_mode(attribute.value)? button_info.select_mode = cow_to_button_select_mode(attribute.value)?
} }
@ -202,6 +215,7 @@ impl ButtonInfo {
None None
} }
#[cfg(feature = "audio")]
fn find_click_sound(grid: &GridInfo) -> Option<AssetPath> { fn find_click_sound(grid: &GridInfo) -> Option<AssetPath> {
// return immediately if present // return immediately if present
if let Some(click_sound) = &grid.click_sound { if let Some(click_sound) = &grid.click_sound {
@ -218,6 +232,7 @@ impl ButtonInfo {
None None
} }
#[cfg(feature = "audio")]
fn find_hover_sound(grid: &GridInfo) -> Option<AssetPath> { fn find_hover_sound(grid: &GridInfo) -> Option<AssetPath> {
// return immediately if present // return immediately if present
if let Some(hover_sound) = &grid.hover_sound { if let Some(hover_sound) = &grid.hover_sound {

View file

@ -59,19 +59,33 @@ enum CurrentElement {
} }
impl Validator { impl Validator {
pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Validator> { pub fn from_str(
Self::_new(gui_handler, Reader::from_str(s)) #[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
s: &str,
) -> Result<Validator> {
Self::_new(
#[cfg(feature = "audio")]
gui_handler,
Reader::from_str(s),
)
} }
pub fn new(gui_handler: &Arc<GuiHandler>, path: &AssetPath) -> Result<Validator> { pub fn new(
#[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
path: &AssetPath,
) -> Result<Validator> {
Self::_new( Self::_new(
#[cfg(feature = "audio")]
gui_handler, gui_handler,
Reader::from_file(path.full_path()).with_context(|| path.full_path())?, Reader::from_file(path.full_path()).with_context(|| path.full_path())?,
) )
} }
#[inline] #[inline]
fn _new<T: BufRead>(gui_handler: &Arc<GuiHandler>, mut reader: Reader<T>) -> Result<Validator> { fn _new<T: BufRead>(
#[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
mut reader: Reader<T>,
) -> Result<Validator> {
// removes white spaces in texts // removes white spaces in texts
reader.trim_text(true); reader.trim_text(true);
@ -96,7 +110,12 @@ impl Validator {
'outer: loop { 'outer: loop {
match reader.read_event_into(&mut buf) { match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) => { Ok(Event::Start(ref e)) => {
let start_result = Self::process_start(gui_handler, e, &current_element)?; let start_result = Self::process_start(
#[cfg(feature = "audio")]
gui_handler,
e,
&current_element,
)?;
match start_result { match start_result {
StartResult::Root(root_info) => { StartResult::Root(root_info) => {
@ -343,7 +362,7 @@ impl Validator {
} }
fn process_start<'a>( fn process_start<'a>(
gui_handler: &Arc<GuiHandler>, #[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
element: &quick_xml::events::BytesStart<'a>, element: &quick_xml::events::BytesStart<'a>,
handle: &CurrentElement, handle: &CurrentElement,
) -> Result<StartResult> { ) -> Result<StartResult> {
@ -384,7 +403,12 @@ impl Validator {
}, },
b"button" => match handle { b"button" => match handle {
CurrentElement::Grid(grid) => { CurrentElement::Grid(grid) => {
match ButtonInfo::new(gui_handler, element.attributes(), grid) { match ButtonInfo::new(
#[cfg(feature = "audio")]
gui_handler,
element.attributes(),
grid,
) {
Ok(button) => StartResult::Button(button), Ok(button) => StartResult::Button(button),
Err(msg) => return Err(msg), Err(msg) => return Err(msg),
} }

View file

@ -31,7 +31,9 @@ pub struct GuiHandlerCreateInfo {
pub font_path: AssetPath, pub font_path: AssetPath,
// sound info // sound info
#[cfg(feature = "audio")]
pub click_sound: AssetPath, pub click_sound: AssetPath,
#[cfg(feature = "audio")]
pub hover_sound: AssetPath, pub hover_sound: AssetPath,
// resource base directory // resource base directory
@ -108,7 +110,11 @@ pub struct GuiHandler {
menu_button: AssetPath, menu_button: AssetPath,
menu_button_selected: AssetPath, menu_button_selected: AssetPath,
#[cfg(feature = "audio")]
click_sound: AssetPath, click_sound: AssetPath,
#[cfg(feature = "audio")]
hover_sound: AssetPath, hover_sound: AssetPath,
// ----- gui handling ----- // ----- gui handling -----
@ -195,6 +201,7 @@ impl GuiHandler {
menu_button_selected menu_button_selected
}, },
#[cfg(feature = "audio")]
click_sound: { click_sound: {
let mut click_sound = gui_handler_create_info.click_sound; let mut click_sound = gui_handler_create_info.click_sound;
click_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path()); click_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path());
@ -202,6 +209,7 @@ impl GuiHandler {
click_sound click_sound
}, },
#[cfg(feature = "audio")]
hover_sound: { hover_sound: {
let mut hover_sound = gui_handler_create_info.hover_sound; let mut hover_sound = gui_handler_create_info.hover_sound;
hover_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path()); hover_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path());
@ -441,10 +449,12 @@ impl GuiHandler {
&self.menu_button_selected &self.menu_button_selected
} }
#[cfg(feature = "audio")]
pub fn click_sound(&self) -> &AssetPath { pub fn click_sound(&self) -> &AssetPath {
&self.click_sound &self.click_sound
} }
#[cfg(feature = "audio")]
pub fn hover_sound(&self) -> &AssetPath { pub fn hover_sound(&self) -> &AssetPath {
&self.hover_sound &self.hover_sound
} }