Skip to content

Commit

Permalink
Added view::image::save().
Browse files Browse the repository at this point in the history
  • Loading branch information
Jort van Waes committed Sep 20, 2023
1 parent d6702d1 commit 00a7bb7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
43 changes: 9 additions & 34 deletions src/controller/minifb_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use crate::{
controller::{minifb_mouse_click_recorder::MouseClickRecorder, user_input::pick_option},
model::{
coloring::TrueColor,
complex_plane::{ComplexPlane, View},
pixel_buffer::PixelBuffer,
pixel_plane::PixelPlane,
complex_plane::View,
rendering::{self, render, set_view},
},
view::minifb_mandelbrot_view::MandelbrotView,
view::{image, minifb_mandelbrot_view::MandelbrotView},
MandelbrotModel, VIEW_0, VIEW_1, VIEW_2, VIEW_3, VIEW_4, VIEW_5, VIEW_6, VIEW_7, VIEW_8, VIEW_9,
};

Expand All @@ -20,8 +18,9 @@ use super::{minifb_key_bindings::KeyBindings, user_input::ask};
// Handle any key events
pub fn handle_key_events(window: &Window, k: &KeyBindings) {
if let Some(key) = window.get_keys_pressed(minifb::KeyRepeat::No).first() {
print!("\nKey pressed: ");
print!("\n{{Key pressed: ");
k.print_key(key);
println!("}}");
k.run(key);
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 25 in src/controller/minifb_controller.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/controller/minifb_controller.rs:25:17 | 20 | if let Some(key) = window.get_keys_pressed(minifb::KeyRepeat::No).first() { | _______________________________________________________________________________- 21 | | print!("\n{{Key pressed: "); 22 | | k.print_key(key); 23 | | println!("}}"); 24 | | k.run(key); 25 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ ... | 37 | | println!(); 38 | | } | |_____- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening note: the lint level is defined here --> src/lib.rs:4:5 | 4 | clippy::nursery, | ^^^^^^^^^^^^^^^ = note: `#[warn(clippy::significant_drop_tightening)]` implied by `#[warn(clippy::nursery)]` help: merge the temporary construction with its single usage | 25 ~ 26 + MandelbrotModel::get_instance().; | help: remove separated single usage | 29 - mandelbrot_model.coloring_function = pick_option(&[ 30 - ("HSV", TrueColor::new_from_hsv_colors), 31 - ("Bernstein polynomials", TrueColor::new_from_bernstein_polynomials), 32 - ]); 29 + |
match key {
Expand All @@ -35,11 +34,12 @@ pub fn handle_key_events(window: &Window, k: &KeyBindings) {
}
_ => (),
}
println!();
}
}

