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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Default labels given by the engine.

use ab_glyph::FontArc;
use glyph_brush::ab_glyph::PxScale;
use glyph_brush::{
    ab_glyph, BrushAction, BrushError, DefaultSectionHasher, FontId, GlyphBrush, GlyphBrushBuilder,
    HorizontalAlign, Layout, OwnedSection, OwnedText, VerticalAlign,
};
use image::{ImageBuffer, Luma};
use std::sync::Arc;

use anyhow::Result;

use super::super::resources::vulkan::shaders::*;
use crate::prelude::*;
use glam::f32::{vec2, Vec2};

/// Info to create default label objects with.
#[derive(Clone)]
pub struct LabelCreateInfo {
    /// Initial position.
    pub transform: Transform,
    /// The appearance of the label.
    pub appearance: Appearance,
    /// Initial text of the label.
    pub text: String,
    /// The scale of the text area.
    pub scale: Vec2,
    /// The align of where the text gets rendered.
    pub align: Direction,
}
impl LabelCreateInfo {
    /// Sets the transform of the label and returns it back.
    #[inline]
    pub fn transform(mut self, transform: Transform) -> Self {
        self.transform = transform;
        self
    }
    /// Sets the appearance of the label and returns it back.
    #[inline]
    pub fn appearance(mut self, appearance: Appearance) -> Self {
        self.appearance = appearance;
        self
    }
    /// Sets the text of the label and returns it back.
    #[inline]
    pub fn text<T>(mut self, text: T) -> Self
    where
        T: Into<String>,
    {
        self.text = text.into();
        self
    }
    /// Sets the scale of the label and returns it back.
    #[inline]
    pub fn scale<T>(mut self, scale: T) -> Self
    where
        T: Into<Vec2>,
    {
        self.scale = scale.into();
        self
    }
    /// Sets the alignment of the label and returns it back.
    #[inline]
    pub fn align<T>(mut self, align: T) -> Self
    where
        T: Into<Direction>,
    {
        self.align = align.into();
        self
    }
}
impl Default for LabelCreateInfo {
    fn default() -> Self {
        Self {
            transform: Transform::default(),
            appearance: Appearance::default(),
            text: String::new(),
            scale: vec2(25.0, 25.0),
            align: Direction::Nw,
        }
    }
}

/// A Label object made to display text.
///
/// # note
///
/// It is recommended to sync or update the text with all other visible labels so the texture of all labels change to the same texture.
/// At the beginning of the game update all the text gets rendered if any labels changed. This produces a new texture which if not synced
/// to every label produces multiple textures, which take more memory.
#[derive(Clone)]
pub struct Label<Object> {
    pub object: Object,
    pub font: Font,
    pub text: String,
    pub scale: Vec2,
    pub align: Direction,
    section: OwnedSection<Extra>,
}
impl Label<NewObject> {
    /// Creates a new label with the given settings.
    pub fn new(font: &Font, create_info: LabelCreateInfo) -> Self {
        let mut object = NewObject::new();
        object.transform = create_info.transform;
        object.appearance = create_info.appearance;
        Self {
            object,
            font: font.clone(),
            text: create_info.text,
            scale: create_info.scale,
            align: create_info.align,
            section: OwnedSection::default(),
        }
    }
    pub fn init(mut self, layer: &Arc<Layer>) -> Result<Label<Object>> {
        let mut labelifier = LABELIFIER.lock();
        self.update_section(
            labelifier.increment_tasks(),
            self.object.appearance.get_transform().size,
        );
        let object = self.object.init(layer)?;
        let label = Label {
            object,
            font: self.font,
            text: self.text,
            scale: self.scale,
            align: self.align,
            section: self.section,
        };
        labelifier.queue(label.clone());
        Ok(label)
    }
    pub fn init_with_parent(
        mut self,
        layer: &Arc<Layer>,
        parent: &Object,
    ) -> Result<Label<Object>> {
        let mut labelifier = LABELIFIER.lock();
        self.update_section(
            labelifier.increment_tasks(),
            self.object.appearance.get_transform().size,
        );
        let object = self.object.init_with_parent(layer, parent)?;
        let label = Label {
            object,
            font: self.font,
            text: self.text,
            scale: self.scale,
            align: self.align,
            section: self.section,
        };
        labelifier.queue(label.clone());
        Ok(label)
    }
    pub fn init_with_optional_parent(
        mut self,
        layer: &Arc<Layer>,
        parent: Option<&Object>,
    ) -> Result<Label<Object>> {
        let mut labelifier = LABELIFIER.lock();
        self.update_section(
            labelifier.increment_tasks(),
            self.object.appearance.get_transform().size,
        );
        let object = self.object.init_with_optional_parent(layer, parent)?;
        let label = Label {
            object,
            font: self.font,
            text: self.text,
            scale: self.scale,
            align: self.align,
            section: self.section,
        };
        labelifier.queue(label.clone());
        Ok(label)
    }
}

