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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use color::{Rgba,ToRgba};
use super::{Property, texture::TextureSampler, AlphaType};
use super::parameter::{Parameter, Changed};

pub struct BasicMaterial{
    color: Option<Rgba<f32>>,
    texture: Option<TextureSampler>,
    properties: Vec<Property>,
    alpha_type: AlphaType,
    changed: Changed,
}

pub struct Builder{
    color: Option<Rgba<f32>>,
    properties: Vec<Property>,
    alpha_type: AlphaType,
}

pub struct BuilderWithTexture{
    color: Option<Rgba<f32>>,
    properties: Vec<Property>,
    texture: TextureSampler,
    alpha_type: AlphaType,
}

impl Builder{
    pub fn new() -> Builder{
        Builder{
            color: None,
            properties: vec![],
            alpha_type: AlphaType::None,
        }
    }

    pub fn color<C: ToRgba>(mut self, color: &C) -> Builder{
        self.color = Some(color.to_rgba());
        self
    }

    pub fn texture<T: Into<TextureSampler>>(self, texture: T) -> BuilderWithTexture{
        BuilderWithTexture{
            color: self.color,
            properties: self.properties,
            texture: texture.into(),
            alpha_type: self.alpha_type,
        }
    }

    pub fn properties(mut self, properties: Vec<Property>) -> Builder{
        self.properties = properties;
        self
    }

    pub fn alpha_type(mut self, alpha_type: AlphaType) -> Builder{
        self.alpha_type = alpha_type;
        self
    }

    pub fn build(self) -> BasicMaterial{
        BasicMaterial{
            color: self.color,
            properties: self.properties,
            alpha_type: self.alpha_type,
            texture: None,
            changed: Changed::default(),
        }
    }
}

impl BuilderWithTexture{
    pub fn color<C: ToRgba>(mut self, color: &C) -> BuilderWithTexture{
        self.color = Some(color.to_rgba());
        self
    }

    pub fn texture<T: Into<TextureSampler>>(self, texture: T) -> BuilderWithTexture{
        BuilderWithTexture{
            color: self.color,
            properties: self.properties,
            alpha_type: self.alpha_type,
            texture: texture.into()
        }
    }

    pub fn alpha_type(mut self, alpha_type: AlphaType) -> BuilderWithTexture{
        self.alpha_type = alpha_type;
        self
    }

    pub fn properties(mut self, properties: Vec<Property>) -> BuilderWithTexture{
        self.properties = properties;
        self
    }

    pub fn build(self) -> BasicMaterial{
        BasicMaterial{
            color: self.color,
            properties: self.properties,
            texture: Some(self.texture.into()),
            alpha_type: self.alpha_type,
            changed: Changed::default(),
        }
    }
}

impl Default for BasicMaterial{
    fn default() -> BasicMaterial{
        Builder::new().build()
    }
}

impl BasicMaterial{
    pub fn color(&self) -> Option<&Rgba<f32>>{
        self.color.as_ref()
    }

    pub fn texture(&self) -> Option<&TextureSampler>{
        self.texture.as_ref()
    }

    pub fn properties(&self) -> &[Property]{
        &self.properties
    }

    pub fn alpha_type(&self) -> AlphaType{
        self.alpha_type
    }
}



impl super::Material for BasicMaterial{
    fn parameters(&mut self) -> Vec<Parameter>{
        let changed = &self.changed;
        self.color.iter_mut()
            .map(move |color| Parameter::new("base_color", color, changed))
            .chain(self.texture.iter_mut().map(move |texture| Parameter::new("texture", texture, changed)))
            .chain(Some(Parameter::new("alpha_type", &mut self.alpha_type, &self.changed)))
            .collect()
    }

    fn parameter(&mut self, name: &str) -> Option<Parameter>{
        match name {
            "base_color" => if let Some(base_color) = self.color.as_mut(){
                Some(Parameter::new("base_color", base_color, &self.changed))
            }else{
                None
            },
            "texture" => if let Some(texture) = self.texture.as_mut(){
                Some(Parameter::new("texture", texture, &self.changed))
            }else{
                None
            },
            "alpha_type" => Some(Parameter::new("alpha_type", &mut self.alpha_type, &self.changed)),
            _ => None,
        }
    }

    fn changed(&self) -> bool{
        self.changed.changed()
    }

    fn reset_changed(&mut self){
        self.changed.reset();
    }
}