types.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #[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 Add for Coordinate {
  133. type Output = Coordinate;
  134. fn add(self, rhs: Self) -> Self::Output {
  135. Coordinate {
  136. x: self.x + rhs.x,
  137. y: self.y + rhs.y,
  138. }
  139. }
  140. }
  141. impl AddAssign for Coordinate {
  142. fn add_assign(&mut self, rhs: Self) {
  143. self.x += rhs.x;
  144. self.y += rhs.y;
  145. }
  146. }
  147. impl From<(f64, f64)> for Coordinate {
  148. fn from(value: (f64, f64)) -> Self {
  149. Self {
  150. x: value.0,
  151. y: value.1,
  152. }
  153. }
  154. }
  155. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  156. #[diff(attr(
  157. #[derive(Debug, PartialEq)]
  158. ))]
  159. #[brw(repr(u32))]
  160. #[repr(u32)]
  161. pub enum WobbleType {
  162. Spiral = 0,
  163. Sinusoidal = 1,
  164. Ellipse = 2,
  165. Vert8 = 3,
  166. Hori8 = 4,
  167. Unknown = 5,
  168. }
  169. impl Default for WobbleType {
  170. fn default() -> Self {
  171. WobbleType::Unknown
  172. }
  173. }
  174. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  175. #[diff(attr(
  176. #[derive(Debug, PartialEq)]
  177. ))]
  178. #[brw(repr(u16))]
  179. #[repr(u16)]
  180. pub enum ObjectType {
  181. Unknown = 0,
  182. Curve = 1,
  183. Point = 2,
  184. Rectangle = 3,
  185. Circle = 4,
  186. Ellipse = 5,
  187. Polygon = 6,
  188. HatchLine = 16,
  189. Hatch = 32,
  190. }
  191. impl Default for ObjectType {
  192. fn default() -> Self {
  193. ObjectType::Unknown
  194. }
  195. }