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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#[allow(unused_imports)]
use self::super::root;

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

use alloc::string::String;

pub mod resources;
pub use resources::*;

#[repr(C)]
#[derive(Debug)]
pub struct ResAnimationContent {
    pub name: [libc::c_char; 28],
    pub count: u8,
    pub anim_content_type: u8,
    padding: [libc::c_char; 2],
}

/**
 * Block Header Kind
 *
 * ANIM_TAG: pat1
 * ANIM_SHARE: pah1
 * ANIM_INFO: pai1
 */

#[repr(C)]
#[derive(Debug)]
pub struct ResAnimationBlock {
    pub block_header_kind: u32,
    pub block_header_size: u32,
    pub num_frames: u16,
    pub is_loop: bool,
    pad: [libc::c_char; 1],
    pub file_count: u16,
    pub anim_cont_count: u16,
    pub anim_cont_offsets_offset: u32,
}

#[repr(C)]
pub struct AnimTransform {
    pub res_animation_block: *mut ResAnimationBlock,
    pub frame: f32,
    pub enabled: bool,
}

#[repr(C)]
#[derive(Debug)]
pub struct AnimTransformNode {
    pub prev: *mut AnimTransformNode,
    pub next: *mut AnimTransformNode,
}

#[repr(C)]
pub struct AnimTransformList {
    pub root: AnimTransformNode,
}

#[repr(C, align(8))]
#[derive(Debug, Copy, Clone)]
pub struct Pane {
    pub vtable: u64,
    pub link: PaneNode,
    pub parent: *mut Pane,
    pub children_list: PaneNode,
    pub pos_x: f32,
    pub pos_y: f32,
    pub pos_z: f32,
    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,
    pub flags: u8,
    pub alpha: u8,
    pub global_alpha: u8,
    pub base_position: u8,
    pub flag_ex: u8,
    // This is supposed to be 3 bytes padding + flags of 4 bytes + padding of 4 bytes
    pad: [u8; 3 + 4 + 4 + 8],
    pub global_matrix: [[f32; 3]; 4],
    pub user_matrix: *const u64,
    pub ext_user_data_list: *const u64,
    pub name: [libc::c_char; 25],
    pub user_data: [libc::c_char; 9],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum PaneFlag {
    Visible,
    InfluencedAlpha,
    LocationAdjust,
    UserAllocated,
    IsGlobalMatrixDirty,
    UserMatrix,
    UserGlobalMatrix,
    IsConstantBufferReady,
    Max,
}

impl Pane {
    pub unsafe fn as_parts(&mut self) -> &mut Parts {
        &mut *(self as *mut Pane as *mut Parts)
    }

    pub unsafe fn as_picture(&mut self) -> &mut Picture {
        &mut *(self as *mut Pane as *mut Picture)
    }

    pub unsafe fn as_textbox(&mut self) -> &mut TextBox {
        &mut *(self as *mut Pane as *mut TextBox)
    }

    pub unsafe fn as_window(&mut self) -> &mut Window {
        &mut *(self as *mut Pane as *mut Window)
    }

