1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use alloc::string::String;

#[allow(unused_imports)]
use self::super::root;

use core::ops::{Deref, DerefMut};

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResVec2 {
    pub x: f32,
    pub y: f32,
}

impl ResVec2 {
    pub fn default() -> ResVec2 {
        ResVec2 { x: 0.0, y: 0.0 }
    }

    pub fn new(x: f32, y: f32) -> ResVec2 {
        ResVec2 { x, y }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResVec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl ResVec3 {
    pub fn default() -> ResVec3 {
        ResVec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        }
    }

    pub fn new(x: f32, y: f32, z: f32) -> ResVec3 {
        ResVec3 { x, y, z }
    }
}

// Maybe needs a vtable.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResColor {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResPane {
    pub block_header_kind: u32,
    pub block_header_size: u32,
    pub flag: u8,
    pub base_position: u8,
    pub alpha: u8,
    pub flag_ex: u8,
    pub name: [libc::c_char; 24],
    pub user_data: [libc::c_char; 8],
    pub pos: ResVec3,
    pub rot_x: f32,
    pub rot_y: f32,
    pub rot_z: f32,
    pub scale_x: f32,
    pub scale_y: f32,
    pub size_x: f32,
    pub size_y: f32,
}

impl ResPane {
    // For null pane
    pub fn new(name: &str) -> ResPane {
        let mut pane = ResPane {
            block_header_kind: u32::from_le_bytes([b'p', b'a', b'n', b'1']),
            block_header_size: 84,
            /// Visible | InfluencedAlpha
            flag: 0x3,
            base_position: 0,
            alpha: 0xFF,
            flag_ex: 0,
            name: [0; 24],
            user_data: [0; 8],
            pos: ResVec3 {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            rot_x: 0.0,
            rot_y: 0.0,
            rot_z: 0.0,
            scale_x: 1.0,
            scale_y: 1.0,
            size_x: 30.0,
            size_y: 40.0,
        };
        pane.set_name(name);
        pane
    }

    pub fn set_name(&mut self, name: &str) {
        assert!(
            name.len() <= 24,
            "Name of pane must be at most 24 characters"
        );
        unsafe {
            core::ptr::copy_nonoverlapping(name.as_ptr(), self.name.as_mut_ptr(), name.len());
        }
    }

    pub fn set_pos(&mut self, pos: ResVec3) {
        self.pos = pos;
    }

    pub fn set_size(&mut self, size: ResVec2) {
        self.size_x = size.x;
        self.size_y = size.y;
    }

    pub fn get_name(&self) -> String {
        self.name
            .iter()
            .take_while(|b| **b != 0)
            .map(|b| *b as char)
            .collect::<String>()
    }

    pub fn name_matches(&self, other: &str) -> bool {
        self.get_name() == other
    }
}

#[repr(C)]
#[derive(Debug, PartialEq)]
enum TextBoxFlag {
    ShadowEnabled,
    ForceAssignTextLength,
    InvisibleBorderEnabled,
    DoubleDrawnBorderEnabled,
    PerCharacterTransformEnabled,
    CenterCeilingEnabled,
    LineWidthOffsetEnabled,
    ExtendedTagEnabled,
    PerCharacterTransformSplitByCharWidth,
    PerCharacterTransformAutoShadowAlpha,
    DrawFromRightToLeft,
    PerCharacterTransformOriginToCenter,
    KeepingFontScaleEnabled,
    PerCharacterTransformFixSpace,
    PerCharacterTransformSplitByCharWidthInsertSpaceEnabled,
    Max,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum TextAlignment {
    Synchronous,
    Left,
    Center,
    Right,
    MaxTextAlignment,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResTextBox {
    pub pane: ResPane,
    pub text_buf_bytes: u16,
    pub text_str_bytes: u16,
    pub material_idx: u16,
    pub font_idx: u16,
    pub text_position: u8,
    pub text_alignment: u8,
    pub text_box_flag: u16,
    pub italic_ratio: f32,
    pub text_str_offset: u32,
    pub text_cols: [ResColor; 2],
    pub font_size: ResVec2,
    pub char_space: f32,
    pub line_space: f32,
    pub text_id_offset: u32,
    pub shadow_offset: ResVec2,
    pub shadow_scale: ResVec2,
    pub shadow_cols: [ResColor; 2],
    pub shadow_italic_ratio: f32,
    pub line_width_offset_offset: u32,
    pub per_character_transform_offset: u32,
    /* Additional Info
        uint16_t           text[];                     // Text.
        char                textId[];                   // The text ID.
        u8 lineWidthOffsetCount; // The quantity of widths and offsets for each line.
        float lineOffset[]; // The offset for each line.
        float lineWidth[]; // The width of each line.
        ResPerCharacterTransform perCharacterTransform     // Information for per-character animation.
        ResAnimationInfo       perCharacterTransformAnimationInfo;     // Animation information for per-character animation.
    */
}

impl Deref for ResTextBox {
    type Target = ResPane;

    fn deref(&self) -> &Self::Target {
        &self.pane
    }
}

impl DerefMut for ResTextBox {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.pane
    }
}

impl ResTextBox {
    pub fn enable_shadow(&mut self) {
        self.text_box_flag |= 0x1 << TextBoxFlag::ShadowEnabled as u8;
    }

