mod.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 miniquad::{BufferId, TextureId};
  19. use std::sync::{Arc, Weak};
  20. use crate::{
  21. error::{Error, Result},
  22. expr::{SExprMachine, SExprVal},
  23. gfx2::{DrawCall, Rectangle},
  24. prop::{PropertyPtr, Role},
  25. scene::{SceneGraph, SceneNode, SceneNodeId, SceneNodeType},
  26. ExecutorPtr,
  27. };
  28. mod button;
  29. pub use button::{Button, ButtonPtr};
  30. pub mod chatview;
  31. pub use chatview::{ChatView, ChatViewPtr};
  32. mod editbox;
  33. pub use editbox::{EditBox, EditBoxPtr};
  34. mod image;
  35. pub use image::{Image, ImagePtr};
  36. mod mesh;
  37. pub use mesh::{Mesh, MeshPtr};
  38. mod layer;
  39. pub use layer::{RenderLayer, RenderLayerPtr};
  40. mod text;
  41. pub use text::{Text, TextPtr};
  42. mod win;
  43. pub use win::{Window, WindowPtr};
  44. pub trait Stoppable {
  45. async fn stop(&self);
  46. }
  47. pub struct DrawUpdate {
  48. pub key: u64,
  49. pub draw_calls: Vec<(u64, DrawCall)>,
  50. pub freed_textures: Vec<TextureId>,
  51. pub freed_buffers: Vec<BufferId>,
  52. }
  53. pub struct OnModify<T> {
  54. ex: ExecutorPtr,
  55. node_name: String,
  56. node_id: SceneNodeId,
  57. me: Weak<T>,
  58. pub tasks: Vec<smol::Task<()>>,
  59. }
  60. impl<T: Send + Sync + 'static> OnModify<T> {
  61. pub fn new(ex: ExecutorPtr, node_name: String, node_id: SceneNodeId, me: Weak<T>) -> Self {
  62. Self { ex, node_name, node_id, me, tasks: vec![] }
  63. }
  64. pub fn when_change<F>(&mut self, prop: PropertyPtr, f: impl Fn(Arc<T>) -> F + Send + 'static)
  65. where
  66. F: std::future::Future<Output = ()> + Send + 'static,
  67. {
  68. let node_name = self.node_name.clone();
  69. let node_id = self.node_id;
  70. let on_modify_sub = prop.subscribe_modify();
  71. let prop_name = prop.name.clone();
  72. let me = self.me.clone();
  73. let task = self.ex.spawn(async move {
  74. loop {
  75. let Ok((role, _)) = on_modify_sub.receive().await else {
  76. error!(target: "app", "Property '{}':{}/'{}' on_modify pipe is broken", node_name, node_id, prop_name);
  77. return
  78. };
  79. if role == Role::Internal {
  80. continue
  81. }
  82. debug!(target: "app", "Property '{}':{}/'{}' modified", node_name, node_id, prop_name);
  83. let Some(self_) = me.upgrade() else {
  84. // Should not happen
  85. panic!(
  86. "'{}':{}/'{}' self destroyed before modify_task was stopped!",
  87. node_name, node_id, prop_name
  88. );
  89. };
  90. debug!(target: "app", "property modified");
  91. f(self_).await;
  92. }
  93. });
  94. self.tasks.push(task);
  95. }
  96. }
  97. pub fn eval_rect(rect: PropertyPtr, parent_rect: &Rectangle) -> Result<()> {
  98. if rect.array_len != 4 {
  99. return Err(Error::PropertyWrongLen)
  100. }
  101. for i in 0..4 {
  102. if !rect.is_expr(i)? {
  103. continue
  104. }
  105. let expr = rect.get_expr(i).unwrap();
  106. let machine = SExprMachine {
  107. globals: vec![
  108. ("w".to_string(), SExprVal::Float32(parent_rect.w)),
  109. ("h".to_string(), SExprVal::Float32(parent_rect.h)),
  110. ],
  111. stmts: &expr,
  112. };
  113. let v = machine.call()?.as_f32()?;
  114. rect.set_cache_f32(i, v).unwrap();
  115. }
  116. Ok(())
  117. }
  118. pub fn read_rect(rect_prop: PropertyPtr) -> Result<Rectangle> {
  119. if rect_prop.array_len != 4 {
  120. return Err(Error::PropertyWrongLen)
  121. }
  122. let mut rect = [0.; 4];
  123. for i in 0..4 {
  124. if rect_prop.is_expr(i)? {
  125. rect[i] = rect_prop.get_cached(i)?.as_f32()?;
  126. } else {
  127. rect[i] = rect_prop.get_f32(i)?;
  128. }
  129. }
  130. Ok(Rectangle::from_array(rect))
  131. }
  132. pub fn get_parent_rect(sg: &SceneGraph, node: &SceneNode) -> Option<Rectangle> {
  133. // read our parent
  134. if node.parents.is_empty() {
  135. info!("RenderLayer {:?} has no parents so skipping", node);
  136. return None
  137. }
  138. if node.parents.len() != 1 {
  139. error!("RenderLayer {:?} has too many parents so skipping", node);
  140. return None
  141. }
  142. let parent_id = node.parents[0].id;
  143. let parent_node = sg.get_node(parent_id).unwrap();
  144. let parent_rect = match parent_node.typ {
  145. SceneNodeType::Window => {
  146. let Some(screen_size_prop) = parent_node.get_property("screen_size") else {
  147. error!(
  148. "RenderLayer {:?} parent node {:?} missing screen_size property",
  149. node, parent_node
  150. );
  151. return None
  152. };
  153. let screen_width = screen_size_prop.get_f32(0).unwrap();
  154. let screen_height = screen_size_prop.get_f32(1).unwrap();
  155. let parent_rect = Rectangle::from_array([0., 0., screen_width, screen_height]);
  156. parent_rect
  157. }
  158. SceneNodeType::RenderLayer => {
  159. // get their rect property
  160. let Some(parent_rect) = parent_node.get_property("rect") else {
  161. error!(
  162. "RenderLayer {:?} parent node {:?} missing rect property",
  163. node, parent_node
  164. );
  165. return None
  166. };
  167. // read parent's rect
  168. let Ok(parent_rect) = read_rect(parent_rect) else {
  169. error!(
  170. "RenderLayer {:?} parent node {:?} malformed rect property",
  171. node, parent_node
  172. );
  173. return None
  174. };
  175. parent_rect
  176. }
  177. _ => {
  178. error!(
  179. "RenderLayer {:?} parent node {:?} wrong type {:?}",
  180. node, parent_node, parent_node.typ
  181. );
  182. return None
  183. }
  184. };
  185. Some(parent_rect)
  186. }