    pub unsafe fn set_visible(&mut self, visible: bool) {
        match visible {
            true => self.flags |= 1 << PaneFlag::Visible as u8,
            false => self.flags &= !(1 << PaneFlag::Visible as u8),
        }
    }

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

#[repr(C)]
#[derive(Debug)]
pub struct Parts {
    pub pane: Pane,
    // Some IntrusiveList
    pub link: PaneNode,
    pub layout: *mut Layout,
}

impl Deref for Parts {
    type Target = Pane;

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

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

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Picture {
    pub pane: Pane,
    pub material: *mut Material,
    pub vertex_colors: [[u8; 4]; 4],
    pub shared_memory: *mut u8,
}

impl Deref for Picture {
    type Target = Pane;

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

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

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum TextBoxFlag {
    // TODO: Check if Horizontal is first or Vertical is
    TextAlignmentHorizontal,
    TextAlignmentVertical,
    IsPTDirty,
    ShadowEnabled,
    InvisibleBorderEnabled,
    DoubleDrawnBorderEnabled,
    WidthLimitEnabled,
    PerCharacterTransformEnabled,
    CenterCeilingEnabled,
    PerCharacterTransformSplitByCharWidth,
    PerCharacterTransformAutoShadowAlpha,
    DrawFromRightToLeft,
    PerCharacterTransformOriginToCenter,
    PerCharacterTransformFixSpace,
    LinefeedByCharacterHeightEnabled,
    PerCharacterTransformSplitByCharWidthInsertSpaceEnabled,
}

pub enum HorizontalPosition {
    Center,
    Left,
    Right,
    MaxHorizontalPosition,
}

pub enum VerticalPosition {
    Center,
    Top,
    Bottom,
    MaxVerticalPosition,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TextBox {
    pub pane: Pane,
    // Actually a union
    pub text_buf: *mut libc::c_char,
    pub p_text_id: *const libc::c_char,
    pub text_colors: [[u8; 4]; 2],
    pub p_font: *const libc::c_void,
    pub font_size_x: f32,
    pub font_size_y: f32,
    pub line_space: f32,
    pub char_space: f32,

    // Actually a union
    pub p_tag_processor: *const libc::c_char,

    pub text_buf_len: u16,
    pub text_len: u16,

    // Use TextBoxFlag
    pub bits: u16,
    pub text_position: u8,

    pub is_utf8: bool,

    pub italic_ratio: f32,

    pub shadow_offset: ResVec2,
    pub shadow_scale: ResVec2,
    pub shadow_color: [ResColor; 2],
    pub shadow_italic_ratio: f32,

    pub p_line_width_offset: *const libc::c_void,

    pub p_material: *mut Material,
    pub p_disp_string_buf: *const libc::c_void,

    pub p_per_character_transform: *const libc::c_void,
}

impl TextBox {
    pub fn set_color(&mut self, r: u8, g: u8, b: u8, a: u8) {
        let input_color = [r, g, b, a];
        let mut dirty: bool = false;
        self.text_colors.iter_mut().for_each(|top_or_bottom_color| {
            if *top_or_bottom_color != input_color {
                dirty = true;
            }
            *top_or_bottom_color = input_color;
        });

        if dirty {
            self.bits |= 1 << TextBoxFlag::IsPTDirty as u8;
        }
    }

    pub unsafe fn set_material_white_color(&mut self, r: f32, g: f32, b: f32, a: f32) {
        (*self.p_material).set_white_color(r, g, b, a);
    }

    pub unsafe fn set_material_black_color(&mut self, r: f32, g: f32, b: f32, a: f32) {
        (*self.p_material).set_black_color(r, g, b, a);
    }

    pub unsafe fn set_default_material_colors(&mut self) {
        self.set_material_white_color(255.0, 255.0, 255.0, 255.0);
        self.set_material_black_color(0.0, 0.0, 0.0, 255.0);
    }

    pub fn text_outline_enable(&mut self, enabled: bool) {
        match enabled {
            true => self.bits &= !(1 << TextBoxFlag::InvisibleBorderEnabled as u8),
            false => self.bits |= 1 << TextBoxFlag::InvisibleBorderEnabled as u8,
        }
    }

    pub fn text_shadow_enable(&mut self, enabled: bool) {
        match enabled {
            true => self.bits |= 1 << TextBoxFlag::ShadowEnabled as u8,
            false => self.bits &= !(1 << TextBoxFlag::ShadowEnabled as u8),
        }
    }

    pub fn set_text_shadow(
        &mut self,
        offset: ResVec2,
        scale: ResVec2,
        colors: [ResColor; 2],
        italic_ratio: f32,
    ) {
        self.shadow_offset = offset;
        self.shadow_scale = scale;
        self.shadow_color = colors;
        self.shadow_italic_ratio = italic_ratio;
    }

