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
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),
);
});
}
}