pub fn handle_left_mouse_clicked(mandelbrot_model: &MandelbrotModel, x: f32, y: f32) {
println!("\nMouseButton::Left -> Info at ({x}, {y})");
println!("\n{{MouseButton::Left -> Info at ({x}, {y})}}");
//let iterations = MandelbrotModel::get_instance().p.iterations_at_point(x as usize, y as usize, MandelbrotModel::get_instance().m.max_iterations); //TODO: fix this
let complex = mandelbrot_model.c.complex_from_pixel_plane(x.into(), y.into());
println!("Complex: {:?}", complex);
Expand All @@ -48,7 +48,7 @@ pub fn handle_left_mouse_clicked(mandelbrot_model: &MandelbrotModel, x: f32, y:
}

pub fn handle_right_mouse_clicked(mandelbrot_model: &mut MandelbrotModel, x: f32, y: f32) {
println!("\nMouseButton::Right -> Move to ({x}, {y})");
println!("\n{{MouseButton::Right -> Move to ({x}, {y})}}");
let new_center = mandelbrot_model.c.complex_from_pixel_plane(x.into(), y.into());
println!("mandelbrot_model.c.center: {:?}", mandelbrot_model.c.center());
println!("new_center: {:?}", new_center);
Expand Down Expand Up @@ -112,7 +112,6 @@ pub fn initialize_keybindings(key_bindings: &mut KeyBindings) {
mandelbrot_model.vars.increment_translation_amount();
println!("translation_amount: {}", mandelbrot_model.vars.translation_amount);
});

key_bindings.add(Key::NumPadMinus, "Decrement translation amount", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();
mandelbrot_model.vars.decrement_translation_amount();
Expand Down Expand Up @@ -193,32 +192,8 @@ pub fn initialize_keybindings(key_bindings: &mut KeyBindings) {
Key::S,
"Saves the current Mandelbrot set view as an image in the saved folder",
|| {
let mut mandelbrot_model = MandelbrotModel::get_instance();
let time_stamp = chrono::Utc::now().to_string();
if mandelbrot_model.config.window_scale == 1.0 {
mandelbrot_model.p.save_as_png(
&time_stamp,
&mandelbrot_model.c.get_view(),
&mandelbrot_model.m,
mandelbrot_model.config.supersampling_amount,
);
} else {
let mut image_p: PixelBuffer = PixelBuffer::new(PixelPlane::new(
mandelbrot_model.config.image_width,
mandelbrot_model.config.image_height,
));
let mut image_c: ComplexPlane =
ComplexPlane::new(mandelbrot_model.config.image_width, mandelbrot_model.config.image_height);
image_p.color_channel_mapping = mandelbrot_model.p.color_channel_mapping;
image_c.set_view(&mandelbrot_model.c.get_view());
rendering::render_complex_plane_into_buffer(&mut mandelbrot_model);
image_p.save_as_png(
&time_stamp,
&mandelbrot_model.c.get_view(),
&mandelbrot_model.m,
mandelbrot_model.config.supersampling_amount,
);
}
let mandelbrot_model = MandelbrotModel::get_instance();
image::save(&mandelbrot_model);
},
);
key_bindings.add(Key::I, "Manually input a Mandelbrot set view", || {
Expand Down
4 changes: 2 additions & 2 deletions src/controller/minifb_key_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ impl KeyBindings {
pub fn print_key(&self, key: &Key) {
for key_action in &self.key_bindings {
if key_action.key == *key {
println!("{}", key_action.to_formatted_str());
print!("{}", key_action.to_formatted_str());
return;
}
}
//KeyBindings does not contain a KeyBinding x with x.key == key, unbound
println!("{:?}", key);
print!("{:?}", key);
}

pub fn run(&self, key: &Key) {
Expand Down
35 changes: 35 additions & 0 deletions src/view/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::model::mandelbrot_model::MandelbrotModel;

pub fn save(mandelbrot_model: &MandelbrotModel) {
let time_stamp = chrono::Utc::now().to_string();
if mandelbrot_model.config.window_scale == 1.0 {

Check warning on line 5 in src/view/image.rs

View workflow job for this annotation

GitHub Actions / clippy

strict comparison of `f32` or `f64`

warning: strict comparison of `f32` or `f64` --> src/view/image.rs:5:8 | 5 | if mandelbrot_model.config.window_scale == 1.0 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(mandelbrot_model.config.window_scale - 1.0).abs() < error_margin` | = note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp = note: `#[warn(clippy::float_cmp)]` implied by `#[warn(clippy::pedantic)]`
mandelbrot_model.p.save_as_png(
&time_stamp,
&mandelbrot_model.c.get_view(),
&mandelbrot_model.m,
mandelbrot_model.config.supersampling_amount,
);
} else {
println!(
"[UNSUPPORTED]: There is currently no support for saving images in the window scale is not 1.0. The window scale is {}.",
mandelbrot_model.config.window_scale
)

Check warning on line 16 in src/view/image.rs

View workflow job for this annotation

GitHub Actions / clippy

consider adding a `;` to the last statement for consistent formatting

warning: consider adding a `;` to the last statement for consistent formatting --> src/view/image.rs:13:9 | 13 | / println!( 14 | | "[UNSUPPORTED]: There is currently no support for saving images in the window scale is not 1.0. The window scale is {}.", 15 | | mandelbrot_model.config.window_scale 16 | | ) | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned help: add a `;` here | 13 ~ println!( 14 + "[UNSUPPORTED]: There is currently no support for saving images in the window scale is not 1.0. The window scale is {}.", 15 + mandelbrot_model.config.window_scale 16 + ); |
}

/*else {
let mut image_p: PixelBuffer = PixelBuffer::new(PixelPlane::new(
mandelbrot_model.config.image_width,
mandelbrot_model.config.image_height,
));
let mut image_c: ComplexPlane = ComplexPlane::new(mandelbrot_model.config.image_width, mandelbrot_model.config.image_height);
image_p.color_channel_mapping = mandelbrot_model.p.color_channel_mapping;
image_c.set_view(&mandelbrot_model.c.get_view());
rendering::render_complex_plane_into_buffer(mandelbrot_model);
image_p.save_as_png(
&time_stamp,
&mandelbrot_model.c.get_view(),
&mandelbrot_model.m,
mandelbrot_model.config.supersampling_amount,
);
}*/
}
1 change: 1 addition & 0 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod image;
pub mod minifb_mandelbrot_view;
pub mod terminal;

0 comments on commit 00a7bb7

Please sign in to comment.