Skip to main content

hyperframes/core/
vfx.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4pub const HF_VFX_CHAIN_VERSION: u32 = 1;
5pub const HF_VFX_ATTR: &str = "data-vfx-chain";
6
7/// VFX capture mode for a shader effect.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
9#[serde(rename_all = "lowercase")]
10pub enum HfVfxCapture {
11    #[default]
12    None,
13    SelfCapture,
14    Backdrop,
15}
16
17/// An instance of a VFX node in a layer's effect chain.
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
19pub struct HfVfxNode {
20    pub id: String,
21    pub def: String,
22    #[serde(default)]
23    pub params: HashMap<String, serde_json::Value>,
24    #[serde(default = "default_true")]
25    pub enabled: bool,
26}
27
28fn default_true() -> bool {
29    true
30}
31
32impl HfVfxNode {
33    pub fn new(id: impl Into<String>, def: impl Into<String>) -> Self {
34        Self {
35            id: id.into(),
36            def: def.into(),
37            params: HashMap::new(),
38            enabled: true,
39        }
40    }
41
42    pub fn with_param(mut self, key: impl Into<String>, val: impl Into<serde_json::Value>) -> Self {
43        self.params.insert(key.into(), val.into());
44        self
45    }
46}
47
48/// A serialized chain of VFX nodes applied to a layer (`data-vfx-chain`).
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
50pub struct HfVfxChain {
51    pub version: u32,
52    pub nodes: Vec<HfVfxNode>,
53}
54
55impl Default for HfVfxChain {
56    fn default() -> Self {
57        Self {
58            version: HF_VFX_CHAIN_VERSION,
59            nodes: Vec::new(),
60        }
61    }
62}
63
64impl HfVfxChain {
65    pub fn new(nodes: Vec<HfVfxNode>) -> Self {
66        Self {
67            version: HF_VFX_CHAIN_VERSION,
68            nodes,
69        }
70    }
71
72    pub fn to_json(&self) -> Result<String, serde_json::Error> {
73        serde_json::to_string(self)
74    }
75
76    pub fn from_json(json_str: &str) -> Result<Self, serde_json::Error> {
77        serde_json::from_str(json_str)
78    }
79}
80
81/// List of standard supported VFX definitions.
82pub const STANDARD_VFX_DEFS: &[&str] = &[
83    "fractal-noise",
84    "displacement-map",
85    "luma-matte",
86    "noise",
87    "wave-warp",
88];
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn test_vfx_chain_roundtrip() {
96        let node1 = HfVfxNode::new("n1", "noise")
97            .with_param("amount", 0.15)
98            .with_param("animated", true);
99        let node2 = HfVfxNode::new("n2", "displacement-map").with_param("maxDisplacement", 20.0);
100
101        let chain = HfVfxChain::new(vec![node1, node2]);
102        let json = chain.to_json().unwrap();
103        let parsed = HfVfxChain::from_json(&json).unwrap();
104
105        assert_eq!(chain, parsed);
106        assert_eq!(parsed.nodes.len(), 2);
107        assert_eq!(parsed.nodes[0].def, "noise");
108    }
109
110    #[test]
111    fn test_standard_defs() {
112        assert!(STANDARD_VFX_DEFS.contains(&"fractal-noise"));
113        assert!(STANDARD_VFX_DEFS.contains(&"wave-warp"));
114        assert!(STANDARD_VFX_DEFS.contains(&"luma-matte"));
115    }
116}