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
use super::*;

const CREDITS_STR: &str = include_str!("../../../CREDITS.md");

static CREDITS: Lazy<Credits> = Lazy::new(|| credits_parser::credits(CREDITS_STR).unwrap());

pub struct Credits {
    pub sections: Vec<CreditsSection>,
}

pub struct CreditsSection {
    pub title: String,
    pub entries: Vec<CreditsEntry>,
}

pub struct CreditsEntry {
    pub name: Option<String>,
    pub handle: Option<CreditsHandle>,
    pub contribution: Option<String>,
}

pub struct CreditsHandle {
    pub name: String,
    pub link: String,
}

peg::parser! {
    grammar credits_parser() for str {
      rule _ = " "*
      rule __ = [' ' | '\n']*

      rule handle() -> CreditsHandle
          = "[" name:$([^']']*) "](" link:$([^')']*) ")" {
              CreditsHandle { name: name.trim().into(), link: link.trim().into() }
          }

      rule entry_name() -> String
          = name:$([^'[' | '-' | '\n']*) { name.trim().into() }

      rule contribution() -> String
          = "-" _ contribution:$([^'\n']*) {
            contribution.trim().into()
          }

      rule entry() -> CreditsEntry
          = "-" _ name:entry_name()? _ handle:handle()? _ contribution:contribution()? "\n" {
            CreditsEntry { name, handle, contribution }
          }

      rule section() -> CreditsSection
          = "##" _ title:$([^'\n']*) __
            entries:(entry() ** __)
          {
            CreditsSection { title: title.trim().into(), entries }
          }

      pub rule credits() -> Credits
          = "# Credits" __ sections:(section() ** __) __ {
            Credits {
                sections,
            }
          }
    }
}

pub fn widget(
    mut ui: In<&mut egui::Ui>,
    meta: Root<GameMeta>,
    localization: Localization<GameMeta>,
    input: Res<GlobalPlayerControls>,
) {
    let outer_margin = egui::style::Margin::symmetric(
        ui.available_width() * 0.1,
        meta.theme.font_styles.bigger.size,
    );

    BorderedFrame::new(&meta.theme.panel.border)
        .margin(outer_margin)
        .padding(meta.theme.panel.padding)
        .show(*ui, |ui| {
            let font_color = meta.theme.panel.font_color;
            let heading_font = meta.theme.font_styles.heading.with_color(font_color);
            let heading_size = heading_font.size;
            let normal_size = meta.theme.font_styles.normal.size;
            let bigger_font = meta.theme.font_styles.bigger.with_color(font_color);
            let bigger_size = bigger_font.size;

            ui.vertical_centered(|ui| {
                ui.label(
                    meta.theme
                        .font_styles
                        .heading
                        .rich(localization.get("credits")),
                );
            });
            ui.set_min_width(ui.available_width());

            ui.with_layout(egui::Layout::bottom_up(egui::Align::Min), |ui| {
                ui.add_space(normal_size / 2.0);

                // Back button
                if BorderedButton::themed(&meta.theme.buttons.normal, localization.get("back"))
                    .show(ui)
                    .focus_by_default(ui)
                    .clicked()
                    || input.values().any(|x| x.menu_back_just_pressed)
                {
                    ui.ctx().set_state(MenuPage::Home);
                }

                ui.with_layout(default(), |ui| {
                    egui::ScrollArea::vertical().show(ui, |ui| {
                        ui.set_width(ui.available_width());
                        for section in &CREDITS.sections {
                            ui.add_space(heading_size / 2.0);
                            ui.label(heading_font.rich(&section.title));
                            ui.add_space(heading_size / 2.0);

                            for entry in &section.entries {
                                ui.add(egui::Separator::default().spacing(normal_size));
                                ui.horizontal(|ui| {
                                    ui.add_space(bigger_size);

                                    if let Some(name) = &entry.name {
                                        ui.label(bigger_font.rich(name));
                                    }

                                    if let Some(handle) = &entry.handle {
                                        ui.label(bigger_font.rich(&handle.name));
                                    }
                                });
                                ui.horizontal(|ui| {
                                    if let Some(contribution) = &entry.contribution {
                                        ui.with_layout(
                                            egui::Layout::right_to_left(egui::Align::Center),
                                            |ui| {
                                                ui.add_space(bigger_size);
                                                ui.label(bigger_font.rich(format!(
                                                    "{} {contribution}",
                                                    if entry.name.is_some()
                                                        || entry.handle.is_some()
                                                    {
                                                        "-"
                                                    } else {
                                                        ""
                                                    }
                                                )));
                                            },
                                        );
                                    }
                                });
                            }
                            ui.add(egui::Separator::default().spacing(normal_size));
                        }
                    });
                });
            });
        });
}