basalt 0.19.1

A window/ui framework built upon vulkan.
Documentation
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
use std::sync::Arc;

use ilmenite::{ImtHoriAlign, ImtTextWrap, ImtVertAlign, ImtWeight};

use crate::atlas::{AtlasCacheCtrl, AtlasCoords};
use crate::image_view::BstImageView;
use crate::interface::Interface;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinPosition {
    /// Position will be done from the window's dimensions
    Window,
    /// Position will be done from the parent's dimensions
    Parent,
    /// Position will be done from the parent's dimensions
    /// and other siblings the same type.
    Floating,
}

impl Default for BinPosition {
    fn default() -> Self {
        BinPosition::Window
    }
}

#[derive(Default, Clone)]
pub struct BinStyle {
    /// Determines the positioning type
    pub position: Option<BinPosition>,
    /// Overrides the z-index automatically calculated.
    pub z_index: Option<i16>,
    /// Offsets the z-index automatically calculated.
    pub add_z_index: Option<i16>,
    /// Hides the bin, with None set parent will decide
    /// the visiblity, setting this explictely will ignore
    /// the parents visiblity.
    pub hidden: Option<bool>,
    /// Set the opacity of the bin's content.
    pub opacity: Option<f32>,
    // Position from Edges
    pub pos_from_t: Option<f32>,
    pub pos_from_b: Option<f32>,
    pub pos_from_l: Option<f32>,
    pub pos_from_r: Option<f32>,
    pub pos_from_t_pct: Option<f32>,
    pub pos_from_b_pct: Option<f32>,
    pub pos_from_l_pct: Option<f32>,
    pub pos_from_r_pct: Option<f32>,
    pub pos_from_l_offset: Option<f32>,
    pub pos_from_t_offset: Option<f32>,
    pub pos_from_r_offset: Option<f32>,
    pub pos_from_b_offset: Option<f32>,
    // Size
    pub width: Option<f32>,
    pub width_pct: Option<f32>,
    /// Used in conjunction with `width_pct` to provide additional flexibility
    pub width_offset: Option<f32>,
    pub height: Option<f32>,
    pub height_pct: Option<f32>,
    /// Used in conjunction with `height_pct` to provide additional flexibility
    pub height_offset: Option<f32>,
    pub margin_t: Option<f32>,
    pub margin_b: Option<f32>,
    pub margin_l: Option<f32>,
    pub margin_r: Option<f32>,
    // Padding
    pub pad_t: Option<f32>,
    pub pad_b: Option<f32>,
    pub pad_l: Option<f32>,
    pub pad_r: Option<f32>,
    // Scrolling
    pub scroll_y: Option<f32>,
    pub scroll_x: Option<f32>,
    pub overflow_y: Option<bool>,
    pub overflow_x: Option<bool>,
    // Border
    pub border_size_t: Option<f32>,
    pub border_size_b: Option<f32>,
    pub border_size_l: Option<f32>,
    pub border_size_r: Option<f32>,
    pub border_color_t: Option<Color>,
    pub border_color_b: Option<Color>,
    pub border_color_l: Option<Color>,
    pub border_color_r: Option<Color>,
    pub border_radius_tl: Option<f32>,
    pub border_radius_tr: Option<f32>,
    pub border_radius_bl: Option<f32>,
    pub border_radius_br: Option<f32>,
    // Background
    pub back_color: Option<Color>,
    pub back_image: Option<String>,
    pub back_image_url: Option<String>,
    pub back_image_atlas: Option<AtlasCoords>,
    pub back_image_raw: Option<Arc<BstImageView>>,
    pub back_image_raw_coords: Option<AtlasCoords>,
    pub back_image_cache: Option<AtlasCacheCtrl>,
    pub back_image_effect: Option<ImageEffect>,
    // Text
    pub text: String,
    pub text_color: Option<Color>,
    pub text_height: Option<f32>,
    pub text_secret: Option<bool>,
    pub line_spacing: Option<f32>,
    pub line_limit: Option<usize>,
    pub text_wrap: Option<ImtTextWrap>,
    pub text_vert_align: Option<ImtVertAlign>,
    pub text_hori_align: Option<ImtHoriAlign>,
    pub font_family: Option<String>,
    pub font_weight: Option<ImtWeight>,
    pub custom_verts: Vec<BinVert>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinStyleError {
    pub ty: BinStyleErrorType,
    pub desc: String,
}

impl std::fmt::Display for BinStyleError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}: {}", self.ty, self.desc)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinStyleErrorType {
    /// Two fields are conflicted only one must be set.
    ConflictingFields,
    /// Too many fields are defining an attribute.
    TooManyConstraints,
    /// Not enough fields are defining an attribute.
    NotEnoughConstraints,
    /// Requested font family & weight are not available.
    MissingFont,
}

