diff --git a/examples/code/examples/code-viewer.rs b/examples/code/examples/code-viewer.rs index 16807d5..1919b28 100644 --- a/examples/code/examples/code-viewer.rs +++ b/examples/code/examples/code-viewer.rs @@ -6,7 +6,7 @@ use tracing::info; use winit::dpi::LogicalSize; use massive_geometry::{Camera, SizeI}; -use massive_scene::PositionedShape; +use massive_scene::Visual; use massive_shell::{shell, ApplicationContext}; use shared::{ application::{Application, UpdateResponse}, @@ -102,12 +102,12 @@ async fn code_viewer(mut ctx: ApplicationContext) -> Result<()> { let page_size = SizeI::new(1280, height as u64); let mut application = Application::default(); let mut current_matrix = application.matrix(page_size); - let matrix = director.cast(current_matrix); - let position = director.cast(matrix.clone().into()); + let matrix = director.stage(current_matrix); + let location = director.stage(matrix.clone().into()); - // Hold the positioned shapes in this context, otherwise they will disappear. - let _positioned_shape = director.cast(PositionedShape::new( - position.clone(), + // Hold the visual in this context, otherwise it will disappear. + let _visual = director.stage(Visual::new( + location.clone(), glyph_runs.into_iter().map(|m| m.into()).collect::>(), )); diff --git a/examples/code/examples/code.rs b/examples/code/examples/code.rs index bcb3803..09c806d 100644 --- a/examples/code/examples/code.rs +++ b/examples/code/examples/code.rs @@ -29,7 +29,7 @@ use winit::dpi::LogicalSize; use crate::attributed_text::TextAttribute; use massive_geometry::{Camera, Color, SizeI}; -use massive_scene::PositionedShape; +use massive_scene::Visual; use massive_shapes::TextWeight; use massive_shell::{shell, ApplicationContext}; @@ -283,11 +283,11 @@ async fn application(mut ctx: ApplicationContext) -> Result<()> { .await?; let mut current_matrix = application.matrix(page_size); - let matrix = director.cast(current_matrix); - let position = director.cast(matrix.clone().into()); + let matrix = director.stage(current_matrix); + let location = director.stage(matrix.clone().into()); - let _positioned_shape = director.cast(PositionedShape::new( - position.clone(), + let _visual = director.stage(Visual::new( + location.clone(), glyph_runs .into_iter() .map(|run| run.into()) diff --git a/examples/hello/examples/hello.rs b/examples/hello/examples/hello.rs index 6c78024..db48d92 100644 --- a/examples/hello/examples/hello.rs +++ b/examples/hello/examples/hello.rs @@ -43,7 +43,7 @@ async fn application(mut ctx: ApplicationContext) -> Result<()> { .new_renderer(font_system, camera, window.inner_size()) .await?; - let _positioned_shapes = legacy::into_positioned_shapes(&mut director, shapes); + let _visuals = legacy::into_visuals(&mut director, shapes); director.action()?; loop { diff --git a/examples/logs/examples/logs.rs b/examples/logs/examples/logs.rs index c5edecc..c4a864a 100644 --- a/examples/logs/examples/logs.rs +++ b/examples/logs/examples/logs.rs @@ -22,7 +22,7 @@ use winit::dpi::LogicalSize; use logs::terminal::{color_schemes, Rgb}; use massive_geometry::{Camera, Color, Identity, Vector3}; -use massive_scene::{Matrix, Position, PositionedShape}; +use massive_scene::{Location, Matrix, Visual}; use massive_shapes::TextWeight; use massive_shell::{shell, ApplicationContext}; use shared::{ @@ -98,14 +98,14 @@ async fn logs(mut receiver: UnboundedReceiver>, mut ctx: ApplicationCont let mut page_size = (1280u32, 1); let mut application = Application::default(); let mut current_matrix = application.matrix(page_size); - let page_matrix = director.cast(current_matrix); - let page_position = director.cast(Position::from(page_matrix.clone())); + let page_matrix = director.stage(current_matrix); + let page_location = director.stage(Location::from(page_matrix.clone())); // We move up the lines by their top position. - let move_up_matrix = director.cast(Matrix::identity()); + let move_up_matrix = director.stage(Matrix::identity()); // Final position for all lines (runs are y-translated, but only increasing). - let position = director.cast(Position { - parent: Some(page_position), + let location = director.stage(Location { + parent: Some(page_location), matrix: move_up_matrix.clone(), }); @@ -113,8 +113,7 @@ async fn logs(mut receiver: UnboundedReceiver>, mut ctx: ApplicationCont let max_lines = 100; - // Hold the positioned lines, otherwise they will disappear. - let mut positioned_lines = VecDeque::new(); + let mut lines = VecDeque::new(); loop { select! { @@ -126,20 +125,20 @@ async fn logs(mut receiver: UnboundedReceiver>, mut ctx: ApplicationCont shape_log_line(&bytes, y, &mut font_system) }; - let line = director.cast(PositionedShape::new(position.clone(), new_runs.into_iter().map( + let line = director.stage(Visual::new(location.clone(), new_runs.into_iter().map( |run| run.into()).collect::>())); - positioned_lines.push_back((y, height, line)); + lines.push_back((y, height, line)); - while positioned_lines.len() > max_lines { - positioned_lines.pop_front(); + while lines.len() > max_lines { + lines.pop_front(); }; // Update page size. - let top_line = positioned_lines.front().unwrap(); + let top_line = lines.front().unwrap(); move_up_matrix.update(Matrix::from_translation((0., -top_line.0, 0.).into())); - let last_line = positioned_lines.back().unwrap(); + let last_line = lines.back().unwrap(); page_size.1 = (last_line.0 + last_line.1 - top_line.0) as u32; director.action()?; diff --git a/examples/markdown/examples/emojis.rs b/examples/markdown/examples/emojis.rs index 107806b..c2c210d 100644 --- a/examples/markdown/examples/emojis.rs +++ b/examples/markdown/examples/emojis.rs @@ -19,9 +19,8 @@ use log::info; use winit::dpi::LogicalSize; use massive_geometry::{Camera, SizeI, Vector3}; -use massive_scene::PositionedShape; +use massive_scene::Visual; use massive_shell::{shell, ApplicationContext}; - use shared::{ application::{Application, UpdateResponse}, positioning, @@ -171,12 +170,12 @@ async fn emojis(mut ctx: ApplicationContext) -> Result<()> { let page_size = SizeI::new(page_width as _, page_height); let mut application = Application::default(); let mut current_matrix = application.matrix(page_size); - let matrix = director.cast(current_matrix); - let position = director.cast(matrix.clone().into()); + let matrix = director.stage(current_matrix); + let location = director.stage(matrix.clone().into()); - // Hold the positioned shapes in this context, otherwise they will disappear. - let _positioned_shape = director.cast(PositionedShape::new( - position.clone(), + // Hold the staged visual, otherwise it will disappear. + let _visual = director.stage(Visual::new( + location.clone(), glyph_runs .into_iter() .map(|run| run.into()) diff --git a/examples/markdown/examples/markdown.rs b/examples/markdown/examples/markdown.rs index 24114af..0457703 100644 --- a/examples/markdown/examples/markdown.rs +++ b/examples/markdown/examples/markdown.rs @@ -16,7 +16,7 @@ use inlyne::{ Element, }; use log::info; -use massive_scene::PositionedShape; +use massive_scene::Visual; use massive_shapes::GlyphRun; use massive_geometry::{Camera, SizeI, Vector3}; @@ -88,12 +88,12 @@ async fn application(mut ctx: ApplicationContext) -> Result<()> { let mut application = Application::default(); let mut current_matrix = application.matrix(page_size); - let matrix = director.cast(current_matrix); - let position = director.cast(matrix.clone().into()); + let matrix = director.stage(current_matrix); + let location = director.stage(matrix.clone().into()); - // Hold the positioned shapes in this context, otherwise they will disappear. - let _positioned_shape = director.cast(PositionedShape::new( - position.clone(), + // Hold the staged visual, otherwise it will disappear. + let _visual = director.stage(Visual::new( + location.clone(), glyph_runs .into_iter() .map(|run| run.into()) diff --git a/examples/syntax/examples/syntax.rs b/examples/syntax/examples/syntax.rs index 5059e0d..0a511bd 100644 --- a/examples/syntax/examples/syntax.rs +++ b/examples/syntax/examples/syntax.rs @@ -11,7 +11,7 @@ use syntect::{ use winit::dpi::LogicalSize; use massive_geometry::{Camera, Color}; -use massive_scene::PositionedShape; +use massive_scene::Visual; use massive_shapes::TextWeight; use massive_shell::{shell, ApplicationContext}; use shared::{ @@ -107,11 +107,11 @@ async fn syntax(mut ctx: ApplicationContext) -> Result<()> { let page_size = (1280, height as u64); let mut application = Application::default(); let mut current_matrix = application.matrix(page_size); - let matrix = director.cast(current_matrix); - let position = director.cast(matrix.clone().into()); + let matrix = director.stage(current_matrix); + let position = director.stage(matrix.clone().into()); - // Hold the positioned shapes in this context, otherwise they will disappear. - let _positioned_shape = director.cast(PositionedShape::new( + // Hold the staged visual, otherwise it will disappear. + let _visual = director.stage(Visual::new( position.clone(), glyph_runs .into_iter() diff --git a/renderer/src/scene/mod.rs b/renderer/src/scene/mod.rs index 2635d98..edfb447 100644 --- a/renderer/src/scene/mod.rs +++ b/renderer/src/scene/mod.rs @@ -5,7 +5,7 @@ use euclid::num::Zero; use dependency_resolver::{resolve, DependencyResolver}; use id_table::IdTable; use massive_geometry::Matrix4; -use massive_scene::{Change, Id, PositionRenderObj, PositionedRenderShape, SceneChange, Shape}; +use massive_scene::{Change, Id, LocationRenderObj, SceneChange, Shape, VisualRenderObj}; use versioning::{Computed, Version, Versioned}; mod dependency_resolver; @@ -18,8 +18,8 @@ pub struct Scene { current_version: Version, matrices: IdTable>>, - positions: IdTable>>, - shapes: IdTable>, + locations: IdTable>>, + visuals: IdTable>, caches: RefCell, } @@ -39,8 +39,8 @@ impl Scene { fn apply(&mut self, change: SceneChange, version: Version) { match change { SceneChange::Matrix(change) => self.matrices.apply_versioned(change, version), - SceneChange::Position(change) => self.positions.apply_versioned(change, version), - SceneChange::PositionedShape(change) => self.shapes.apply(change), + SceneChange::Location(change) => self.locations.apply_versioned(change, version), + SceneChange::Visual(change) => self.visuals.apply(change), } } @@ -50,16 +50,16 @@ impl Scene { ) -> impl Iterator + Clone)> { let mut map: HashMap> = HashMap::new(); - for positioned in self.shapes.iter_some() { - let position_id = positioned.position; - map.entry(position_id).or_default().push(&positioned.shapes); + for visual in self.visuals.iter_some() { + let visual_id = visual.position; + map.entry(visual_id).or_default().push(&visual.shapes); } // Update all matrices that are in use. { let mut caches = self.caches.borrow_mut(); - for position_id in map.keys() { - self.resolve_positioned_matrix(*position_id, &mut caches); + for visual_id in map.keys() { + self.resolve_visual_matrix(*visual_id, &mut caches); } } @@ -67,34 +67,33 @@ impl Scene { let caches = self.caches.borrow(); - map.into_iter().map(move |(position_id, shapes)| { + map.into_iter().map(move |(visual_id, shapes)| { // We can't return a reference to matrix, because this would also borrow `caches`. - let matrix = *caches.positions_matrix[position_id]; + let matrix = *caches.location_matrix[visual_id]; (matrix, shapes.into_iter().flatten()) }) } - /// Compute - if needed - the matrix of a position. + /// Compute - if needed - the matrix of a location. /// - /// When this function returns the matrix at `position_id` is up to date with the current + /// When this function returns the matrix at `location_id` is up to date with the current /// version and can be used for rendering. - /// - fn resolve_positioned_matrix(&self, position_id: Id, caches: &mut SceneCaches) { - resolve::(self.current_version, self, caches, position_id); + fn resolve_visual_matrix(&self, location_id: Id, caches: &mut SceneCaches) { + resolve::(self.current_version, self, caches, location_id); } } -/// The dependency resolver for finally positioned matrix. -struct PositionedMatrix; +/// The dependency resolver for final matrix of a [`Visual`]. +struct VisualMatrix; -impl DependencyResolver for PositionedMatrix { +impl DependencyResolver for VisualMatrix { type SharedStorage = Scene; type ComputedStorage = SceneCaches; - type Source = PositionRenderObj; + type Source = LocationRenderObj; type Computed = Matrix4; fn source(scene: &Scene, id: Id) -> &Versioned { - scene.positions.get_unwrapped(id) + scene.locations.get_unwrapped(id) } fn resolve_dependencies( @@ -108,7 +107,7 @@ impl DependencyResolver for PositionedMatrix { // Find out the max version of all the immediate and (indirect / computed) dependencies. // Get the _three_ versions of the elements this one is computed on. - // a) The self position's version. + // a) The self location's version. // b) The local matrix's version. // c) The computed matrix of the parent (representing all its dependencies). let max_deps_version = source @@ -119,7 +118,7 @@ impl DependencyResolver for PositionedMatrix { if let Some(parent_id) = parent_id { // Make sure the parent is up to date. resolve::(current_version, scene, caches, parent_id); - caches.positions_matrix[parent_id] + caches.location_matrix[parent_id] .max_deps_version .max(max_deps_version) } else { @@ -128,7 +127,7 @@ impl DependencyResolver for PositionedMatrix { } fn computed_mut(caches: &mut SceneCaches, id: Id) -> &mut Computed { - caches.positions_matrix.mut_or_default(id) + caches.location_matrix.mut_or_default(id) } fn compute(scene: &Scene, caches: &SceneCaches, source: &Self::Source) -> Self::Computed { @@ -136,7 +135,7 @@ impl DependencyResolver for PositionedMatrix { let local_matrix = &**scene.matrices.get_unwrapped(matrix_id); parent_id.map_or_else( || *local_matrix, - |parent_id| *caches.positions_matrix[parent_id] * local_matrix, + |parent_id| *caches.location_matrix[parent_id] * local_matrix, ) } } @@ -183,7 +182,7 @@ impl Default for Computed { Self { validated_at: 0, max_deps_version: 0, - // OO: is there a wait to use `::ZERO` / the trait `ConstZero` from num_traits for + // OO: is there a way to use `::ZERO` / the trait `ConstZero` from num_traits for // example? value: Matrix4::zero(), } @@ -192,6 +191,6 @@ impl Default for Computed { #[derive(Debug, Default)] struct SceneCaches { - // The result of a positioned computation. - positions_matrix: IdTable>, + // The result of a location matrix computation. + location_matrix: IdTable>, } diff --git a/scene/src/change_tracker.rs b/scene/src/change_tracker.rs index a7d05f5..0c5f8ff 100644 --- a/scene/src/change_tracker.rs +++ b/scene/src/change_tracker.rs @@ -2,7 +2,7 @@ use std::{any::TypeId, mem}; use massive_geometry as geometry; -use crate::{Id, Object, PositionRenderObj, PositionedRenderShape, PositionedShape}; +use crate::{Id, LocationRenderObj, Object, Visual, VisualRenderObj}; #[derive(Debug)] pub enum Change { @@ -14,8 +14,8 @@ pub enum Change { #[derive(Debug)] pub enum SceneChange { Matrix(Change), - Position(Change), - PositionedShape(Change), + Location(Change), + Visual(Change), } impl SceneChange { @@ -24,9 +24,7 @@ impl SceneChange { SceneChange::Matrix(Change::Delete(id)) => { Some((TypeId::of::(), *id)) } - SceneChange::PositionedShape(Change::Delete(id)) => { - Some((TypeId::of::(), *id)) - } + SceneChange::Visual(Change::Delete(id)) => Some((TypeId::of::(), *id)), _ => None, } } diff --git a/scene/src/lib.rs b/scene/src/lib.rs index be14755..6c2a6a4 100644 --- a/scene/src/lib.rs +++ b/scene/src/lib.rs @@ -63,7 +63,8 @@ impl Director { } } - pub fn cast(&mut self, value: T) -> Handle { + /// Put an object on the stage. + pub fn stage(&mut self, value: T) -> Handle { let ti = TypeId::of::(); let id = self.id_generators.entry(ti).or_default().allocate(); Handle::new(id, value, self.change_tracker.clone()) diff --git a/scene/src/objects.rs b/scene/src/objects.rs index e8be6c2..85da3b2 100644 --- a/scene/src/objects.rs +++ b/scene/src/objects.rs @@ -11,33 +11,36 @@ pub enum Shape { } #[derive(Debug)] -pub struct PositionedShape { - pub position: Handle, - /// DR: Clients should be able to use [`PositionedShape`] directly as a an abstract thing. Like - /// for example a line which contains multiple Shapes (runs, quads, etc.). Therefore - /// `Vec` and not just `Shape`. +pub struct Visual { + pub location: Handle, + /// DR: Clients should be able to use [`Visual`] directly as a an abstract thing. Like for + /// example a line which contains multiple Shapes (runs, quads, etc.). Therefore `Vec` + /// and not just `Shape`. /// - /// GI: Another idea is to add `Shape::Combined(Vec)`, but this makes extraction per + /// DI: Another idea is to add `Shape::Combined(Vec)`, but this makes extraction per /// renderer a bit more complex. This would also point to sharing Shapes as handles ... which /// could go in direction of layout? pub shapes: Vec, } #[derive(Debug)] -pub struct PositionedRenderShape { +pub struct VisualRenderObj { pub position: Id, pub shapes: Vec, } -impl Object for PositionedShape { +impl Object for Visual { // We keep the position handle here. - type Keep = Handle; + type Keep = Handle; // And upload the render shape. - type Change = PositionedRenderShape; + type Change = VisualRenderObj; fn split(self) -> (Self::Keep, Self::Change) { - let PositionedShape { position, shapes } = self; - let shape = PositionedRenderShape { + let Visual { + location: position, + shapes, + } = self; + let shape = VisualRenderObj { position: position.id(), shapes, }; @@ -45,14 +48,14 @@ impl Object for PositionedShape { } fn promote_change(change: Change) -> SceneChange { - SceneChange::PositionedShape(change) + SceneChange::Visual(change) } } -impl PositionedShape { - pub fn new(position: Handle, shapes: impl Into>) -> Self { +impl Visual { + pub fn new(location: Handle, shapes: impl Into>) -> Self { Self { - position, + location, shapes: shapes.into(), } } @@ -65,37 +68,37 @@ impl From for Vec { } #[derive(Debug, Clone)] -pub struct Position { - pub parent: Option>, +pub struct Location { + pub parent: Option>, pub matrix: Handle, } -impl From> for Position { +impl From> for Location { fn from(matrix: Handle) -> Self { - Position { + Location { parent: None, matrix, } } } -impl Object for Position { +impl Object for Location { type Keep = Self; - type Change = PositionRenderObj; + type Change = LocationRenderObj; fn promote_change(change: Change) -> SceneChange { - SceneChange::Position(change) + SceneChange::Location(change) } fn split(self) -> (Self::Keep, Self::Change) { let parent = self.parent.as_ref().map(|p| p.id()); let matrix = self.matrix.id(); - (self, PositionRenderObj { parent, matrix }) + (self, LocationRenderObj { parent, matrix }) } } #[derive(Debug)] -pub struct PositionRenderObj { +pub struct LocationRenderObj { pub parent: Option, pub matrix: Id, } @@ -117,7 +120,7 @@ impl Object for Matrix { pub mod legacy { use super::Handle; - use crate::{Director, Position, PositionedShape, SceneChange}; + use crate::{Director, Location, SceneChange, Visual}; use anyhow::Result; use massive_geometry::Matrix4; use massive_shapes::{GlyphRunShape, QuadsShape, Shape}; @@ -132,20 +135,17 @@ pub mod legacy { // The shapes must be alive { - // Keep the positioned shapes until the director ran through. - let _positioned = into_positioned_shapes(&mut director, shapes); + // Keep the visuals until the director ran through. + let _visuals = into_visuals(&mut director, shapes); director.action()?; } Ok(channel_rx.try_recv().unwrap_or_default()) } - pub fn into_positioned_shapes( - director: &mut Director, - shapes: Vec, - ) -> Vec> { - let mut position_handles: HashMap<*const Matrix4, Handle> = HashMap::new(); - let mut positioned_shapes = Vec::with_capacity(shapes.len()); + pub fn into_visuals(director: &mut Director, shapes: Vec) -> Vec> { + let mut location_handles: HashMap<*const Matrix4, Handle> = HashMap::new(); + let mut visuals = Vec::with_capacity(shapes.len()); for shape in shapes { let matrix = match &shape { @@ -153,25 +153,25 @@ pub mod legacy { Shape::Quads(QuadsShape { model_matrix, .. }) => model_matrix, }; - let position = position_handles.entry(Rc::as_ptr(matrix)).or_insert_with( - || -> Handle { - let matrix = director.cast(**matrix); - director.cast(matrix.into()) + let position = location_handles.entry(Rc::as_ptr(matrix)).or_insert_with( + || -> Handle { + let matrix = director.stage(**matrix); + director.stage(matrix.into()) }, ); - let positioned = match shape { + let visual = match shape { Shape::GlyphRun(GlyphRunShape { run, .. }) => { - PositionedShape::new(position.clone(), super::Shape::from(run)) + Visual::new(position.clone(), super::Shape::from(run)) } Shape::Quads(QuadsShape { quads, .. }) => { - PositionedShape::new(position.clone(), super::Shape::from(quads)) + Visual::new(position.clone(), super::Shape::from(quads)) } }; - positioned_shapes.push(director.cast(positioned)); + visuals.push(director.stage(visual)); } - positioned_shapes + visuals } }