scene.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_channel::Sender;
  19. use async_lock::Mutex;
  20. use darkfi_serial::{SerialDecodable, SerialEncodable};
  21. use futures::{stream::FuturesUnordered, StreamExt};
  22. use std::{fmt, str::FromStr, sync::Arc};
  23. use crate::{
  24. error::{Error, Result},
  25. prop::{Property, PropertyPtr, PropertyType},
  26. ui,
  27. };
  28. pub type SceneNodeId = u32;
  29. #[derive(Debug, Copy, Clone, PartialEq, SerialEncodable, SerialDecodable)]
  30. #[repr(u8)]
  31. pub enum SceneNodeType {
  32. Null = 0,
  33. Root = 1,
  34. Window = 2,
  35. WindowInput = 6,
  36. Keyboard = 7,
  37. Mouse = 8,
  38. RenderLayer = 3,
  39. RenderObject = 4,
  40. RenderMesh = 5,
  41. RenderText = 9,
  42. RenderTexture = 13,
  43. Fonts = 10,
  44. Font = 11,
  45. Plugins = 14,
  46. Plugin = 15,
  47. ChatView = 16,
  48. EditBox = 17,
  49. Image = 18,
  50. }
  51. pub struct ScenePath(Vec<String>);
  52. impl<S: Into<String>> From<S> for ScenePath {
  53. fn from(path: S) -> Self {
  54. let path: String = path.into();
  55. (&path).parse().expect("invalid ScenePath &str")
  56. }
  57. }
  58. impl fmt::Display for ScenePath {
  59. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  60. write!(f, "/")?;
  61. for token in &self.0 {
  62. write!(f, "{}/", token)?;
  63. }
  64. Ok(())
  65. }
  66. }
  67. impl FromStr for ScenePath {
  68. type Err = Error;
  69. fn from_str(s: &str) -> Result<Self> {
  70. if s.is_empty() || s.chars().nth(0).unwrap() != '/' {
  71. return Err(Error::InvalidScenePath);
  72. }
  73. if s == "/" {
  74. return Ok(ScenePath(vec![]));
  75. }
  76. let mut tokens = s.split('/');
  77. // Should start with a /
  78. let initial = tokens.next().expect("should not be empty");
  79. if !initial.is_empty() {
  80. return Err(Error::InvalidScenePath);
  81. }
  82. let mut path = vec![];
  83. for token in tokens {
  84. // There should not be any double slashes //
  85. if token.is_empty() {
  86. return Err(Error::InvalidScenePath);
  87. }
  88. path.push(token.to_string());
  89. }
  90. Ok(ScenePath(path))
  91. }
  92. }
  93. pub type SceneGraphPtr = Arc<std::sync::Mutex<SceneGraph>>;
  94. pub type SceneGraphPtr2 = Arc<Mutex<SceneGraph>>;
  95. pub struct SceneGraph {
  96. // Node 0 is always the root
  97. nodes: Vec<SceneNode>,
  98. freed: Vec<SceneNodeId>,
  99. }
  100. impl SceneGraph {
  101. pub const ROOT_ID: SceneNodeId = 0;
  102. pub fn new() -> Self {
  103. let root = SceneNode {
  104. name: "/".to_string(),
  105. id: 0,
  106. typ: SceneNodeType::Root,
  107. parents: vec![],
  108. children: vec![],
  109. props: vec![],
  110. sigs: vec![],
  111. methods: vec![],
  112. pimpl: Pimpl::Null,
  113. };
  114. Self { nodes: vec![root], freed: vec![] }
  115. }
  116. pub fn add_node<S: Into<String>>(&mut self, name: S, typ: SceneNodeType) -> &mut SceneNode {
  117. let node = SceneNode {
  118. name: name.into(),
  119. // We set this at the end
  120. id: 0,
  121. typ,
  122. parents: vec![],
  123. children: vec![],
  124. props: vec![],
  125. sigs: vec![],
  126. methods: vec![],
  127. pimpl: Pimpl::Null,
  128. };
  129. let node_id = if self.freed.is_empty() {
  130. let node_id = self.nodes.len() as SceneNodeId;
  131. self.nodes.push(node);
  132. node_id
  133. } else {
  134. let node_id = self.freed.pop().unwrap();
  135. let _ = std::mem::replace(&mut self.nodes[node_id as usize], node);
  136. node_id
  137. };
  138. self.nodes[node_id as usize].id = node_id;
  139. &mut self.nodes[node_id as usize]
  140. }
  141. pub fn remove_node(&mut self, id: SceneNodeId) -> Result<()> {
  142. let node = self.get_node_mut(id).ok_or(Error::NodeNotFound)?;
  143. if !node.parents.is_empty() {
  144. return Err(Error::NodeHasParents);
  145. }
  146. if !node.children.is_empty() {
  147. return Err(Error::NodeHasChildren);
  148. }
  149. node.name.clear();
  150. node.typ = SceneNodeType::Null;
  151. node.props.clear();
  152. self.freed.push(id);
  153. Ok(())
  154. }
  155. fn root(&self) -> &SceneNode {
  156. &self.nodes[0]
  157. }
  158. fn root_mut(&mut self) -> &mut SceneNode {
  159. &mut self.nodes[0]
  160. }
  161. fn exists(&self, id: SceneNodeId) -> bool {
  162. id < self.nodes.len() as SceneNodeId && !self.freed.contains(&id)
  163. }
  164. pub fn get_node(&self, id: SceneNodeId) -> Option<&SceneNode> {
  165. if self.exists(id) {
  166. Some(&self.nodes[id as usize])
  167. } else {
  168. None
  169. }
  170. }
  171. pub fn get_node_mut(&mut self, id: SceneNodeId) -> Option<&mut SceneNode> {
  172. if self.exists(id) {
  173. Some(&mut self.nodes[id as usize])
  174. } else {
  175. None
  176. }
  177. }
  178. pub fn link(&mut self, child_id: SceneNodeId, parent_id: SceneNodeId) -> Result<()> {
  179. // Check both nodes are not already linked
  180. let is_linked = self.is_linked(child_id, parent_id)?;
  181. if is_linked {
  182. return Err(Error::NodesAreLinked);
  183. }
  184. let parent = self.get_node(parent_id).unwrap();
  185. let parent_inf =
  186. SceneNodeInfo { name: parent.name.clone(), id: parent_id, typ: parent.typ };
  187. let child_name = &self.get_node(child_id).unwrap().name;
  188. if parent.has_child(child_name) {
  189. return Err(Error::NodeChildNameConflict);
  190. }
  191. // Link parent into child
  192. let child = self.get_node_mut(child_id).unwrap();
  193. if child.has_parent(&parent_inf.name) {
  194. return Err(Error::NodeParentNameConflict);
  195. }
  196. let child_inf = SceneNodeInfo { name: child.name.clone(), id: child_id, typ: child.typ };
  197. assert!(!child.has_parent_id(parent_id));
  198. child.parents.push(parent_inf);
  199. // Link child into parent
  200. let parent = self.get_node_mut(parent_id).unwrap();
  201. assert!(!parent.has_child(&child_inf.name));
  202. parent.children.push(child_inf);
  203. Ok(())
  204. }
  205. pub fn unlink(&mut self, child_id: SceneNodeId, parent_id: SceneNodeId) -> Result<()> {
  206. // Check both nodes are actually linked
  207. let is_linked = self.is_linked(child_id, parent_id)?;
  208. if !is_linked {
  209. return Err(Error::NodesNotLinked);
  210. }
  211. // Unlink parent from child
  212. let child = self.get_node_mut(child_id).unwrap();
  213. child.remove_parent(parent_id);
  214. // Unlink child from parent
  215. let parent = self.get_node_mut(parent_id).unwrap();
  216. parent.remove_child(child_id);
  217. Ok(())
  218. }
  219. pub fn is_linked(&self, child_id: SceneNodeId, parent_id: SceneNodeId) -> Result<bool> {
  220. let parent = self.get_node(parent_id).ok_or(Error::ParentNodeNotFound)?;
  221. let child = self.get_node(child_id).ok_or(Error::ChildNodeNotFound)?;
  222. let parent_has_child = parent.has_child_id(child_id);
  223. let child_has_parent = child.has_parent_id(parent_id);
  224. // Internal consistency checks
  225. if parent_has_child {
  226. assert!(child_has_parent);
  227. } else {
  228. assert!(!child_has_parent);
  229. }
  230. Ok(parent_has_child)
  231. }
  232. pub fn lookup_node_id<P: Into<ScenePath>>(&self, path: P) -> Option<SceneNodeId> {
  233. let path: ScenePath = path.into();
  234. let mut current_id = Self::ROOT_ID;
  235. for node_name in path.0 {
  236. let parent_node = self.get_node(current_id).unwrap();
  237. match parent_node.get_child(&node_name) {
  238. Some(child_id) => {
  239. current_id = child_id;
  240. }
  241. None => return None,
  242. }
  243. }
  244. Some(current_id)
  245. }
  246. pub fn lookup_node<P: Into<ScenePath>>(&self, path: P) -> Option<&SceneNode> {
  247. let node_id = self.lookup_node_id(path)?;
  248. Some(self.get_node(node_id).unwrap())
  249. }
  250. pub fn lookup_node_mut<P: Into<ScenePath>>(&mut self, path: P) -> Option<&mut SceneNode> {
  251. let node_id = self.lookup_node_id(path)?;
  252. Some(self.get_node_mut(node_id).unwrap())
  253. }
  254. pub fn rename_node<S: Into<String>>(
  255. &mut self,
  256. node_id: SceneNodeId,
  257. node_name: S,
  258. ) -> Result<()> {
  259. let node_name = node_name.into();
  260. for sibling_inf in self.node_siblings(node_id)? {
  261. if sibling_inf.name == node_name {
  262. return Err(Error::NodeSiblingNameConflict)
  263. }
  264. }
  265. let node = self.get_node_mut(node_id).unwrap();
  266. node.name = node_name.clone();
  267. // Now update it for all children and parents too
  268. let parent_ids: Vec<_> = node.parents.iter().map(|parent_inf| parent_inf.id).collect();
  269. let child_ids: Vec<_> = node.children.iter().map(|child_inf| child_inf.id).collect();
  270. 'next_parent: for parent_id in parent_ids {
  271. let parent = self.get_node_mut(parent_id).unwrap();
  272. for child in &mut parent.children {
  273. if child.id == node_id {
  274. child.name = node_name.clone();
  275. continue 'next_parent
  276. }
  277. }
  278. panic!("child {} not found in parent {}!", node_id, parent.id)
  279. }
  280. 'next_child: for child_id in child_ids {
  281. let child = self.get_node_mut(child_id).unwrap();
  282. for parent in &mut child.parents {
  283. if parent.id == node_id {
  284. parent.name = node_name.clone();
  285. continue 'next_child
  286. }
  287. }
  288. panic!("parent {} not found in child {}!", node_id, child.id)
  289. }
  290. Ok(())
  291. }
  292. fn node_siblings(&self, node_id: SceneNodeId) -> Result<Vec<SceneNodeInfo>> {
  293. let mut siblings = vec![];
  294. let node = self.get_node(node_id).ok_or(Error::NodeNotFound)?;
  295. for parent_inf in &node.parents {
  296. let parent = self.get_node(parent_inf.id).ok_or(Error::ParentNodeNotFound)?;
  297. let mut sibling_infs = parent
  298. .children
  299. .iter()
  300. .cloned()
  301. .filter(|child_inf| child_inf.id != node_id)
  302. .collect();
  303. siblings.append(&mut sibling_infs);
  304. }
  305. Ok(siblings)
  306. }
  307. pub fn scan_dangling(&self) -> Vec<SceneNodeId> {
  308. let mut dangling = vec![];
  309. for node in &self.nodes {
  310. if node.id == Self::ROOT_ID {
  311. continue
  312. }
  313. if self.freed.contains(&node.id) {
  314. continue
  315. }
  316. if node.parents.is_empty() {
  317. dangling.push(node.id);
  318. }
  319. }
  320. dangling
  321. }
  322. }
  323. #[derive(Clone)]
  324. pub struct SceneNodeInfo {
  325. pub name: String,
  326. pub id: SceneNodeId,
  327. pub typ: SceneNodeType,
  328. }
  329. pub struct SceneNode {
  330. pub name: String,
  331. pub id: SceneNodeId,
  332. pub typ: SceneNodeType,
  333. pub parents: Vec<SceneNodeInfo>,
  334. pub children: Vec<SceneNodeInfo>,
  335. pub props: Vec<PropertyPtr>,
  336. pub sigs: Vec<Signal>,
  337. pub methods: Vec<Method>,
  338. pub pimpl: Pimpl,
  339. }
  340. impl SceneNode {
  341. fn has_parent_id(&self, parent_id: SceneNodeId) -> bool {
  342. self.parents.iter().any(|parent| parent.id == parent_id)
  343. }
  344. fn has_child_id(&self, child_id: SceneNodeId) -> bool {
  345. self.children.iter().any(|child| child.id == child_id)
  346. }
  347. fn has_parent(&self, parent_name: &str) -> bool {
  348. self.parents.iter().any(|parent| parent.name == parent_name)
  349. }
  350. fn has_child(&self, child_name: &str) -> bool {
  351. self.children.iter().any(|child| child.name == child_name)
  352. }
  353. fn get_child(&self, child_name: &str) -> Option<SceneNodeId> {
  354. for child in &self.children {
  355. if child.name == child_name {
  356. return Some(child.id);
  357. }
  358. }
  359. None
  360. }
  361. // Panics if parent is not linked
  362. fn remove_parent(&mut self, parent_id: SceneNodeId) {
  363. let parent_idx = self.parents.iter().position(|parent| parent.id == parent_id).unwrap();
  364. self.parents.swap_remove(parent_idx);
  365. }
  366. // Panics if child is not linked
  367. fn remove_child(&mut self, child_id: SceneNodeId) {
  368. let child_idx = self.children.iter().position(|child| child.id == child_id).unwrap();
  369. self.children.swap_remove(child_idx);
  370. }
  371. pub fn get_children(&self, allowed_types: &[SceneNodeType]) -> Vec<SceneNodeInfo> {
  372. self.children
  373. .iter()
  374. .cloned()
  375. .filter(move |child_inf| allowed_types.contains(&child_inf.typ))
  376. .collect()
  377. }
  378. pub fn get_children2(&self) -> Vec<SceneNodeInfo> {
  379. self.children.iter().cloned().collect()
  380. }
  381. pub fn add_property(&mut self, prop: Property) -> Result<()> {
  382. if self.has_property(&prop.name) {
  383. return Err(Error::PropertyAlreadyExists);
  384. }
  385. self.props.push(Arc::new(prop));
  386. Ok(())
  387. }
  388. fn has_property(&self, name: &str) -> bool {
  389. self.props.iter().any(|prop| prop.name == name)
  390. }
  391. pub fn get_property(&self, name: &str) -> Option<PropertyPtr> {
  392. self.props.iter().find(|prop| prop.name == name).map(|prop| prop.clone())
  393. }
  394. // Convenience methods
  395. pub fn get_property_bool(&self, name: &str) -> Result<bool> {
  396. self.get_property(name).ok_or(Error::PropertyNotFound)?.get_bool(0)
  397. }
  398. pub fn get_property_u32(&self, name: &str) -> Result<u32> {
  399. self.get_property(name).ok_or(Error::PropertyNotFound)?.get_u32(0)
  400. }
  401. pub fn get_property_f32(&self, name: &str) -> Result<f32> {
  402. self.get_property(name).ok_or(Error::PropertyNotFound)?.get_f32(0)
  403. }
  404. pub fn get_property_str(&self, name: &str) -> Result<String> {
  405. self.get_property(name).ok_or(Error::PropertyNotFound)?.get_str(0)
  406. }
  407. pub fn get_property_enum(&self, name: &str) -> Result<String> {
  408. self.get_property(name).ok_or(Error::PropertyNotFound)?.get_enum(0)
  409. }
  410. pub fn get_property_node_id(&self, name: &str) -> Result<SceneNodeId> {
  411. self.get_property(name).ok_or(Error::PropertyNotFound)?.get_node_id(0)
  412. }
  413. // Setters
  414. pub fn set_property_bool(&self, name: &str, val: bool) -> Result<()> {
  415. self.get_property(name).ok_or(Error::PropertyNotFound)?.set_bool(0, val)
  416. }
  417. pub fn set_property_u32(&self, name: &str, val: u32) -> Result<()> {
  418. self.get_property(name).ok_or(Error::PropertyNotFound)?.set_u32(0, val)
  419. }
  420. pub fn set_property_f32(&self, name: &str, val: f32) -> Result<()> {
  421. self.get_property(name).ok_or(Error::PropertyNotFound)?.set_f32(0, val)
  422. }
  423. pub fn set_property_str<S: Into<String>>(&self, name: &str, val: S) -> Result<()> {
  424. self.get_property(name).ok_or(Error::PropertyNotFound)?.set_str(0, val)
  425. }
  426. pub fn set_property_node_id(&self, name: &str, val: SceneNodeId) -> Result<()> {
  427. self.get_property(name).ok_or(Error::PropertyNotFound)?.set_node_id(0, val)
  428. }
  429. pub fn add_signal<S: Into<String>>(
  430. &mut self,
  431. name: S,
  432. desc: S,
  433. fmt: Vec<(S, S, PropertyType)>,
  434. ) -> Result<()> {
  435. let name = name.into();
  436. if self.has_signal(&name) {
  437. return Err(Error::SignalAlreadyExists);
  438. }
  439. let fmt = fmt
  440. .into_iter()
  441. .map(|(n, d, t)| CallArg { name: n.into(), desc: d.into(), typ: t })
  442. .collect();
  443. self.sigs.push(Signal {
  444. name: name.into(),
  445. desc: desc.into(),
  446. fmt,
  447. slots: vec![],
  448. freed: vec![],
  449. });
  450. Ok(())
  451. }
  452. fn has_signal(&self, name: &str) -> bool {
  453. self.sigs.iter().any(|sig| sig.name == name)
  454. }
  455. pub fn get_signal(&self, name: &str) -> Option<&Signal> {
  456. self.sigs.iter().find(|sig| sig.name == name)
  457. }
  458. fn get_signal_mut(&mut self, name: &str) -> Option<&mut Signal> {
  459. self.sigs.iter_mut().find(|sig| sig.name == name)
  460. }
  461. pub fn register(&mut self, sig_name: &str, slot: Slot) -> Result<SlotId> {
  462. let sig = self.get_signal_mut(sig_name).ok_or(Error::SignalNotFound)?;
  463. let slot_id = if sig.freed.is_empty() {
  464. let slot_id = sig.slots.len() as SlotId;
  465. sig.slots.push(slot);
  466. slot_id
  467. } else {
  468. let slot_id = sig.freed.pop().unwrap();
  469. let _ = std::mem::replace(&mut sig.slots[slot_id as usize], slot);
  470. slot_id
  471. };
  472. Ok(slot_id)
  473. }
  474. pub fn unregister(&mut self, sig_name: &str, slot_id: SlotId) -> Result<()> {
  475. let sig = self.get_signal_mut(sig_name).ok_or(Error::SignalNotFound)?;
  476. if !sig.slot_exists(slot_id) {
  477. return Err(Error::SlotNotFound);
  478. }
  479. sig.freed.push(slot_id);
  480. Ok(())
  481. }
  482. pub async fn trigger(&self, sig_name: &str, data: Vec<u8>) -> Result<()> {
  483. let sig = self.get_signal(sig_name).ok_or(Error::SignalNotFound)?;
  484. let futures = FuturesUnordered::new();
  485. for (_, slot) in sig.get_slots() {
  486. // Trigger the slot
  487. futures.push(async {
  488. // Ignore the result
  489. let _ = slot.notify.send(data.clone()).await;
  490. });
  491. }
  492. let _: Vec<_> = futures.collect().await;
  493. Ok(())
  494. }
  495. pub fn add_method<S: Into<String>>(
  496. &mut self,
  497. name: S,
  498. args: Vec<(S, S, PropertyType)>,
  499. result: Vec<(S, S, PropertyType)>,
  500. method_fn: MethodRequestFn,
  501. ) -> Result<()> {
  502. let name = name.into();
  503. if self.has_signal(&name) {
  504. return Err(Error::MethodAlreadyExists);
  505. }
  506. let args = args
  507. .into_iter()
  508. .map(|(n, d, t)| CallArg { name: n.into(), desc: d.into(), typ: t })
  509. .collect();
  510. let result = result
  511. .into_iter()
  512. .map(|(n, d, t)| CallArg { name: n.into(), desc: d.into(), typ: t })
  513. .collect();
  514. self.methods.push(Method { name: name.into(), args, result, method_fn });
  515. Ok(())
  516. }
  517. pub fn get_method(&self, name: &str) -> Option<&Method> {
  518. self.methods.iter().find(|method| method.name == name)
  519. }
  520. fn get_method_mut(&mut self, name: &str) -> Option<&mut Method> {
  521. self.methods.iter_mut().find(|method| method.name == name)
  522. }
  523. pub fn call_method(
  524. &mut self,
  525. name: &str,
  526. arg_data: Vec<u8>,
  527. response_fn: MethodResponseFn,
  528. ) -> Result<()> {
  529. let method = self.get_method(name).ok_or(Error::MethodNotFound)?;
  530. (method.method_fn)(arg_data, response_fn);
  531. Ok(())
  532. }
  533. }
  534. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  535. pub struct CallArg {
  536. pub name: String,
  537. pub desc: String,
  538. pub typ: PropertyType,
  539. }
  540. type SlotFn = Box<dyn Fn(Vec<u8>) + Send>;
  541. pub type SlotId = u32;
  542. pub struct Slot {
  543. pub name: String,
  544. pub notify: Sender<Vec<u8>>,
  545. }
  546. pub struct Signal {
  547. pub name: String,
  548. pub desc: String,
  549. pub fmt: Vec<CallArg>,
  550. slots: Vec<Slot>,
  551. freed: Vec<SlotId>,
  552. }
  553. impl Signal {
  554. fn slot_exists(&self, slot_id: SlotId) -> bool {
  555. if slot_id >= self.slots.len() as SlotId {
  556. return false;
  557. }
  558. return !self.freed.contains(&slot_id);
  559. }
  560. pub fn get_slots<'a>(&'a self) -> impl Iterator<Item = (SlotId, &'a Slot)> {
  561. self.slots
  562. .iter()
  563. .enumerate()
  564. .filter(|(slot_id, _)| !self.freed.contains(&(*slot_id as SlotId)))
  565. .map(|(slot_id, slot)| (slot_id as SlotId, slot))
  566. }
  567. pub fn lookup_slot_id(&self, slot_name: &str) -> Option<SlotId> {
  568. for (slot_id, slot) in self.get_slots() {
  569. if slot.name == slot_name {
  570. return Some(slot_id);
  571. }
  572. }
  573. None
  574. }
  575. }
  576. type MethodRequestFn = Box<dyn Fn(Vec<u8>, MethodResponseFn) + Send + Sync>;
  577. pub type MethodResponseFn = Box<dyn Fn(Result<Vec<u8>>) + Send + Sync>;
  578. pub struct Method {
  579. pub name: String,
  580. pub args: Vec<CallArg>,
  581. pub result: Vec<CallArg>,
  582. method_fn: MethodRequestFn,
  583. }
  584. pub enum Pimpl {
  585. Null,
  586. Window(ui::WindowPtr),
  587. RenderLayer(ui::RenderLayerPtr),
  588. Mesh(ui::MeshPtr),
  589. Text(ui::TextPtr),
  590. EditBox(ui::EditBoxPtr),
  591. ChatView(ui::ChatViewPtr),
  592. Image(ui::ImagePtr),
  593. }
  594. impl std::fmt::Debug for SceneNode {
  595. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  596. write!(f, "'{}':{}", self.name, self.id)
  597. }
  598. }