impl std::fmt::Display for BinStyleErrorType {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::ConflictingFields => write!(f, "Conflicting Fields"),
            Self::TooManyConstraints => write!(f, "Too Many Constraints"),
            Self::NotEnoughConstraints => write!(f, "Not Enough Constraints"),
            Self::MissingFont => write!(f, "Missing Font"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinStyleWarn {
    pub ty: BinStyleWarnType,
    pub desc: String,
}

impl std::fmt::Display for BinStyleWarn {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}: {}", self.ty, self.desc)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinStyleWarnType {
    /// Field is set, but isn't used or incompatible with other styles.
    UselessField,
}

impl std::fmt::Display for BinStyleWarnType {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::UselessField => write!(f, "Useless Field"),
        }
    }
}

/// A struct representing errors and warnings returned by `style_update`.
///
/// To remove the `#[must_use]` attribute, enable the `style_validation_debug_on_drop` feature.
/// This feature will call the `debug` method automatically if no other method was used.
#[cfg_attr(not(feature = "style_validation_debug_on_drop"), must_use)]
pub struct BinStyleValidation {
    errors: Vec<BinStyleError>,
    warnings: Vec<BinStyleWarn>,
    location: Option<String>,
    used: bool,
}

impl BinStyleValidation {
    fn new() -> Self {
        Self {
            errors: Vec::new(),
            warnings: Vec::new(),
            location: None,
            used: false,
        }
    }

    #[track_caller]
    fn error<D: Into<String>>(&mut self, ty: BinStyleErrorType, desc: D) {
        self.errors.push(BinStyleError {
            ty,
            desc: desc.into(),
        });

        if self.location.is_none() {
            self.location = Some(format!("{}", std::panic::Location::caller()));
        }
    }

    #[track_caller]
    fn warning<D: Into<String>>(&mut self, ty: BinStyleWarnType, desc: D) {
        self.warnings.push(BinStyleWarn {
            ty,
            desc: desc.into(),
        });

        if self.location.is_none() {
            self.location = Some(format!("{}", std::panic::Location::caller()));
        }
    }

    /// Expect `BinStyle` provided to `style_update()` is valid panicking if that is not the case.
    pub fn expect_valid(mut self) {
        self.used = true;

        if !self.errors.is_empty() {
            let mut panic_msg = format!(
                "BinStyleValidation-Error {{ caller: {},",
                self.location.take().unwrap()
            );
            let error_count = self.errors.len();

            if error_count == 1 {
                panic_msg = format!(
                    "{} error: Error {{ ty: {}, desc: {} }} }}",
                    panic_msg, self.errors[0].ty, self.errors[0].desc
                );
            } else {
                for (i, error) in self.errors.iter().enumerate() {
                    if i == 0 {
                        panic_msg = format!(
                            "{} errors: [ Error {{ ty: {}, desc: {} }},",
                            panic_msg, error.ty, error.desc
                        );
                    } else if i == error_count - 1 {
                        panic_msg = format!(
                            "{} Error {{ ty: {}, desc: {} }} ] }}",
                            panic_msg, error.ty, error.desc
                        );
                    } else {
                        panic_msg = format!(
                            "{} Error {{ ty: {}, desc: {} }},",
                            panic_msg, error.ty, error.desc
                        );
                    }
                }
            }

            panic!("{}", panic_msg);
        }
    }

    /// Does the same thing as `expect_valid`, but in the case of no errors, it'll print pretty warnings to the terminal.
    pub fn expect_valid_debug_warn(mut self) {
        self.used = true;

        if self.errors.is_empty() {
            if !self.warnings.is_empty() {
                let mut msg = format!(
                    "BinStyleValidation-Warn: {}:\n",
                    self.location.take().unwrap()
                );

                for warning in &self.warnings {
                    msg = format!("{}  {}: {}\n", msg, warning.ty, warning.desc);
                }

                msg.pop();
                println!("{}", msg);
            }
        } else {
            self.expect_valid();
        }
    }

