Skip to main content

unbanished_formats/ui/
sprites.rs

1//! Sprite sheets and cursors: the named regions of a texture, and a cursor's image and hot spot.
2
3use super::*;
4
5/// A sprite of a sprite sheet.
6#[derive(Debug, Clone)]
7pub struct Sprite {
8    /// The sprite's name.
9    pub name: String,
10    /// The image file it came from.
11    pub source: String,
12    /// Offset of the trimmed image inside the original source image.
13    pub offset: [i32; 2],
14    /// The width in pixels.
15    pub width: i32,
16    /// The height in pixels.
17    pub height: i32,
18    /// Not identified yet.
19    pub unknown: i32,
20    /// u0, v0, u1, v1 in the sheet image.
21    pub uv: [f32; 4],
22}
23
24/// Original: `UI::SpriteSheet`, Runtime `0x18011ffc0`.
25#[derive(Debug, Clone)]
26pub struct SpriteSheet {
27    /// The material's path.
28    pub material_path: String,
29    /// The image file of the sheet.
30    pub source_image: String,
31    /// The sheet's width in pixels.
32    pub width: i32,
33    /// The sheet's height in pixels.
34    pub height: i32,
35    /// Not identified yet.
36    pub unknown_flag: bool,
37    /// The sprites.
38    pub sprites: Vec<Sprite>,
39    /// Material instance whose `diffuse` texture is the sheet image.
40    pub material: Ref,
41}
42
43/// Read a [`SpriteSheet`] from the bytes of its resource.
44pub fn sprite_sheet(data: &[u8]) -> Result<SpriteSheet> {
45    expect(data, "SpriteSheet", |r| {
46        r.block(|r, _| {
47            let material_path = r.string()?;
48            let source_image = r.string()?;
49            let width = r.i32()?;
50            let height = r.i32()?;
51            let unknown_flag = r.bool()?;
52            let sprites = (0..r.count()?)
53                .map(|_| {
54                    let name = r.string()?;
55                    let source = r.string()?;
56                    let offset = i32s(r)?;
57                    let width = r.i32()?;
58                    let height = r.i32()?;
59                    let unknown = r.i32()?;
60                    Ok(Sprite {
61                        name,
62                        source,
63                        offset,
64                        width,
65                        height,
66                        unknown,
67                        uv: r.f32s()?,
68                    })
69                })
70                .collect::<Result<_>>()?;
71            Ok(SpriteSheet {
72                material_path,
73                source_image,
74                width,
75                height,
76                unknown_flag,
77                sprites,
78                material: r.external_ref()?,
79            })
80        })
81    })
82}
83
84/// A mouse pointer: the sprite sheet whose image is the pointer, and its hot spot.
85///
86/// Original: `UI::Cursor::Serialize`, Runtime `0x1800cf980` (version 1).
87#[derive(Debug, Clone)]
88pub struct Cursor {
89    /// The sprite sheet whose image is the pointer.
90    pub sprite_sheet: Ref,
91    /// The point of the image that is the pointer's position, in pixels.
92    pub hotspot: [i32; 2],
93}
94
95/// Read a [`Cursor`] from the bytes of its resource.
96pub fn cursor(data: &[u8]) -> Result<Cursor> {
97    expect(data, "Cursor", |r| {
98        r.block(|r, _| {
99            Ok(Cursor {
100                sprite_sheet: r.external_ref()?,
101                hotspot: [r.i32()?, r.i32()?],
102            })
103        })
104    })
105}
106
107impl SpriteSheet {
108    /// A sprite by name.
109    pub fn sprite(&self, name: &str) -> Option<&Sprite> {
110        self.sprites.iter().find(|s| s.name == name)
111    }
112}