From 7e6705a0120f352eefc475b50df30dd03dd33648 Mon Sep 17 00:00:00 2001 From: Nova Date: Mon, 22 Aug 2022 14:42:38 -0400 Subject: [PATCH] feat(panel): apply material --- src/nodes/item.rs | 2 +- src/wayland/panel_item.rs | 40 ++++++++++++++++++++++++++++++++++++--- src/wayland/surface.rs | 17 ++++++++++------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/nodes/item.rs b/src/nodes/item.rs index 85e6238..e3a5529 100644 --- a/src/nodes/item.rs +++ b/src/nodes/item.rs @@ -87,7 +87,7 @@ pub struct Item { uid: String, type_info: &'static TypeInfo, captured_acceptor: Mutex>, - specialization: ItemType, + pub specialization: ItemType, } impl Item { pub fn new(node: &Arc, type_info: &'static TypeInfo, specialization: ItemType) -> Self { diff --git a/src/wayland/panel_item.rs b/src/wayland/panel_item.rs index 904a6c9..e5e4f43 100644 --- a/src/wayland/panel_item.rs +++ b/src/wayland/panel_item.rs @@ -1,17 +1,23 @@ use crate::{ - core::{client::INTERNAL_CLIENT, registry::Registry}, + core::{ + client::{Client, INTERNAL_CLIENT}, + registry::Registry, + }, nodes::{ core::Node, item::{Item, ItemType, TypeInfo}, spatial::Spatial, }, }; +use anyhow::{anyhow, Result}; use glam::Mat4; use lazy_static::lazy_static; use nanoid::nanoid; -use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::{reexports::wayland_server::protocol::wl_surface::WlSurface, wayland::compositor}; use std::sync::Arc; +use super::surface::CoreSurface; + lazy_static! { static ref ITEM_TYPE_INFO_PANEL: TypeInfo = TypeInfo { type_name: "panel", @@ -56,7 +62,35 @@ impl PanelItem { .items .add(Item::new(&node, &ITEM_TYPE_INFO_PANEL, specialization)); let _ = node.item.set(item); - // node.add_local_method("getPath", PanelItem::get_path_flex); + node.add_local_signal("applySurfaceMaterial", PanelItem::apply_surface_material); node } + + fn apply_surface_material(node: &Node, calling_client: Arc, data: &[u8]) -> Result<()> { + let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?; + let material_idx = flex_vec.idx(0).get_u64()?; + let model_node = calling_client + .scenegraph + .get_node(flex_vec.idx(0).as_str()) + .ok_or_else(|| anyhow!("Model node not found"))?; + let model = model_node + .model + .get() + .ok_or_else(|| anyhow!("Node is not a model"))?; + + if let ItemType::Panel(panel_item) = &node.item.get().unwrap().specialization { + compositor::with_states(&panel_item.toplevel_surface, |states| { + if let Some(core_surface) = states.data_map.get::() { + if let Some(sk_mat) = core_surface.sk_mat.get() { + model + .pending_material_replacements + .lock() + .insert(material_idx as u32, sk_mat.clone()); + } + } + }); + } + + Ok(()) + } } diff --git a/src/wayland/surface.rs b/src/wayland/surface.rs index ca58e01..a874953 100644 --- a/src/wayland/surface.rs +++ b/src/wayland/surface.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, fmt::Error}; +use std::{cell::RefCell, fmt::Error, sync::Arc}; use glam::vec2; use once_cell::sync::OnceCell; @@ -14,9 +14,9 @@ use stereokit::{ use super::shaders::SIMULA_SHADER_BYTES; pub struct CoreSurface { - pub(crate) wl_tex: RefCell>>, + pub wl_tex: RefCell>>, sk_tex: OnceCell, - sk_mat: OnceCell, + pub sk_mat: OnceCell>>, } impl CoreSurface { @@ -39,10 +39,13 @@ impl CoreSurface { .sk_mat .get_or_try_init(|| { let shader = Shader::from_mem(sk, SIMULA_SHADER_BYTES).unwrap(); - Material::create(sk, &shader).ok_or(Error).map(|mat| { - mat.set_parameter("diffuse", self.sk_tex.get().unwrap()); - mat - }) + Material::create(sk, &shader) + .ok_or(Error) + .map(|mat| { + mat.set_parameter("diffuse", self.sk_tex.get().unwrap()); + mat + }) + .map(|mat| Arc::new(SendWrapper::new(mat))) }) .unwrap(); if let Some(smithay_tex) = self.wl_tex.borrow().as_ref() {