wrap.rs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 crate::{
  19. error::{Error, Result},
  20. expr::{SExprMachine, SExprVal},
  21. gfx::{Dimension, Point, Rectangle},
  22. scene::SceneNode as SceneNode3,
  23. };
  24. use super::{PropertyPtr, Role};
  25. #[derive(Clone)]
  26. pub struct PropertyBool {
  27. prop: PropertyPtr,
  28. role: Role,
  29. idx: usize,
  30. }
  31. impl PropertyBool {
  32. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str, idx: usize) -> Result<Self> {
  33. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  34. // Test if it works
  35. let _ = prop.get_bool(idx)?;
  36. Ok(Self { prop, role, idx })
  37. }
  38. pub fn get(&self) -> bool {
  39. self.prop.get_bool(self.idx).unwrap()
  40. }
  41. pub fn set(&self, val: bool) {
  42. self.prop.set_bool(self.role, self.idx, val).unwrap()
  43. }
  44. pub fn prop(&self) -> PropertyPtr {
  45. self.prop.clone()
  46. }
  47. }
  48. #[derive(Clone)]
  49. pub struct PropertyUint32 {
  50. prop: PropertyPtr,
  51. role: Role,
  52. idx: usize,
  53. }
  54. impl PropertyUint32 {
  55. pub fn from(prop: PropertyPtr, role: Role, idx: usize) -> Result<Self> {
  56. // Test if it works
  57. let _ = prop.get_u32(idx)?;
  58. Ok(Self { prop, role, idx })
  59. }
  60. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str, idx: usize) -> Result<Self> {
  61. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  62. // Test if it works
  63. let _ = prop.get_u32(idx)?;
  64. Ok(Self { prop, role, idx })
  65. }
  66. pub fn get(&self) -> u32 {
  67. self.prop.get_u32(self.idx).unwrap()
  68. }
  69. pub fn set(&self, val: u32) {
  70. self.prop.set_u32(self.role, self.idx, val).unwrap()
  71. }
  72. pub fn prop(&self) -> PropertyPtr {
  73. self.prop.clone()
  74. }
  75. }
  76. #[derive(Clone)]
  77. pub struct PropertyFloat32 {
  78. prop: PropertyPtr,
  79. role: Role,
  80. idx: usize,
  81. }
  82. impl PropertyFloat32 {
  83. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str, idx: usize) -> Result<Self> {
  84. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  85. // Test if it works
  86. let _ = prop.get_f32(idx)?;
  87. Ok(Self { prop, role, idx })
  88. }
  89. pub fn get(&self) -> f32 {
  90. self.prop.get_f32(self.idx).unwrap()
  91. }
  92. pub fn set(&self, val: f32) {
  93. self.prop.set_f32(self.role, self.idx, val).unwrap()
  94. }
  95. pub fn prop(&self) -> PropertyPtr {
  96. self.prop.clone()
  97. }
  98. }
  99. #[derive(Clone)]
  100. pub struct PropertyStr {
  101. prop: PropertyPtr,
  102. role: Role,
  103. idx: usize,
  104. }
  105. impl PropertyStr {
  106. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str, idx: usize) -> Result<Self> {
  107. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  108. // Test if it works
  109. let _ = prop.get_str(idx)?;
  110. Ok(Self { prop, role, idx })
  111. }
  112. pub fn get(&self) -> String {
  113. self.prop.get_str(self.idx).unwrap()
  114. }
  115. pub fn set<S: Into<String>>(&self, val: S) {
  116. self.prop.set_str(self.role, self.idx, val.into()).unwrap()
  117. }
  118. pub fn prop(&self) -> PropertyPtr {
  119. self.prop.clone()
  120. }
  121. }
  122. #[derive(Clone)]
  123. pub struct PropertyColor {
  124. prop: PropertyPtr,
  125. role: Role,
  126. }
  127. impl PropertyColor {
  128. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str) -> Result<Self> {
  129. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  130. if !prop.is_bounded() || prop.get_len() != 4 {
  131. return Err(Error::PropertyWrongLen)
  132. }
  133. // Test if it works
  134. let _ = prop.get_f32(0)?;
  135. Ok(Self { prop, role })
  136. }
  137. pub fn get(&self) -> [f32; 4] {
  138. [
  139. self.prop.get_f32(0).unwrap(),
  140. self.prop.get_f32(1).unwrap(),
  141. self.prop.get_f32(2).unwrap(),
  142. self.prop.get_f32(3).unwrap(),
  143. ]
  144. }
  145. pub fn set(&self, val: [f32; 4]) {
  146. self.prop.set_f32(self.role, 0, val[0]).unwrap();
  147. self.prop.set_f32(self.role, 1, val[1]).unwrap();
  148. self.prop.set_f32(self.role, 2, val[2]).unwrap();
  149. self.prop.set_f32(self.role, 3, val[3]).unwrap();
  150. }
  151. pub fn prop(&self) -> PropertyPtr {
  152. self.prop.clone()
  153. }
  154. }
  155. #[derive(Clone)]
  156. pub struct PropertyDimension {
  157. prop: PropertyPtr,
  158. role: Role,
  159. }
  160. impl PropertyDimension {
  161. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str) -> Result<Self> {
  162. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  163. if !prop.is_bounded() || prop.get_len() != 2 {
  164. return Err(Error::PropertyWrongLen)
  165. }
  166. // Test if it works
  167. let _ = prop.get_f32(0)?;
  168. Ok(Self { prop, role })
  169. }
  170. pub fn get(&self) -> Dimension {
  171. [self.prop.get_f32(0).unwrap(), self.prop.get_f32(1).unwrap()].into()
  172. }
  173. pub fn set(&self, dim: Dimension) {
  174. self.prop.set_f32(self.role, 0, dim.w).unwrap();
  175. self.prop.set_f32(self.role, 1, dim.h).unwrap();
  176. }
  177. pub fn prop(&self) -> PropertyPtr {
  178. self.prop.clone()
  179. }
  180. }
  181. #[derive(Clone)]
  182. pub struct PropertyPoint {
  183. prop: PropertyPtr,
  184. role: Role,
  185. }
  186. impl PropertyPoint {
  187. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str) -> Result<Self> {
  188. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  189. if !prop.is_bounded() || prop.get_len() != 2 {
  190. return Err(Error::PropertyWrongLen)
  191. }
  192. // Test if it works
  193. let _ = prop.get_f32(0)?;
  194. Ok(Self { prop, role })
  195. }
  196. pub fn get(&self) -> Point {
  197. [self.prop.get_f32(0).unwrap(), self.prop.get_f32(1).unwrap()].into()
  198. }
  199. pub fn set(&self, pos: Point) {
  200. self.prop.set_f32(self.role, 0, pos.x).unwrap();
  201. self.prop.set_f32(self.role, 1, pos.y).unwrap();
  202. }
  203. pub fn prop(&self) -> PropertyPtr {
  204. self.prop.clone()
  205. }
  206. }
  207. #[derive(Clone)]
  208. pub struct PropertyRect {
  209. prop: PropertyPtr,
  210. role: Role,
  211. }
  212. impl PropertyRect {
  213. pub fn wrap(node: &SceneNode3, role: Role, prop_name: &str) -> Result<Self> {
  214. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  215. if !prop.is_bounded() || prop.get_len() != 4 {
  216. return Err(Error::PropertyWrongLen)
  217. }
  218. // Test if it works
  219. let _ = prop.get_f32(0)?;
  220. Ok(Self { prop, role })
  221. }
  222. pub fn eval(&self, parent_rect: &Rectangle) -> Result<()> {
  223. for i in 0..4 {
  224. if !self.prop.is_expr(i)? {
  225. continue
  226. }
  227. let expr = self.prop.get_expr(i).unwrap();
  228. let machine = SExprMachine {
  229. globals: vec![
  230. ("w".to_string(), SExprVal::Float32(parent_rect.w)),
  231. ("h".to_string(), SExprVal::Float32(parent_rect.h)),
  232. ],
  233. stmts: &expr,
  234. };
  235. let v = machine.call()?.as_f32()?;
  236. self.prop.set_cache_f32(self.role, i, v).unwrap();
  237. }
  238. Ok(())
  239. }
  240. pub fn get(&self) -> Rectangle {
  241. Rectangle::from_array([
  242. self.prop.get_f32(0).unwrap(),
  243. self.prop.get_f32(1).unwrap(),
  244. self.prop.get_f32(2).unwrap(),
  245. self.prop.get_f32(3).unwrap(),
  246. ])
  247. }
  248. pub fn get_opt(&self) -> Option<Rectangle> {
  249. Some(Rectangle::from_array([
  250. self.prop.get_f32(0).ok()?,
  251. self.prop.get_f32(1).ok()?,
  252. self.prop.get_f32(2).ok()?,
  253. self.prop.get_f32(3).ok()?,
  254. ]))
  255. }
  256. pub fn set(&self, rect: &Rectangle) {
  257. self.prop.set_f32(self.role, 0, rect.x).unwrap();
  258. self.prop.set_f32(self.role, 1, rect.y).unwrap();
  259. self.prop.set_f32(self.role, 2, rect.y).unwrap();
  260. self.prop.set_f32(self.role, 3, rect.y).unwrap();
  261. }
  262. pub fn prop(&self) -> PropertyPtr {
  263. self.prop.clone()
  264. }
  265. }