diff --git a/src/builder/builder.rs b/src/builder/builder.rs index cbe8a32..1364e68 100644 --- a/src/builder/builder.rs +++ b/src/builder/builder.rs @@ -23,13 +23,21 @@ pub struct GuiBuilder { impl GuiBuilder { pub fn new(gui_handler: &Arc, path: &AssetPath) -> Result> { - let validator = Validator::new(gui_handler, path)?; + let validator = Validator::new( + #[cfg(feature = "audio")] + gui_handler, + path, + )?; Self::_new(gui_handler, validator) } pub fn from_str(gui_handler: &Arc, s: &str) -> Result> { - let validator = Validator::from_str(gui_handler, s)?; + let validator = Validator::from_str( + #[cfg(feature = "audio")] + gui_handler, + s, + )?; Self::_new(gui_handler, validator) } diff --git a/src/builder/snippet.rs b/src/builder/snippet.rs index 42c0092..0e73de4 100644 --- a/src/builder/snippet.rs +++ b/src/builder/snippet.rs @@ -18,7 +18,11 @@ pub struct GuiSnippet { impl GuiSnippet { pub fn new(gui_handler: Arc, path: &AssetPath) -> Result> { - let validator = Validator::new(&gui_handler, path)?; + let validator = Validator::new( + #[cfg(feature = "audio")] + &gui_handler, + path, + )?; let root = validator.root(); diff --git a/src/builder/validator/buttoninfo.rs b/src/builder/validator/buttoninfo.rs index bbc4268..7cbdc1b 100644 --- a/src/builder/validator/buttoninfo.rs +++ b/src/builder/validator/buttoninfo.rs @@ -2,6 +2,7 @@ use crate::prelude::*; use anyhow::Result; use utilities::prelude::*; +#[cfg(feature = "audio")] use assetpath::AssetPath; use super::gridinfo::GridInfo; @@ -12,10 +13,12 @@ use std::str::from_utf8; use std::sync::{Arc, RwLock, Weak}; use super::validator::{ - cow_to_button_select_mode, cow_to_fill_type, cow_to_path, cow_to_str, cow_to_text_alignment, - str_into, + cow_to_button_select_mode, cow_to_fill_type, cow_to_str, cow_to_text_alignment, str_into, }; +#[cfg(feature = "audio")] +use super::validator::cow_to_path; + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum NeighbourDirection { East, @@ -45,7 +48,10 @@ pub struct ButtonInfo { pub normal: Option, pub selected: Option, + #[cfg(feature = "audio")] pub click_sound: AssetPath, + + #[cfg(feature = "audio")] pub hover_sound: AssetPath, pub text: RwLock, @@ -68,7 +74,7 @@ pub struct ButtonInfo { impl ButtonInfo { pub fn new<'a>( - gui_handler: &Arc, + #[cfg(feature = "audio")] gui_handler: &Arc, attributes: quick_xml::events::attributes::Attributes<'a>, grid: &Arc, ) -> Result { @@ -84,11 +90,13 @@ impl ButtonInfo { normal: Self::find_menu_button(grid), selected: Self::find_menu_button_selected(grid), + #[cfg(feature = "audio")] click_sound: match Self::find_click_sound(grid) { Some(sound) => sound, None => gui_handler.click_sound().clone(), }, + #[cfg(feature = "audio")] hover_sound: match Self::find_hover_sound(grid) { Some(sound) => sound, None => gui_handler.hover_sound().clone(), @@ -122,8 +130,13 @@ impl ButtonInfo { b"y_size" => button_info.y_dim = str_into(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)), + + #[cfg(feature = "audio")] 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"select_mode" => { button_info.select_mode = cow_to_button_select_mode(attribute.value)? } @@ -202,6 +215,7 @@ impl ButtonInfo { None } + #[cfg(feature = "audio")] fn find_click_sound(grid: &GridInfo) -> Option { // return immediately if present if let Some(click_sound) = &grid.click_sound { @@ -218,6 +232,7 @@ impl ButtonInfo { None } + #[cfg(feature = "audio")] fn find_hover_sound(grid: &GridInfo) -> Option { // return immediately if present if let Some(hover_sound) = &grid.hover_sound { diff --git a/src/builder/validator/validator.rs b/src/builder/validator/validator.rs index 2fec9d1..b2dc02f 100644 --- a/src/builder/validator/validator.rs +++ b/src/builder/validator/validator.rs @@ -59,19 +59,33 @@ enum CurrentElement { } impl Validator { - pub fn from_str(gui_handler: &Arc, s: &str) -> Result { - Self::_new(gui_handler, Reader::from_str(s)) + pub fn from_str( + #[cfg(feature = "audio")] gui_handler: &Arc, + s: &str, + ) -> Result { + Self::_new( + #[cfg(feature = "audio")] + gui_handler, + Reader::from_str(s), + ) } - pub fn new(gui_handler: &Arc, path: &AssetPath) -> Result { + pub fn new( + #[cfg(feature = "audio")] gui_handler: &Arc, + path: &AssetPath, + ) -> Result { Self::_new( + #[cfg(feature = "audio")] gui_handler, Reader::from_file(path.full_path()).with_context(|| path.full_path())?, ) } #[inline] - fn _new(gui_handler: &Arc, mut reader: Reader) -> Result { + fn _new( + #[cfg(feature = "audio")] gui_handler: &Arc, + mut reader: Reader, + ) -> Result { // removes white spaces in texts reader.trim_text(true); @@ -96,7 +110,12 @@ impl Validator { 'outer: loop { match reader.read_event_into(&mut buf) { Ok(Event::Start(ref e)) => { - let start_result = Self::process_start(gui_handler, e, ¤t_element)?; + let start_result = Self::process_start( + #[cfg(feature = "audio")] + gui_handler, + e, + ¤t_element, + )?; match start_result { StartResult::Root(root_info) => { @@ -343,7 +362,7 @@ impl Validator { } fn process_start<'a>( - gui_handler: &Arc, + #[cfg(feature = "audio")] gui_handler: &Arc, element: &quick_xml::events::BytesStart<'a>, handle: &CurrentElement, ) -> Result { @@ -384,7 +403,12 @@ impl Validator { }, b"button" => match handle { 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), Err(msg) => return Err(msg), } diff --git a/src/guihandler/guihandler.rs b/src/guihandler/guihandler.rs index 056fb01..66b614a 100644 --- a/src/guihandler/guihandler.rs +++ b/src/guihandler/guihandler.rs @@ -31,7 +31,9 @@ pub struct GuiHandlerCreateInfo { pub font_path: AssetPath, // sound info + #[cfg(feature = "audio")] pub click_sound: AssetPath, + #[cfg(feature = "audio")] pub hover_sound: AssetPath, // resource base directory @@ -108,7 +110,11 @@ pub struct GuiHandler { menu_button: AssetPath, menu_button_selected: AssetPath, + + #[cfg(feature = "audio")] click_sound: AssetPath, + + #[cfg(feature = "audio")] hover_sound: AssetPath, // ----- gui handling ----- @@ -195,6 +201,7 @@ impl GuiHandler { menu_button_selected }, + #[cfg(feature = "audio")] click_sound: { let mut click_sound = gui_handler_create_info.click_sound; click_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path()); @@ -202,6 +209,7 @@ impl GuiHandler { click_sound }, + #[cfg(feature = "audio")] hover_sound: { let mut hover_sound = gui_handler_create_info.hover_sound; hover_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path()); @@ -441,10 +449,12 @@ impl GuiHandler { &self.menu_button_selected } + #[cfg(feature = "audio")] pub fn click_sound(&self) -> &AssetPath { &self.click_sound } + #[cfg(feature = "audio")] pub fn hover_sound(&self) -> &AssetPath { &self.hover_sound }