Skip to content

Commit

Permalink
Add scene visibility (#17)
Browse files Browse the repository at this point in the history
* feat: add scene visibility

* Minor changes
  • Loading branch information
nixon-voxell authored Sep 23, 2024
1 parent 932731d commit cadf877
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ repository.workspace = true
readme.workspace = true

[dependencies]
# Local dependencies
typst_element = { version = "0.1.0", path = "crates/typst_element" }
typst_vello = { version = "0.1.0", path = "crates/typst_vello" }
velyst_macros = { version = "0.1.0", path = "crates/velyst_macros" }

bevy = { workspace = true }
bevy_vello = { workspace = true }
typst = { workspace = true }
Expand All @@ -49,7 +52,6 @@ comemo = { workspace = true }
chrono = { version = "0.4.24", default-features = false, features = ["clock", "std"] }
ecow = { workspace = true }
thiserror = { workspace = true }
velyst_macros = { version = "0.1.0", path = "crates/velyst_macros" }

[features]
default = ["embed-fonts"]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod prelude {
pub use crate::asset::TypstAsset;
pub use crate::renderer::{
TypstAssetHandle, TypstContent, TypstContext, TypstFunc, TypstLabel, TypstPath,
VelystCommandExt, VelystScene, VelystSet,
VelystAppExt, VelystScene, VelystSet,
};
pub use crate::world::{TypstWorld, TypstWorldRef};
pub use velyst_macros::{TypstFunc, TypstPath};
Expand Down
62 changes: 38 additions & 24 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ impl Plugin for VelystRendererPlugin {
app.configure_sets(
Update,
(
VelystSet::Layout,
VelystSet::Render,
VelystSet::AssetLoading,
VelystSet::Compile,
VelystSet::Layout,
VelystSet::Render,
)
.chain(),
);
Expand All @@ -25,17 +25,17 @@ impl Plugin for VelystRendererPlugin {
/// Velyst rendering pipeline.
#[derive(SystemSet, Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum VelystSet {
/// Layout [`Content`] into a [`TypstScene`] which gets stored inside [`VelystScene`].
Layout,
/// Render [`TypstScene`] into a [`VelloScene`].
Render,
/// Loading and reloading of [`TypstAsset`].
AssetLoading,
/// Compile [`TypstFunc`] into a [`TypstContent`].
Compile,
/// Layout [`Content`] into a [`TypstScene`] which gets stored inside [`VelystScene`].
Layout,
/// Render [`TypstScene`] into a [`VelloScene`].
Render,
}

pub trait VelystCommandExt {
pub trait VelystAppExt {
/// Load [`TypstAsset`] using [`TypstPath::path()`] and detect changes made towards the asset.
fn register_typst_asset<P: TypstPath>(&mut self) -> &mut Self;

Expand All @@ -46,7 +46,7 @@ pub trait VelystCommandExt {
fn render_typst_func<F: TypstFunc>(&mut self) -> &mut Self;
}

impl VelystCommandExt for App {
impl VelystAppExt for App {
fn register_typst_asset<P: TypstPath>(&mut self) -> &mut Self {
self.add_systems(
PreStartup,
Expand Down Expand Up @@ -147,18 +147,23 @@ fn layout_typst_content<F: TypstFunc>(
/// System implementation for rendering [`VelystScene`] into [`VelloScene`].
fn render_velyst_scene<F: TypstFunc>(
mut commands: Commands,
mut q_scenes: Query<(&mut VelloScene, &mut Style)>,
mut q_scenes: Query<(&mut VelloScene, &mut Style, &mut Visibility)>,
mut scene: ResMut<VelystScene<F>>,
func: Res<F>,
) {
let scene = scene.bypass_change_detection();

if let Some((mut vello_scene, mut style)) = scene.entity.and_then(|e| q_scenes.get_mut(e).ok())
if let Some((mut vello_scene, mut style, mut viz)) =
scene.entity.and_then(|e| q_scenes.get_mut(e).ok())
{
// Scene
**vello_scene = scene.render();
let size = scene.size();
// Style
style.width = Val::Px(size.x as f32);
style.height = Val::Px(size.y as f32);
// Visibility
*viz = scene.visibility;
} else {
scene.entity = Some(
commands
Expand All @@ -167,7 +172,13 @@ fn render_velyst_scene<F: TypstFunc>(
coordinate_space: CoordinateSpace::ScreenSpace,
..default()
})
.insert((NodeBundle::default(), func.render_layers()))
.insert((
NodeBundle {
visibility: scene.visibility,
..default()
},
func.render_layers(),
))
.id(),
);
}
Expand Down Expand Up @@ -329,10 +340,12 @@ impl<F: TypstFunc> Default for TypstContent<F> {
/// Storage of a [`TypstScene`] in a resource as well as
/// caching the render and interaction entities.
#[derive(Resource, Deref, DerefMut)]
pub struct VelystScene<F> {
pub struct VelystScene<F: TypstFunc> {
#[deref]
/// Underlying [`TypstScene`] data.
scene: TypstScene,
/// Visibility of the scene.
pub visibility: Visibility,
/// Entity that contains [`VelloSceneBundle`] for rendering the typst scene.
entity: Option<Entity>,
/// Cached entities mapped by [`TypLabel`].
Expand All @@ -343,18 +356,7 @@ pub struct VelystScene<F> {
phantom: PhantomData<F>,
}

impl<F> Default for VelystScene<F> {
fn default() -> Self {
Self {
scene: default(),
entity: None,
cached_entities: default(),
phantom: PhantomData,
}
}
}

impl<F> VelystScene<F> {
impl<F: TypstFunc> VelystScene<F> {
pub fn new(scene: TypstScene) -> Self {
Self { scene, ..default() }
}
Expand All @@ -369,6 +371,18 @@ impl<F> VelystScene<F> {
}
}

impl<F: TypstFunc> Default for VelystScene<F> {
fn default() -> Self {
Self {
scene: default(),
visibility: Visibility::Inherited,
entity: None,
cached_entities: default(),
phantom: PhantomData,
}
}
}

#[derive(Component, Deref, DerefMut)]
pub struct TypstLabel(TypLabel);

Expand Down

0 comments on commit cadf877

Please sign in to comment.