refactor: node aspect drawable

This commit is contained in:
Nova
2023-02-08 21:06:24 -05:00
parent 4620e0ca09
commit 40e7c6cd20
6 changed files with 51 additions and 40 deletions

View File

@@ -5,7 +5,7 @@ use crate::{
Node, Node,
}, },
}; };
use color_eyre::eyre::{ensure, Result}; use color_eyre::eyre::{bail, ensure, Result};
use glam::Vec3A; use glam::Vec3A;
use mint::Vector3; use mint::Vector3;
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -15,6 +15,8 @@ use stardust_xr::{schemas::flex::deserialize, values::Transform};
use std::{collections::VecDeque, sync::Arc}; use std::{collections::VecDeque, sync::Arc};
use stereokit::{lifecycle::StereoKitDraw, lines::LinePoint as SkLinePoint, values::Color128}; use stereokit::{lifecycle::StereoKitDraw, lines::LinePoint as SkLinePoint, values::Color128};
use super::Drawable;
static LINES_REGISTRY: Registry<Lines> = Registry::new(); static LINES_REGISTRY: Registry<Lines> = Registry::new();
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@@ -37,8 +39,8 @@ pub struct Lines {
impl Lines { impl Lines {
fn add_to(node: &Arc<Node>, points: Vec<LinePointRaw>, cyclic: bool) -> Result<Arc<Lines>> { fn add_to(node: &Arc<Node>, points: Vec<LinePointRaw>, cyclic: bool) -> Result<Arc<Lines>> {
ensure!( ensure!(
node.model.get().is_none(), node.drawable.get().is_none(),
"Internal: Node already has lines attached!" "Internal: Node already has a drawable attached!"
); );
let lines = LINES_REGISTRY.add(Lines { 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_points", Lines::set_points_flex);
node.add_local_signal("set_cyclic", Lines::set_cyclic_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) Ok(lines)
} }
@@ -84,17 +86,21 @@ impl Lines {
} }
pub fn set_points_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> { pub fn set_points_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
let mut points: Vec<LinePointRaw> = deserialize(data)?; let mut points: Vec<LinePointRaw> = deserialize(data)?;
for p in &mut points { for p in &mut points {
p.color[0] = p.color[0].powf(2.2); p.color[0] = p.color[0].powf(2.2);
p.color[1] = p.color[1].powf(2.2); p.color[1] = p.color[1].powf(2.2);
p.color[2] = p.color[2].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(()) Ok(())
} }
pub fn set_cyclic_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> { pub fn set_cyclic_flex(node: &Node, _calling_client: Arc<Client>, 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(()) Ok(())
} }
} }

View File

@@ -2,6 +2,12 @@ pub mod lines;
pub mod model; pub mod model;
pub mod text; pub mod text;
use self::{
lines::Lines,
model::{Model, ModelNode},
text::Text,
};
use super::Node; use super::Node;
use crate::core::client::Client; use crate::core::client::Client;
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
@@ -21,6 +27,13 @@ pub fn create_interface(client: &Arc<Client>) -> Result<()> {
node.add_to_scenegraph().map(|_| ()) node.add_to_scenegraph().map(|_| ())
} }
pub enum Drawable {
Lines(Arc<Lines>),
Model(Arc<Model>),
ModelNode(Arc<ModelNode>),
Text(Arc<Text>),
}
#[instrument(level = "debug", skip(sk))] #[instrument(level = "debug", skip(sk))]
pub fn draw(sk: &StereoKitDraw) { pub fn draw(sk: &StereoKitDraw) {
lines::draw_all(sk); lines::draw_all(sk);

View File

@@ -3,8 +3,9 @@ use crate::core::client::Client;
use crate::core::destroy_queue; use crate::core::destroy_queue;
use crate::core::registry::Registry; use crate::core::registry::Registry;
use crate::core::resource::ResourceID; use crate::core::resource::ResourceID;
use crate::nodes::drawable::Drawable;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial}; 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 mint::{ColumnMatrix4, Vector2, Vector3, Vector4};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -131,8 +132,8 @@ impl Model {
"Internal: Node does not have a spatial attached!" "Internal: Node does not have a spatial attached!"
); );
ensure!( ensure!(
node.model.get().is_none(), node.drawable.get().is_none(),
"Internal: Node already has a model attached!" "Internal: Node already has a drawable attached!"
); );
let model = Model { let model = Model {
space: node.spatial.get().unwrap().clone(), space: node.spatial.get().unwrap().clone(),
@@ -158,7 +159,7 @@ impl Model {
) )
.ok_or_else(|| eyre!("Resource not found"))?, .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) Ok(model_arc)
} }
@@ -167,6 +168,8 @@ impl Model {
_calling_client: Arc<Client>, _calling_client: Arc<Client>,
data: &[u8], data: &[u8],
) -> Result<()> { ) -> Result<()> {
let Some(Drawable::Model(model)) = node.drawable.get() else {bail!("Not a drawable??")};
#[derive(Deserialize)] #[derive(Deserialize)]
struct MaterialParameterInfo { struct MaterialParameterInfo {
idx: u32, idx: u32,
@@ -175,9 +178,7 @@ impl Model {
} }
let info: MaterialParameterInfo = deserialize(data)?; let info: MaterialParameterInfo = deserialize(data)?;
node.model model
.get()
.unwrap()
.pending_material_parameters .pending_material_parameters
.lock() .lock()
.insert((info.idx as i32, info.name), info.value); .insert((info.idx as i32, info.name), info.value);

View File

@@ -1,11 +1,12 @@
use crate::{ use crate::{
core::{client::Client, destroy_queue, registry::Registry, resource::ResourceID}, core::{client::Client, destroy_queue, registry::Registry, resource::ResourceID},
nodes::{ nodes::{
drawable::Drawable,
spatial::{find_spatial_parent, parse_transform, Spatial}, spatial::{find_spatial_parent, parse_transform, Spatial},
Node, Node,
}, },
}; };
use color_eyre::eyre::{ensure, eyre, Result}; use color_eyre::eyre::{bail, ensure, eyre, Result};
use glam::{vec3, Mat4, Vec2}; use glam::{vec3, Mat4, Vec2};
use mint::Vector2; use mint::Vector2;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
@@ -60,8 +61,8 @@ impl Text {
"Internal: Node does not have a spatial attached!" "Internal: Node does not have a spatial attached!"
); );
ensure!( ensure!(
node.model.get().is_none(), node.drawable.get().is_none(),
"Internal: Node already has text attached!" "Internal: Node already has a drawable attached!"
); );
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?; 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_character_height", Text::set_character_height_flex);
node.add_local_signal("set_text", Text::set_text_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) Ok(text)
} }
@@ -145,14 +146,16 @@ impl Text {
_calling_client: Arc<Client>, _calling_client: Arc<Client>,
data: &[u8], data: &[u8],
) -> Result<()> { ) -> Result<()> {
let height = flexbuffers::Reader::get_root(data)?.get_f64()? as f32; let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")};
node.text.get().unwrap().data.lock().character_height = height;
text.data.lock().character_height = deserialize(data)?;
Ok(()) Ok(())
} }
pub fn set_text_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> { pub fn set_text_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
let text = flexbuffers::Reader::get_root(data)?.get_str()?.to_string(); let Some(Drawable::Text(text)) = node.drawable.get() else {bail!("Not a drawable??")};
node.text.get().unwrap().data.lock().text = text;
text.data.lock().text = deserialize(data)?;
Ok(()) Ok(())
} }
} }

