use fj_interop::Color;
use crate::{
objects::{AnyObject, Region, Stored},
presentation::Presentation,
storage::Handle,
};
use super::{Command, Event, Layer};
impl Layer<Presentation> {
pub fn set_color(&mut self, region: Handle<Region>, color: Color) {
let mut events = Vec::new();
self.process(SetColor { region, color }, &mut events);
}
pub fn derive_object(
&mut self,
original: AnyObject<Stored>,
derived: AnyObject<Stored>,
) {
let mut events = Vec::new();
self.process(DeriveObject { original, derived }, &mut events);
}
}
pub struct SetColor {
region: Handle<Region>,
color: Color,
}
impl Command<Presentation> for SetColor {
type Result = ();
type Event = Self;
fn decide(
self,
_: &Presentation,
events: &mut Vec<Self::Event>,
) -> Self::Result {
events.push(self);
}
}
impl Event<Presentation> for SetColor {
fn evolve(&self, state: &mut Presentation) {
state.color.insert(self.region.clone(), self.color);
}
}
pub struct DeriveObject {
original: AnyObject<Stored>,
derived: AnyObject<Stored>,
}
impl Command<Presentation> for DeriveObject {
type Result = ();
type Event = SetColor;
fn decide(
self,
state: &Presentation,
events: &mut Vec<Self::Event>,
) -> Self::Result {
if let (AnyObject::Region(original), AnyObject::Region(derived)) =
(self.original, self.derived)
{
if let Some(color) = state.color.get(&original.0).cloned() {
events.push(SetColor {
region: derived.into(),
color,
});
}
}
}
}
#[derive(Clone)]
pub enum PresentationEvent {
SetColor {
region: Handle<Region>,
color: Color,
},
}