From 40e7c6cd20af9e06192c151e0d7caf2da0ebaedf Mon Sep 17 00:00:00 2001 From: Nova Date: Wed, 8 Feb 2023 21:06:24 -0500 Subject: [PATCH] refactor: node aspect drawable --- src/nodes/drawable/lines.rs | 18 ++++++++++++------ src/nodes/drawable/mod.rs | 13 +++++++++++++ src/nodes/drawable/model.rs | 15 ++++++++------- src/nodes/drawable/text.rs | 19 +++++++++++-------- src/nodes/mod.rs | 12 +++--------- src/wayland/panel_item.rs | 14 ++++---------- 6 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/nodes/drawable/lines.rs b/src/nodes/drawable/lines.rs index 54a0003..7675ef2 100644 --- a/src/nodes/drawable/lines.rs +++ b/src/nodes/drawable/lines.rs @@ -5,7 +5,7 @@ use crate::{ Node, }, }; -use color_eyre::eyre::{ensure, Result}; +use color_eyre::eyre::{bail, ensure, Result}; use glam::Vec3A; use mint::Vector3; use parking_lot::Mutex; @@ -15,6 +15,8 @@ use stardust_xr::{schemas::flex::deserialize, values::Transform}; use std::{collections::VecDeque, sync::Arc}; use stereokit::{lifecycle::StereoKitDraw, lines::LinePoint as SkLinePoint, values::Color128}; +use super::Drawable; + static LINES_REGISTRY: Registry = Registry::new(); #[derive(Debug, Clone, Deserialize)] @@ -37,8 +39,8 @@ pub struct Lines { impl Lines { fn add_to(node: &Arc, points: Vec, cyclic: bool) -> Result> { ensure!( - node.model.get().is_none(), - "Internal: Node already has lines attached!" + node.drawable.get().is_none(), + "Internal: Node already has a drawable attached!" ); let lines = LINES_REGISTRY.add(Lines { @@ -47,7 +49,7 @@ impl Lines { }); node.add_local_signal("set_points", Lines::set_points_flex); node.add_local_signal("set_cyclic", Lines::set_cyclic_flex); - let _ = node.lines.set(lines.clone()); + let _ = node.drawable.set(Drawable::Lines(lines.clone())); Ok(lines) } @@ -84,17 +86,21 @@ impl Lines { } pub fn set_points_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { + let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")}; + let mut points: Vec = deserialize(data)?; for p in &mut points { p.color[0] = p.color[0].powf(2.2); p.color[1] = p.color[1].powf(2.2); p.color[2] = p.color[2].powf(2.2); } - node.lines.get().unwrap().data.lock().points = points; + lines.data.lock().points = points; Ok(()) } pub fn set_cyclic_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { - node.lines.get().unwrap().data.lock().cyclic = deserialize(data)?; + let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")}; + + lines.data.lock().cyclic = deserialize(data)?; Ok(()) } } diff --git a/src/nodes/drawable/mod.rs b/src/nodes/drawable/mod.rs index 56f08ff..2aa1a56 100644 --- a/src/nodes/drawable/mod.rs +++ b/src/nodes/drawable/mod.rs @@ -2,6 +2,12 @@ pub mod lines; pub mod model; pub mod text; +use self::{ + lines::Lines, + model::{Model, ModelNode}, + text::Text, +}; + use super::Node; use crate::core::client::Client; use color_eyre::eyre::Result; @@ -21,6 +27,13 @@ pub fn create_interface(client: &Arc) -> Result<()> { node.add_to_scenegraph().map(|_| ()) } +pub enum Drawable { + Lines(Arc), + Model(Arc), + ModelNode(Arc), + Text(Arc), +} + #[instrument(level = "debug", skip(sk))] pub fn draw(sk: &StereoKitDraw) { lines::draw_all(sk); diff --git a/src/nodes/drawable/model.rs b/src/nodes/drawable/model.rs index 45faa74..1ba5012 100644 --- a/src/nodes/drawable/model.rs +++ b/src/nodes/drawable/model.rs @@ -3,8 +3,9 @@ use crate::core::client::Client; use crate::core::destroy_queue; use crate::core::registry::Registry; use crate::core::resource::ResourceID; +use crate::nodes::drawable::Drawable; use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; -use color_eyre::eyre::{ensure, eyre, Result}; +use color_eyre::eyre::{bail, ensure, eyre, Result}; use mint::{ColumnMatrix4, Vector2, Vector3, Vector4}; use once_cell::sync::OnceCell; use parking_lot::Mutex; @@ -131,8 +132,8 @@ impl Model { "Internal: Node does not have a spatial attached!" ); ensure!( - node.model.get().is_none(), - "Internal: Node already has a model attached!" + node.drawable.get().is_none(), + "Internal: Node already has a drawable attached!" ); let model = Model { space: node.spatial.get().unwrap().clone(), @@ -158,7 +159,7 @@ impl Model { ) .ok_or_else(|| eyre!("Resource not found"))?, ); - let _ = node.model.set(model_arc.clone()); + let _ = node.drawable.set(Drawable::Model(model_arc.clone())); Ok(model_arc) } @@ -167,6 +168,8 @@ impl Model { _calling_client: Arc, data: &[u8], ) -> Result<()> { + let Some(Drawable::Model(model)) = node.drawable.get() else {bail!("Not a drawable??")}; + #[derive(Deserialize)] struct MaterialParameterInfo { idx: u32, @@ -175,9 +178,7 @@ impl Model { } let info: MaterialParameterInfo = deserialize(data)?; - node.model - .get() - .unwrap() + model .pending_material_parameters .lock() .insert((info.idx as i32, info.name), info.value); diff --git a/src/nodes/drawable/text.rs b/src/nodes/drawable/text.rs index 76fe8d1..8046d61 100644 --- a/src/nodes/drawable/text.rs +++ b/src/nodes/drawable/text.rs @@ -1,11 +1,12 @@ use crate::{ core::{client::Client, destroy_queue, registry::Registry, resource::ResourceID}, nodes::{ + drawable::Drawable, spatial::{find_spatial_parent, parse_transform, Spatial}, Node, }, }; -use color_eyre::eyre::{ensure, eyre, Result}; +use color_eyre::eyre::{bail, ensure, eyre, Result}; use glam::{vec3, Mat4, Vec2}; use mint::Vector2; use once_cell::sync::OnceCell; @@ -60,8 +61,8 @@ impl Text { "Internal: Node does not have a spatial attached!" ); ensure!( - node.model.get().is_none(), - "Internal: Node already has text attached!" + node.drawable.get().is_none(), + "Internal: Node already has a drawable attached!" ); let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?; @@ -87,7 +88,7 @@ impl Text { }); node.add_local_signal("set_character_height", Text::set_character_height_flex); node.add_local_signal("set_text", Text::set_text_flex); - let _ = node.text.set(text.clone()); + let _ = node.drawable.set(Drawable::Text(text.clone())); Ok(text) } @@ -145,14 +146,16 @@ impl Text { _calling_client: Arc, data: &[u8], ) -> Result<()> { - let height = flexbuffers::Reader::get_root(data)?.get_f64()? as f32; - node.text.get().unwrap().data.lock().character_height = height; + let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")}; + + text.data.lock().character_height = deserialize(data)?; Ok(()) } pub fn set_text_flex(node: &Node, _calling_client: Arc, data: &[u8]) -> Result<()> { - let text = flexbuffers::Reader::get_root(data)?.get_str()?.to_string(); - node.text.get().unwrap().data.lock().text = text; + let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")}; + + text.data.lock().text = deserialize(data)?; Ok(()) } } diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 93ab72a..8307d5f 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -34,9 +34,7 @@ use self::alias::Alias; use self::data::{PulseReceiver, PulseSender}; use self::audio::Sound; -use self::drawable::lines::Lines; -use self::drawable::model::Model; -use self::drawable::text::Text; +use self::drawable::Drawable; use self::fields::Field; use self::input::{InputHandler, InputMethod}; use self::items::{Item, ItemAcceptor, ItemUI}; @@ -69,9 +67,7 @@ pub struct Node { pub pulse_receiver: OnceCell>, // Drawable - pub lines: OnceCell>, - pub model: OnceCell>, - pub text: OnceCell>, + pub drawable: OnceCell, // Input pub input_method: OnceCell>, @@ -125,9 +121,7 @@ impl Node { zone: OnceCell::new(), pulse_sender: OnceCell::new(), pulse_receiver: OnceCell::new(), - lines: OnceCell::new(), - model: OnceCell::new(), - text: OnceCell::new(), + drawable: OnceCell::new(), input_method: OnceCell::new(), input_handler: OnceCell::new(), item: OnceCell::new(), diff --git a/src/wayland/panel_item.rs b/src/wayland/panel_item.rs index 6ea2cd7..567c37d 100644 --- a/src/wayland/panel_item.rs +++ b/src/wayland/panel_item.rs @@ -10,13 +10,14 @@ use crate::{ registry::Registry, }, nodes::{ + drawable::Drawable, items::{self, Item, ItemSpecialization, ItemType, TypeInfo}, spatial::Spatial, Node, }, wayland::seat::{KeyboardEvent, PointerEvent}, }; -use color_eyre::eyre::{eyre, Result}; +use color_eyre::eyre::{bail, eyre, Result}; use glam::Mat4; use lazy_static::lazy_static; use mint::Vector2; @@ -250,10 +251,7 @@ impl PanelItem { .scenegraph .get_node(info.model_path) .ok_or_else(|| eyre!("Model node not found"))?; - let model = model_node - .model - .get() - .ok_or_else(|| eyre!("Node is not a model"))?; + let Some(Drawable::Model(model)) = model_node.drawable.get() else {bail!("Node is not a model")}; debug!(?info, "Apply toplevel material"); if let ItemType::Panel(panel_item) = &node.item.get().unwrap().specialization { @@ -285,11 +283,7 @@ impl PanelItem { .scenegraph .get_node(info.model_path) .ok_or_else(|| eyre!("Model node not found"))?; - let model = model_node - .model - .get() - .ok_or_else(|| eyre!("Node is not a model"))?; - + let Some(Drawable::Model(model)) = model_node.drawable.get() else {bail!("Node is not a model")}; core_surface.apply_material(model.clone(), info.idx); Ok(())