mod.rs 6.1 KB

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