impl<T> Label<T> {
    fn update_section(&mut self, id: usize, size: Vec2) {
        let dimensions: (f32, f32) = ((1000.0 * size[0]), (1000.0 * size[1]));

        let text = OwnedText {
            text: self.text.clone(),
            scale: PxScale {
                x: self.scale.x,
                y: self.scale.y,
            },
            font_id: self.font.id(),
            extra: Extra { id },
        };

        let (h, v): (HorizontalAlign, VerticalAlign) = self.align.into();
        let x = match h {
            HorizontalAlign::Left => 0.0,
            HorizontalAlign::Center => dimensions.0 * 0.5,
            HorizontalAlign::Right => dimensions.0,
        };
        let y = match v {
            VerticalAlign::Top => 0.0,
            VerticalAlign::Center => dimensions.1 * 0.5,
            VerticalAlign::Bottom => dimensions.1,
        };

        self.section = OwnedSection::default()
            .with_bounds(dimensions)
            .with_layout(Layout::default().h_align(h).v_align(v))
            .with_screen_position((x, y))
            .add_text(text);
    }
}
impl Label<Object> {
    /// Updates the local information of this label from the layer, in case it has changed if for example the parent was changed too.
    pub fn update(&mut self) {
        self.object.update();
    }
    /// Changes the text of the label and updates it on the layer.
    pub fn update_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
        self.sync();
    }
    /// Syncs the public layer side label to be the same as the current.
    pub fn sync(&mut self) {
        let mut labelifier = LABELIFIER.lock();
        self.update_section(
            labelifier.increment_tasks(),
            self.object.appearance.get_transform().size,
        );
        labelifier.queue(self.clone());
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct TextVertex {
    rect: [Vertex; 4],
    extra: Extra,
}

impl TextVertex {
    pub fn indices(&self, id: u32) -> Vec<u32> {
        vec![id, 1 + id, 2 + id, 1 + id, 2 + id, 3 + id]
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Default)]
struct Extra {
    id: usize,
}

impl std::hash::Hash for Extra {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_usize(self.id);
    }
}

/// A label maker holding
pub(crate) struct Labelifier {
    /// the default material,
    material: Material,
    /// RustType font cache,
    glyph_brush: GlyphBrush<TextVertex, Extra, FontArc, DefaultSectionHasher>,
    /// the global font texture,
    cache_pixel_buffer: ImageBuffer<Luma<u8>, Vec<u8>>,
    /// tasks to be executed on next update,
    queued: Vec<DrawTask>,
    /// the amount of tasks
    tasks: usize,
    /// and the boolean if it should update.
    ready: bool,
}

