Skip to content

Commit

Permalink
refactor: revert back to image scale in centre
Browse files Browse the repository at this point in the history
  • Loading branch information
THEGOLDENPRO committed Oct 25, 2024
1 parent fd94c9d commit b565353
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
19 changes: 4 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
29 changes: 7 additions & 22 deletions src/zoom_pan.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b565353

Please sign in to comment.