types.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 num_enum::{IntoPrimitive, TryFromPrimitive};
  8. use rand::{thread_rng, Rng};
  9. use serde::{Deserialize, Serialize};
  10. use serde_repr::{Deserialize_repr, Serialize_repr};
  11. use strum::EnumIter;
  12. use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
  13. /// Generic field with structure of length + data
  14. pub type Field = ArrayOfPrimitive<u8>;
  15. pub type U16 = FieldOf<u16>;
  16. pub type I16 = FieldOf<i16>;
  17. pub type U32 = FieldOf<u32>;
  18. pub type I32 = FieldOf<i32>;
  19. pub type F64 = FieldOf<f64>;
  20. pub type Bool = FieldOf<u32>;
  21. impl From<FieldOf<u32>> for bool {
  22. fn from(value: FieldOf<u32>) -> Self {
  23. value.value != 0
  24. }
  25. }
  26. impl From<bool> for FieldOf<u32> {
  27. fn from(value: bool) -> Self {
  28. Self {
  29. value: match value {
  30. true => 1,
  31. false => 0,
  32. },
  33. }
  34. }
  35. }
  36. #[binrw]
  37. #[derive(Clone, Default, PartialEq)]
  38. pub struct WString {
  39. // Temporary variable that holds number of elements
  40. #[br(temp)]
  41. #[bw(try_calc(u32::try_from(value.len() * core::mem::size_of::<u16>())))]
  42. size: u32,
  43. #[br(count = size / core::mem::size_of::<u16>() as u32)]
  44. pub value: Vec<u16>,
  45. }
  46. impl From<&WString> for String {
  47. fn from(value: &WString) -> Self {
  48. String::from_utf16_lossy(&value.value)
  49. }
  50. }
  51. impl From<String> for WString {
  52. fn from(value: String) -> Self {
  53. let value: Vec<u16> = value.encode_utf16().collect();
  54. Self { value }
  55. }
  56. }
  57. // Custom Diff implementation to only diff internal value
  58. impl Diff for WString {
  59. type Repr = VecDiff<u16>;
  60. fn diff(&self, other: &Self) -> Self::Repr {
  61. self.value.diff(&other.value)
  62. }
  63. fn apply(&mut self, diff: &Self::Repr) {
  64. self.value.apply(diff)
  65. }
  66. fn identity() -> Self {
  67. Self { value: vec![] }
  68. }
  69. }
  70. impl Debug for WString {
  71. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  72. write!(f, "\"{}\"", String::from(self))
  73. }
  74. }
  75. impl Display for WString {
  76. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  77. write!(f, "{}", String::from(self))
  78. }
  79. }
  80. #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
  81. #[diff(attr(
  82. #[derive(Debug, PartialEq)]
  83. ))]
  84. pub struct Rgba {
  85. pub red: u8,
  86. pub green: u8,
  87. pub blue: u8,
  88. pub alpha: u8,
  89. }
  90. impl From<(u8, u8, u8)> for Rgba {
  91. fn from(value: (u8, u8, u8)) -> Self {
  92. Self {
  93. red: value.0,
  94. green: value.1,
  95. blue: value.2,
  96. alpha: 0,
  97. }
  98. }
  99. }
  100. impl Rgba {
  101. pub fn random() -> Self {
  102. Self {
  103. red: thread_rng().gen(),
  104. green: thread_rng().gen(),
  105. blue: thread_rng().gen(),
  106. alpha: 0,
  107. }
  108. }
  109. }
  110. #[derive(
  111. Copy, Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite, Serialize, Deserialize,
  112. )]
  113. #[diff(attr(
  114. #[derive(Debug, PartialEq)]
  115. ))]
  116. #[serde(rename_all = "PascalCase")]
  117. pub struct Coordinate {
  118. pub x: f64,
  119. pub y: f64,
  120. }
  121. impl Display for Coordinate {
  122. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  123. write!(f, "({:.2}, {:.2})", self.x, self.y)
  124. }
  125. }
  126. impl Sub for Coordinate {
  127. type Output = Coordinate;
  128. fn sub(self, rhs: Self) -> Self::Output {
  129. Coordinate {
  130. x: self.x - rhs.x,
  131. y: self.y - rhs.y,
  132. }
  133. }
  134. }
  135. impl SubAssign for Coordinate {
  136. fn sub_assign(&mut self, rhs: Self) {
  137. self.x -= rhs.x;
  138. self.y -= rhs.y;
  139. }
  140. }
  141. impl Add for Coordinate {
  142. type Output = Coordinate;
  143. fn add(self, rhs: Self) -> Self::Output {
  144. Coordinate {
  145. x: self.x + rhs.x,
  146. y: self.y + rhs.y,
  147. }
  148. }
  149. }
  150. impl AddAssign for Coordinate {
  151. fn add_assign(&mut self, rhs: Self) {
  152. self.x += rhs.x;
  153. self.y += rhs.y;
  154. }
  155. }
  156. impl Mul for Coordinate {
  157. type Output = Coordinate;
  158. fn mul(self, rhs: Self) -> Self::Output {
  159. Coordinate {
  160. x: self.x * rhs.x,
  161. y: self.y * rhs.y,
  162. }
  163. }
  164. }
  165. impl Div for Coordinate {
  166. type Output = Coordinate;
  167. fn div(self, rhs: Self) -> Self::Output {
  168. Coordinate {
  169. x: self.x / rhs.x,
  170. y: self.y / rhs.y,
  171. }
  172. }
  173. }
  174. impl From<(f64, f64)> for Coordinate {
  175. fn from(value: (f64, f64)) -> Self {
  176. Self {
  177. x: value.0,
  178. y: value.1,
  179. }
  180. }
  181. }
  182. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  183. #[diff(attr(
  184. #[derive(Debug, PartialEq)]
  185. ))]
  186. #[brw(repr(u32))]
  187. #[repr(u32)]
  188. pub enum WobbleType {
  189. Spiral = 0,
  190. Sinusoidal = 1,
  191. Ellipse = 2,
  192. Vert8 = 3,
  193. Hori8 = 4,
  194. Unknown = 5,
  195. }
  196. impl Default for WobbleType {
  197. fn default() -> Self {
  198. WobbleType::Unknown
  199. }
  200. }
  201. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  202. #[diff(attr(
  203. #[derive(Debug, PartialEq)]
  204. ))]
  205. #[brw(repr(u16))]
  206. #[repr(u16)]
  207. pub enum ObjectType {
  208. Unknown = 0,
  209. Curve = 1,
  210. Point = 2,
  211. Rectangle = 3,
  212. Circle = 4,
  213. Ellipse = 5,
  214. Polygon = 6,
  215. HatchLine = 16,
  216. Hatch = 32,
  217. }
  218. impl Default for ObjectType {
  219. fn default() -> Self {
  220. ObjectType::Unknown
  221. }
  222. }
  223. #[derive(
  224. Copy,
  225. Clone,
  226. Debug,
  227. Diff,
  228. PartialEq,
  229. strum::Display,
  230. Serialize_repr,
  231. Deserialize_repr,
  232. EnumIter,
  233. IntoPrimitive,
  234. TryFromPrimitive,
  235. )]
  236. #[diff(attr(
  237. #[derive(Debug, PartialEq)]
  238. ))]
  239. #[repr(u32)]
  240. pub enum PulseWidth {
  241. Ns2 = 2,
  242. Ns4 = 4,
  243. Ns6 = 6,
  244. Ns8 = 8,
  245. Ns12 = 12,
  246. Ns20 = 20,
  247. Ns30 = 30,
  248. Ns45 = 45,
  249. Ns60 = 60,
  250. Ns80 = 80,
  251. Ns100 = 100,
  252. Ns150 = 150,
  253. Ns200 = 200,
  254. Ns250 = 250,
  255. Ns350 = 350,
  256. Ns500 = 500,
  257. }