    /// Returns `true` if errors are present.
    pub fn errors_present(&self) -> bool {
        !self.errors.is_empty()
    }

    /// Return an `Iterator` of `BinStyleError`
    ///
    /// # Notes
    /// - This method should only be called once. As it move the errors out.
    pub fn errors(&mut self) -> impl Iterator<Item = BinStyleError> {
        self.used = true;
        self.errors.split_off(0).into_iter()
    }

    /// Returns `true` if warnings are present.
    pub fn warnings_present(&self) -> bool {
        !self.warnings.is_empty()
    }

    /// Return an `Iterator` of `BinStyleWarn`
    ///
    /// # Notes
    /// - This method should only be called once. As it move the warnings out.
    pub fn warnings(&mut self) -> impl Iterator<Item = BinStyleWarn> {
        self.used = true;
        self.warnings.split_off(0).into_iter()
    }

    /// Acknowlege the style update may have not be successful and just print pretty errors/warnings to the terminal.
    pub fn debug(mut self) {
        self.debug_impl();
    }

    fn debug_impl(&mut self) {
        if self.used {
            return;
        }

        self.used = true;

        if !self.errors.is_empty() || !self.warnings.is_empty() {
            let mut msg = format!("BinStyleValidation: {}:\n", self.location.take().unwrap());

            for error in &self.errors {
                msg = format!("{}  Error {}: {}\n", msg, error.ty, error.desc);
            }

            for warning in &self.warnings {
                msg = format!("{}  Warning {}: {}\n", msg, warning.ty, warning.desc);
            }

            msg.pop();
            println!("{}", msg);
        }
    }
}

#[cfg(feature = "style_validation_debug_on_drop")]
impl Drop for BinStyleValidation {
    fn drop(&mut self) {
        self.debug_impl();
    }
}

macro_rules! useless_field {
    ($style:ident, $field:ident, $name:literal, $validation:ident) => {
        if $style.$field.is_some() {
            $validation.warning(
                BinStyleWarnType::UselessField,
                format!("'{}' is defined, but is ignored.", $name),
            );
        }
    };
}