impl Labelifier {
    /// Makes a new label maker.
    pub fn new() -> Result<Self> {
        let glyph_brush = GlyphBrushBuilder::using_fonts(vec![]).build(); // beginning fonts
        let cache_pixel_buffer = ImageBuffer::from_pixel(
            glyph_brush.texture_dimensions().0,
            glyph_brush.texture_dimensions().1,
            image::Luma([0u8]),
        );
        // vec![
        //     0;
        //     (glyph_brush.texture_dimensions().0 * glyph_brush.texture_dimensions().1)
        //         as usize
        // ];

        let dimensions = glyph_brush.texture_dimensions();
        let settings = TextureSettings {
            srgb: false,
            sampler: Sampler::default(),
        };

        // Make the cache a texture.
        let texture = Texture::from_raw(
            cache_pixel_buffer.as_raw(),
            dimensions,
            Format::R8,
            1,
            settings,
        )?;

        let resources = &RESOURCES;
        let vulkan = resources.vulkan().clone();
        let text_shaders = Shaders::from_modules(
            vertex_shader(vulkan.device.clone())?,
            text_fragment_shader(vulkan.device.clone())?,
            "main",
        );

        let material_settings = MaterialSettingsBuilder::default()
            .texture(texture)
            .build()?;

        let material = Material::new_with_shaders(material_settings, &text_shaders, false, vec![])?;

        Ok(Self {
            material,
            glyph_brush,
            cache_pixel_buffer,
            queued: vec![],
            ready: false,
            tasks: 0,
        })
    }

    /// Increments the tasks number by one and returns the last id.
    fn increment_tasks(&mut self) -> usize {
        let tasks = self.tasks;
        self.tasks += 1;
        tasks
    }

    fn update_each_object(&mut self, brush_action: BrushAction<TextVertex>) -> Result<()> {
        let BrushAction::Draw(text_vertices) = brush_action else {
            return Ok(());
        };

        for text_vertex in text_vertices {
            let task = &mut self.queued[text_vertex.extra.id];
            task.indices
                .append(&mut text_vertex.indices(task.vertices.len() as u32));
            task.vertices.extend_from_slice(&text_vertex.rect);
        }

        // Creates a new texture to be inserted into every syncing label.
        // Unsynced label keep holding the old texture.

        // let dimensions = self.cache.dimensions();
        let settings = TextureSettings {
            srgb: false,
            sampler: Sampler::default(),
        };

        // Make the cache a texture.
        self.material.texture = Some(Texture::from_raw(
            self.cache_pixel_buffer.as_raw(),
            self.glyph_brush.texture_dimensions(),
            Format::R8,
            1,
            settings,
        )?);

        let queued = std::mem::take(&mut self.queued);

        for task in queued.into_iter() {
            let mut label = task.label.clone();

            //temp
            let visible = if task.vertices.is_empty() {
                false
            } else {
                let model = ModelData::new(task.into_data())?;
                label.object.appearance.set_model(Model::Custom(model));
                true
            };
            label
                .object
                .appearance
                .set_material(Some(self.material.clone()));
            label.object.appearance.set_visible(visible);
            let node = label.object.as_node();
            let mut object = node.lock();
            object.object = label.object.clone();
        }
        Ok(())
    }

    /// Updates everything.
    pub fn update(&mut self) -> Result<()> {
        // Update the labelifier in case something has changed.
        if !self.ready {
            return Ok(());
        }

        // let dimensions = self.glyph_brush.texture_dimensions();
        let brush_action: glyph_brush::BrushAction<TextVertex> = loop {
            let result = self.glyph_brush.process_queued(
                |rect, src_data| {
                    let width = (rect.max[0] - rect.min[0]) as usize;
                    let height = (rect.max[1] - rect.min[1]) as usize;

                    for y in 0..height {
                        for x in 0..width {
                            let src_index = y * width + x;
                            let pixel = Luma([src_data[src_index]]);
                            self.cache_pixel_buffer.put_pixel(
                                rect.min[0] + x as u32,
                                rect.min[1] + y as u32,
                                pixel,
                            )
                        }
                    }
                    // let mut dst_index = (rect.min[1] * dimensions.0 + rect.min[0]) as usize;
                    // let mut src_index = 0;
                    // for _ in 0..height {
                    //     let dst_slice = &mut self.cache_pixel_buffer[dst_index..dst_index + width];
                    //     let src_slice = &src_data[src_index..src_index + width];
                    //     dst_slice.copy_from_slice(src_slice);

                    //     dst_index += dimensions.0 as usize;
                    //     src_index += width;
                    // }
                },
                to_vertex,
            );
            match result {
                Ok(brush_action) => {
                    break brush_action;
                }
                Err(BrushError::TextureTooSmall { suggested }) => {
                    dbg!(suggested);
                    self.glyph_brush.resize_texture(suggested.0, suggested.1);
                    let mut new_buffer =
                        ImageBuffer::from_pixel(suggested.0, suggested.1, Luma([0u8]));
                    for y in 0..self.cache_pixel_buffer.height() {
                        for x in 0..self.cache_pixel_buffer.width() {
                            let pixel = self.cache_pixel_buffer.get_pixel(x, y);
                            new_buffer.put_pixel(x, y, *pixel);
                        }
                    }
                    self.cache_pixel_buffer = new_buffer;
                }
            }
        };

        Self::update_each_object(self, brush_action)?;

        self.tasks = 0;
        self.queued = vec![];
        self.ready = false;
        Ok(())
    }
    pub fn queue(&mut self, label: Label<Object>) {
        self.ready = true;

        self.glyph_brush.queue(label.section.to_borrowed());

        self.queued.push(DrawTask {
            label,
            vertices: vec![],
            indices: vec![],
        });
    }
}

