types.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use std::fmt::{Debug, Display};
  2. use binrw::{binrw, BinRead, BinWrite};
  3. use diff::{Diff, VecDiff};
  4. use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
  5. /// Generic field with structure of length + data
  6. pub type Field = ArrayOfPrimitive<u8>;
  7. pub type U32 = FieldOf<u32>;
  8. pub type U16 = FieldOf<u16>;
  9. pub type F64 = FieldOf<f64>;
  10. #[binrw]
  11. #[derive(Clone, Default, PartialEq)]
  12. pub struct WString {
  13. // Temporary variable that holds number of elements
  14. #[br(temp)]
  15. #[bw(try_calc(u32::try_from(value.len() * core::mem::size_of::<u16>())))]
  16. size: u32,
  17. #[br(count = size / core::mem::size_of::<u16>() as u32)]
  18. pub value: Vec<u16>,
  19. }
  20. impl From<&WString> for String {
  21. fn from(value: &WString) -> Self {
  22. String::from_utf16_lossy(&value.value)
  23. }
  24. }
  25. impl From<String> for WString {
  26. fn from(value: String) -> Self {
  27. let value: Vec<u16> = value.encode_utf16().collect();
  28. Self { value }
  29. }
  30. }
  31. // Custom Diff implementation to only diff internal value
  32. impl Diff for WString {
  33. type Repr = VecDiff<u16>;
  34. fn diff(&self, other: &Self) -> Self::Repr {
  35. self.value.diff(&other.value)
  36. }
  37. fn apply(&mut self, diff: &Self::Repr) {
  38. self.value.apply(diff)
  39. }
  40. fn identity() -> Self {
  41. Self { value: vec![] }
  42. }
  43. }
  44. impl Debug for WString {
  45. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  46. write!(f, "\"{}\"", String::from(self))
  47. }
  48. }
  49. impl Display for WString {
  50. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  51. write!(f, "{}", String::from(self))
  52. }
  53. }
  54. #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
  55. #[diff(attr(
  56. #[derive(Debug, PartialEq)]
  57. ))]
  58. pub struct Rgba {
  59. pub red: u8,
  60. pub green: u8,
  61. pub blue: u8,
  62. pub alpha: u8,
  63. }
  64. impl From<(u8, u8, u8)> for Rgba {
  65. fn from(value: (u8, u8, u8)) -> Self {
  66. Self {
  67. red: value.0,
  68. green: value.1,
  69. blue: value.2,
  70. alpha: 0,
  71. }
  72. }
  73. }
  74. #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
  75. #[diff(attr(
  76. #[derive(Debug, PartialEq)]
  77. ))]
  78. pub struct Point {
  79. pub x: f64,
  80. pub y: f64,
  81. }
  82. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  83. #[diff(attr(
  84. #[derive(Debug, PartialEq)]
  85. ))]
  86. #[brw(repr(u32))]
  87. #[repr(u32)]
  88. pub enum WobbleType {
  89. Spiral = 0,
  90. Sinusoidal = 1,
  91. Ellipse = 2,
  92. Vert8 = 3,
  93. Hori8 = 4,
  94. Unknown = 5,
  95. }
  96. impl Default for WobbleType {
  97. fn default() -> Self {
  98. WobbleType::Unknown
  99. }
  100. }
  101. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  102. #[diff(attr(
  103. #[derive(Debug, PartialEq)]
  104. ))]
  105. #[brw(repr(u16))]
  106. #[repr(u16)]
  107. pub enum ObjectType {
  108. Unknown = 0,
  109. Curve = 1,
  110. Point = 2,
  111. Rectangle = 3,
  112. Circle = 4,
  113. Ellipse = 5,
  114. Polygon = 6,
  115. Hatch = 32,
  116. }
  117. impl Default for ObjectType {
  118. fn default() -> Self {
  119. ObjectType::Unknown
  120. }
  121. }