summaryrefslogtreecommitdiff
path: root/rust/crates/graphics/src/params.rs
blob: 6c0fedd2d2fe2bffc2494c88ff036bcb20e797b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use scene::params::{ParamValue, ParamValues};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamGroup {
    Stroke,
}

impl ParamGroup {
    pub fn as_str(&self) -> &'static str {
        match self {
            ParamGroup::Stroke => "stroke",
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct SelectOption {
    pub value: &'static str,
    pub label: &'static str,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ParamDefinition {
    Number {
        key: &'static str,
        label: &'static str,
        group: Option<ParamGroup>,
        default: f64,
        min: f64,
        max: Option<f64>,
        step: f64,
        short_label: Option<&'static str>,
    },
    Color {
        key: &'static str,
        label: &'static str,
        group: Option<ParamGroup>,
        default: &'static str,
    },
    Select {
        key: &'static str,
        label: &'static str,
        group: Option<ParamGroup>,
        default: &'static str,
        options: &'static [SelectOption],
    },
}

impl ParamDefinition {
    pub fn key(&self) -> &'static str {
        match self {
            ParamDefinition::Number { key, .. } => key,
            ParamDefinition::Color { key, .. } => key,
            ParamDefinition::Select { key, .. } => key,
        }
    }

    pub fn default_value(&self) -> ParamValue {
        match self {
            ParamDefinition::Number { default, .. } => ParamValue::Number(*default),
            ParamDefinition::Color { default, .. } => ParamValue::String((*default).to_string()),
            ParamDefinition::Select { default, .. } => ParamValue::String((*default).to_string()),
        }
    }
}

pub fn build_default_param_values(params: &[ParamDefinition]) -> ParamValues {
    params
        .iter()
        .map(|param| (param.key().to_string(), param.default_value()))
        .collect()
}