diff options
author | 2019-10-23 01:21:23 +0200 | |
---|---|---|
committer | 2019-10-23 01:21:23 +0200 | |
commit | 38b6c84e7761c049b17d178deb9c866386a53946 (patch) | |
tree | 64921276031527078a447add30e188f6e4cc217a /wgpu/src/renderer/image.rs | |
parent | f8a232c8af4c50557fbf0c2e0b2ba46fb63f6adc (diff) | |
download | iced-38b6c84e7761c049b17d178deb9c866386a53946.tar.gz iced-38b6c84e7761c049b17d178deb9c866386a53946.tar.bz2 iced-38b6c84e7761c049b17d178deb9c866386a53946.zip |
Implement basic image rendering in `iced_wgpu`
Diffstat (limited to 'wgpu/src/renderer/image.rs')
-rw-r--r-- | wgpu/src/renderer/image.rs | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/wgpu/src/renderer/image.rs b/wgpu/src/renderer/image.rs index 85ac3ad5..1a9b01bb 100644 --- a/wgpu/src/renderer/image.rs +++ b/wgpu/src/renderer/image.rs @@ -1,12 +1,33 @@ use crate::{Primitive, Renderer}; -use iced_native::{image, Image, Layout, MouseCursor, Node, Style}; +use iced_native::{image, Image, Layout, Length, MouseCursor, Node, Style}; impl image::Renderer for Renderer { - fn node(&self, _image: &Image) -> Node { - Node::new(Style::default()) + fn node(&self, image: &Image) -> Node { + let (width, height) = self.image_pipeline.dimensions(&image.path); + + let aspect_ratio = width as f32 / height as f32; + + let mut style = Style::default().align_self(image.align_self); + + style = match (image.width, image.height) { + (Length::Units(width), _) => style.width(image.width).height( + Length::Units((width as f32 / aspect_ratio).round() as u16), + ), + (_, _) => style + .width(Length::Units(width as u16)) + .height(Length::Units(height as u16)), + }; + + Node::new(style) } - fn draw(&mut self, _image: &Image, _layout: Layout<'_>) -> Self::Output { - (Primitive::None, MouseCursor::OutOfBounds) + fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output { + ( + Primitive::Image { + path: image.path.clone(), + bounds: layout.bounds(), + }, + MouseCursor::OutOfBounds, + ) } } |