impl BinStyle {
    #[track_caller]
    pub(crate) fn validate(
        &self,
        interface: &Arc<Interface>,
        has_parent: bool,
    ) -> BinStyleValidation {
        let mut validation = BinStyleValidation::new();

        match self.position.unwrap_or(BinPosition::Window) {
            BinPosition::Window | BinPosition::Parent => {
                useless_field!(self, margin_t, "margin_t", validation);
                useless_field!(self, margin_b, "margin_b", validation);
                useless_field!(self, margin_l, "margin_l", validation);
                useless_field!(self, margin_r, "margin_r", validation);

                if self.pos_from_t.is_some() && self.pos_from_t_pct.is_some() {
                    validation.error(
                        BinStyleErrorType::ConflictingFields,
                        "Both 'pos_from_t' and 'pos_from_t_pct' are set.",
                    );
                }

                if self.pos_from_b.is_some() && self.pos_from_b_pct.is_some() {
                    validation.error(
                        BinStyleErrorType::ConflictingFields,
                        "Both 'pos_from_b' and 'pos_from_b_pct' are set.",
                    );
                }

                if self.pos_from_l.is_some() && self.pos_from_l_pct.is_some() {
                    validation.error(
                        BinStyleErrorType::ConflictingFields,
                        "Both 'pos_from_l' and 'pos_from_l_pct' are set.",
                    );
                }

                if self.pos_from_r.is_some() && self.pos_from_r_pct.is_some() {
                    validation.error(
                        BinStyleErrorType::ConflictingFields,
                        "Both 'pos_from_r' and 'pos_from_r_pct' are set.",
                    );
                }

                if self.width.is_some() && self.width_pct.is_some() {
                    validation.error(
                        BinStyleErrorType::ConflictingFields,
                        "Both 'width' and 'width_pct' are set.",
                    );
                }

                if self.height.is_some() && self.height_pct.is_some() {
                    validation.error(
                        BinStyleErrorType::ConflictingFields,
                        "Both 'height' and 'height_pct' are set.",
                    );
                }

                if validation.errors.is_empty() {
                    let pft = self.pos_from_t.is_some() || self.pos_from_t_pct.is_some();
                    let pfb = self.pos_from_b.is_some() || self.pos_from_b_pct.is_some();
                    let pfl = self.pos_from_l.is_some() || self.pos_from_l_pct.is_some();
                    let pfr = self.pos_from_r.is_some() || self.pos_from_r_pct.is_some();
                    let width = self.width.is_some() || self.width_pct.is_some();
                    let height = self.height.is_some() || self.height_pct.is_some();

                    match (pft, pfb, height) {
                        (true, true, true) => {
                            let pft_field = if self.pos_from_t.is_some() {
                                "pos_from_t"
                            } else {
                                "pos_from_t_pct"
                            };

                            let pfb_field = if self.pos_from_b.is_some() {
                                "pos_from_b"
                            } else {
                                "pos_from_b_pct"
                            };

                            let height_field = if self.height.is_some() {
                                "height"
                            } else {
                                "height_pct"
                            };

                            validation.error(
                                BinStyleErrorType::TooManyConstraints,
                                format!(
                                    "'{}', '{}' & '{}' are all defined. Only two can be defined.",
                                    pft_field, pfb_field, height_field,
                                ),
                            );
                        },
                        (true, false, false) => {
                            let pft_field = if self.pos_from_t.is_some() {
                                "pos_from_t"
                            } else {
                                "pos_from_t_pct"
                            };

                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                format!(
                                    "'{}' is defined, but one of `pos_from_b`, `pos_from_b_pct`, \
                                     `height` or `height_pct` must also be defined.",
                                    pft_field,
                                ),
                            );
                        },
                        (false, true, false) => {
                            let pfb_field = if self.pos_from_b.is_some() {
                                "pos_from_b"
                            } else {
                                "pos_from_b_pct"
                            };

                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                format!(
                                    "'{}' is defined, but one of `pos_from_t`, `pos_from_t_pct`, \
                                     `height` or `height_pct` must also be defined.",
                                    pfb_field,
                                ),
                            );
                        },
                        (false, false, true) => {
                            let height_field = if self.height.is_some() {
                                "height"
                            } else {
                                "height_pct"
                            };

                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                format!(
                                    "'{}' is defined, but one of `pos_from_t`, `pos_from_t_pct`, \
                                     `pos_from_b` or `pos_from_b_pct` must also be defined.",
                                    height_field,
                                ),
                            );
                        },
                        _ => (),
                    }

                    match (pfl, pfr, width) {
                        (true, true, true) => {
                            let pfl_field = if self.pos_from_l.is_some() {
                                "pos_from_l"
                            } else {
                                "pos_from_l_pct"
                            };

                            let pfr_field = if self.pos_from_r.is_some() {
                                "pos_from_r"
                            } else {
                                "pos_from_r_pct"
                            };

                            let width_field = if self.width.is_some() {
                                "width"
                            } else {
                                "width_pct"
                            };

                            validation.error(
                                BinStyleErrorType::TooManyConstraints,
                                format!(
                                    "'{}', '{}' & '{}' are all defined. Only two can be defined.",
                                    pfl_field, pfr_field, width_field,
                                ),
                            );
                        },
                        (true, false, false) => {
                            let pfl_field = if self.pos_from_t.is_some() {
                                "pos_from_l"
                            } else {
                                "pos_from_l_pct"
                            };

                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                format!(
                                    "'{}' is defined, but one of `pos_from_r`, `pos_from_r_pct`, \
                                     `width` or `width_pct` must also be defined.",
                                    pfl_field,
                                ),
                            );
                        },
                        (false, true, false) => {
                            let pfr_field = if self.pos_from_t.is_some() {
                                "pos_from_r"
                            } else {
                                "pos_from_r_pct"
                            };

                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                format!(
                                    "'{}' is defined, but one of `pos_from_l`, `pos_from_l_pct`, \
                                     `width` or `width_pct` must also be defined.",
                                    pfr_field,
                                ),
                            );
                        },
                        (false, false, true) => {
                            let width_field = if self.pos_from_t.is_some() {
                                "width"
                            } else {
                                "width_pct"
                            };

                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                format!(
                                    "'{}' is defined, but one of `pos_from_l`, `pos_from_l_pct`, \
                                     `pos_from_r` or `pos_from_r_pct` must also be defined.",
                                    width_field,
                                ),
                            );
                        },
                        _ => (),
                    }
                }
            },
            BinPosition::Floating => {
                useless_field!(self, pos_from_t, "pos_from_t", validation);
                useless_field!(self, pos_from_b, "pos_from_b", validation);
                useless_field!(self, pos_from_l, "pos_from_l", validation);
                useless_field!(self, pos_from_r, "pos_from_r", validation);
                useless_field!(self, pos_from_t_pct, "pos_from_t_pct", validation);
                useless_field!(self, pos_from_b_pct, "pos_from_b_pct", validation);
                useless_field!(self, pos_from_l_pct, "pos_from_l_pct", validation);
                useless_field!(self, pos_from_r_pct, "pos_from_r_pct", validation);
                useless_field!(self, pos_from_t_offset, "pos_from_t_offset", validation);
                useless_field!(self, pos_from_t_offset, "pos_from_b_offset", validation);
                useless_field!(self, pos_from_t_offset, "pos_from_l_offset", validation);
                useless_field!(self, pos_from_t_offset, "pos_from_r_offset", validation);

                if !has_parent {
                    validation.error(
                        BinStyleErrorType::NotEnoughConstraints,
                        "Floating Bin's must have a parent.",
                    );
                }

                if self.width.is_none() && self.width_pct.is_none() {
                    validation.error(
                        BinStyleErrorType::NotEnoughConstraints,
                        "'width' or 'width_pct' must be defined.",
                    );
                }

                if self.height.is_none() && self.height_pct.is_none() {
                    validation.error(
                        BinStyleErrorType::NotEnoughConstraints,
                        "'height' or 'height_pct' must be defined.",
                    );
                }
            },
        }

        let mut back_image_defined = Vec::new();

        if self.back_image.is_some() {
            back_image_defined.push("back_image");
        }

        if self.back_image_url.is_some() {
            back_image_defined.push("back_image_url");
        }

        if self.back_image_atlas.is_some() {
            back_image_defined.push("back_image_atlas");
        }

        if self.back_image_raw.is_some() {
            back_image_defined.push("back_image_raw");
        }

        match back_image_defined.len() {
            0 => {
                useless_field!(
                    self,
                    back_image_raw_coords,
                    "back_image_raw_coords",
                    validation
                );

                useless_field!(self, back_image_cache, "back_image_cache", validation);
                useless_field!(self, back_image_effect, "back_image_effect", validation);
            },
            1 => {
                let back_color_has_effect = match self.back_image_effect {
                    Some(ImageEffect::Invert) | None => false,
                    Some(_) => true,
                };

                if !back_color_has_effect {
                    useless_field!(self, back_color, "back_color", validation);
                }

                if self.back_image_raw.is_none() {
                    useless_field!(
                        self,
                        back_image_raw_coords,
                        "back_image_raw_coords",
                        validation
                    );
                }

                if self.back_image_raw.is_some() || self.back_image_atlas.is_some() {
                    useless_field!(self, back_image_cache, "back_image_cache", validation);
                }
            },
            _ => {
                let mut fields = String::new();

                for (i, field) in back_image_defined.iter().enumerate() {
                    if i == 0 {
                        fields = format!("'{}'", field);
                    }
                    if i == back_image_defined.len() - 1 {
                        fields = format!("{} & '{}'", fields, field);
                    } else {
                        fields = format!("{}, '{}'", fields, field);
                    }
                }

                validation.error(
                    BinStyleErrorType::TooManyConstraints,
                    format!("{} are all defined. Only one can be defined.", fields),
                );
            },
        }

        if !self.text.is_empty() {
            match self.font_family.as_ref() {
                Some(font_family) => {
                    match self.font_weight {
                        Some(font_weight) => {
                            if !interface.has_font(font_family, self.font_weight.unwrap()) {
                                validation.error(
                                    BinStyleErrorType::MissingFont,
                                    format!(
                                        "Font family '{}' with weight of {:?} has not been loaded.",
                                        font_family, font_weight,
                                    ),
                                );
                            }
                        },
                        None => {
                            validation.error(
                                BinStyleErrorType::NotEnoughConstraints,
                                "When 'font_family' is defined, 'font_weight' must also be \
                                 defined.",
                            );
                        },
                    }
                },
                None => {
                    match interface.default_font() {
                        Some((default_family, _)) => {
                            if let Some(font_weight) = self.font_weight {
                                if !interface.has_font(&default_family, self.font_weight.unwrap()) {
                                    validation.error(
                                        BinStyleErrorType::MissingFont,
                                        format!(
                                            "Font family '{}'(default) with weight of {:?} has \
                                             not been loaded.",
                                            default_family, font_weight,
                                        ),
                                    );
                                }
                            }
                        },
                        None => {
                            validation.error(
                                BinStyleErrorType::MissingFont,
                                "No default font has been set.",
                            );
                        },
                    }
                },
            }
        }

        validation
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageEffect {
    BackColorAdd,
    BackColorBehind,
    BackColorSubtract,
    BackColorMultiply,
    BackColorDivide,
    GlyphWithColor,
    Invert,
}

