gled-model 2.32.1

The data model of gled: projects, scenes, effects, animations, curves and assets
Documentation
pub mod variables;

use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumDiscriminants, EnumIter, EnumString};
use variables::{Variables, VariablesCount};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Argument {
    pub name: String,
    pub kind: ArgumentKind,
}

impl Default for Argument {
    fn default() -> Self {
        Self {
            name: "New Argument".to_string(),
            kind: ArgumentKind::default(),
        }
    }
}

impl Argument {
    pub fn function_name(&self) -> String {
        self.name.to_ascii_lowercase().replace(" ", "_")
    }

    pub fn function(&self) -> String {
        self.kind.variables().function(&self.function_name())
    }

    pub(crate) fn shader_code_for_getter(&self, count: VariablesCount) -> String {
        self.kind
            .variables()
            .shader_code_for_getter(&self.function_name(), count)
    }
}

/// `ArgumentKindId` is the kind without its settings. It reads as the
/// variant name ("Center") and parses from it in any case, as OSC sends it.
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq, EnumDiscriminants)]
#[strum_discriminants(
    name(ArgumentKindId),
    derive(Serialize, Deserialize, AsRefStr, EnumIter, EnumString),
    strum(ascii_case_insensitive)
)]
pub enum ArgumentKind {
    Center,
    Selection {
        variants: Vec<String>,
    },
    Slider {
        min: u32,
        max: u32,
    },
    Checkbox,
    #[default]
    Percentage,
    Degrees,
}

impl ArgumentKind {
    pub fn variables(&self) -> Variables {
        match self {
            Self::Center => Variables::Vec2F32,
            Self::Selection { .. } => Variables::U32,
            Self::Slider { .. } => Variables::U32,
            Self::Checkbox => Variables::U32,
            Self::Percentage => Variables::F32,
            Self::Degrees => Variables::F32,
        }
    }
}

impl From<ArgumentKindId> for ArgumentKind {
    fn from(kind: ArgumentKindId) -> Self {
        match kind {
            ArgumentKindId::Center => Self::Center,
            ArgumentKindId::Selection => Self::Selection { variants: vec![] },
            ArgumentKindId::Slider => Self::Slider { min: 0, max: 100 },
            ArgumentKindId::Checkbox => Self::Checkbox,
            ArgumentKindId::Percentage => Self::Percentage,
            ArgumentKindId::Degrees => Self::Degrees,
        }
    }
}