fj_core/layers/
presentation.rs1use 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 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 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
31pub struct SetColor {
33 region: Handle<Region>,
35
36 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
59pub struct DeriveObject {
61 original: AnyObject<Stored>,
63
64 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#[derive(Clone)]
92pub enum PresentationEvent {
93 SetColor {
95 region: Handle<Region>,
97
98 color: Color,
100 },
101}