@@ -15,7 +15,8 @@ use crate::components::wall::WallBlock;
15
15
use crate :: grid:: grid:: Grid ;
16
16
use crate :: math:: constants:: * ;
17
17
use crate :: math:: fft:: calc_mic_spectrum;
18
- use crate :: math:: transformations:: f32_map_range;
18
+ use crate :: math:: transformations:: { coords_to_index, f32_map_range} ;
19
+ use crate :: render:: draw:: GradientResource ;
19
20
use crate :: ui:: state:: * ;
20
21
21
22
pub fn draw_egui (
@@ -26,6 +27,7 @@ pub fn draw_egui(
26
27
mut egui_context : EguiContexts ,
27
28
mut ui_state : ResMut < UiState > ,
28
29
mut grid : ResMut < Grid > ,
30
+ gradient : Res < GradientResource > ,
29
31
mut wallblock_set : ParamSet < (
30
32
Query < ( Entity , & mut WallBlock ) , Without < Overlay > > ,
31
33
Query < ( Entity , & mut WallBlock ) , ( Without < Overlay > , With < Selected > ) > ,
@@ -139,6 +141,46 @@ pub fn draw_egui(
139
141
. set_title ( "Select a file to load" )
140
142
. load_file :: < SaveFileContents > ( ) ;
141
143
}
144
+
145
+ if ui
146
+ . button ( "Save Image" )
147
+ . on_hover_text ( "Save the current iamge of the simulation" )
148
+ . clicked ( )
149
+ {
150
+ let mut pixels: Vec < [ u8 ; 3 ] > = Vec :: new ( ) ;
151
+
152
+ for y in ui_state. e_al ..( SIMULATION_WIDTH + ui_state. e_al ) {
153
+ for x in ui_state. e_al ..( SIMULATION_HEIGHT + ui_state. e_al ) {
154
+ let pressure =
155
+ grid. cells [ coords_to_index ( x, y, ui_state. e_al ) ] . pressure ;
156
+
157
+ let color = gradient. 0 . at ( ( pressure) as f64 ) ;
158
+
159
+ pixels. push ( [
160
+ ( color. r * 255. ) as u8 ,
161
+ ( color. g * 255. ) as u8 ,
162
+ ( color. b * 255. ) as u8 ,
163
+ ] ) ;
164
+ }
165
+ }
166
+
167
+ let data = lodepng:: encode_memory (
168
+ & pixels,
169
+ SIMULATION_WIDTH as usize ,
170
+ SIMULATION_HEIGHT as usize ,
171
+ lodepng:: ColorType :: RGB ,
172
+ 8 ,
173
+ )
174
+ . expect ( "" ) ;
175
+
176
+ commands
177
+ . dialog ( )
178
+ . add_filter ( "Jpeg" , & [ "jpeg" ] )
179
+ . set_file_name ( "save.jpg" )
180
+ . set_directory ( "./" )
181
+ . set_title ( "Select a file to save to" )
182
+ . save_file :: < SaveFileContents > ( data) ;
183
+ }
142
184
} ) ;
143
185
144
186
// Sources
0 commit comments