wrap.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 std::sync::Arc;
  19. use super::{Property, PropertyPtr};
  20. use crate::{
  21. error::{Error, Result},
  22. scene::SceneNode,
  23. };
  24. pub struct PropertyBool {
  25. prop: PropertyPtr,
  26. idx: usize,
  27. }
  28. impl PropertyBool {
  29. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  30. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  31. // Test if it works
  32. let _ = prop.get_bool(idx)?;
  33. Ok(Self { prop, idx })
  34. }
  35. pub fn get(&self) -> bool {
  36. self.prop.get_bool(self.idx).unwrap()
  37. }
  38. pub fn set(&self, val: bool) {
  39. self.prop.set_bool(self.idx, val).unwrap()
  40. }
  41. pub fn prop(&self) -> PropertyPtr {
  42. self.prop.clone()
  43. }
  44. }
  45. pub struct PropertyUint32 {
  46. prop: PropertyPtr,
  47. idx: usize,
  48. }
  49. impl PropertyUint32 {
  50. pub fn from(prop: PropertyPtr, idx: usize) -> Result<Self> {
  51. // Test if it works
  52. let _ = prop.get_u32(idx)?;
  53. Ok(Self { prop, idx })
  54. }
  55. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  56. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  57. // Test if it works
  58. let _ = prop.get_u32(idx)?;
  59. Ok(Self { prop, idx })
  60. }
  61. pub fn get(&self) -> u32 {
  62. self.prop.get_u32(self.idx).unwrap()
  63. }
  64. pub fn set(&self, val: u32) {
  65. self.prop.set_u32(self.idx, val).unwrap()
  66. }
  67. pub fn prop(&self) -> PropertyPtr {
  68. self.prop.clone()
  69. }
  70. }
  71. pub struct PropertyFloat32 {
  72. prop: PropertyPtr,
  73. idx: usize,
  74. }
  75. impl PropertyFloat32 {
  76. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  77. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  78. // Test if it works
  79. let _ = prop.get_f32(idx)?;
  80. Ok(Self { prop, idx })
  81. }
  82. pub fn get(&self) -> f32 {
  83. self.prop.get_f32(self.idx).unwrap()
  84. }
  85. pub fn set(&self, val: f32) {
  86. self.prop.set_f32(self.idx, val).unwrap()
  87. }
  88. pub fn prop(&self) -> PropertyPtr {
  89. self.prop.clone()
  90. }
  91. }
  92. pub struct PropertyStr {
  93. prop: PropertyPtr,
  94. idx: usize,
  95. }
  96. impl PropertyStr {
  97. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  98. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  99. // Test if it works
  100. let _ = prop.get_str(idx)?;
  101. Ok(Self { prop, idx })
  102. }
  103. pub fn get(&self) -> String {
  104. self.prop.get_str(self.idx).unwrap()
  105. }
  106. pub fn set<S: Into<String>>(&self, val: S) {
  107. self.prop.set_str(self.idx, val.into()).unwrap()
  108. }
  109. pub fn prop(&self) -> PropertyPtr {
  110. self.prop.clone()
  111. }
  112. }
  113. pub struct PropertyColor {
  114. prop: PropertyPtr,
  115. }
  116. impl PropertyColor {
  117. pub fn wrap(node: &SceneNode, prop_name: &str) -> Result<Self> {
  118. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  119. if !prop.is_bounded() || prop.get_len() != 4 {
  120. return Err(Error::PropertyWrongLen)
  121. }
  122. // Test if it works
  123. let _ = prop.get_f32(0)?;
  124. Ok(Self { prop })
  125. }
  126. pub fn get(&self) -> [f32; 4] {
  127. [
  128. self.prop.get_f32(0).unwrap(),
  129. self.prop.get_f32(1).unwrap(),
  130. self.prop.get_f32(2).unwrap(),
  131. self.prop.get_f32(3).unwrap(),
  132. ]
  133. }
  134. pub fn set(&self, val: [f32; 4]) {
  135. self.prop.set_f32(0, val[0]).unwrap();
  136. self.prop.set_f32(1, val[1]).unwrap();
  137. self.prop.set_f32(2, val[2]).unwrap();
  138. self.prop.set_f32(3, val[3]).unwrap();
  139. }
  140. pub fn prop(&self) -> PropertyPtr {
  141. self.prop.clone()
  142. }
  143. }