vn-settings 0.6.0

Various settings intended to simulate visual novels
Documentation
use super::Settings;

use simple_color::Color;

#[derive(Clone, Debug)]
pub struct BoxColors<T> {
    pub text_line: T,
    pub text_fill: T,
    pub name_line: T,
    pub name_fill: T,
}

impl<T: From<Color>> BoxColors<T> {
    pub fn common() -> Self {
        Self {
            text_line: Color::WHITE.into(),
            text_fill: Color::BLACK.into(),
            name_line: Color::WHITE.into(),
            name_fill: Color::BLACK.into(),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BoxColorParameter {
    TextLine,
    TextFill,
    NameLine,
    NameFill,
}

impl<T> Settings<BoxColorParameter> for BoxColors<T> {
    type Value = T;

    fn get_mut(&mut self, parameter: &BoxColorParameter) -> &mut T {
        use BoxColorParameter::*;
        match parameter {
            TextLine => &mut self.text_line,
            TextFill => &mut self.text_fill,
            NameLine => &mut self.name_line,
            NameFill => &mut self.name_fill,
        }
    }
}

resettable!(<T> BoxColors<T> { text_line, text_fill, name_line, name_fill });

#[derive(Clone, Debug)]
pub struct SelectableBoxColors<T> {
    pub default: BoxColors<T>,
    pub selected: BoxColors<T>,
}

impl<T: From<Color>> SelectableBoxColors<T> {
    pub fn common() -> Self {
        Self {
            default: BoxColors::common(),
            selected: BoxColors {
                text_fill: Color::gray(64).into(),
                ..BoxColors::common()
            },
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SelectableBoxColorParameter {
    pub selected: bool,
    pub parameter: BoxColorParameter,
}

impl<T> Settings<SelectableBoxColorParameter> for SelectableBoxColors<T> {
    type Value = T;

    fn get_mut(&mut self, parameter: &SelectableBoxColorParameter) -> &mut T {
        if parameter.selected {
            &mut self.selected
        } else {
            &mut self.default
        }
        .get_mut(&parameter.parameter)
    }
}

resettable!(<T> SelectableBoxColors<T> { default, selected });

#[derive(Clone)]
pub struct ColorSettings<T> {
    pub background: T,
    pub foreground: T,
    pub dialog_box: BoxColors<T>,
    pub select_box: SelectableBoxColors<T>,
    pub revert_box: SelectableBoxColors<T>,
    pub menu: SelectableBoxColors<T>,
}

impl<T: From<Color>> ColorSettings<T> {
    pub fn common() -> Self {
        Self {
            background: Color::BLACK.into(),
            foreground: Color::BLACK.a(0).into(),
            dialog_box: BoxColors::common(),
            select_box: SelectableBoxColors::common(),
            revert_box: SelectableBoxColors::common(),
            menu: SelectableBoxColors {
                default: BoxColors {
                    text_fill: Color::gray(30).a(235).into(),
                    text_line: Color::gray(175).into(),
                    name_fill: Color::gray(30).a(235).into(),
                    name_line: Color::WHITE.into(),
                },
                selected: BoxColors {
                    text_fill: Color::rgba(70, 90, 150, 235).into(),
                    text_line: Color::WHITE.into(),
                    name_fill: Color::gray(30).a(235).into(),
                    name_line: Color::WHITE.into(),
                },
            },
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ColorParameter {
    Background,
    Foreground,
    Dialog(BoxColorParameter),
    Choice(SelectableBoxColorParameter),
    Revert(SelectableBoxColorParameter),
    Menu(SelectableBoxColorParameter),
}

impl<T> Settings<ColorParameter> for ColorSettings<T> {
    type Value = T;

    fn get_mut(&mut self, parameter: &ColorParameter) -> &mut T {
        use ColorParameter::*;
        match parameter {
            Background => &mut self.background,
            Foreground => &mut self.foreground,
            Dialog(parameter) => self.dialog_box.get_mut(parameter),
            Choice(parameter) => self.select_box.get_mut(parameter),
            Revert(parameter) => self.revert_box.get_mut(parameter),
            Menu(parameter) => self.menu.get_mut(parameter),
        }
    }
}

resettable!(<T> ColorSettings<T> { background, foreground, dialog_box, select_box, revert_box, menu });

impl BoxColorParameter {
    fn parse(name: &str) -> Option<Self> {
        Some(match name {
            "text-fill" => Self::TextFill,
            "text-line" => Self::TextLine,
            "name-fill" => Self::NameFill,
            "name-line" => Self::NameLine,
            _ => return None,
        })
    }
}

impl SelectableBoxColorParameter {
    fn parse(name: &str) -> Option<Self> {
        let (selection, parameter_name) = name.split_once('-')?;
        let selected = match selection {
            "default" => false,
            "select" => true,
            _ => return None,
        };
        Some(Self {
            selected,
            parameter: BoxColorParameter::parse(parameter_name)?,
        })
    }
}

impl ColorParameter {
    pub fn parse(name: &str) -> Option<Self> {
        Some(match name {
            "background" => Self::Background,
            "foreground" => Self::Foreground,
            _ => {
                let (kind, parameter_name) = name.split_once('-')?;
                match kind {
                    "dialog" => Self::Dialog(BoxColorParameter::parse(parameter_name)?),
                    "choice" => Self::Choice(SelectableBoxColorParameter::parse(parameter_name)?),
                    "revert" => Self::Revert(SelectableBoxColorParameter::parse(parameter_name)?),
                    "menu" => Self::Menu(SelectableBoxColorParameter::parse(parameter_name)?),
                    _ => return None,
                }
            }
        })
    }
}