refactor: node aspect drawable

This commit is contained in:
Nova
2023-02-08 21:06:24 -05:00
parent 45839ebf60
commit 3d57bed1c0
6 changed files with 51 additions and 40 deletions

View File

@@ -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<Lines> = Registry::new();
#[derive(Debug, Clone, Deserialize)]
@@ -37,8 +39,8 @@ pub struct Lines {
impl Lines {
fn add_to(node: &Arc<Node>, points: Vec<LinePointRaw>, cyclic: bool) -> Result<Arc<Lines>> {
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<Client>, data: &[u8]) -> Result<()> {
let Some(Drawable::Lines(lines)) = node.drawable.get() else {bail!("Not a drawable??")};
let mut points: Vec<LinePointRaw> = 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<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(())
}
}

View File

@@ -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<Client>) -> Result<()> {
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))]
pub fn draw(sk: &StereoKitDraw) {
lines::draw_all(sk);

View File

@@ -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<Client>,
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);

View File

@@ -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<Client>,
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<Client>, 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(())
}
}

View File

@@ -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<Arc<PulseReceiver>>,
// Drawable
pub lines: OnceCell<Arc<Lines>>,
pub model: OnceCell<Arc<Model>>,
pub text: OnceCell<Arc<Text>>,
pub drawable: OnceCell<Drawable>,
// Input
pub input_method: OnceCell<Arc<InputMethod>>,
@@ -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(),

View File

@@ -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(())