Skip to content

Commit df3ed8e

Browse files
committed
integrated egui-toast for notification
1 parent 980455b commit df3ed8e

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ fastblur = "0.1.1"
7171
rfd = "0.14.1"
7272
futures-lite = "2.3.0"
7373
anyhow = "1.0.83"
74+
egui-toast = "0.13.0"

src/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod hit_sound;
77
mod home;
88
mod loader;
99
mod misc;
10+
mod notification;
1011
mod project;
1112
mod score;
1213
mod selection;
@@ -24,7 +25,9 @@ use crate::exporter::Exporter;
2425
use crate::home::HomePlugin;
2526
use crate::misc::MiscPlugin;
2627
use crate::misc::WorkingDirectory;
28+
use crate::notification::NotificationPlugin;
2729
use crate::project::project_loaded;
30+
use crate::project::LoadProjectEvent;
2831
use crate::project::ProjectPlugin;
2932
use crate::score::ScorePlugin;
3033
use crate::tab::game::GameCamera;
@@ -45,7 +48,6 @@ use bevy_egui::egui::{Color32, Frame};
4548
use bevy_egui::{EguiContext, EguiPlugin};
4649
use bevy_mod_picking::prelude::*;
4750
use egui_dock::{DockArea, DockState, NodeIndex, Style};
48-
use project::LoadProjectEvent;
4951

5052
fn main() {
5153
App::new()
@@ -67,6 +69,7 @@ fn main() {
6769
.add_plugins(FrameTimeDiagnosticsPlugin::default())
6870
.add_plugins(AssetsPlugin)
6971
.add_plugins(TranslationPlugin)
72+
.add_plugins(NotificationPlugin)
7073
.add_systems(Startup, setup_egui_image_loader_system)
7174
.add_systems(Startup, setup_egui_font_system)
7275
.add_systems(Startup, setup_plugin)

src/notification.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use bevy::prelude::*;
2+
use bevy_egui::EguiContext;
3+
use egui::{Align2, WidgetText};
4+
use egui_toast::{Toast, ToastKind, ToastOptions, Toasts};
5+
6+
#[derive(Resource, Deref, DerefMut)]
7+
pub struct ToastsStorage(Toasts);
8+
9+
pub struct NotificationPlugin;
10+
11+
impl Plugin for NotificationPlugin {
12+
fn build(&self, app: &mut bevy::prelude::App) {
13+
app.init_resource::<ToastsStorage>()
14+
.add_systems(Update, show_egui_notifies);
15+
}
16+
}
17+
18+
pub trait ToastsExt {
19+
fn error(&mut self, message: impl Into<WidgetText>);
20+
}
21+
22+
impl ToastsExt for Toasts {
23+
fn error(&mut self, text: impl Into<WidgetText>) {
24+
self.add(Toast {
25+
text: text.into(),
26+
kind: ToastKind::Error,
27+
options: ToastOptions::default()
28+
.duration_in_seconds(8.0)
29+
.show_progress(true),
30+
});
31+
}
32+
}
33+
34+
impl Default for ToastsStorage {
35+
fn default() -> Self {
36+
Self(Toasts::new().anchor(Align2::RIGHT_TOP, (-10.0, 10.0)))
37+
}
38+
}
39+
40+
fn show_egui_notifies(mut context: Query<&mut EguiContext>, mut toasts: ResMut<ToastsStorage>) {
41+
if let Ok(mut ctx) = context.get_single_mut() {
42+
toasts.show(ctx.get_mut())
43+
}
44+
}

src/project.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ use serde::{Deserialize, Serialize};
44

55
use std::{fs::File, path::PathBuf};
66

7-
use crate::{loader::{phichain::PhiChainLoader, Loader}, serialzation::PhiChainChart};
7+
use crate::{
8+
loader::{phichain::PhiChainLoader, Loader},
9+
notification::{ToastsExt, ToastsStorage},
10+
serialzation::PhiChainChart,
11+
};
812

9-
#[derive(Serialize, Deserialize)]
13+
#[derive(Debug, Serialize, Deserialize, Default)]
1014
pub struct ProjectMeta {
1115
pub composer: String,
1216
pub charter: String,
@@ -38,6 +42,10 @@ impl ProjectPath {
3842
self.0.join("music.wav")
3943
}
4044

45+
pub fn illustration_path(&self) -> PathBuf {
46+
self.0.join("illustration.png")
47+
}
48+
4149
pub fn meta_path(&self) -> PathBuf {
4250
self.0.join("meta.json")
4351
}
@@ -49,6 +57,9 @@ impl ProjectPath {
4957
if !self.music_path().is_file() {
5058
bail!("music.wav is missing");
5159
}
60+
if !self.illustration_path().is_file() {
61+
bail!("illustration.png is missing");
62+
}
5263
if !self.meta_path().is_file() {
5364
bail!("meta.json is missing");
5465
}
@@ -89,7 +100,11 @@ impl Plugin for ProjectPlugin {
89100
#[derive(Event, Debug)]
90101
pub struct LoadProjectEvent(pub PathBuf);
91102

92-
fn load_project_system(mut commands: Commands, mut events: EventReader<LoadProjectEvent>) {
103+
fn load_project_system(
104+
mut commands: Commands,
105+
mut events: EventReader<LoadProjectEvent>,
106+
mut toasts: ResMut<ToastsStorage>,
107+
) {
93108
if events.len() > 1 {
94109
warn!("Mutiple projects are requested, ignoring previous ones");
95110
}
@@ -100,9 +115,9 @@ fn load_project_system(mut commands: Commands, mut events: EventReader<LoadProje
100115
let file = File::open(project.root_dir.join("chart.json")).unwrap();
101116
PhiChainLoader::load(file, &mut commands);
102117
commands.insert_resource(project);
103-
},
118+
}
104119
Err(error) => {
105-
error!("{:?}", error)
120+
toasts.error(format!("Failed to open project: {:?}", error));
106121
}
107122
}
108123
}

0 commit comments

Comments
 (0)