    pub fn text_alignment(&mut self, align: TextAlignment) {
        self.text_alignment = align as u8;
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResPicture {
    pub pane: ResPane,
    pub vtx_cols: [ResColor; 4],
    pub material_idx: u16,
    pub tex_coord_count: u8,
    pub flags: u8,
    /* Additional Info
        ResVec2 texCoords[texCoordCount][VERTEX_MAX];
        uint32_t shapeBinaryIndex;
    */
}

impl Deref for ResPicture {
    type Target = ResPane;

    fn deref(&self) -> &Self::Target {
        &self.pane
    }
}

impl DerefMut for ResPicture {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.pane
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResPictureWithTex<const TEX_COORD_COUNT: usize> {
    pub picture: ResPicture,
    pub tex_coords: [[ResVec2; TEX_COORD_COUNT]; 4],
}

impl<const TEX_COORD_COUNT: usize> Deref for ResPictureWithTex<TEX_COORD_COUNT> {
    type Target = ResPane;

    fn deref(&self) -> &Self::Target {
        &self.picture
    }
}

impl<const TEX_COORD_COUNT: usize> DerefMut for ResPictureWithTex<TEX_COORD_COUNT> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.picture
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResParts {
    pub pane: ResPane,
    pub property_count: u32,
    pub magnify: ResVec2,
}

impl Deref for ResParts {
    type Target = ResPane;

    fn deref(&self) -> &Self::Target {
        &self.pane
    }
}

impl DerefMut for ResParts {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.pane
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResPartsProperty {
    pub name: [libc::c_char; 24],
    pub usage_flag: u8,
    pub basic_usage_flag: u8,
    pub material_usage_flag: u8,
    pub system_ext_user_data_override_flag: u8,
    pub property_offset: u32,
    pub ext_user_data_offset: u32,
    pub pane_basic_info_offset: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResPartsWithProperty<const PROPERTY_COUNT: usize> {
    pub parts: ResParts,
    pub property_table: [ResPartsProperty; PROPERTY_COUNT],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindowInflation {
    pub left: i16,
    pub right: i16,
    pub top: i16,
    pub bottom: i16,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindowFrameSize {
    pub left: u16,
    pub right: u16,
    pub top: u16,
    pub bottom: u16,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindowContent {
    pub vtx_cols: [ResColor; 4],
    pub material_idx: u16,
    pub tex_coord_count: u8,
    pub padding: [u8; 1],
    /* Additional Info
        nn::util::Float2 texCoords[texCoordCount][VERTEX_MAX];
    */
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindowContentWithTexCoords<const TEX_COORD_COUNT: usize> {
    pub window_content: ResWindowContent,
    // This has to be wrong.
    // Should be [[ResVec2; TEX_COORD_COUNT]; 4]?
    pub tex_coords: [[ResVec3; TEX_COORD_COUNT]; 1],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindowFrame {
    pub material_idx: u16,
    pub texture_flip: u8,
    pub padding: [u8; 1],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindow {
    pub pane: ResPane,
    pub inflation: ResWindowInflation,
    pub frame_size: ResWindowFrameSize,
    pub frame_count: u8,
    pub window_flags: u8,
    pub padding: [u8; 2],
    pub content_offset: u32,
    pub frame_offset_table_offset: u32,
    pub content: ResWindowContent,
    /* Additional Info

        ResWindowContent content;

        detail::uint32_t frameOffsetTable[frameCount];
        ResWindowFrame frames;

    */
}

impl Deref for ResWindow {
    type Target = ResPane;

    fn deref(&self) -> &Self::Target {
        &self.pane
    }
}

impl DerefMut for ResWindow {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.pane
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ResWindowWithTexCoordsAndFrames<const TEX_COORD_COUNT: usize, const FRAME_COUNT: usize> {
    pub window: ResWindow,
    pub content: ResWindowContentWithTexCoords<TEX_COORD_COUNT>,
    pub frame_offset_table: [u32; FRAME_COUNT],
    pub frames: [ResWindowFrame; FRAME_COUNT],
}

impl<const TEX_COORD_COUNT: usize, const FRAME_COUNT: usize> Deref
    for ResWindowWithTexCoordsAndFrames<TEX_COORD_COUNT, FRAME_COUNT>
{
    type Target = ResPane;

    fn deref(&self) -> &Self::Target {
        &self.window
    }
}

impl<const TEX_COORD_COUNT: usize, const FRAME_COUNT: usize> DerefMut
    for ResWindowWithTexCoordsAndFrames<TEX_COORD_COUNT, FRAME_COUNT>
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.window
    }
}