feat: disabled/enabled

This commit is contained in:
Nova
2023-02-16 00:30:25 -05:00
parent e5acb3013f
commit 969e4de882
6 changed files with 48 additions and 18 deletions

View File

@@ -9,6 +9,7 @@ use color_eyre::eyre::{bail, ensure, Result};
use glam::Vec3A;
use mint::Vector3;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use prisma::{Flatten, Lerp, Rgba};
use serde::Deserialize;
use stardust_xr::{schemas::flex::deserialize, values::Transform};
@@ -33,6 +34,7 @@ struct LineData {
}
pub struct Lines {
enabled: Arc<AtomicBool>,
space: Arc<Spatial>,
data: Mutex<LineData>,
}
@@ -44,6 +46,7 @@ impl Lines {
);
let lines = LINES_REGISTRY.add(Lines {
enabled: node.enabled.clone(),
space: node.get_aspect("Lines", "spatial", |n| &n.spatial)?.clone(),
data: Mutex::new(LineData { points, cyclic }),
});
@@ -112,7 +115,9 @@ impl Drop for Lines {
pub fn draw_all(draw_ctx: &StereoKitDraw) {
for lines in LINES_REGISTRY.get_valid_contents() {
lines.draw(draw_ctx);
if lines.enabled.load(Ordering::Relaxed) {
lines.draw(draw_ctx);
}
}
}

View File

@@ -9,6 +9,7 @@ use color_eyre::eyre::{bail, ensure, eyre, Result};
use mint::{ColumnMatrix4, Vector2, Vector3, Vector4};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use rustc_hash::FxHashMap;
use send_wrapper::SendWrapper;
use serde::Deserialize;
@@ -117,6 +118,7 @@ impl MaterialParameter {
}
pub struct Model {
enabled: Arc<AtomicBool>,
space: Arc<Spatial>,
resource_id: ResourceID,
pending_model_path: OnceCell<PathBuf>,
@@ -136,6 +138,7 @@ impl Model {
"Internal: Node already has a drawable attached!"
);
let model = Model {
enabled: node.enabled.clone(),
space: node.spatial.get().unwrap().clone(),
resource_id,
pending_model_path: OnceCell::new(),
@@ -244,7 +247,9 @@ impl Drop for Model {
pub fn draw_all(sk: &StereoKitDraw) {
for model in MODEL_REGISTRY.get_valid_contents() {
model.draw(sk);
if model.enabled.load(Ordering::Relaxed) {
model.draw(sk);
}
}
}

View File

@@ -11,6 +11,7 @@ use glam::{vec3, Mat4, Vec2};
use mint::Vector2;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use prisma::{Flatten, Rgba};
use send_wrapper::SendWrapper;
use serde::Deserialize;
@@ -37,6 +38,7 @@ struct TextData {
}
pub struct Text {
enabled: Arc<AtomicBool>,
space: Arc<Spatial>,
font_path: Option<PathBuf>,
style: OnceCell<SendWrapper<TextStyle>>,
@@ -67,6 +69,7 @@ impl Text {
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
let text = TEXT_REGISTRY.add(Text {
enabled: node.enabled.clone(),
space: node.spatial.get().unwrap().clone(),
font_path: font_resource_id.and_then(|res| {
res.get_file(
@@ -170,7 +173,9 @@ impl Drop for Text {
pub fn draw_all(sk: &StereoKitDraw) {
for text in TEXT_REGISTRY.get_valid_contents() {
text.draw(sk);
if text.enabled.load(Ordering::Relaxed) {
text.draw(sk);
}
}
}

View File

@@ -18,6 +18,7 @@ use color_eyre::eyre::{ensure, Result};
use glam::Mat4;
use nanoid::nanoid;
use parking_lot::Mutex;
use portable_atomic::AtomicBool;
use serde::Deserialize;
use stardust_xr::schemas::flat::{Datamap, InputDataType};
use stardust_xr::schemas::{flat::InputData, flex::deserialize};
@@ -166,6 +167,7 @@ impl DistanceLink {
}
pub struct InputHandler {
enabled: Arc<AtomicBool>,
node: Weak<Node>,
spatial: Arc<Spatial>,
pub field: Weak<Field>,
@@ -178,6 +180,7 @@ impl InputHandler {
);
let handler = InputHandler {
enabled: node.enabled.clone(),
node: Arc::downgrade(node),
spatial: node.spatial.get().unwrap().clone(),
field: Arc::downgrade(field),
@@ -266,15 +269,18 @@ pub fn process_input() {
.filter(|method| *method.enabled.lock())
.filter(|method| method.datamap.lock().is_some())
});
let handlers = INPUT_HANDLER_REGISTRY
.get_valid_contents()
.into_iter()
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
.filter(|handler| handler.field.upgrade().is_some());
for method in methods {
debug_span!("Process input method").in_scope(|| {
// Get all valid input handlers and convert them to DistanceLink objects
let mut distance_links: Vec<DistanceLink> = debug_span!("Generate distance links")
.in_scope(|| {
INPUT_HANDLER_REGISTRY
.get_valid_contents()
.into_iter()
.filter(|handler| handler.field.upgrade().is_some())
handlers
.clone()
.filter_map(|handler| DistanceLink::from(method.clone(), handler))
.collect()
});

View File

@@ -15,6 +15,7 @@ use color_eyre::eyre::{ensure, eyre, Result};
use lazy_static::lazy_static;
use nanoid::nanoid;
use parking_lot::Mutex;
use portable_atomic::Ordering;
use serde::Deserialize;
use stardust_xr::schemas::flex::{deserialize, serialize};
use stardust_xr::values::Transform;
@@ -307,6 +308,10 @@ impl ItemAcceptor {
}
fn capture_flex(node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
if !node.enabled.load(Ordering::Relaxed) {
return Ok(());
}
let acceptor = node.item_acceptor.get().unwrap();
let item_path: &str = deserialize(data)?;
let item_node = calling_client.get_node("Item", item_path)?;

View File

@@ -11,22 +11,22 @@ pub mod spatial;
pub mod startup;
use color_eyre::eyre::{eyre, Result};
use core::hash::BuildHasherDefault;
use dashmap::DashMap;
use nanoid::nanoid;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use portable_atomic::{AtomicBool, Ordering};
use rustc_hash::FxHasher;
use stardust_xr::messenger::MessageSenderHandle;
use stardust_xr::scenegraph::ScenegraphError;
use stardust_xr::schemas::flex::deserialize;
use std::fmt::Debug;
use std::future::Future;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak};
use std::vec::Vec;
use tracing::{debug_span, instrument};
use core::hash::BuildHasherDefault;
use dashmap::DashMap;
use rustc_hash::FxHasher;
use crate::core::client::Client;
use crate::core::registry::Registry;
@@ -45,6 +45,7 @@ pub type Signal = fn(&Node, Arc<Client>, &[u8]) -> Result<()>;
pub type Method = fn(&Node, Arc<Client>, &[u8]) -> Result<Vec<u8>>;
pub struct Node {
pub enabled: Arc<AtomicBool>,
pub(super) uid: String,
path: String,
client: Weak<Client>,
@@ -52,7 +53,7 @@ pub struct Node {
// trailing_slash_pos: usize,
local_signals: DashMap<String, Signal, BuildHasherDefault<FxHasher>>,
local_methods: DashMap<String, Method, BuildHasherDefault<FxHasher>>,
destroyable: AtomicBool,
destroyable: bool,
pub alias: OnceCell<Arc<Alias>>,
aliases: Registry<Alias>,
@@ -94,15 +95,13 @@ impl Node {
pub fn get_path(&self) -> &str {
self.path.as_str()
}
pub fn is_destroyable(&self) -> bool {
self.destroyable.load(Ordering::Relaxed)
}
pub fn create(client: &Arc<Client>, parent: &str, name: &str, destroyable: bool) -> Self {
let mut path = parent.to_string();
path.push('/');
path.push_str(name);
let node = Node {
enabled: Arc::new(AtomicBool::new(true)),
uid: nanoid!(),
client: Arc::downgrade(client),
message_sender_handle: client.message_sender_handle.clone(),
@@ -110,7 +109,7 @@ impl Node {
// trailing_slash_pos: parent.len(),
local_signals: Default::default(),
local_methods: Default::default(),
destroyable: AtomicBool::from(destroyable),
destroyable,
alias: OnceCell::new(),
aliases: Registry::new(),
@@ -129,6 +128,7 @@ impl Node {
sound: OnceCell::new(),
startup_settings: OnceCell::new(),
};
node.add_local_signal("set_enabled", Node::set_enabled_flex);
node.add_local_signal("destroy", Node::destroy_flex);
node
}
@@ -145,8 +145,12 @@ impl Node {
}
}
pub fn set_enabled_flex(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
node.enabled.store(deserialize(data)?, Ordering::Relaxed);
Ok(())
}
pub fn destroy_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
if node.is_destroyable() {
if node.destroyable {
node.destroy();
}
Ok(())