mod.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_trait::async_trait;
  19. use rand::{rngs::OsRng, Rng};
  20. use std::sync::{Arc, Mutex as SyncMutex, OnceLock, Weak};
  21. use crate::{
  22. error::{Error, Result},
  23. expr::{Op, SExprCode, SExprMachine, SExprVal},
  24. gfx::{
  25. GfxBufferId, GfxDrawCall, GfxDrawInstruction, GfxDrawMesh, Rectangle, RenderApi, Vertex,
  26. },
  27. mesh::Color,
  28. prop::{PropertyBool, PropertyFloat32, PropertyPtr, PropertyRect, PropertyUint32, Role},
  29. scene::{Pimpl, SceneNodePtr, SceneNodeWeak},
  30. util::enumerate,
  31. ExecutorPtr,
  32. };
  33. use super::{DrawUpdate, OnModify, UIObject};
  34. pub mod shape;
  35. use shape::VectorShape;
  36. pub type VectorArtPtr = Arc<VectorArt>;
  37. pub struct VectorArt {
  38. node: SceneNodeWeak,
  39. render_api: RenderApi,
  40. tasks: OnceLock<Vec<smol::Task<()>>>,
  41. shape: VectorShape,
  42. dc_key: u64,
  43. is_visible: PropertyBool,
  44. rect: PropertyRect,
  45. z_index: PropertyUint32,
  46. parent_rect: SyncMutex<Option<Rectangle>>,
  47. }
  48. impl VectorArt {
  49. pub async fn new(
  50. node: SceneNodeWeak,
  51. shape: VectorShape,
  52. render_api: RenderApi,
  53. ex: ExecutorPtr,
  54. ) -> Pimpl {
  55. debug!(target: "ui::vector_art", "VectorArt::new()");
  56. let node_ref = &node.upgrade().unwrap();
  57. let is_visible = PropertyBool::wrap(node_ref, Role::Internal, "is_visible", 0).unwrap();
  58. let rect = PropertyRect::wrap(node_ref, Role::Internal, "rect").unwrap();
  59. let z_index = PropertyUint32::wrap(node_ref, Role::Internal, "z_index", 0).unwrap();
  60. let node_name = node_ref.name.clone();
  61. let node_id = node_ref.id;
  62. let self_ = Arc::new(Self {
  63. node,
  64. render_api,
  65. tasks: OnceLock::new(),
  66. shape,
  67. dc_key: OsRng.gen(),
  68. is_visible,
  69. rect,
  70. z_index,
  71. parent_rect: SyncMutex::new(None),
  72. });
  73. Pimpl::VectorArt(self_)
  74. }
  75. async fn redraw(self: Arc<Self>) {
  76. let Some(parent_rect) = self.parent_rect.lock().unwrap().clone() else { return };
  77. let Some(draw_update) = self.get_draw_calls(parent_rect).await else {
  78. error!(target: "ui::vector_art", "Mesh failed to draw");
  79. return;
  80. };
  81. self.render_api.replace_draw_calls(draw_update.draw_calls);
  82. //debug!(target: "ui::vector_art", "replace draw calls done");
  83. }
  84. fn get_draw_instrs(&self) -> Vec<GfxDrawInstruction> {
  85. if !self.is_visible.get() {
  86. return vec![]
  87. }
  88. let rect = self.rect.get();
  89. let verts = self.shape.eval(rect.w, rect.h).expect("bad shape");
  90. //debug!(target: "ui::vector_art", "=> {verts:#?}");
  91. let vertex_buffer = self.render_api.new_vertex_buffer(verts);
  92. let index_buffer = self.render_api.new_index_buffer(self.shape.indices.clone());
  93. let mesh = GfxDrawMesh {
  94. vertex_buffer,
  95. index_buffer,
  96. texture: None,
  97. num_elements: self.shape.indices.len() as i32,
  98. };
  99. vec![GfxDrawInstruction::Move(rect.pos()), GfxDrawInstruction::Draw(mesh)]
  100. }
  101. async fn get_draw_calls(&self, parent_rect: Rectangle) -> Option<DrawUpdate> {
  102. //debug!(target: "ui::vector_art", "VectorArt::draw_cached()");
  103. if let Err(e) = self.rect.eval(&parent_rect) {
  104. warn!(target: "ui::vector_art", "Rect eval failure: {e}");
  105. return None
  106. }
  107. let instrs = self.get_draw_instrs();
  108. Some(DrawUpdate {
  109. key: self.dc_key,
  110. draw_calls: vec![(
  111. self.dc_key,
  112. GfxDrawCall { instrs, dcs: vec![], z_index: self.z_index.get() },
  113. )],
  114. })
  115. }
  116. }
  117. #[async_trait]
  118. impl UIObject for VectorArt {
  119. fn z_index(&self) -> u32 {
  120. self.z_index.get()
  121. }
  122. async fn start(self: Arc<Self>, ex: ExecutorPtr) {
  123. let me = Arc::downgrade(&self);
  124. let node_ref = &self.node.upgrade().unwrap();
  125. let node_name = node_ref.name.clone();
  126. let node_id = node_ref.id;
  127. let mut on_modify = OnModify::new(ex, node_name, node_id, me.clone());
  128. on_modify.when_change(self.is_visible.prop(), Self::redraw);
  129. on_modify.when_change(self.rect.prop(), Self::redraw);
  130. on_modify.when_change(self.z_index.prop(), Self::redraw);
  131. self.tasks.set(on_modify.tasks);
  132. }
  133. async fn draw(&self, parent_rect: Rectangle) -> Option<DrawUpdate> {
  134. debug!(target: "ui::vector_art", "VectorArt::draw({:?})", self.node.upgrade().unwrap());
  135. *self.parent_rect.lock().unwrap() = Some(parent_rect);
  136. self.get_draw_calls(parent_rect).await
  137. }
  138. }
  139. impl Drop for VectorArt {
  140. fn drop(&mut self) {
  141. self.render_api.replace_draw_calls(vec![(self.dc_key, Default::default())]);
  142. }
  143. }