1
+ use std:: path:: PathBuf ;
2
+
1
3
use bevy:: prelude:: * ;
2
4
use bevy_egui:: EguiContext ;
3
5
use rfd:: FileDialog ;
4
6
5
7
use crate :: {
6
- file:: { pick_folder, PickingEvent , PickingKind } ,
7
- project:: { project_not_loaded, LoadProjectEvent } ,
8
+ file:: { pick_file, pick_folder, PickingEvent , PickingKind } ,
9
+ notification:: { ToastsExt , ToastsStorage } ,
10
+ project:: { create_project, project_not_loaded, LoadProjectEvent , ProjectMeta } ,
8
11
} ;
9
12
13
+ #[ derive( Resource , Debug , Default ) ]
14
+ pub struct CreateProjectForm {
15
+ meta : ProjectMeta ,
16
+ music : Option < PathBuf > ,
17
+ illustration : Option < PathBuf > ,
18
+ }
19
+
10
20
pub struct HomePlugin ;
11
21
12
22
impl Plugin for HomePlugin {
13
23
fn build ( & self , app : & mut App ) {
14
- app. add_systems ( Update , ui_system. run_if ( project_not_loaded ( ) ) )
15
- . add_systems ( Update , load_project_system. run_if ( project_not_loaded ( ) ) ) ;
24
+ app. insert_resource ( CreateProjectForm :: default ( ) )
25
+ . add_systems ( Update , ui_system. run_if ( project_not_loaded ( ) ) )
26
+ . add_systems ( Update , load_project_system. run_if ( project_not_loaded ( ) ) )
27
+ . add_systems (
28
+ Update ,
29
+ (
30
+ handle_select_illustration_system,
31
+ handle_select_music_system,
32
+ handle_create_project_system,
33
+ )
34
+ . run_if ( project_not_loaded ( ) ) ,
35
+ ) ;
16
36
}
17
37
}
18
38
@@ -27,6 +47,39 @@ fn ui_system(world: &mut World) {
27
47
if ui. button ( "Load Project" ) . clicked ( ) {
28
48
pick_folder ( world, PickingKind :: OpenProject , FileDialog :: new ( ) ) ;
29
49
}
50
+
51
+ ui. separator ( ) ;
52
+
53
+ ui. horizontal ( |ui| {
54
+ let form = world. resource_mut :: < CreateProjectForm > ( ) ;
55
+ ui. label ( format ! ( "{:?}" , form. music) ) ;
56
+ if ui. button ( "Select Music" ) . clicked ( ) {
57
+ pick_file ( world, PickingKind :: SelectMusic , FileDialog :: new ( ) ) ;
58
+ }
59
+ } ) ;
60
+ ui. horizontal ( |ui| {
61
+ let form = world. resource_mut :: < CreateProjectForm > ( ) ;
62
+ ui. label ( format ! ( "{:?}" , form. illustration) ) ;
63
+ if ui. button ( "Select Illustration" ) . clicked ( ) {
64
+ pick_file ( world, PickingKind :: SelectIllustration , FileDialog :: new ( ) ) ;
65
+ }
66
+ } ) ;
67
+
68
+ let form = world. resource_mut :: < CreateProjectForm > ( ) ;
69
+ if ui. button ( "Create Project" ) . clicked ( ) {
70
+ if form. music . is_none ( ) {
71
+ let mut toasts = world. resource_mut :: < ToastsStorage > ( ) ;
72
+ toasts. error ( "Music is not selected" ) ;
73
+ return ;
74
+ } ;
75
+ if form. illustration . is_none ( ) {
76
+ let mut toasts = world. resource_mut :: < ToastsStorage > ( ) ;
77
+ toasts. error ( "Illustration is not selected" ) ;
78
+ return ;
79
+ } ;
80
+
81
+ pick_folder ( world, PickingKind :: CreateProject , FileDialog :: new ( ) ) ;
82
+ }
30
83
} ) ;
31
84
}
32
85
@@ -43,3 +96,65 @@ fn load_project_system(
43
96
}
44
97
}
45
98
}
99
+
100
+ fn handle_select_illustration_system (
101
+ mut events : EventReader < PickingEvent > ,
102
+ mut form : ResMut < CreateProjectForm > ,
103
+ ) {
104
+ for PickingEvent { path, kind } in events. read ( ) {
105
+ if !matches ! ( kind, PickingKind :: SelectIllustration ) {
106
+ continue ;
107
+ }
108
+ form. illustration = path. clone ( ) ;
109
+ }
110
+ }
111
+
112
+ fn handle_select_music_system (
113
+ mut events : EventReader < PickingEvent > ,
114
+ mut form : ResMut < CreateProjectForm > ,
115
+ ) {
116
+ for PickingEvent { path, kind } in events. read ( ) {
117
+ if !matches ! ( kind, PickingKind :: SelectMusic ) {
118
+ continue ;
119
+ }
120
+ form. music = path. clone ( ) ;
121
+ }
122
+ }
123
+
124
+ fn handle_create_project_system (
125
+ mut events : EventReader < PickingEvent > ,
126
+ form : Res < CreateProjectForm > ,
127
+ mut load_project_events : EventWriter < LoadProjectEvent > ,
128
+
129
+ mut toasts : ResMut < ToastsStorage > ,
130
+ ) {
131
+ for PickingEvent { path, kind } in events. read ( ) {
132
+ if !matches ! ( kind, PickingKind :: CreateProject ) {
133
+ continue ;
134
+ }
135
+
136
+ let Some ( root_path) = path else {
137
+ return ;
138
+ } ;
139
+
140
+ let Some ( ref music_path) = form. music else {
141
+ return ;
142
+ } ;
143
+
144
+ let Some ( ref illustration_path) = form. illustration else {
145
+ return ;
146
+ } ;
147
+
148
+ match create_project (
149
+ root_path. clone ( ) ,
150
+ music_path. clone ( ) ,
151
+ illustration_path. clone ( ) ,
152
+ form. meta . clone ( ) ,
153
+ ) {
154
+ Ok ( _) => {
155
+ load_project_events. send ( LoadProjectEvent ( root_path. clone ( ) ) ) ;
156
+ } ,
157
+ Err ( error) => toasts. error ( format ! ( "{:?}" , error) ) ,
158
+ }
159
+ }
160
+ }
0 commit comments