vn-settings 0.7.0

Various settings intended to simulate visual novels
Documentation
use super::{Settings, lookup, push_all};

#[derive(Clone)]
pub struct BoxLayoutSettings<T> {
    pub hor: T,
    pub ver: T,
    pub width: T,
    pub height: T,
    pub corner: T,
    pub line: T,
    pub text_size: T,
    pub name_size: T,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BoxLayoutParameter {
    Hor,
    Ver,
    Width,
    Height,
    Corner,
    Line,
    TextSize,
    NameSize,
}

impl BoxLayoutParameter {
    pub const fn main(self) -> LayoutParameter {
        LayoutParameter::Main(self)
    }

    pub const fn list(self) -> LayoutParameter {
        LayoutParameter::List(self)
    }

    pub const fn menu(self) -> LayoutParameter {
        LayoutParameter::Menu(self)
    }
}

impl<T> Settings<BoxLayoutParameter> for BoxLayoutSettings<T> {
    type Value = T;

    fn get_mut(&mut self, parameter: &BoxLayoutParameter) -> &mut T {
        use BoxLayoutParameter::*;
        match parameter {
            Hor => &mut self.hor,
            Ver => &mut self.ver,
            Width => &mut self.width,
            Height => &mut self.height,
            Corner => &mut self.corner,
            Line => &mut self.line,
            TextSize => &mut self.text_size,
            NameSize => &mut self.name_size,
        }
    }
}

resettable!(<T> BoxLayoutSettings<T> { hor, ver, width, height, corner, line, text_size, name_size });

const BOX_LAYOUT_PARAMETERS: [(&str, BoxLayoutParameter); 8] = [
    ("hor", BoxLayoutParameter::Hor),
    ("ver", BoxLayoutParameter::Ver),
    ("width", BoxLayoutParameter::Width),
    ("height", BoxLayoutParameter::Height),
    ("corner", BoxLayoutParameter::Corner),
    ("line", BoxLayoutParameter::Line),
    ("text-size", BoxLayoutParameter::TextSize),
    ("name-size", BoxLayoutParameter::NameSize),
];

impl BoxLayoutParameter {
    pub fn parse(name: &str) -> Option<Self> {
        lookup(&BOX_LAYOUT_PARAMETERS, name)
    }
}

#[derive(Clone)]
pub struct ViewLayoutSettings<T> {
    pub hor: T,
    pub ver: T,
    pub zoom: T,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ViewLayoutParameter {
    Hor,
    Ver,
    Zoom,
}

impl<T> Settings<ViewLayoutParameter> for ViewLayoutSettings<T> {
    type Value = T;

    fn get_mut(&mut self, parameter: &ViewLayoutParameter) -> &mut T {
        use ViewLayoutParameter::*;
        match parameter {
            Hor => &mut self.hor,
            Ver => &mut self.ver,
            Zoom => &mut self.zoom,
        }
    }
}

resettable!(<T> ViewLayoutSettings<T> { hor, ver, zoom });

const VIEW_LAYOUT_PARAMETERS: [(&str, ViewLayoutParameter); 3] = [
    ("hor", ViewLayoutParameter::Hor),
    ("ver", ViewLayoutParameter::Ver),
    ("zoom", ViewLayoutParameter::Zoom),
];

impl ViewLayoutParameter {
    pub fn parse(name: &str) -> Option<Self> {
        lookup(&VIEW_LAYOUT_PARAMETERS, name)
    }
}

#[derive(Clone)]
pub struct LayoutSettings<T> {
    pub main: BoxLayoutSettings<T>,
    pub list: BoxLayoutSettings<T>,
    pub menu: BoxLayoutSettings<T>,
    pub view: ViewLayoutSettings<T>,
}

impl<T: From<f32>> LayoutSettings<T> {
    #[must_use]
    pub fn common() -> Self {
        Self {
            main: BoxLayoutSettings {
                hor: 0.0.into(),
                ver: 3.0.into(),
                width: 12.0.into(),
                height: 2.0.into(),
                corner: 0.25.into(),
                line: (1.0 / 0x40 as f32).into(),
                text_size: (1.0 / 3.0).into(),
                name_size: (1.0 / 6.0).into(),
            },
            list: BoxLayoutSettings {
                hor: 0.0.into(),
                ver: (-4.0).into(),
                width: 8.0.into(),
                height: 0.5.into(),
                corner: 0.25.into(),
                line: (1.0 / 0x40 as f32).into(),
                text_size: (1.0 / 4.0).into(),
                name_size: (1.0 / 6.0).into(),
            },
            menu: BoxLayoutSettings {
                hor: 0.0.into(),
                ver: 0.0.into(),
                width: 5.0.into(),
                height: 0.5.into(),
                corner: 0.08.into(),
                line: (1.0 / 0x40 as f32).into(),
                text_size: 0.25.into(),
                name_size: 0.35.into(),
            },
            view: ViewLayoutSettings {
                hor: 0.0.into(),
                ver: 0.0.into(),
                zoom: 1.0.into(),
            },
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum LayoutParameter {
    Main(BoxLayoutParameter),
    List(BoxLayoutParameter),
    Menu(BoxLayoutParameter),
    View(ViewLayoutParameter),
}

impl<T> Settings<LayoutParameter> for LayoutSettings<T> {
    type Value = T;

    fn get_mut(&mut self, parameter: &LayoutParameter) -> &mut T {
        use LayoutParameter::*;
        match parameter {
            Main(parameter) => self.main.get_mut(parameter),
            List(parameter) => self.list.get_mut(parameter),
            Menu(parameter) => self.menu.get_mut(parameter),
            View(parameter) => self.view.get_mut(parameter),
        }
    }
}

resettable!(<T> LayoutSettings<T> { main, list, menu });

type BoxLayoutKind = fn(BoxLayoutParameter) -> LayoutParameter;

const BOX_LAYOUT_KINDS: [(&str, BoxLayoutKind); 3] = [
    ("Main", LayoutParameter::Main),
    ("List", LayoutParameter::List),
    ("Menu", LayoutParameter::Menu),
];

impl LayoutParameter {
    pub fn parse(name: &str) -> Option<Self> {
        let (subpath, parameter_name) = name.split_once(':')?;
        if subpath == "View" {
            Some(Self::View(ViewLayoutParameter::parse(parameter_name)?))
        } else {
            Some(lookup(&BOX_LAYOUT_KINDS, subpath)?(
                BoxLayoutParameter::parse(parameter_name)?,
            ))
        }
    }

    pub(crate) fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
        for (kind, _) in BOX_LAYOUT_KINDS {
            push_all(&BOX_LAYOUT_PARAMETERS, &format!("{prefix}{kind}:"), out);
        }
        push_all(&VIEW_LAYOUT_PARAMETERS, &format!("{prefix}View:"), out);
    }
}