impl ImageEffect {
    pub(crate) fn vert_type(&self) -> i32 {
        match *self {
            ImageEffect::BackColorAdd => 102,
            ImageEffect::BackColorBehind => 103,
            ImageEffect::BackColorSubtract => 104,
            ImageEffect::BackColorMultiply => 105,
            ImageEffect::BackColorDivide => 106,
            ImageEffect::Invert => 107,
            ImageEffect::GlyphWithColor => 108,
        }
    }
}

#[derive(Default, Clone, Debug, PartialEq)]
pub struct BinVert {
    pub position: (f32, f32, i16),
    pub color: Color,
}

#[derive(Clone, Debug, PartialEq, Default)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub fn as_array(&self) -> [f32; 4] {
        [self.r, self.g, self.b, self.a]
    }

    fn ffh(mut c1: u8, mut c2: u8) -> f32 {
        if (97..=102).contains(&c1) {
            c1 -= 87;
        } else if (65..=70).contains(&c1) {
            c1 -= 65;
        } else if (48..=57).contains(&c1) {
            c1 = c1.checked_sub(48).unwrap();
        } else {
            c1 = 0;
        }

        if (97..=102).contains(&c2) {
            c2 -= 87;
        } else if (65..=70).contains(&c2) {
            c2 -= 65;
        } else if (48..=57).contains(&c2) {
            c2 = c2.checked_sub(48).unwrap();
        } else {
            c2 = 0;
        }
        ((c1 * 16) + c2) as f32 / 255.0
    }

    pub fn clamp(&mut self) {
        if self.r > 1.0 {
            self.r = 1.0;
        } else if self.r < 0.0 {
            self.r = 0.0;
        }
        if self.g > 1.0 {
            self.g = 1.0;
        } else if self.g < 0.0 {
            self.g = 0.0;
        }
        if self.b > 1.0 {
            self.b = 1.0;
        } else if self.b < 0.0 {
            self.b = 0.0;
        }
        if self.a > 1.0 {
            self.a = 1.0;
        } else if self.a < 0.0 {
            self.a = 0.0;
        }
    }

    pub fn to_linear(&mut self) {
        self.r = f32::powf((self.r + 0.055) / 1.055, 2.4);
        self.g = f32::powf((self.g + 0.055) / 1.055, 2.4);
        self.b = f32::powf((self.b + 0.055) / 1.055, 2.4);
        self.a = f32::powf((self.a + 0.055) / 1.055, 2.4);
    }

    pub fn to_nonlinear(&mut self) {
        self.r = (self.r.powf(1.0 / 2.4) * 1.055) - 0.055;
        self.g = (self.g.powf(1.0 / 2.4) * 1.055) - 0.055;
        self.b = (self.b.powf(1.0 / 2.4) * 1.055) - 0.055;
        self.a = (self.a.powf(1.0 / 2.4) * 1.055) - 0.055;
    }

    pub fn srgb_hex(code: &str) -> Self {
        let mut color = Self::from_hex(code);
        color.to_linear();
        color
    }

    pub fn from_hex(code: &str) -> Self {
        let mut iter = code.bytes();
        let mut red = 0.0;
        let mut green = 0.0;
        let mut blue = 0.0;
        let mut alpha = 1.0;

        red = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };
        green = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };
        blue = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };
        alpha = match iter.next() {
            Some(c1) => {
                match iter.next() {
                    Some(c2) => Self::ffh(c1, c2),
                    None => {
                        return Color {
                            r: red,
                            g: green,
                            b: blue,
                            a: alpha,
                        }
                    },
                }
            },
            None => {
                return Color {
                    r: red,
                    g: green,
                    b: blue,
                    a: alpha,
                }
            },
        };

        Color {
            r: red,
            g: green,
            b: blue,
            a: alpha,
        }
    }
}