types.rs 7.3 KB

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