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 {
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)
}
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)
}

View file

@ -18,7 +18,11 @@ pub struct GuiSnippet {
impl GuiSnippet {
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();

View file

@ -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<FillTypeInfo>,
pub selected: Option<FillTypeInfo>,
#[cfg(feature = "audio")]
pub click_sound: AssetPath,
#[cfg(feature = "audio")]
pub hover_sound: AssetPath,
pub text: RwLock<String>,
@ -68,7 +74,7 @@ pub struct ButtonInfo {
impl ButtonInfo {
pub fn new<'a>(
gui_handler: &Arc<GuiHandler>,
#[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
attributes: quick_xml::events::attributes::Attributes<'a>,
grid: &Arc<GridInfo>,
) -> Result<ButtonInfo> {
@ -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<AssetPath> {
// 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<AssetPath> {
// return immediately if present
if let Some(hover_sound) = &grid.hover_sound {

View file

@ -59,19 +59,33 @@ enum CurrentElement {
}
impl Validator {
pub fn from_str(gui_handler: &Arc<GuiHandler>, s: &str) -> Result<Validator> {
Self::_new(gui_handler, Reader::from_str(s))
pub fn from_str(
#[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(
#[cfg(feature = "audio")]
gui_handler,
Reader::from_file(path.full_path()).with_context(|| path.full_path())?,
)
}
#[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
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, &current_element)?;
let start_result = Self::process_start(
#[cfg(feature = "audio")]
gui_handler,
e,
&current_element,
)?;
match start_result {
StartResult::Root(root_info) => {
@ -343,7 +362,7 @@ impl Validator {
}
fn process_start<'a>(
gui_handler: &Arc<GuiHandler>,
#[cfg(feature = "audio")] gui_handler: &Arc<GuiHandler>,
element: &quick_xml::events::BytesStart<'a>,
handle: &CurrentElement,
) -> Result<StartResult> {
@ -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),
}

View file

@ -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
}