feat(panel): apply material

This commit is contained in:
Nova
2022-08-22 14:42:38 -04:00
parent cd86b3a001
commit 7e6705a012
3 changed files with 48 additions and 11 deletions

View File

@@ -87,7 +87,7 @@ pub struct Item {
uid: String,
type_info: &'static TypeInfo,
captured_acceptor: Mutex<Weak<ItemAcceptor>>,
specialization: ItemType,
pub specialization: ItemType,
}
impl Item {
pub fn new(node: &Arc<Node>, type_info: &'static TypeInfo, specialization: ItemType) -> Self {

View File

@@ -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<Client>, 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::<CoreSurface>() {
if let Some(sk_mat) = core_surface.sk_mat.get() {
model
.pending_material_replacements
.lock()
.insert(material_idx as u32, sk_mat.clone());
}
}
});
}
Ok(())
}
}

View File

@@ -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<Option<SendWrapper<Gles2Texture>>>,
pub wl_tex: RefCell<Option<SendWrapper<Gles2Texture>>>,
sk_tex: OnceCell<SKTexture>,
sk_mat: OnceCell<Material>,
pub sk_mat: OnceCell<Arc<SendWrapper<Material>>>,
}
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() {