types.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. use std::{
  2. fmt::{Debug, Display},
  3. ops::{Add, AddAssign, Div, Mul, Sub, SubAssign},
  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. #[serde(rename_all = "PascalCase")]
  114. pub struct Coordinate {
  115. pub x: f64,
  116. pub y: f64,
  117. }
  118. impl Display for Coordinate {
  119. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  120. write!(f, "({}, {})", self.x, self.y)
  121. }
  122. }
  123. impl Sub for Coordinate {
  124. type Output = Coordinate;
  125. fn sub(self, rhs: Self) -> Self::Output {
  126. Coordinate {
  127. x: self.x - rhs.x,
  128. y: self.y - rhs.y,
  129. }
  130. }
  131. }
  132. impl SubAssign for Coordinate {
  133. fn sub_assign(&mut self, rhs: Self) {
  134. self.x -= rhs.x;
  135. self.y -= rhs.y;
  136. }
  137. }
  138. impl Add for Coordinate {
  139. type Output = Coordinate;
  140. fn add(self, rhs: Self) -> Self::Output {
  141. Coordinate {
  142. x: self.x + rhs.x,
  143. y: self.y + rhs.y,
  144. }
  145. }
  146. }
  147. impl AddAssign for Coordinate {
  148. fn add_assign(&mut self, rhs: Self) {
  149. self.x += rhs.x;
  150. self.y += rhs.y;
  151. }
  152. }
  153. impl Mul for Coordinate {
  154. type Output = Coordinate;
  155. fn mul(self, rhs: Self) -> Self::Output {
  156. Coordinate {
  157. x: self.x * rhs.x,
  158. y: self.y * rhs.y,
  159. }
  160. }
  161. }
  162. impl Div for Coordinate {
  163. type Output = Coordinate;
  164. fn div(self, rhs: Self) -> Self::Output {
  165. Coordinate {
  166. x: self.x / rhs.x,
  167. y: self.y / rhs.y,
  168. }
  169. }
  170. }
  171. impl From<(f64, f64)> for Coordinate {
  172. fn from(value: (f64, f64)) -> Self {
  173. Self {
  174. x: value.0,
  175. y: value.1,
  176. }
  177. }
  178. }
  179. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  180. #[diff(attr(
  181. #[derive(Debug, PartialEq)]
  182. ))]
  183. #[brw(repr(u32))]
  184. #[repr(u32)]
  185. pub enum WobbleType {
  186. Spiral = 0,
  187. Sinusoidal = 1,
  188. Ellipse = 2,
  189. Vert8 = 3,
  190. Hori8 = 4,
  191. Unknown = 5,
  192. }
  193. impl Default for WobbleType {
  194. fn default() -> Self {
  195. WobbleType::Unknown
  196. }
  197. }
  198. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  199. #[diff(attr(
  200. #[derive(Debug, PartialEq)]
  201. ))]
  202. #[brw(repr(u16))]
  203. #[repr(u16)]
  204. pub enum ObjectType {
  205. Unknown = 0,
  206. Curve = 1,
  207. Point = 2,
  208. Rectangle = 3,
  209. Circle = 4,
  210. Ellipse = 5,
  211. Polygon = 6,
  212. HatchLine = 16,
  213. Hatch = 32,
  214. }
  215. impl Default for ObjectType {
  216. fn default() -> Self {
  217. ObjectType::Unknown
  218. }
  219. }