-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2220 from hannobraun/presentation
Add presentation layer
- Loading branch information
Showing
10 changed files
with
172 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
//! See [`Layers`]. | ||
pub mod objects; | ||
pub mod presentation; | ||
pub mod validation; | ||
|
||
mod layer; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! Layer infrastructure for [`Presentation`] | ||
use fj_interop::Color; | ||
|
||
use crate::{ | ||
objects::{AnyObject, Region, Stored}, | ||
presentation::Presentation, | ||
storage::Handle, | ||
}; | ||
|
||
use super::{Command, Event, Layer}; | ||
|
||
impl Layer<Presentation> { | ||
/// Set the color of a region | ||
pub fn set_color(&mut self, region: Handle<Region>, color: Color) { | ||
let mut events = Vec::new(); | ||
self.process(SetColor { region, color }, &mut events); | ||
} | ||
|
||
/// Mark an object as being derived from another | ||
pub fn derive_object( | ||
&mut self, | ||
original: AnyObject<Stored>, | ||
derived: AnyObject<Stored>, | ||
) { | ||
let mut events = Vec::new(); | ||
self.process(DeriveObject { original, derived }, &mut events); | ||
} | ||
} | ||
|
||
/// Set the color of a region | ||
pub struct SetColor { | ||
/// The region to set the color for | ||
region: Handle<Region>, | ||
|
||
/// The color to set | ||
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); | ||
} | ||
} | ||
|
||
/// Handle an object being derived from another | ||
pub struct DeriveObject { | ||
/// The original object | ||
original: AnyObject<Stored>, | ||
|
||
/// The derived object | ||
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, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Command for `Layer<Presentation>` | ||
pub enum PresentationCommand {} | ||
|
||
/// Event produced by `Layer<Presentation>` | ||
#[derive(Clone)] | ||
pub enum PresentationEvent { | ||
/// The color of a region is being set | ||
SetColor { | ||
/// The region the color is being set for | ||
region: Handle<Region>, | ||
|
||
/// The color being set | ||
color: Color, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! Presentation data for the object graph | ||
//! | ||
//! See [`Presentation`]. | ||
use std::collections::BTreeMap; | ||
|
||
use fj_interop::Color; | ||
|
||
use crate::{objects::Region, storage::Handle}; | ||
|
||
/// Presentation data for the object graph | ||
/// | ||
/// Assigns attributes relating to the presentation of objects (currently just a | ||
/// color) to those objects (currently only to regions). | ||
/// | ||
/// This data is made available through [`Layers`]. | ||
/// | ||
/// [`Layers`]: crate::layers::Layers | ||
#[derive(Default)] | ||
pub struct Presentation { | ||
/// Color assigned to regions | ||
/// | ||
/// Having a color is optional, so map does not necessarily contain | ||
/// assignments for all existing regions. | ||
pub color: BTreeMap<Handle<Region>, Color>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters