Skip to content

Commit

Permalink
wip: getting closer to solving #17
Browse files Browse the repository at this point in the history
  • Loading branch information
THEGOLDENPRO committed Oct 20, 2024
1 parent cecfbff commit fd94c9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
12 changes: 10 additions & 2 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, Rect};
use eframe::egui::{self, Color32, ImageSource, Margin, Pos2, Rect};

use crate::{image::{Image, ImageOptimization}, info_box::InfoBox, zoom_pan::ZoomPan};

Expand Down Expand Up @@ -121,11 +121,19 @@ 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(),
ui.max_rect().center(),
initial_image_position,
cursor_position
);

Expand Down
24 changes: 14 additions & 10 deletions src/zoom_pan.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use eframe::egui::{self, Pos2, Response, Vec2};
use eframe::egui::{self, Pos2, Rect, Response, Vec2};
use log::debug;

/// Struct that controls the zoom and panning of the image.
pub struct ZoomPan {
zoom_factor: f32,
last_zoom_factor: f32,
is_panning: bool,
pan_offset: egui::Vec2,
drag_start: Option<egui::Pos2>,
pan_offset: Vec2,
drag_start: Option<Pos2>,
}

impl ZoomPan {
Expand All @@ -17,16 +17,18 @@ impl ZoomPan {
last_zoom_factor: 1.0,
drag_start: None,
is_panning: false,
pan_offset: egui::Vec2::ZERO,
pan_offset: Vec2::ZERO,
}
}

// Method to handle zoom input (scrolling)
pub fn handle_zoom(&mut self, ctx: &egui::Context) {
self.last_zoom_factor = self.zoom_factor;

if ctx.input(|i| i.smooth_scroll_delta.y) != 0.0 {
let zoom_delta = ctx.input(|i| i.smooth_scroll_delta.y) * self.zoom_factor * 0.004;
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);
}
}
Expand Down Expand Up @@ -68,9 +70,11 @@ impl ZoomPan {
}
}

pub fn get_transformation(&mut self, image_size: Vec2, image_position: Pos2, cursor_pos: Pos2) -> (Vec2, Pos2) {
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 - image_position) / self.zoom_factor;
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;
Expand All @@ -81,9 +85,9 @@ impl ZoomPan {

// Now update the image position also relative to that.
let scaled_size = image_size * self.zoom_factor;
let new_image_position = image_position - scaled_size * 0.5 + self.pan_offset;
let new_image_position = initial_image_position + self.pan_offset;

debug!(">> {}", self.pan_offset);
debug!(">> {} | {}", initial_image_position, new_image_position);

(scaled_size, new_image_position)
}
Expand Down

0 comments on commit fd94c9d

Please sign in to comment.