View File

@@ -34,9 +34,7 @@ use self::alias::Alias;
use self::data::{PulseReceiver, PulseSender}; use self::data::{PulseReceiver, PulseSender};
use self::audio::Sound; use self::audio::Sound;
use self::drawable::lines::Lines; use self::drawable::Drawable;
use self::drawable::model::Model;
use self::drawable::text::Text;
use self::fields::Field; use self::fields::Field;
use self::input::{InputHandler, InputMethod}; use self::input::{InputHandler, InputMethod};
use self::items::{Item, ItemAcceptor, ItemUI}; use self::items::{Item, ItemAcceptor, ItemUI};
@@ -69,9 +67,7 @@ pub struct Node {
pub pulse_receiver: OnceCell<Arc<PulseReceiver>>, pub pulse_receiver: OnceCell<Arc<PulseReceiver>>,
// Drawable // Drawable
pub lines: OnceCell<Arc<Lines>>, pub drawable: OnceCell<Drawable>,
pub model: OnceCell<Arc<Model>>,
pub text: OnceCell<Arc<Text>>,
// Input // Input
pub input_method: OnceCell<Arc<InputMethod>>, pub input_method: OnceCell<Arc<InputMethod>>,
@@ -125,9 +121,7 @@ impl Node {
zone: OnceCell::new(), zone: OnceCell::new(),
pulse_sender: OnceCell::new(), pulse_sender: OnceCell::new(),
pulse_receiver: OnceCell::new(), pulse_receiver: OnceCell::new(),
lines: OnceCell::new(), drawable: OnceCell::new(),
model: OnceCell::new(),
text: OnceCell::new(),
input_method: OnceCell::new(), input_method: OnceCell::new(),
input_handler: OnceCell::new(), input_handler: OnceCell::new(),
item: OnceCell::new(), item: OnceCell::new(),

View File

@@ -10,13 +10,14 @@ use crate::{
registry::Registry, registry::Registry,
}, },
nodes::{ nodes::{
drawable::Drawable,
items::{self, Item, ItemSpecialization, ItemType, TypeInfo}, items::{self, Item, ItemSpecialization, ItemType, TypeInfo},
spatial::Spatial, spatial::Spatial,
Node, Node,
}, },
wayland::seat::{KeyboardEvent, PointerEvent}, wayland::seat::{KeyboardEvent, PointerEvent},
}; };
use color_eyre::eyre::{eyre, Result}; use color_eyre::eyre::{bail, eyre, Result};
use glam::Mat4; use glam::Mat4;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use mint::Vector2; use mint::Vector2;
@@ -250,10 +251,7 @@ impl PanelItem {
.scenegraph .scenegraph
.get_node(info.model_path) .get_node(info.model_path)
.ok_or_else(|| eyre!("Model node not found"))?; .ok_or_else(|| eyre!("Model node not found"))?;
let model = model_node let Some(Drawable::Model(model)) = model_node.drawable.get() else {bail!("Node is not a model")};
.model
.get()
.ok_or_else(|| eyre!("Node is not a model"))?;
debug!(?info, "Apply toplevel material"); debug!(?info, "Apply toplevel material");
if let ItemType::Panel(panel_item) = &node.item.get().unwrap().specialization { if let ItemType::Panel(panel_item) = &node.item.get().unwrap().specialization {
@@ -285,11 +283,7 @@ impl PanelItem {
.scenegraph .scenegraph
.get_node(info.model_path) .get_node(info.model_path)
.ok_or_else(|| eyre!("Model node not found"))?; .ok_or_else(|| eyre!("Model node not found"))?;
let model = model_node let Some(Drawable::Model(model)) = model_node.drawable.get() else {bail!("Node is not a model")};
.model
.get()
.ok_or_else(|| eyre!("Node is not a model"))?;
core_surface.apply_material(model.clone(), info.idx); core_surface.apply_material(model.clone(), info.idx);
Ok(()) Ok(())