diff --git a/src/app.rs b/src/app.rs index 7d99f95..7b7744e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,7 @@ use std::time::{Duration, Instant}; use rdev::display_size; use cirrus_theming::Theme; -use eframe::egui::{self, Color32, ImageSource, Margin, Pos2, Rect}; +use eframe::egui::{self, Color32, ImageSource, Margin, Rect}; use crate::{image::{Image, ImageOptimization}, info_box::InfoBox, zoom_pan::ZoomPan}; @@ -63,7 +63,7 @@ impl eframe::App for Roseate { ..Default::default() }; - self.zoom_pan.handle_zoom(ctx); + self.zoom_pan.handle_zoom_input(ctx); self.info_box.handle_input(ctx); egui::CentralPanel::default().frame(central_panel_frame).show(ctx, |ui| { @@ -121,20 +121,9 @@ impl eframe::App for Roseate { ctx, "image_scale_height", scaled_image_height, 1.5, simple_easing::cubic_in_out ) as u32; - let window_size = window_rect.size(); - - // this needs to fucking go, terrible hack - let initial_image_position = Pos2::new( - (window_size.x - scaled_image_width_animated as f32) / 2.0, - (window_size.y - scaled_image_height_animated as f32) / 2.0, - ); - - let cursor_position = ctx.input(|i| i.pointer.hover_pos()).unwrap_or_default(); - let (zoom_scaled_size, pan_image_position) = self.zoom_pan.get_transformation( (scaled_image_width_animated as f32, scaled_image_height_animated as f32).into(), - initial_image_position, - cursor_position + ui.max_rect().center() ); let zoom_pan_rect = Rect::from_min_size(pan_image_position, zoom_scaled_size); @@ -148,7 +137,7 @@ impl eframe::App for Roseate { .rounding(10.0) .paint_at(ui, zoom_pan_rect); - self.zoom_pan.handle_pan(ctx, &response, self.info_box.response.as_ref()); + self.zoom_pan.handle_pan_input(ctx, &response, self.info_box.response.as_ref()); }); ctx.request_repaint_after_secs(0.5); // We need to request repaints just in diff --git a/src/zoom_pan.rs b/src/zoom_pan.rs index b2221f1..bd7d655 100644 --- a/src/zoom_pan.rs +++ b/src/zoom_pan.rs @@ -1,5 +1,4 @@ -use eframe::egui::{self, Pos2, Rect, Response, Vec2}; -use log::debug; +use eframe::egui::{self, Pos2, Response, Vec2}; /// Struct that controls the zoom and panning of the image. pub struct ZoomPan { @@ -22,19 +21,20 @@ impl ZoomPan { } // Method to handle zoom input (scrolling) - pub fn handle_zoom(&mut self, ctx: &egui::Context) { + pub fn handle_zoom_input(&mut self, ctx: &egui::Context) { self.last_zoom_factor = self.zoom_factor; let scroll_delta = ctx.input(|i| i.smooth_scroll_delta.y); if scroll_delta != 0.0 { let zoom_delta = scroll_delta * self.zoom_factor * 0.004; + self.zoom_factor = (self.zoom_factor + zoom_delta).clamp(0.5, 100.0); } } // Method to handle panning (dragging) - pub fn handle_pan(&mut self, ctx: &egui::Context, image_response: &Response, info_box_response: Option<&Response>) { + pub fn handle_pan_input(&mut self, ctx: &egui::Context, image_response: &Response, info_box_response: Option<&Response>) { let mut can_pan = false; // "&& self.is_panning" allows for the panning to continue even @@ -70,26 +70,11 @@ impl ZoomPan { } } - pub fn get_transformation(&mut self, image_size: Vec2, initial_image_position: Pos2, cursor_pos: Pos2) -> (Vec2, Pos2) { - // Get the cursor position relative to the image also while being zoomed. - let cursor_relative_to_image = (cursor_pos - initial_image_position) / self.zoom_factor; - - debug!("--> {}", cursor_relative_to_image); - - // Get the change since the last zoom factor. - let zoom_factor_change = self.zoom_factor / self.last_zoom_factor; - - // Keep the image centred around the cursor by adjust - // the pan offset relative to the cursor and zoom difference. - self.pan_offset += cursor_relative_to_image * (1.0 - zoom_factor_change); - - // Now update the image position also relative to that. + pub fn get_transformation(&self, image_size: egui::Vec2, image_position: egui::Pos2) -> (Vec2, Pos2) { let scaled_size = image_size * self.zoom_factor; - let new_image_position = initial_image_position + self.pan_offset; - - debug!(">> {} | {}", initial_image_position, new_image_position); + let image_position = image_position - scaled_size * 0.5 + self.pan_offset; - (scaled_size, new_image_position) + (scaled_size, image_position) } pub fn has_been_messed_with(&mut self) -> bool {