fn to_vertex(
    glyph_brush::GlyphVertex {
        tex_coords,
        pixel_coords,
        bounds,
        extra,
    }: glyph_brush::GlyphVertex<Extra>,
) -> TextVertex {
    let rect = glyph_brush::Rectangle {
        min: [
            (pixel_coords.min.x / bounds.width() - 0.5) * 2.0,
            (pixel_coords.min.y / bounds.height() - 0.5) * 2.0,
        ],
        max: [
            (pixel_coords.max.x / bounds.width() - 0.5) * 2.0,
            (pixel_coords.max.y / bounds.height() - 0.5) * 2.0,
        ],
    };

    TextVertex {
        rect: [
            tvert(rect.min[0], rect.min[1], tex_coords.min.x, tex_coords.min.y),
            tvert(rect.min[0], rect.max[1], tex_coords.min.x, tex_coords.max.y),
            tvert(rect.max[0], rect.min[1], tex_coords.max.x, tex_coords.min.y),
            tvert(rect.max[0], rect.max[1], tex_coords.max.x, tex_coords.max.y),
        ],
        extra: *extra,
    }
    // let vertices: Vec<Vertex> = task
    //     .glyphs
    //     .clone()
    //     .iter()
    //     .flat_map(|g| {
    //         if let Ok(Some((uv_rect, screen_rect))) =
    //             self.glyph_brush.rect_for(label.font.id(), g)
    //         {
    //             let gl_rect = rusttype::Rect {
    //                 min: point(
    //                     (screen_rect.min.x as f32 / dimensions[0] - 0.5) * 2.0,
    //                     (screen_rect.min.y as f32 / dimensions[1] - 0.5) * 2.0,
    //                 ),
    //                 max: point(
    //                     (screen_rect.max.x as f32 / dimensions[0] - 0.5) * 2.0,
    //                     (screen_rect.max.y as f32 / dimensions[1] - 0.5) * 2.0,
    //                 ),
    //             };
    //             indices.extend([1 + id, 2 + id, id, 2 + id, id, 3 + id]);
    //             id += 4;
    //             vec![
    //                 Vertex {
    //                     position: vec2(gl_rect.min.x, gl_rect.max.y),
    //                     tex_position: vec2(uv_rect.min.x, uv_rect.max.y),
    //                 },
    //                 Vertex {
    //                     position: vec2(gl_rect.min.x, gl_rect.min.y),
    //                     tex_position: vec2(uv_rect.min.x, uv_rect.min.y),
    //                 },
    //                 Vertex {
    //                     position: vec2(gl_rect.max.x, gl_rect.min.y),
    //                     tex_position: vec2(uv_rect.max.x, uv_rect.min.y),
    //                 },
    //                 Vertex {
    //                     position: vec2(gl_rect.max.x, gl_rect.max.y),
    //                     tex_position: vec2(uv_rect.max.x, uv_rect.max.y),
    //                 },
    //             ]
    //             .into_iter()
    //         } else {
    //             vec![].into_iter()
    //         }
    //     })
    //     .collect();
}

