fj_core/layers/
presentation.rs

1//! Layer infrastructure for [`Presentation`]
2
3use fj_interop::Color;
4
5use crate::{
6    objects::{AnyObject, Region, Stored},
7    presentation::Presentation,
8    storage::Handle,
9};
10
11use super::{Command, Event, Layer};
12
13impl Layer<Presentation> {
14    /// Set the color of a region
15    pub fn set_color(&mut self, region: Handle<Region>, color: Color) {
16        let mut events = Vec::new();
17        self.process(SetColor { region, color }, &mut events);
18    }
19
20    /// Mark an object as being derived from another
21    pub fn derive_object(
22        &mut self,
23        original: AnyObject<Stored>,
24        derived: AnyObject<Stored>,
25    ) {
26        let mut events = Vec::new();
27        self.process(DeriveObject { original, derived }, &mut events);
28    }
29}
30
31/// Set the color of a region
32pub struct SetColor {
33    /// The region to set the color for
34    region: Handle<Region>,
35
36    /// The color to set
37    color: Color,
38}
39
40impl Command<Presentation> for SetColor {
41    type Result = ();
42    type Event = Self;
43
44    fn decide(
45        self,
46        _: &Presentation,
47        events: &mut Vec<Self::Event>,
48    ) -> Self::Result {
49        events.push(self);
50    }
51}
52
53impl Event<Presentation> for SetColor {
54    fn evolve(&self, state: &mut Presentation) {
55        state.color.insert(self.region.clone(), self.color);
56    }
57}
58
59/// Handle an object being derived from another
60pub struct DeriveObject {
61    /// The original object
62    original: AnyObject<Stored>,
63
64    /// The derived object
65    derived: AnyObject<Stored>,
66}
67
68impl Command<Presentation> for DeriveObject {
69    type Result = ();
70    type Event = SetColor;
71
72    fn decide(
73        self,
74        state: &Presentation,
75        events: &mut Vec<Self::Event>,
76    ) -> Self::Result {
77        if let (AnyObject::Region(original), AnyObject::Region(derived)) =
78            (self.original, self.derived)
79        {
80            if let Some(color) = state.color.get(&original.0).cloned() {
81                events.push(SetColor {
82                    region: derived.into(),
83                    color,
84                });
85            }
86        }
87    }
88}
89
90/// Event produced by `Layer<Presentation>`
91#[derive(Clone)]
92pub enum PresentationEvent {
93    /// The color of a region is being set
94    SetColor {
95        /// The region the color is being set for
96        region: Handle<Region>,
97
98        /// The color being set
99        color: Color,
100    },
101}