Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Term changes: cast -> stage, Position -> Location, positioned -> Visual #30

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/code/examples/code-viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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::<Vec<_>>(),
));

Expand Down
10 changes: 5 additions & 5 deletions examples/code/examples/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 13 additions & 14 deletions examples/logs/examples/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -98,23 +98,22 @@ async fn logs(mut receiver: UnboundedReceiver<Vec<u8>>, 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(),
});

let mut y = 0.;

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! {
Expand All @@ -126,20 +125,20 @@ async fn logs(mut receiver: UnboundedReceiver<Vec<u8>>, 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::<Vec<_>>()));

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()?;
Expand Down
13 changes: 6 additions & 7 deletions examples/markdown/examples/emojis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions examples/markdown/examples/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions examples/syntax/examples/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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()
Expand Down
57 changes: 28 additions & 29 deletions renderer/src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,8 +18,8 @@ pub struct Scene {
current_version: Version,

matrices: IdTable<Option<Versioned<Matrix4>>>,
positions: IdTable<Option<Versioned<PositionRenderObj>>>,
shapes: IdTable<Option<PositionedRenderShape>>,
locations: IdTable<Option<Versioned<LocationRenderObj>>>,
visuals: IdTable<Option<VisualRenderObj>>,

caches: RefCell<SceneCaches>,
}
Expand All @@ -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),
}
}

Expand All @@ -50,51 +50,50 @@ impl Scene {
) -> impl Iterator<Item = (Matrix4, impl Iterator<Item = &Shape> + Clone)> {
let mut map: HashMap<Id, Vec<&[Shape]>> = 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);
}
}

// Create the group iterator.

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::<PositionedMatrix>(self.current_version, self, caches, position_id);
fn resolve_visual_matrix(&self, location_id: Id, caches: &mut SceneCaches) {
resolve::<VisualMatrix>(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<Self::Source> {
scene.positions.get_unwrapped(id)
scene.locations.get_unwrapped(id)
}

fn resolve_dependencies(
Expand All @@ -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
Expand All @@ -119,7 +118,7 @@ impl DependencyResolver for PositionedMatrix {
if let Some(parent_id) = parent_id {
// Make sure the parent is up to date.
resolve::<Self>(current_version, scene, caches, parent_id);
caches.positions_matrix[parent_id]
caches.location_matrix[parent_id]
.max_deps_version
.max(max_deps_version)
} else {
Expand All @@ -128,15 +127,15 @@ impl DependencyResolver for PositionedMatrix {
}

fn computed_mut(caches: &mut SceneCaches, id: Id) -> &mut Computed<Self::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 {
let (parent_id, matrix_id) = (source.parent, source.matrix);
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,
)
}
}
Expand Down Expand Up @@ -183,7 +182,7 @@ impl Default for Computed<Matrix4> {
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(),
}
Expand All @@ -192,6 +191,6 @@ impl Default for Computed<Matrix4> {

#[derive(Debug, Default)]
struct SceneCaches {
// The result of a positioned computation.
positions_matrix: IdTable<Computed<Matrix4>>,
// The result of a location matrix computation.
location_matrix: IdTable<Computed<Matrix4>>,
}
10 changes: 4 additions & 6 deletions scene/src/change_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand All @@ -14,8 +14,8 @@ pub enum Change<T> {
#[derive(Debug)]
pub enum SceneChange {
Matrix(Change<geometry::Matrix4>),
Position(Change<PositionRenderObj>),
PositionedShape(Change<PositionedRenderShape>),
Location(Change<LocationRenderObj>),
Visual(Change<VisualRenderObj>),
}

impl SceneChange {
Expand All @@ -24,9 +24,7 @@ impl SceneChange {
SceneChange::Matrix(Change::Delete(id)) => {
Some((TypeId::of::<geometry::Matrix4>(), *id))
}
SceneChange::PositionedShape(Change::Delete(id)) => {
Some((TypeId::of::<PositionedShape>(), *id))
}
SceneChange::Visual(Change::Delete(id)) => Some((TypeId::of::<Visual>(), *id)),
_ => None,
}
}
Expand Down
Loading