123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- use std::fmt::{Debug, Display};
- use binrw::{binrw, BinRead, BinWrite};
- use diff::{Diff, VecDiff};
- use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
- /// Generic field with structure of length + data
- pub type Field = ArrayOfPrimitive<u8>;
- pub type U32 = FieldOf<u32>;
- pub type U16 = FieldOf<u16>;
- pub type F64 = FieldOf<f64>;
- #[binrw]
- #[derive(Clone, Default, PartialEq)]
- pub struct WString {
- // Temporary variable that holds number of elements
- #[br(temp)]
- #[bw(try_calc(u32::try_from(value.len() * core::mem::size_of::<u16>())))]
- size: u32,
- #[br(count = size / core::mem::size_of::<u16>() as u32)]
- pub value: Vec<u16>,
- }
- impl From<&WString> for String {
- fn from(value: &WString) -> Self {
- String::from_utf16_lossy(&value.value)
- }
- }
- impl From<String> for WString {
- fn from(value: String) -> Self {
- let value: Vec<u16> = value.encode_utf16().collect();
- Self { value }
- }
- }
- // Custom Diff implementation to only diff internal value
- impl Diff for WString {
- type Repr = VecDiff<u16>;
- fn diff(&self, other: &Self) -> Self::Repr {
- self.value.diff(&other.value)
- }
- fn apply(&mut self, diff: &Self::Repr) {
- self.value.apply(diff)
- }
- fn identity() -> Self {
- Self { value: vec![] }
- }
- }
- impl Debug for WString {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "\"{}\"", String::from(self))
- }
- }
- impl Display for WString {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", String::from(self))
- }
- }
- #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
- #[diff(attr(
- #[derive(Debug, PartialEq)]
- ))]
- pub struct Rgba {
- pub red: u8,
- pub green: u8,
- pub blue: u8,
- pub alpha: u8,
- }
- impl From<(u8, u8, u8)> for Rgba {
- fn from(value: (u8, u8, u8)) -> Self {
- Self {
- red: value.0,
- green: value.1,
- blue: value.2,
- alpha: 0,
- }
- }
- }
- #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
- #[diff(attr(
- #[derive(Debug, PartialEq)]
- ))]
- pub struct Point {
- pub x: f64,
- pub y: f64,
- }
- #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
- #[diff(attr(
- #[derive(Debug, PartialEq)]
- ))]
- #[brw(repr(u32))]
- #[repr(u32)]
- pub enum WobbleType {
- Spiral = 0,
- Sinusoidal = 1,
- Ellipse = 2,
- Vert8 = 3,
- Hori8 = 4,
- Unknown = 5,
- }
- impl Default for WobbleType {
- fn default() -> Self {
- WobbleType::Unknown
- }
- }
- #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
- #[diff(attr(
- #[derive(Debug, PartialEq)]
- ))]
- #[brw(repr(u16))]
- #[repr(u16)]
- pub enum ObjectType {
- Unknown = 0,
- Curve = 1,
- Point = 2,
- Rectangle = 3,
- Circle = 4,
- Ellipse = 5,
- Polygon = 6,
- Hatch = 32,
- }
- impl Default for ObjectType {
- fn default() -> Self {
- ObjectType::Unknown
- }
- }
|