use super::ParamKind;
use std::sync::Arc;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Material {
pub(crate) label: Arc<str>,
pub(crate) fragment: String,
pub(crate) params: Vec<(String, ParamKind)>,
pub(crate) reads_texture: bool,
pub(crate) reads_backdrop: bool,
pub(crate) animated: bool,
}
impl Material {
pub fn new(label: impl Into<Arc<str>>, fragment: impl Into<String>) -> Self {
Self {
label: label.into(),
fragment: fragment.into(),
params: Vec::new(),
reads_texture: false,
reads_backdrop: false,
animated: false,
}
}
pub fn param(mut self, name: impl Into<String>, kind: ParamKind) -> Self {
self.params.push((name.into(), kind));
self
}
pub fn reads_texture(mut self) -> Self {
self.reads_texture = true;
self
}
pub fn reads_backdrop(mut self) -> Self {
self.reads_backdrop = true;
self
}
pub fn animated(mut self) -> Self {
self.animated = true;
self
}
pub fn label(&self) -> &str {
&self.label
}
pub fn fragment(&self) -> &str {
&self.fragment
}
pub fn params(&self) -> &[(String, ParamKind)] {
&self.params
}
pub(crate) fn content_key(&self) -> String {
let mut key = String::with_capacity(self.fragment.len() + 64);
key.push_str(&self.fragment);
key.push('\u{0}');
for (name, kind) in &self.params {
key.push_str(name);
key.push(':');
key.push_str(&format!("{kind:?}"));
key.push(';');
}
key.push_str(&format!(
"\u{0}{}{}{}",
u8::from(self.reads_texture),
u8::from(self.reads_backdrop),
u8::from(self.animated)
));
key
}
}