types.rs 5.8 KB

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