@@ -8,8 +8,12 @@ use crate::structure::{AtomEntity, Crystal};
88#[ derive( Component ) ]
99pub ( crate ) struct FileUploadText ;
1010
11+ // Component for load default button
12+ #[ derive( Component ) ]
13+ pub ( crate ) struct LoadDefaultButton ;
14+
1115// System to set up file upload UI
12- pub fn setup_file_ui ( mut commands : Commands ) {
16+ pub ( crate ) fn setup_file_ui ( mut commands : Commands ) {
1317 commands. spawn ( (
1418 Text :: new ( "Drag and drop an XYZ file here to visualize" ) ,
1519 TextFont {
@@ -25,15 +29,40 @@ pub fn setup_file_ui(mut commands: Commands) {
2529 } ,
2630 FileUploadText ,
2731 ) ) ;
32+
33+ // Add button to load default structure
34+ commands
35+ . spawn ( (
36+ Button ,
37+ Node {
38+ position_type : PositionType :: Absolute ,
39+ top : Val :: Px ( 40.0 ) ,
40+ left : Val :: Px ( 10.0 ) ,
41+ padding : UiRect :: all ( Val :: Px ( 10.0 ) ) ,
42+ ..default ( )
43+ } ,
44+ BackgroundColor ( Color :: srgb ( 0.15 , 0.15 , 0.15 ) ) ,
45+ LoadDefaultButton ,
46+ ) )
47+ . with_children ( |parent| {
48+ parent. spawn ( (
49+ Text :: new ( "Load Default Structure" ) ,
50+ TextFont {
51+ font_size : 18.0 ,
52+ ..default ( )
53+ } ,
54+ TextColor ( Color :: WHITE ) ,
55+ ) ) ;
56+ } ) ;
2857}
2958
3059// System to update file upload UI
31- pub fn update_file_ui (
60+ pub ( crate ) fn update_file_ui (
3261 file_drag_drop : Res < crate :: io:: FileDragDrop > ,
3362 mut text_query : Query < & mut Text , With < FileUploadText > > ,
3463) {
3564 if let Ok ( mut text) = text_query. single_mut ( ) {
36- if let Some ( ref path) = file_drag_drop. dragged_file {
65+ if let Some ( path) = file_drag_drop. dragged_file ( ) {
3766 if let Some ( file_name) = path. file_name ( ) . and_then ( |n| n. to_str ( ) ) {
3867 // Update the text content
3968 * * text = format ! ( "Loaded: {}" , file_name) ;
@@ -44,25 +73,34 @@ pub fn update_file_ui(
4473 }
4574}
4675
47- // System to clear existing atoms when new crystal is loaded
48- #[ allow( dead_code) ]
49- pub fn clear_old_atoms ( mut commands : Commands , atom_query : Query < Entity , With < AtomEntity > > ) {
50- for entity in atom_query. iter ( ) {
51- commands. entity ( entity) . despawn ( ) ;
76+ // System to handle button click to load default structure
77+ pub ( crate ) fn handle_load_default_button (
78+ mut interaction_query : Query <
79+ ( & Interaction , & mut BackgroundColor ) ,
80+ ( Changed < Interaction > , With < LoadDefaultButton > ) ,
81+ > ,
82+ mut commands : Commands ,
83+ ) {
84+ for ( interaction, mut color) in & mut interaction_query {
85+ match * interaction {
86+ Interaction :: Pressed => {
87+ * color = BackgroundColor ( Color :: srgb ( 0.35 , 0.35 , 0.35 ) ) ;
88+ // Load default water molecule
89+ crate :: io:: load_default_crystal ( commands. reborrow ( ) ) ;
90+ }
91+ Interaction :: Hovered => {
92+ * color = BackgroundColor ( Color :: srgb ( 0.25 , 0.25 , 0.25 ) ) ;
93+ }
94+ Interaction :: None => {
95+ * color = BackgroundColor ( Color :: srgb ( 0.15 , 0.15 , 0.15 ) ) ;
96+ }
97+ }
5298 }
5399}
54100
55101// Updated setup_scene to handle crystal changes
56- pub fn setup_scene (
57- mut commands : Commands ,
58- mut meshes : ResMut < Assets < Mesh > > ,
59- mut materials : ResMut < Assets < StandardMaterial > > ,
60- crystal : Res < Crystal > ,
61- ) {
62- // Only spawn atoms if we have a crystal resource
63- spawn_atoms ( & mut commands, & mut meshes, & mut materials, & crystal) ;
64-
65- // Add ambient light
102+ pub ( crate ) fn setup_scene ( mut commands : Commands ) {
103+ // Start with empty scene - only add ambient light
66104 commands. insert_resource ( AmbientLight {
67105 color : Color :: WHITE ,
68106 brightness : 0.3 ,
@@ -71,23 +109,25 @@ pub fn setup_scene(
71109}
72110
73111// System to respawn atoms when crystal changes
74- pub fn update_scene (
112+ pub ( crate ) fn update_scene (
75113 mut commands : Commands ,
76114 mut meshes : ResMut < Assets < Mesh > > ,
77115 mut materials : ResMut < Assets < StandardMaterial > > ,
78- crystal : Res < Crystal > ,
116+ crystal : Option < Res < Crystal > > ,
79117 atom_query : Query < Entity , With < AtomEntity > > ,
80118) {
81- if crystal. is_changed ( ) {
82- // Clear existing atoms
83- for entity in atom_query. iter ( ) {
84- commands. entity ( entity) . despawn ( ) ;
85- }
119+ if let Some ( crystal) = crystal {
120+ if crystal. is_changed ( ) {
121+ // Clear existing atoms
122+ for entity in atom_query. iter ( ) {
123+ commands. entity ( entity) . despawn ( ) ;
124+ }
86125
87- // Spawn new atoms
88- spawn_atoms ( & mut commands, & mut meshes, & mut materials, & crystal) ;
126+ // Spawn new atoms
127+ spawn_atoms ( & mut commands, & mut meshes, & mut materials, & crystal) ;
89128
90- println ! ( "Scene updated with new crystal structure" ) ;
129+ println ! ( "Scene updated with new crystal structure" ) ;
130+ }
91131 }
92132}
93133
@@ -130,7 +170,7 @@ fn spawn_atoms(
130170}
131171
132172// System to set up the camera
133- pub fn setup_camera ( mut commands : Commands ) {
173+ pub ( crate ) fn setup_camera ( mut commands : Commands ) {
134174 // Spawn camera
135175 commands
136176 . spawn ( (
@@ -150,7 +190,7 @@ pub fn setup_camera(mut commands: Commands) {
150190}
151191
152192// Simple camera controls
153- pub fn camera_controls (
193+ pub ( crate ) fn camera_controls (
154194 mut camera_query : Query < & mut Transform , With < Camera3d > > ,
155195 keyboard_input : Res < ButtonInput < KeyCode > > ,
156196 time : Res < Time > ,
0 commit comments