struct DrawTask {
    pub label: Label<Object>,
    pub vertices: Vec<Vertex>,
    pub indices: Vec<u32>,
    // pub glyphs: Vec<PositionedGlyph<'a>>,
}

impl DrawTask {
    pub fn into_data(self) -> Data {
        Data::Dynamic {
            vertices: self.vertices,
            indices: self.indices,
        }
    }
}

// fn layout_paragraph<'a>(label: &Label<Object>, dimensions: [f32; 2]) -> Vec<PositionedGlyph<'a>> {
//     if label.text.is_empty() {
//         return vec![];
//     };
//     let mut result: Vec<Vec<PositionedGlyph>> = vec![vec![]];
//     let scale = Scale {
//         x: label.scale[0],
//         y: label.scale[1],
//     };

//     let v_metrics = label.font.font().v_metrics(scale);
//     let advance_height = v_metrics.ascent - v_metrics.descent + v_metrics.line_gap;
//     let mut caret = point(0.0, v_metrics.ascent);
//     let mut last_glyph_id = None;
//     for c in label.text.chars() {
//         if c.is_control() {
//             match c {
//                 '\r' => {
//                     caret = point(0.0, caret.y + advance_height);
//                     result.push(vec![]);
//                 }
//                 '\n' => {}
//                 _ => {}
//             }
//             continue;
//         }
//         let base_glyph = label.font.font().glyph(c);
//         if let Some(id) = last_glyph_id.take() {
//             caret.x += label.font.font().pair_kerning(scale, id, base_glyph.id());
//         }
//         last_glyph_id = Some(base_glyph.id());
//         let mut glyph = base_glyph.scaled(scale).positioned(caret);
//         if let Some(bb) = glyph.pixel_bounding_box() {
//             if bb.max.x > dimensions[0] as i32 {
//                 result.push(vec![]);
//                 caret = point(0.0, caret.y + advance_height);
//                 glyph.set_position(caret);
//                 last_glyph_id = None;
//             }
//         }
//         caret.x += glyph.unpositioned().h_metrics().advance_width;
//         result.last_mut().unwrap().push(glyph);
//     }

//     let yshift = dimensions[1] - result.len() as f32 * advance_height + v_metrics.descent;
//     for line in result.clone().into_iter().enumerate() {
//         if let Some(last) = line.1.last() {
//             let xshift =
//                 dimensions[0] - last.position().x - last.unpositioned().h_metrics().advance_width;
//             for glyph in result[line.0].clone().iter().enumerate() {
//                 result[line.0][glyph.0].set_position(point(
//                     glyph.1.position().x + xshift * label.align[0],
//                     glyph.1.position().y + yshift * label.align[1],
//                 ))
//             }
//         };
//     }
//     result.into_iter().flatten().collect()
// }

/// A font to be used with the default label system.
#[derive(Clone)]
pub struct Font {
    id: FontId,
}

impl Font {
    /// Loads a font into the resources.
    ///
    /// Makes a new font using the bytes in a vec of a truetype or opentype font.
    /// Returns an error in case the given bytes do not work.
    pub fn from_vec(data: impl Into<Vec<u8>>) -> Result<Self> {
        let labelifier = &LABELIFIER;
        let font = FontArc::try_from_vec(data.into())?;
        let id = labelifier.lock().glyph_brush.add_font(font);
        Ok(Self { id })
    }
    /// Loads a font into the resources.
    ///
    /// Makes a new font using the bytes of a truetype or opentype font.
    /// Returns an error in case the given bytes do not work.
    pub fn from_slice(data: &'static [u8]) -> Result<Self> {
        let labelifier = &LABELIFIER;
        let font = FontArc::try_from_slice(data)?;
        let id = labelifier.lock().glyph_brush.add_font(font);
        Ok(Self { id })
    }
    /// Returns the font ID.
    pub fn id(&self) -> FontId {
        self.id
    }
}