    pub fn set_text_alignment(
        &mut self,
        horizontal: HorizontalPosition,
        vertical: VerticalPosition,
    ) {
        self.text_position =
            horizontal as u8 * HorizontalPosition::MaxHorizontalPosition as u8 + vertical as u8;
    }
}

impl Deref for TextBox {
    type Target = Pane;

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

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

#[repr(C)]
pub union MaterialColor {
    pub byte_color: [[u8; 4]; 2],
    pub p_float_color: *mut *mut f32,
}

use core::fmt;
impl fmt::Debug for MaterialColor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        unsafe {
            f.debug_struct("MaterialColor")
                .field("byteColor", &self.byte_color)
                .field("pFloatColor", &self.p_float_color)
                .finish()
        }
    }
}

#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum MaterialColorType {
    BlackColor,
    WhiteColor,
}

#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum MaterialFlags {
    UserAllocated,
    TextureOnly,
    ThresholdingAlphaInterpolation,
    BlackColorFloat,
    WhiteColorFloat,
    DynamicAllocatedColorData,
}

#[repr(C)]
#[derive(Debug)]
pub struct Material {
    pub vtable: u64,
    pub m_colors: MaterialColor,
    // Actually a struct
    m_mem_cap: u32,
    // Actually a struct
    m_mem_count: u32,
    pub m_p_mem: *mut libc::c_void,
    pub m_p_shader_info: *const libc::c_void,
    pub m_p_name: *const libc::c_char,
    pub m_vertex_shader_constant_buffer_offset: u32,
    pub m_pixel_shader_constant_buffer_offset: u32,
    pub m_p_user_shader_constant_buffer_information: *const libc::c_void,
    pub m_p_blend_state: *const libc::c_void,
    pub m_packed_values: u8,
    pub m_flag: u8,
    pub m_shader_variation: u16,
}

impl Material {
    pub fn set_color_int(&mut self, idx: usize, r: u8, g: u8, b: u8, a: u8) {
        let input_color = [r, g, b, a];
        unsafe {
            self.m_colors.byte_color[idx] = input_color;
        }
    }

    pub fn set_color_float(&mut self, idx: usize, r: f32, g: f32, b: f32, a: f32) {
        unsafe {
            *(*(self.m_colors.p_float_color.add(idx)).add(0)) = r;
            *(*(self.m_colors.p_float_color.add(idx)).add(1)) = g;
            *(*(self.m_colors.p_float_color.add(idx)).add(2)) = b;
            *(*(self.m_colors.p_float_color.add(idx)).add(3)) = a;
        }
    }

    pub fn set_color(&mut self, color_type: MaterialColorType, r: f32, g: f32, b: f32, a: f32) {
        let (is_float_flag, idx) = if color_type == MaterialColorType::BlackColor {
            (MaterialFlags::BlackColorFloat as u8, 0)
        } else {
            (MaterialFlags::WhiteColorFloat as u8, 1)
        };
        if self.m_flag & (0x1 << is_float_flag) != 0 {
            self.set_color_float(idx, r, g, b, a);
        } else {
            self.set_color_int(idx, r as u8, g as u8, b as u8, a as u8);
        }
    }

    pub fn set_white_color(&mut self, r: f32, g: f32, b: f32, a: f32) {
        self.set_color(MaterialColorType::WhiteColor, r, g, b, a);
    }

    pub fn set_black_color(&mut self, r: f32, g: f32, b: f32, a: f32) {
        self.set_color(MaterialColorType::BlackColor, r, g, b, a);
    }

    pub fn set_white_res_color(&mut self, white: ResColor) {
        self.set_white_color(
            white.r as f32,
            white.g as f32,
            white.b as f32,
            white.a as f32,
        );
    }

    pub fn set_black_res_color(&mut self, black: ResColor) {
        self.set_black_color(
            black.r as f32,
            black.g as f32,
            black.b as f32,
            black.a as f32,
        );
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Window {
    pub pane: Pane,
    // TODO
}

impl Deref for Window {
    type Target = Pane;

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

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

#[derive(Debug, Copy, Clone)]
pub struct PaneNode {
    pub prev: *mut PaneNode,
    pub next: *mut PaneNode,
}

#[repr(C)]
pub struct Group {
    pub pane_list: PaneNode,
    pub name: *const libc::c_char,
}

#[repr(C)]
pub struct GroupContainer {}

#[repr(C)]
#[derive(Debug)]
pub struct Layout {
    pub vtable: u64,
    pub anim_trans_list: AnimTransformNode,
    pub root_pane: *mut Pane,
    pub group_container: u64,
    pub layout_size: f64,
    pub layout_name: *const libc::c_char,
}