Skip to content

Commit

Permalink
refactor, feat: improve codebase readability and structure scheduling…
Browse files Browse the repository at this point in the history
… a pan reset now also schedules a scale reset too
  • Loading branch information
THEGOLDENPRO committed Nov 1, 2024
1 parent c96a4d5 commit a334038
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
29 changes: 13 additions & 16 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ impl eframe::App for Roseate {

if window_rect.width() != self.last_window_rect.width() || window_rect.height() != self.last_window_rect.height() {
if !self.zoom_pan.has_been_messed_with() {
self.window_scaling.schedule_image_scale_to_window_size();
self.window_scaling.schedule_scale_image_to_window_size();
self.last_window_rect = window_rect;
}
self.last_window_rect = window_rect;
}

if self.image.is_none() {
Expand Down Expand Up @@ -83,32 +83,29 @@ impl eframe::App for Roseate {
self.window_scaling.update(&window_rect, &image.image_size);

ui.centered_and_justified(|ui| {
let (scaled_image_width, scaled_image_height) = self.window_scaling.get_scaled_image_size(
image.image_size
let scaled_image_size = self.window_scaling.relative_image_size(
Vec2::new(image.image_size.width as f32, image.image_size.height as f32)
);

if self.zoom_pan.is_pan_out_of_bounds([scaled_image_width, scaled_image_height].into()) {
if self.zoom_pan.is_pan_out_of_bounds(scaled_image_size) {
self.zoom_pan.schedule_pan_reset(Duration::from_millis(300));
};

// NOTE: umm do we move this to window scaling... *probably* if we
// want to stay consistent with zoom_pan but this isn't important right now.
let scaled_image_width_animated = egui_animation::animate_eased(
ctx, "image_scale_width", scaled_image_width, 1.5, simple_easing::cubic_in_out
) as u32;
ctx, "image_scale_width", scaled_image_size.x, 1.5, simple_easing::cubic_in_out
) as u32 as f32;
let scaled_image_height_animated = egui_animation::animate_eased(
ctx, "image_scale_height", scaled_image_height, 1.5, simple_easing::cubic_in_out
) as u32;
ctx, "image_scale_height", scaled_image_size.y, 1.5, simple_easing::cubic_in_out
) as u32 as f32;

let image_size = Vec2::new(
scaled_image_width_animated as f32,
scaled_image_height_animated as f32
);
let scaled_image_size = Vec2::new(scaled_image_width_animated, scaled_image_height_animated);

let zoom_scaled_size = image_size * self.zoom_pan.zoom_factor;
let image_position = ui.max_rect().center() - zoom_scaled_size * 0.5 + self.zoom_pan.pan_offset;
let zoom_scaled_image_size = self.zoom_pan.relative_image_size(scaled_image_size);
let image_position = ui.max_rect().center() - zoom_scaled_image_size * 0.5 + self.zoom_pan.pan_offset;

let zoom_pan_rect = Rect::from_min_size(image_position, zoom_scaled_size);
let zoom_pan_rect = Rect::from_min_size(image_position, zoom_scaled_image_size);

let response = ui.allocate_rect(zoom_pan_rect, egui::Sense::hover());

Expand Down
2 changes: 1 addition & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn apply_image_optimizations(mut optimizations: Vec<ImageOptimization>, imag
"Uhhhhh, you don't have a monitor. WHAT!"
);

let marginal_allowance: f32 = 1.2;
let marginal_allowance: f32 = 1.3; // TODO: Make this adjustable in the config too as down sample strength.

let (width, height) = (
primary_display_maybe.width as f32 * marginal_allowance,
Expand Down
12 changes: 6 additions & 6 deletions src/window_scaling.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::{Duration, Instant};

use eframe::egui::Rect;
use eframe::egui::{Rect, Vec2};
use imagesize::ImageSize;
use log::debug;

Expand All @@ -20,7 +20,7 @@ impl WindowScaling {

/// Resizes the image to the window size after a short delay
/// or later in the update loop (hence being named 'schedule_').
pub fn schedule_image_scale_to_window_size(&mut self) {
pub fn schedule_scale_image_to_window_size(&mut self) {
debug!("The image has been scheduled to resize to the window size.");
self.resize_to_window_timer = Some(Instant::now());
}
Expand All @@ -46,10 +46,10 @@ impl WindowScaling {
}
}

pub fn get_scaled_image_size(&self, actual_image_size: ImageSize) -> (f32, f32) {
(
actual_image_size.width as f32 * self.scale_factor,
actual_image_size.height as f32 * self.scale_factor
pub fn relative_image_size(&self, image_size: Vec2) -> Vec2 {
Vec2::new(
image_size.x as f32 * self.scale_factor,
image_size.y as f32 * self.scale_factor
)
}
}
8 changes: 8 additions & 0 deletions src/zoom_pan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ impl ZoomPan {
false
}
}

pub fn relative_image_size(&self, image_size: Vec2) -> Vec2 {
image_size * self.zoom_factor
}
}

impl ZoomPan {
Expand All @@ -148,6 +152,10 @@ impl ZoomPan {
);

debug!("Pan offset reset has been scheduled.");

// As resetting the pan will just snap us back to the center
// of the image we might as well schedule a reset for image scale too.
self.schedule_scale_reset(delay);
}
}

Expand Down

0 comments on commit a334038

Please sign in to comment.