Implement clipping for images

This commit is contained in:
Héctor Ramón Jiménez 2019-10-27 02:29:23 +01:00
parent 63c10b67ab
commit 0a0aa3edd9
3 changed files with 48 additions and 19 deletions

View file

@ -32,7 +32,19 @@ impl Application for Example {
}
fn view(&mut self) -> Element<Message> {
let content = Scrollable::new(&mut self.scroll).spacing(20).padding(20);
let content = (0..3).fold(
Scrollable::new(&mut self.scroll).spacing(20).padding(20),
|content, _| {
content.push(
Image::new(format!(
"{}/examples/resources/ferris.png",
env!("CARGO_MANIFEST_DIR")
))
.width(Length::Units(400))
.align_self(Align::Center),
)
},
);
//let content = (0..self.paragraph_count)
// .fold(content, |column, _| column.push(lorem_ipsum()))
@ -46,19 +58,9 @@ impl Application for Example {
Column::new()
.height(Length::Fill)
.max_width(Length::Units(600))
.align_self(Align::Center)
.justify_content(Justify::Center)
.push((0..3).fold(content, |content, _| {
content.push(
Image::new(format!(
"{}/examples/resources/ferris.png",
env!("CARGO_MANIFEST_DIR")
))
.width(Length::Units(400))
.align_self(Align::Center),
)
}))
.padding(20)
.push(content)
.into()
}
}

View file

@ -1,4 +1,5 @@
use crate::Transformation;
use iced_native::Rectangle;
use std::cell::RefCell;
use std::collections::HashMap;
@ -218,6 +219,7 @@ impl Pipeline {
encoder: &mut wgpu::CommandEncoder,
instances: &[Image],
transformation: Transformation,
bounds: Rectangle<u32>,
target: &wgpu::TextureView,
) {
let matrix: [f32; 16] = transformation.into();
@ -291,6 +293,12 @@ impl Pipeline {
0,
&[(&self.vertices, 0), (&self.instances, 0)],
);
render_pass.set_scissor_rect(
bounds.x,
bounds.y,
bounds.width,
bounds.height,
);
render_pass.draw_indexed(
0..QUAD_INDICES.len() as u32,

View file

@ -1,7 +1,7 @@
use crate::{quad, Image, Primitive, Quad, Transformation};
use iced_native::{
renderer::Debugger, renderer::Windowed, Background, Color, Layout,
MouseCursor, Point, Widget,
MouseCursor, Point, Rectangle, Widget,
};
use raw_window_handle::HasRawWindowHandle;
@ -43,19 +43,21 @@ pub struct Target {
}
pub struct Layer {
bounds: Rectangle<u32>,
y_offset: u32,
quads: Vec<Quad>,
images: Vec<Image>,
layers: Vec<Layer>,
y_offset: u32,
}
impl Layer {
pub fn new(y_offset: u32) -> Self {
pub fn new(bounds: Rectangle<u32>, y_offset: u32) -> Self {
Self {
bounds,
y_offset,
quads: Vec::new(),
images: Vec::new(),
layers: Vec::new(),
y_offset,
}
}
}
@ -147,7 +149,15 @@ impl Renderer {
depth_stencil_attachment: None,
});
let mut layer = Layer::new(0);
let mut layer = Layer::new(
Rectangle {
x: 0,
y: 0,
width: u32::from(target.width),
height: u32::from(target.height),
},
0,
);
self.draw_primitive(primitive, &mut layer);
self.flush(target.transformation, &layer, &mut encoder, &frame.view);
@ -263,7 +273,15 @@ impl Renderer {
offset,
content,
} => {
let mut new_layer = Layer::new(layer.y_offset + offset);
let mut new_layer = Layer::new(
Rectangle {
x: bounds.x as u32,
y: bounds.y as u32 - layer.y_offset,
width: bounds.width as u32,
height: bounds.height as u32,
},
layer.y_offset + offset,
);
// TODO: Primitive culling
self.draw_primitive(content, &mut new_layer);
@ -296,6 +314,7 @@ impl Renderer {
encoder,
&layer.images,
translated,
layer.bounds,
target,
);