Struct Scene

Source
pub struct Scene { /* private fields */ }

Implementations§

Source§

impl Scene

Source

pub fn new() -> Scene

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn node(&self, id: NodeId) -> Result<&Node, SceneError>

Source

pub fn nodes(&self) -> impl Iterator<Item = (NodeId, &Node)>

Source

pub fn resolve_path( &self, parent: Option<NodeId>, components: &[&str], ) -> Result<NodeId, ScenePathError>

Resolves literal name components one hierarchy level at a time. None starts among scene roots; Some(parent) starts among that node’s direct children. An empty relative path selects its live parent; an empty absolute path returns ScenePathError::EmptyPath.

Names are case-sensitive UTF-8, with no normalization, splitting, escaping, or special ./.. semantics. For example, ["a/b", ""] selects the empty-named child of root a/b. This is the scene hierarchy, not the authoring system’s expanded identifier namespace.

Duplicate roots/siblings are allowed by mutations but always produce ScenePathError::Ambiguous at the first ambiguous component. A suffix never disambiguates a prefix. Missing names return NotFound, including the component index and its resolved parent. The supplied parent is validated first, followed by the entire path’s bounds (100,000 components, 4,096 bytes per name), before any names are resolved.

No allocation or recursion. Uses existing child links and Self::nodes for roots; work is bounded by scene slots/visited sibling groups and input names. The result is a current NodeId, not a persistent path binding: removing it and recreating its names can make the same path select a new identity. Stale parent handles are rejected even for empty paths.

use ferravex_play::scene::{Scene, ScenePathError, Transform};
let mut scene = Scene::new();
let actor = scene.create("actor", Transform::default(), None)?;
let socket = scene.create("hand/socket", Transform::default(), Some(actor))?;
assert_eq!(scene.resolve_path(None, &["actor", "hand/socket"])?, socket);
assert_eq!(scene.resolve_path(Some(actor), &["hand/socket"])?, socket);
scene.remove_subtree(actor)?;
assert_eq!(scene.resolve_path(Some(actor), &[]),
    Err(ScenePathError::InvalidNode(actor)));
Source

pub fn path_from( &self, parent: Option<NodeId>, id: NodeId, ) -> Result<Vec<&str>, ScenePathError>

Inspects id as an unambiguous path from parent, excluding the parent’s own name. None includes the root’s name; Some(id) returns an empty path. The parent is checked first, then id. Both must be live, and id must lie in the parent’s subtree, or ScenePathError::NotDescendant is returned.

Walks existing parent links iteratively, then uses Self::resolve_path to check the same duplicate-name policy. On success, resolving the returned components from the same parent returns exactly id. Duplicate ancestors above or equal to an explicit parent do not affect relative paths.

Allocates only a vector of borrowed names, bounded by scene depth (at most 100,000 entries); names are not copied. The borrow prevents scene edits while the path is in use. Owned copies of paths can change meaning after edits; keep the NodeId to follow the same live node across reparenting. Handles are scene-local and must not be reused after decoding a new scene.

use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let actor = scene.create("actor", Transform::default(), None)?;
let socket = scene.create("socket", Transform::default(), Some(actor))?;
let path = scene.path_from(None, socket)?;
assert_eq!(path, ["actor", "socket"]);
assert_eq!(scene.resolve_path(None, &path)?, socket);
assert_eq!(scene.path_from(Some(actor), socket)?, ["socket"]);
scene.set_parents(&[(actor, Some(socket)), (socket, None)])?;
assert_eq!(scene.path_from(None, actor)?, ["socket", "actor"]);
Source

pub fn subtree( &self, root: NodeId, ) -> Result<impl Iterator<Item = (NodeId, &Node)>, SceneError>

Visits a root and its descendants in depth-first, parent-before-child order, respecting each node’s child order. A stale root is rejected before traversal starts. The iterator borrows the scene, preventing edits while it is in use, and uses a heap stack rather than recursion.

Collect IDs first when applying edits to the visited nodes:

use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let actor = scene.create("actor", Transform::default(), None)?;
let socket = scene.create("socket", Transform::default(), Some(actor))?;
let ids: Vec<_> = scene.subtree(actor)?.map(|(id, _)| id).collect();
assert_eq!(ids, [actor, socket]);
for id in ids {
    scene.set_local(id, Transform::default())?;
}
Source

pub fn create( &mut self, name: impl Into<String>, local: Transform, parent: Option<NodeId>, ) -> Result<NodeId, SceneError>

Source

pub fn set_local( &mut self, id: NodeId, local: Transform, ) -> Result<(), SceneError>

Source

pub fn set_parent( &mut self, id: NodeId, parent: Option<NodeId>, ) -> Result<(), SceneError>

Reparent while retaining local TRS. Cycles and invalid handles are rejected before any links change. World-space position may change.

Source

pub fn set_parents( &mut self, changes: &[(NodeId, Option<NodeId>)], ) -> Result<(), SceneError>

Applies reparenting edits atomically, retaining every local transform. Validates the final hierarchy, so edits can succeed even when applying them individually in the given order would temporarily create a cycle. Any stale handle or final cycle leaves the entire scene unchanged.

Repeated node IDs use the last requested parent; all supplied handles are checked, including overwritten requests. Moved children are appended in order of their first appearance. Unchanged links retain their order. Work and scratch space are linear in scene slots plus requested edits; traversal does not recurse, even for deeply nested hierarchies.

use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let carrier = scene.create("carrier", Transform::default(), None)?;
let payload = scene.create("payload", Transform::default(), Some(carrier))?;
// Make payload the root and attach carrier beneath it in one edit.
scene.set_parents(&[(carrier, Some(payload)), (payload, None)])?;
assert_eq!(scene.node(carrier)?.parent(), Some(payload));
assert_eq!(scene.node(payload)?.parent(), None);
Source

pub fn world_matrix(&self, id: NodeId) -> Result<[f32; 16], SceneError>

Source

pub fn remove_subtree(&mut self, root: NodeId) -> Result<usize, SceneError>

Source

pub fn prefab(&self, root: NodeId) -> Result<Prefab, SceneError>

Captures local transforms and hierarchy; external parent links are omitted.

Source

pub fn instantiate( &mut self, prefab: &Prefab, parent: Option<NodeId>, ) -> Result<NodeId, SceneError>

Inserts an independent copy under parent, retaining prefab-local TRS. All entries, links and capacity are checked before insertion; any returned error leaves existing nodes, child order and handle allocation unchanged.

use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let source = scene.create("enemy", Transform::default(), None)?;
let prefab = scene.prefab(source)?;
let copy = scene.instantiate(&prefab, None)?;
assert_ne!(source, copy);
assert_eq!(scene.node(copy)?.name(), "enemy");
Source

pub fn clone_subtree( &mut self, root: NodeId, parent: Option<NodeId>, ) -> Result<NodeId, SceneError>

Source

pub fn encode(&self) -> Result<Vec<u8>, SceneError>

GSC1 format: magic, u16 version, u32 count; each node has u32 parent (MAX=root), u16 UTF-8 name length, name bytes, and 10 little-endian f32 TRS values. Handles are remapped; serialized data never trusts slot generations.

Source

pub fn decode(bytes: &[u8]) -> Result<Scene, SceneError>

Trait Implementations§

Source§

impl Default for Scene

Source§

fn default() -> Scene

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Scene

§

impl RefUnwindSafe for Scene

§

impl Send for Scene

§

impl Sync for Scene

§

impl Unpin for Scene

§

impl UnwindSafe for Scene

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.