ngal-gui 0.3.0

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

const MINT: Color32 = Color32::from_rgb(125, 229, 186);

impl NgalApp {
    /// Render the configured splash sequence with responsive letterboxing and fades.
    pub fn draw_splash(&mut self, ctx: &egui::Context) {
        self.splash_timer += ctx.input(|i| i.unstable_dt.max(0.0));
        let skip = ctx.input(|i| i.pointer.any_click() || !i.keys_down.is_empty());
        if self.game_config.splash_screens.is_empty() {
            self.screen = Screen::Menu;
            return;
        }

        let fade_in = 0.55;
        let hold = 1.7;
        let fade_out = 0.55;
        let duration = fade_in + hold + fade_out;
        if skip || self.splash_timer >= duration {
            self.splash_index += 1;
            self.splash_timer = 0.0;
            if self.splash_index >= self.game_config.splash_screens.len() {
                self.screen = Screen::Menu;
                return;
            }
        }

        let filename = self.game_config.splash_screens[self.splash_index].clone();
        let texture = self.splash_textures.get(&filename).cloned().or_else(|| {
            let loaded = load_texture(ctx, &format!("assets/portraits/{filename}"))
                .or_else(|| load_texture(ctx, &filename));
            if let Some(tex) = &loaded {
                self.splash_textures.insert(filename.clone(), tex.clone());
            }
            loaded
        });
        let alpha = if self.splash_timer < fade_in {
            self.splash_timer / fade_in
        } else if self.splash_timer < fade_in + hold {
            1.0
        } else {
            1.0 - (self.splash_timer - fade_in - hold) / fade_out
        };

        egui::CentralPanel::default()
            .frame(egui::Frame::NONE.fill(Color32::from_rgb(8, 11, 12)))
            .show(ctx, |ui| {
                let rect = ui.max_rect();
                ui.painter()
                    .rect_filled(rect, 0.0, Color32::from_rgb(8, 11, 12));
                if let Some(texture) = texture {
                    let size = texture.size_vec2();
                    if size.x > 0.0 && size.y > 0.0 {
                        let scale = (rect.width() / size.x).min(rect.height() / size.y);
                        let image_rect = Rect::from_center_size(rect.center(), size * scale);
                        ui.painter().image(
                            texture.id(),
                            image_rect,
                            Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)),
                            Color32::from_white_alpha((alpha.clamp(0.0, 1.0) * 255.0) as u8),
                        );
                    }
                } else {
                    ui.vertical_centered(|ui| {
                        ui.add_space(rect.height() * 0.44);
                        ui.label(
                            RichText::new(&self.game_config.title)
                                .size(30.0)
                                .strong()
                                .color(MINT),
                        );
                    });
                }
                ui.painter().text(
                    rect.left_bottom() + Vec2::new(22.0, -18.0),
                    egui::Align2::LEFT_BOTTOM,
                    "NGAL",
                    egui::FontId::proportional(12.0),
                    Color32::from_rgba_premultiplied(
                        200,
                        235,
                        220,
                        (alpha.clamp(0.0, 1.0) * 190.0) as u8,
                    ),
                );
            });
        ctx.request_repaint();
    }
}