2020-12-18 10:15:30 +01:00
|
|
|
//! Zoom and pan on an image.
|
|
|
|
|
use crate::backend::{self, Backend};
|
2020-04-23 15:34:55 -07:00
|
|
|
use crate::{Primitive, Renderer};
|
2020-12-18 10:15:30 +01:00
|
|
|
|
2020-05-27 14:20:07 -07:00
|
|
|
use iced_native::{
|
|
|
|
|
image::{self, viewer},
|
|
|
|
|
mouse, Rectangle, Vector,
|
|
|
|
|
};
|
2020-04-23 15:34:55 -07:00
|
|
|
|
2020-12-18 10:15:30 +01:00
|
|
|
impl<B> viewer::Renderer for Renderer<B>
|
|
|
|
|
where
|
|
|
|
|
B: Backend + backend::Image,
|
|
|
|
|
{
|
2020-04-23 15:34:55 -07:00
|
|
|
fn draw(
|
|
|
|
|
&mut self,
|
2020-05-27 14:20:07 -07:00
|
|
|
state: &viewer::State,
|
2020-04-23 15:34:55 -07:00
|
|
|
bounds: Rectangle,
|
|
|
|
|
image_bounds: Rectangle,
|
2020-05-27 13:39:26 -07:00
|
|
|
translation: Vector,
|
2020-04-23 15:34:55 -07:00
|
|
|
handle: image::Handle,
|
|
|
|
|
is_mouse_over: bool,
|
|
|
|
|
) -> Self::Output {
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
Primitive::Clip {
|
|
|
|
|
bounds,
|
2020-05-27 13:39:26 -07:00
|
|
|
content: Box::new(Primitive::Translate {
|
|
|
|
|
translation,
|
|
|
|
|
content: Box::new(Primitive::Image {
|
|
|
|
|
handle,
|
|
|
|
|
bounds: image_bounds,
|
|
|
|
|
}),
|
2020-04-23 15:34:55 -07:00
|
|
|
}),
|
2020-05-27 13:39:26 -07:00
|
|
|
offset: Vector::new(0, 0),
|
2020-04-23 15:34:55 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
if state.is_cursor_clicked() {
|
2020-05-14 11:54:05 -07:00
|
|
|
mouse::Interaction::Grabbing
|
2020-04-23 16:22:53 -07:00
|
|
|
} else if is_mouse_over
|
|
|
|
|
&& (image_bounds.width > bounds.width
|
|
|
|
|
|| image_bounds.height > bounds.height)
|
|
|
|
|
{
|
2020-05-14 11:54:05 -07:00
|
|
|
mouse::Interaction::Grab
|
2020-04-23 15:34:55 -07:00
|
|
|
} else {
|
2020-05-14 11:54:05 -07:00
|
|
|
mouse::Interaction::Idle
|
2020-04-23 15:34:55 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|