ngal-gui 0.3.0

一个用 Rust 编写的galgame引擎,ngal引擎的egui版本
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
use crate::app::{NgalApp, Screen};
use crate::save::SaveData;
use crate::texture::load_texture;
use eframe::egui::{
    self, Color32, FontId, Pos2, Rect, RichText, Stroke, StrokeKind, Ui, UiBuilder, Vec2,
};

pub fn draw_dialog(ui: &mut Ui, screen: Rect, app: &mut NgalApp, ctx: &egui::Context) -> bool {
    if app.line_index >= app.parser.nodes.len() {
        app.screen = Screen::Menu;
        app.last_screen = Screen::Menu;
        let default_bg_path = app.game_config.get_background_path();
        app.background = load_texture(ctx, &default_bg_path);
        app.current_bg_path = Some(default_bg_path);
        return false;
    }

    let mut interacted = false;

    // ==========================================
    // 0. 如果开启了历史记录面板(Log / Backlog)
    // ==========================================
    if app.show_log {
        let log_rect = Rect::from_min_max(
            Pos2::new(screen.left() + 40.0, screen.top() + 40.0),
            Pos2::new(screen.right() - 40.0, screen.bottom() - 40.0),
        );

        // 半透明毛玻璃背景遮罩
        ui.painter()
            .rect_filled(screen, 0.0, Color32::from_black_alpha(180));
        ui.painter().rect_filled(
            log_rect,
            12.0,
            Color32::from_rgba_premultiplied(18, 22, 38, 245),
        );
        ui.painter().rect_stroke(
            log_rect,
            12.0,
            Stroke::new(1.5_f32, Color32::from_rgb(70, 130, 180)),
            StrokeKind::Inside,
        );

        let mut close_log = false;
        let mut jump_target_index: Option<usize> = None;

        ui.allocate_new_ui(UiBuilder::new().max_rect(log_rect.shrink(24.0)), |ui| {
            ui.horizontal(|ui| {
                ui.label(
                    RichText::new("对话历史 (Backlog) - 点击记录可直接跳转回溯")
                        .size(16.0)
                        .strong()
                        .color(Color32::from_rgb(255, 220, 90)),
                );
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    if ui
                        .button(RichText::new("✖ 关闭").size(14.0).color(Color32::WHITE))
                        .clicked()
                    {
                        close_log = true;
                    }
                });
            });
            ui.add_space(10.0);
            ui.separator();
            ui.add_space(10.0);

            // 滚动区域展示历史记录
            egui::ScrollArea::vertical()
                .max_height(log_rect.height() - 80.0)
                .show(ui, |ui| {
                    for (idx, (speaker, text)) in app.history.iter().enumerate() {
                        ui.horizontal(|ui| {
                            let speaker_text = if speaker.is_empty() {
                                "旁白".to_string()
                            } else {
                                speaker.clone()
                            };

                            let item_text = format!("【{}】: {}", speaker_text, text);
                            if ui
                                .add(
                                    egui::Button::new(
                                        RichText::new(item_text)
                                            .size(14.0)
                                            .color(Color32::from_rgb(220, 220, 220)),
                                    )
                                    .frame(false),
                                )
                                .clicked()
                            {
                                jump_target_index = Some(idx);
                            }
                        });
                        ui.add_space(6.0);
                    }
                });
        });

        if close_log {
            app.show_log = false;
        }

        if let Some(history_idx) = jump_target_index {
            if history_idx < app.history.len() {
                let target_text = &app.history[history_idx].1;
                for (n_idx, node) in app.parser.nodes.iter().enumerate() {
                    if let Some(txt) = &node.text {
                        if txt == target_text {
                            app.line_index = n_idx;
                            app.execute_node(ctx);
                            app.show_log = false;
                            break;
                        }
                    }
                }
            }
        }

        // Closing the backlog or jumping from it must not also advance the story.
        return true;
    }

    // ==========================================
    // 1. 底层:对话框与工具菜单
    // ==========================================
    let box_height = screen.height() * 0.22;
    let margin = 20.0;
    let dialog_rect = Rect::from_min_max(
        Pos2::new(
            screen.left() + margin,
            screen.bottom() - box_height - margin,
        ),
        Pos2::new(screen.right() - margin, screen.bottom() - margin),
    );

    // 角色名字
    if !app.speaker.is_empty() {
        let font_id = FontId::proportional(16.0);
        let galley = ui.painter().layout_no_wrap(
            app.speaker.clone(),
            font_id,
            Color32::from_rgb(255, 220, 90),
        );
        let padding = 16.0;
        let name_size = Vec2::new(galley.size().x + padding * 2.0, galley.size().y + 10.0);
        let name_pos = Pos2::new(dialog_rect.left(), dialog_rect.top() - name_size.y - 6.0);
        let name_rect = Rect::from_min_size(name_pos, name_size);

        let name_alpha = (app.config.nameplate_opacity.clamp(0.1, 1.0) * 255.0) as u8;
        ui.painter().rect_filled(
            name_rect,
            6.0,
            Color32::from_rgba_premultiplied(30, 40, 65, name_alpha),
        );
        ui.painter().rect_stroke(
            name_rect,
            6.0,
            Stroke::new(1.0_f32, Color32::from_rgb(70, 130, 180)),
            StrokeKind::Inside,
        );

        let text_pos = name_rect.min + Vec2::new(padding, 5.0);
        ui.painter()
            .galley(text_pos, galley, Color32::from_rgb(255, 220, 90));
    }

    // 对话框背景与边框
    let alpha = (app.config.dialog_opacity * 255.0).clamp(0.0, 255.0) as u8;
    let bg_color = Color32::from_rgba_premultiplied(15, 18, 28, alpha);
    let border_color = Color32::from_rgba_premultiplied(70, 130, 180, alpha.max(100));

    ui.painter().rect_filled(dialog_rect, 10.0, bg_color);
    ui.painter().rect_stroke(
        dialog_rect,
        10.0,
        Stroke::new(1.5_f32, border_color),
        StrokeKind::Inside,
    );

    // Keep dialogue full-width and anchor the persistent toolbar to its lower-right.
    let content_rect = dialog_rect.shrink(18.0);
    let toolbar_height = if content_rect.width() < 520.0 {
        56.0
    } else {
        28.0
    };
    let text_rect = Rect::from_min_max(
        content_rect.min,
        Pos2::new(
            content_rect.right(),
            (content_rect.bottom() - toolbar_height).max(content_rect.top()),
        ),
    );
    ui.allocate_new_ui(UiBuilder::new().max_rect(text_rect), |ui| {
        let Some(current_node) = app.parser.nodes.get(app.line_index) else {
            return;
        };
        let display_text = if app.waiting_input {
            app.input_prompt.clone()
        } else if !current_node.choices.is_empty() {
            current_node
                .text
                .clone()
                .unwrap_or_else(|| "请选择分支".to_string())
        } else {
            app.typewriter.visible_text().to_string()
        };
        ui.add(
            egui::Label::new(
                RichText::new(display_text)
                    .size(app.config.dialog_font_size)
                    .color(Color32::from_rgb(240, 240, 240)),
            )
            .wrap(),
        );
    });

    let toolbar_rect = Rect::from_min_max(
        Pos2::new(
            content_rect.left(),
            (content_rect.bottom() - toolbar_height).max(content_rect.top()),
        ),
        content_rect.max,
    );
    ui.allocate_new_ui(UiBuilder::new().max_rect(toolbar_rect), |ui| {
        ui.with_layout(egui::Layout::bottom_up(egui::Align::RIGHT), |ui| {
            ui.horizontal_wrapped(|ui| {
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    let accent = app.config.accent_color();
                    let hover_time = ctx.input(|input| input.time) as f32;
                    ui.ctx()
                        .request_repaint_after(std::time::Duration::from_millis(40));
                    let toolbar_btn = |ui: &mut Ui, label: &str, active: bool| -> bool {
                        let text = if active {
                            format!("{label} ·")
                        } else {
                            label.to_owned()
                        };
                        let response = ui
                            .add(
                                egui::Label::new(RichText::new(text).size(12.0).strong().color(
                                    if active {
                                        {
                                            Color32::from_rgb(accent.0, accent.1, accent.2)
                                        }
                                    } else {
                                        Color32::from_rgb(235, 240, 238)
                                            .gamma_multiply(0.92 + 0.08 * (hover_time.sin().abs()))
                                    },
                                ))
                                .selectable(false)
                                .sense(egui::Sense::click()),
                            )
                            .on_hover_cursor(egui::CursorIcon::PointingHand)
                            .on_hover_text(if active {
                                format!("{label}(已开启)")
                            } else {
                                label.to_owned()
                            });
                        response.clicked()
                    };

                    if toolbar_btn(ui, "Settings", false) {
                        interacted = true;
                        app.screen = Screen::Settings;
                    }
                    if toolbar_btn(ui, "Save", false) {
                        interacted = true;
                        app.last_screen = Screen::Game;
                        app.screen = Screen::Save;
                        app.saveload_cache_dirty = true;
                    }
                    if toolbar_btn(ui, "Q.S", false) {
                        interacted = true;
                        let data = app.create_save_data();
                        if SaveData::save_to_slot(99, &data).is_ok() {
                            app.save_slots_cache.insert(99, Some(data));
                            app.save_thumb_cache.insert(99, None);
                            app.save_notice = Some(("快速存档已保存".into(), 2.0));
                        } else {
                            app.save_notice = Some(("快速存档失败".into(), 4.0));
                        }
                    }
                    if toolbar_btn(ui, "Q.L", false) {
                        interacted = true;
                        match SaveData::load_from_slot(99) {
                            Some(data) => app.load_save_data(data, ctx),
                            None => app.save_notice = Some(("没有可读取的快速存档".into(), 3.0)),
                        }
                    }
                    if toolbar_btn(ui, "Load", false) {
                        interacted = true;
                        app.last_screen = Screen::Game;
                        app.screen = Screen::Load;
                        app.saveload_cache_dirty = true;
                    }
                    if toolbar_btn(ui, "History", false) {
                        interacted = true;
                        app.show_log = !app.show_log;
                    }
                    if toolbar_btn(ui, "Skip", app.is_skipping) {
                        interacted = true;
                        app.is_skipping = !app.is_skipping;
                    }
                    if toolbar_btn(ui, "Auto", app.auto_play) {
                        interacted = true;
                        app.auto_play = !app.auto_play;
                    }
                });
            });
        });
    });

    // ==========================================
    // 2. Choose 选择支或 Input 输入状态
    // ==========================================
    let Some(current_node) = app.parser.nodes.get(app.line_index) else {
        return interacted;
    };
    if !current_node.choices.is_empty() {
        let choices = current_node.choices.clone();
        let box_height = (choices.len() as f32 * 46.0) + 70.0;
        let center_rect = Rect::from_center_size(screen.center(), Vec2::new(480.0, box_height));

        ui.painter()
            .rect_filled(screen, 0.0, Color32::from_black_alpha(100));

        ui.painter().rect_filled(
            center_rect,
            12.0,
            Color32::from_rgba_premultiplied(18, 22, 38, 245),
        );
        ui.painter().rect_stroke(
            center_rect,
            12.0,
            Stroke::new(1.5_f32, Color32::from_rgb(70, 130, 180)),
            StrokeKind::Inside,
        );

        ui.allocate_new_ui(UiBuilder::new().max_rect(center_rect.shrink(20.0)), |ui| {
            ui.vertical_centered(|ui| {
                ui.label(
                    RichText::new("请做出你的选择")
                        .size(18.0)
                        .strong()
                        .color(Color32::from_rgb(255, 220, 90)),
                );
                ui.add_space(14.0);
                for (text, target) in choices {
                    let btn = ui.add_sized(
                        Vec2::new(400.0, 38.0),
                        egui::Button::new(RichText::new(&text).size(16.0).color(Color32::WHITE)),
                    );
                    if btn.clicked() {
                        interacted = true;
                        app.jump_to_label(&target, ctx);
                    }
                    ui.add_space(6.0);
                }
            });
        });
        return interacted;
    }

    if app.waiting_input {
        let center_rect = Rect::from_center_size(screen.center(), Vec2::new(440.0, 160.0));

        ui.painter()
            .rect_filled(screen, 0.0, Color32::from_black_alpha(100));

        ui.painter().rect_filled(
            center_rect,
            12.0,
            Color32::from_rgba_premultiplied(18, 22, 38, 245),
        );
        ui.painter().rect_stroke(
            center_rect,
            12.0,
            Stroke::new(1.5_f32, Color32::from_rgb(70, 130, 180)),
            StrokeKind::Inside,
        );

        ui.allocate_new_ui(UiBuilder::new().max_rect(center_rect.shrink(20.0)), |ui| {
            ui.vertical_centered(|ui| {
                let prompt_title = if app.input_prompt.is_empty() {
                    "请输入内容"
                } else {
                    &app.input_prompt
                };
                ui.label(
                    RichText::new(prompt_title)
                        .size(18.0)
                        .strong()
                        .color(Color32::from_rgb(255, 220, 90)),
                );
                ui.add_space(16.0);
                let resp = ui.add(
                    egui::TextEdit::singleline(&mut app.input_buffer)
                        .desired_width(360.0)
                        .font(FontId::proportional(16.0)),
                );
                ui.add_space(16.0);
                if ui
                    .add_sized(
                        Vec2::new(120.0, 32.0),
                        egui::Button::new(RichText::new("确定").size(15.0)),
                    )
                    .clicked()
                    || (resp.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)))
                {
                    if !app.input_buffer.is_empty() {
                        app.parser
                            .variables
                            .insert(app.input_var_name.clone(), app.input_buffer.clone());
                    }
                    app.waiting_input = false;
                    app.advance(ctx);
                    interacted = true;
                }
            });
        });
        return interacted;
    }
    interacted
}