types.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. use std::{
  2. fmt::{Debug, Display},
  3. ops::{Add, AddAssign, Sub},
  4. };
  5. use binrw::{binrw, BinRead, BinWrite};
  6. use diff::{Diff, VecDiff};
  7. use rand::{thread_rng, Rng};
  8. use serde::{Deserialize, Serialize};
  9. use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
  10. /// Generic field with structure of length + data
  11. pub type Field = ArrayOfPrimitive<u8>;
  12. pub type U16 = FieldOf<u16>;
  13. pub type I16 = FieldOf<i16>;
  14. pub type U32 = FieldOf<u32>;
  15. pub type I32 = FieldOf<i32>;
  16. pub type F64 = FieldOf<f64>;
  17. pub type Bool = FieldOf<u32>;
  18. impl From<FieldOf<u32>> for bool {
  19. fn from(value: FieldOf<u32>) -> Self {
  20. value.value != 0
  21. }
  22. }
  23. impl From<bool> for FieldOf<u32> {
  24. fn from(value: bool) -> Self {
  25. Self {
  26. value: match value {
  27. true => 1,
  28. false => 0,
  29. },
  30. }
  31. }
  32. }
  33. #[binrw]
  34. #[derive(Clone, Default, PartialEq)]
  35. pub struct WString {
  36. // Temporary variable that holds number of elements
  37. #[br(temp)]
  38. #[bw(try_calc(u32::try_from(value.len() * core::mem::size_of::<u16>())))]
  39. size: u32,
  40. #[br(count = size / core::mem::size_of::<u16>() as u32)]
  41. pub value: Vec<u16>,
  42. }
  43. impl From<&WString> for String {
  44. fn from(value: &WString) -> Self {
  45. String::from_utf16_lossy(&value.value)
  46. }
  47. }
  48. impl From<String> for WString {
  49. fn from(value: String) -> Self {
  50. let value: Vec<u16> = value.encode_utf16().collect();
  51. Self { value }
  52. }
  53. }
  54. // Custom Diff implementation to only diff internal value
  55. impl Diff for WString {
  56. type Repr = VecDiff<u16>;
  57. fn diff(&self, other: &Self) -> Self::Repr {
  58. self.value.diff(&other.value)
  59. }
  60. fn apply(&mut self, diff: &Self::Repr) {
  61. self.value.apply(diff)
  62. }
  63. fn identity() -> Self {
  64. Self { value: vec![] }
  65. }
  66. }
  67. impl Debug for WString {
  68. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  69. write!(f, "\"{}\"", String::from(self))
  70. }
  71. }
  72. impl Display for WString {
  73. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  74. write!(f, "{}", String::from(self))
  75. }
  76. }
  77. #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
  78. #[diff(attr(
  79. #[derive(Debug, PartialEq)]
  80. ))]
  81. pub struct Rgba {
  82. pub red: u8,
  83. pub green: u8,
  84. pub blue: u8,
  85. pub alpha: u8,
  86. }
  87. impl From<(u8, u8, u8)> for Rgba {
  88. fn from(value: (u8, u8, u8)) -> Self {
  89. Self {
  90. red: value.0,
  91. green: value.1,
  92. blue: value.2,
  93. alpha: 0,
  94. }
  95. }
  96. }
  97. impl Rgba {
  98. pub fn random() -> Self {
  99. Self {
  100. red: thread_rng().gen_range(0..=255),
  101. green: thread_rng().gen_range(0..=255),
  102. blue: thread_rng().gen_range(0..=255),
  103. alpha: 0,
  104. }
  105. }
  106. }
  107. #[derive(
  108. Copy, Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite, Serialize, Deserialize,
  109. )]
  110. #[diff(attr(
  111. #[derive(Debug, PartialEq)]
  112. ))]
  113. pub struct Point {
  114. pub x: f64,
  115. pub y: f64,
  116. }
  117. impl Display for Point {
  118. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  119. write!(f, "({}, {})", self.x, self.y)
  120. }
  121. }
  122. impl Sub for Point {
  123. type Output = Point;
  124. fn sub(self, rhs: Self) -> Self::Output {
  125. Point {
  126. x: self.x - rhs.x,
  127. y: self.y - rhs.y,
  128. }
  129. }
  130. }
  131. impl Add for Point {
  132. type Output = Point;
  133. fn add(self, rhs: Self) -> Self::Output {
  134. Point {
  135. x: self.x + rhs.x,
  136. y: self.y + rhs.y,
  137. }
  138. }
  139. }
  140. impl AddAssign for Point {
  141. fn add_assign(&mut self, rhs: Self) {
  142. self.x += rhs.x;
  143. self.y += rhs.y;
  144. }
  145. }
  146. impl From<(f64, f64)> for Point {
  147. fn from(value: (f64, f64)) -> Self {
  148. Self {
  149. x: value.0,
  150. y: value.1,
  151. }
  152. }
  153. }
  154. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  155. #[diff(attr(
  156. #[derive(Debug, PartialEq)]
  157. ))]
  158. #[brw(repr(u32))]
  159. #[repr(u32)]
  160. pub enum WobbleType {
  161. Spiral = 0,
  162. Sinusoidal = 1,
  163. Ellipse = 2,
  164. Vert8 = 3,
  165. Hori8 = 4,
  166. Unknown = 5,
  167. }
  168. impl Default for WobbleType {
  169. fn default() -> Self {
  170. WobbleType::Unknown
  171. }
  172. }
  173. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  174. #[diff(attr(
  175. #[derive(Debug, PartialEq)]
  176. ))]
  177. #[brw(repr(u16))]
  178. #[repr(u16)]
  179. pub enum ObjectType {
  180. Unknown = 0,
  181. Curve = 1,
  182. Point = 2,
  183. Rectangle = 3,
  184. Circle = 4,
  185. Ellipse = 5,
  186. Polygon = 6,
  187. HatchLine = 16,
  188. Hatch = 32,
  189. }
  190. impl Default for ObjectType {
  191. fn default() -> Self {
  192. ObjectType::Unknown
  193. }
  194. }