wrap.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use std::sync::Arc;
  2. use crate::{scene::SceneNode, error::{Error, Result}};
  3. use super::Property;
  4. pub struct PropertyBool {
  5. prop: Arc<Property>,
  6. idx: usize,
  7. }
  8. impl PropertyBool {
  9. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  10. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  11. // Test if it works
  12. let _ = prop.get_bool(idx)?;
  13. Ok(Self { prop, idx })
  14. }
  15. pub fn get(&self) -> bool {
  16. self.prop.get_bool(self.idx).unwrap()
  17. }
  18. pub fn set(&self, val: bool) {
  19. self.prop.set_bool(self.idx, val).unwrap()
  20. }
  21. }
  22. pub struct PropertyUint32 {
  23. prop: Arc<Property>,
  24. idx: usize,
  25. }
  26. impl PropertyUint32 {
  27. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  28. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  29. // Test if it works
  30. let _ = prop.get_u32(idx)?;
  31. Ok(Self { prop, idx })
  32. }
  33. pub fn get(&self) -> u32 {
  34. self.prop.get_u32(self.idx).unwrap()
  35. }
  36. pub fn set(&self, val: u32) {
  37. self.prop.set_u32(self.idx, val).unwrap()
  38. }
  39. }
  40. pub struct PropertyFloat32 {
  41. prop: Arc<Property>,
  42. idx: usize,
  43. }
  44. impl PropertyFloat32 {
  45. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  46. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  47. // Test if it works
  48. let _ = prop.get_f32(idx)?;
  49. Ok(Self { prop, idx })
  50. }
  51. pub fn get(&self) -> f32 {
  52. self.prop.get_f32(self.idx).unwrap()
  53. }
  54. pub fn set(&self, val: f32) {
  55. self.prop.set_f32(self.idx, val).unwrap()
  56. }
  57. }
  58. pub struct PropertyStr {
  59. prop: Arc<Property>,
  60. idx: usize,
  61. }
  62. impl PropertyStr {
  63. pub fn wrap(node: &SceneNode, prop_name: &str, idx: usize) -> Result<Self> {
  64. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  65. // Test if it works
  66. let _ = prop.get_str(idx)?;
  67. Ok(Self { prop, idx })
  68. }
  69. pub fn get(&self) -> String {
  70. self.prop.get_str(self.idx).unwrap()
  71. }
  72. pub fn set<S: Into<String>>(&self, val: S) {
  73. self.prop.set_str(self.idx, val.into()).unwrap()
  74. }
  75. }
  76. pub struct PropertyColor {
  77. prop: Arc<Property>,
  78. }
  79. impl PropertyColor {
  80. pub fn wrap(node: &SceneNode, prop_name: &str) -> Result<Self> {
  81. let prop = node.get_property(prop_name).ok_or(Error::PropertyNotFound)?;
  82. if !prop.is_bounded() || prop.get_len() != 4 {
  83. return Err(Error::PropertyWrongLen)
  84. }
  85. // Test if it works
  86. let _ = prop.get_f32(0)?;
  87. Ok(Self { prop })
  88. }
  89. pub fn get(&self) -> [f32; 4] {
  90. [self.prop.get_f32(0).unwrap(),
  91. self.prop.get_f32(1).unwrap(),
  92. self.prop.get_f32(2).unwrap(),
  93. self.prop.get_f32(3).unwrap()]
  94. }
  95. pub fn set(&self, val: [f32; 4]) {
  96. self.prop.set_f32(0, val[0]).unwrap();
  97. self.prop.set_f32(1, val[1]).unwrap();
  98. self.prop.set_f32(2, val[2]).unwrap();
  99. self.prop.set_f32(3, val[3]).unwrap();
  100. }
  101. }