Skip to content

Commit

Permalink
move scale factor calc to renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Gabriel committed Feb 27, 2024
1 parent da7f424 commit d8e4e75
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
30 changes: 26 additions & 4 deletions src/femtovg_area/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl WidgetImpl for FemtoVGArea {
impl GLAreaImpl for FemtoVGArea {
fn resize(&self, width: i32, height: i32) {
self.ensure_canvas();

let mut bc = self.canvas.borrow_mut();
let canvas = bc.as_mut().unwrap(); // this unwrap is safe as long as we call "ensure_canvas" before

Expand All @@ -80,6 +81,12 @@ impl GLAreaImpl for FemtoVGArea {
height as u32,
self.obj().scale_factor() as f32,
);

// update scale factor
self.inner()
.as_mut()
.expect("Did you call init before using FemtoVgArea?")
.update_scale_factor(canvas);
}
fn render(&self, _context: &gtk::gdk::GLContext) -> bool {
self.ensure_canvas();
Expand Down Expand Up @@ -236,10 +243,6 @@ impl FemtoVgAreaMut {
self.active_tool = active_tool;
}

pub fn set_scale_factor(&mut self, scale_factor: f32) {
self.scale_factor = scale_factor;
}

pub fn render_native_resolution(
&mut self,
canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>,
Expand Down Expand Up @@ -436,4 +439,23 @@ impl FemtoVgAreaMut {

Ok(background_image_id)
}

pub fn update_scale_factor(&mut self, canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>) {
let image_width = self.background_image.width() as f32;
let image_height = self.background_image.height() as f32;
let aspect_ratio = image_width / image_height;

let canvas_width = canvas.width() as f32;
let canvas_height = canvas.height() as f32;

self.scale_factor = if canvas_width / aspect_ratio <= canvas_height {
canvas_width / aspect_ratio / image_height
} else {
canvas_height * aspect_ratio / image_width
};
}

pub fn get_scale_factor(&self) -> f32 {
self.scale_factor
}
}
20 changes: 12 additions & 8 deletions src/femtovg_area/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{cell::RefCell, rc::Rc};
use gdk_pixbuf::{glib::subclass::types::ObjectSubclassIsExt, Pixbuf};
use gtk::glib;
use relm4::{
gtk::{self},
gtk::{self, prelude::WidgetExt},
Sender,
};

Expand All @@ -26,13 +26,6 @@ impl Default for FemtoVGArea {
}

impl FemtoVGArea {
pub fn set_scale_factor(&mut self, scale_factor: f32) {
self.imp()
.inner()
.as_mut()
.expect("Did you call init before usind FemtoVgArea?")
.set_scale_factor(scale_factor);
}
pub fn set_active_tool(&mut self, active_tool: Rc<RefCell<dyn Tool>>) {
self.imp()
.inner()
Expand Down Expand Up @@ -66,6 +59,17 @@ impl FemtoVGArea {
self.imp().request_render(action);
}

pub fn get_scale_factor(&self) -> f32 {
let renderer_scale_factor = self
.imp()
.inner()
.as_mut()
.expect("Did you call init before using FemtoVgArea?")
.get_scale_factor();
let dpi_scale_factor = self.scale_factor() as f32;

renderer_scale_factor / dpi_scale_factor
}
pub fn init(
&mut self,
sender: Sender<SketchBoardInput>,
Expand Down
28 changes: 1 addition & 27 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type RenderedImage = Img<Vec<RGBA<u8>>>;
#[derive(Debug, Clone)]
pub enum SketchBoardInput {
InputEvent(InputEvent),
Resize(Vec2D),
ToolbarEvent(ToolbarEvent),
RenderResult(RenderedImage, Action),
}
Expand Down Expand Up @@ -140,21 +139,9 @@ pub struct SketchBoard {
active_tool: Rc<RefCell<dyn Tool>>,
tools: ToolsManager,
style: Style,
image_dimensions: Vec2D,
scale_factor: f32,
}

impl SketchBoard {
pub fn calculate_scale_factor(&mut self, new_dimensions: Vec2D) {
let aspect_ratio = self.image_dimensions.x / self.image_dimensions.y;
self.scale_factor = if new_dimensions.x / aspect_ratio <= new_dimensions.y {
new_dimensions.x / aspect_ratio / self.image_dimensions.y
} else {
new_dimensions.y * aspect_ratio / self.image_dimensions.x
};

self.renderer.set_scale_factor(self.scale_factor);
}
fn refresh_screen(&mut self) {
self.renderer.queue_render();
}
Expand Down Expand Up @@ -422,22 +409,13 @@ impl Component for SketchBoard {
Vec2D::new(x as f32, y as f32)));
}
},

connect_resize[sender] => move |_, x, y| {
sender.input(SketchBoardInput::Resize(Vec2D::new(x as f32,y as f32)));
}
}
},
}

fn update(&mut self, msg: SketchBoardInput, sender: ComponentSender<Self>, _root: &Self::Root) {
// handle resize ourselves, pass everything else to tool
let result = match msg {
SketchBoardInput::Resize(dim) => {
self.calculate_scale_factor(dim);
ToolUpdateResult::Redraw
}

SketchBoardInput::InputEvent(mut ie) => {
if let InputEvent::Key(ke) = ie {
if ke.key == Key::z && ke.modifier == ModifierType::CONTROL_MASK {
Expand All @@ -462,9 +440,7 @@ impl Component for SketchBoard {
.handle_event(ToolEvent::Input(ie))
}
} else {
ie.remap_event_coordinates(
self.scale_factor / self.renderer.scale_factor() as f32,
);
ie.remap_event_coordinates(self.renderer.get_scale_factor());
self.active_tool
.borrow_mut()
.handle_event(ToolEvent::Input(ie))
Expand Down Expand Up @@ -499,11 +475,9 @@ impl Component for SketchBoard {
let tools = ToolsManager::new();

let mut model = Self {
image_dimensions: Vec2D::new(image.width() as f32, image.height() as f32),
renderer: FemtoVGArea::default(),
active_tool: tools.get(&config.initial_tool()),
style: Style::default(),
scale_factor: 1.0,
tools,
};

Expand Down

0 comments on commit d8e4e75

Please sign in to comment.