ngal-gui 0.3.0

一个用 Rust 编写的galgame引擎,ngal引擎的egui版本
use crate::app::{NgalApp, Screen};
use eframe::egui::{
    self, Align2, Color32, FontId, Pos2, Rect, RichText, Stroke, StrokeKind, UiBuilder, Vec2,
};

const PANEL: Color32 = Color32::from_rgba_premultiplied(12, 16, 21, 232);

#[derive(Clone, Copy)]
enum MenuAction {
    StartGame,
    LoadGame,
    Settings,
    Exit,
}

impl NgalApp {
    pub(crate) fn draw_menu(&mut self, ctx: &egui::Context) {
        egui::CentralPanel::default()
            .frame(egui::Frame::NONE)
            .show(ctx, |ui| {
                let rect = ui.max_rect();
                let (accent_r, accent_g, accent_b) = self.config.accent_color();
                let accent = Color32::from_rgb(accent_r, accent_g, accent_b);
                let dim = Color32::from_black_alpha(
                    (self.config.menu_opacity * 185.0).clamp(20.0, 230.0) as u8,
                );
                if let Some(tex) = &self.background {
                    ui.painter().image(
                        tex.id(),
                        rect,
                        Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)),
                        Color32::WHITE,
                    );
                    ui.painter().rect_filled(rect, 0.0, dim);
                } else {
                    ui.painter()
                        .rect_filled(rect, 0.0, Color32::from_rgb(15, 18, 20));
                }

                let mut action = None;
                let layout = self.game_config.menu_layout.clamp(1, 5);
                let title = self.game_config.title.clone();
                let draw_title = |ui: &mut egui::Ui, size: f32| {
                    ui.label(
                        RichText::new(&title)
                            .size(size)
                            .strong()
                            .color(Color32::WHITE),
                    );
                    ui.add_space(6.0);
                    ui.label(
                        RichText::new("视觉小说引擎")
                            .size((size * 0.34).max(13.0))
                            .color(Color32::from_gray(180)),
                    );
                };
                let render_buttons =
                    |ui: &mut egui::Ui, width: f32, action: &mut Option<MenuAction>| {
                        let width = width.min(ui.available_width()).max(180.0);
                        for (index, (label, selected)) in [
                            ("开始游戏", MenuAction::StartGame),
                            ("读取存档", MenuAction::LoadGame),
                            ("设置", MenuAction::Settings),
                            ("退出", MenuAction::Exit),
                        ]
                        .iter()
                        .enumerate()
                        {
                            let button = egui::Button::new(RichText::new(*label).size(17.0).color(
                                if index == 0 {
                                    Color32::from_rgb(18, 35, 31)
                                } else {
                                    Color32::from_gray(225)
                                },
                            ))
                            .fill(if index == 0 {
                                accent
                            } else {
                                Color32::from_rgba_premultiplied(30, 37, 42, 235)
                            })
                            .stroke(Stroke::new(
                                1.0_f32,
                                if index == 0 {
                                    accent
                                } else {
                                    Color32::from_gray(75)
                                },
                            ));
                            if ui.add_sized(Vec2::new(width, 44.0), button).clicked() {
                                *action = Some(*selected);
                            }
                            if index < 3 {
                                ui.add_space(8.0);
                            }
                        }
                    };

                match layout {
                    1 => {
                        ui.vertical_centered(|ui| {
                            ui.add_space((rect.height() * 0.19).clamp(48.0, 180.0));
                            draw_title(ui, (rect.width() * 0.045).clamp(30.0, 54.0));
                            ui.add_space((rect.height() * 0.07).clamp(24.0, 56.0));
                            render_buttons(ui, 260.0, &mut action);
                        });
                    }
                    2 => {
                        let width = rect.width().min(520.0) * 0.62;
                        let left = (rect.width() * 0.09).min(96.0);
                        let top = (rect.height() * 0.18).min(150.0);
                        ui.allocate_new_ui(
                            UiBuilder::new().max_rect(Rect::from_min_size(
                                Pos2::new(rect.left() + left, rect.top() + top),
                                Vec2::new(width, rect.height() - top - 32.0),
                            )),
                            |ui| {
                                draw_title(ui, (rect.width() * 0.04).clamp(28.0, 48.0));
                                ui.add_space(38.0);
                                render_buttons(ui, width.min(270.0), &mut action);
                            },
                        );
                    }
                    3 => {
                        let panel_width =
                            (rect.width() * 0.34).clamp(280.0, 420.0).min(rect.width());
                        let panel_rect = Rect::from_min_max(
                            Pos2::new(rect.right() - panel_width, rect.top()),
                            rect.max,
                        );
                        ui.painter().rect_filled(panel_rect, 0.0, PANEL);
                        ui.allocate_new_ui(
                            UiBuilder::new().max_rect(panel_rect.shrink(28.0)),
                            |ui| {
                                ui.add_space((rect.height() * 0.12).min(100.0));
                                draw_title(ui, (panel_width * 0.1).clamp(28.0, 40.0));
                                ui.add_space(42.0);
                                render_buttons(ui, ui.available_width(), &mut action);
                            },
                        );
                    }
                    4 => {
                        ui.vertical_centered(|ui| {
                            ui.add_space((rect.height() * 0.23).clamp(60.0, 200.0));
                            draw_title(ui, (rect.width() * 0.043).clamp(30.0, 52.0));
                        });
                        let bar_height = 78.0_f32.min(rect.height() * 0.2);
                        let bar_rect = Rect::from_min_max(
                            Pos2::new(rect.left(), rect.bottom() - bar_height),
                            rect.right_bottom(),
                        );
                        ui.painter().rect_filled(bar_rect, 0.0, PANEL);
                        ui.allocate_new_ui(
                            UiBuilder::new().max_rect(bar_rect.shrink(14.0)),
                            |ui| {
                                ui.horizontal_wrapped(|ui| {
                                    ui.spacing_mut().item_spacing.x = 8.0;
                                    render_buttons(
                                        ui,
                                        ((ui.available_width() - 24.0) / 4.0).max(94.0),
                                        &mut action,
                                    );
                                });
                            },
                        );
                    }
                    _ => {
                        let card_size = Vec2::new(
                            rect.width().min(540.0) - 32.0,
                            rect.height().min(470.0) - 32.0,
                        );
                        let card_rect = Rect::from_center_size(rect.center(), card_size);
                        ui.painter().rect_filled(card_rect, 8.0, PANEL);
                        ui.painter().rect_stroke(
                            card_rect,
                            8.0,
                            Stroke::new(1.0_f32, accent.gamma_multiply(0.55)),
                            StrokeKind::Inside,
                        );
                        ui.allocate_new_ui(
                            UiBuilder::new().max_rect(card_rect.shrink(28.0)),
                            |ui| {
                                ui.vertical_centered(|ui| {
                                    ui.add_space(8.0);
                                    draw_title(ui, (card_size.x * 0.09).clamp(28.0, 40.0));
                                    ui.add_space(30.0);
                                    render_buttons(ui, 260.0, &mut action);
                                });
                            },
                        );
                    }
                }

                if let Some(action) = action {
                    match action {
                        MenuAction::StartGame => self.start_game(ctx),
                        MenuAction::LoadGame => {
                            self.last_screen = Screen::Menu;
                            self.screen = Screen::Load;
                            self.saveload_cache_dirty = true;
                        }
                        MenuAction::Settings => {
                            self.last_screen = Screen::Menu;
                            self.screen = Screen::Settings;
                        }
                        MenuAction::Exit => ctx.send_viewport_cmd(egui::ViewportCommand::Close),
                    }
                }
                ui.painter().text(
                    rect.right_bottom() - Vec2::new(16.0, 12.0),
                    Align2::RIGHT_BOTTOM,
                    format!("v{}", env!("CARGO_PKG_VERSION")),
                    FontId::proportional(12.0),
                    Color32::from_gray(150),
                );
            });
    }
}