use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SfxChannels {
map: [u8; MAX_SFX_TRACKS],
}
impl SfxChannels {
const NONE: u8 = u8::MAX;
pub const AUTO: SfxChannels = SfxChannels {
map: [Self::NONE; MAX_SFX_TRACKS],
};
pub fn new(channels: &[u8]) -> Self {
let mut map = [Self::NONE; MAX_SFX_TRACKS];
for (m, c) in map.iter_mut().zip(channels) {
*m = *c;
}
SfxChannels { map }
}
pub fn is_auto(&self) -> bool {
self.map[0] == Self::NONE
}
pub fn resolve(&self, tracks: usize, channels: usize) -> Option<[u8; MAX_SFX_TRACKS]> {
if tracks == 0 || tracks > MAX_SFX_TRACKS || tracks > channels {
return None;
}
let mut out = [Self::NONE; MAX_SFX_TRACKS];
for (t, o) in out.iter_mut().enumerate().take(tracks) {
let ch = if self.is_auto() {
(channels - 1 - t) as u8
} else {
self.map[t]
};
if ch == Self::NONE || ch as usize >= channels {
return None;
}
*o = ch;
}
for i in 0..tracks {
for j in i + 1..tracks {
if out[i] == out[j] {
return None;
}
}
}
Some(out)
}
}
impl Default for SfxChannels {
fn default() -> Self {
SfxChannels::AUTO
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SfxError {
BadSlot,
BadChannels,
Busy,
}
impl crate::text::Localized for SfxError {
fn text_key(&self) -> &'static str {
match self {
SfxError::BadSlot => "err-sfx-bad-slot",
SfxError::BadChannels => "err-sfx-bad-channels",
SfxError::Busy => "err-sfx-busy",
}
}
fn catalogs(&self) -> &'static [crate::text::Catalog] {
crate::text::table::CATALOGS
}
}
impl core::fmt::Display for SfxError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
crate::text::write_localized(f, self, crate::text::DEFAULT_LANG)
}
}