Merge pull request #3033 from iced-rs/layer-merging
Basic Layer Merging for `graphics::layer::Stack`
This commit is contained in:
commit
4a836d5420
12 changed files with 183 additions and 30 deletions
|
|
@ -60,8 +60,8 @@ pub trait Renderer {
|
|||
/// Fills a [`Quad`] with the provided [`Background`].
|
||||
fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>);
|
||||
|
||||
/// Clears all of the recorded primitives in the [`Renderer`].
|
||||
fn clear(&mut self);
|
||||
/// Resets the [`Renderer`] to start drawing in the `new_bounds` from scratch.
|
||||
fn reset(&mut self, new_bounds: Rectangle);
|
||||
}
|
||||
|
||||
/// A polygon with four sides.
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ impl Renderer for () {
|
|||
|
||||
fn end_transformation(&mut self) {}
|
||||
|
||||
fn clear(&mut self) {}
|
||||
fn reset(&mut self, _new_bounds: Rectangle) {}
|
||||
|
||||
fn fill_quad(
|
||||
&mut self,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ pub trait Layer: Default {
|
|||
/// Creates a new [`Layer`] with the given bounds.
|
||||
fn with_bounds(bounds: Rectangle) -> Self;
|
||||
|
||||
/// Returns the current bounds of the [`Layer`].
|
||||
fn bounds(&self) -> Rectangle;
|
||||
|
||||
/// Flushes and settles any pending group of primitives in the [`Layer`].
|
||||
///
|
||||
/// This will be called when a [`Layer`] is finished. It allows layers to efficiently
|
||||
|
|
@ -20,6 +23,24 @@ pub trait Layer: Default {
|
|||
|
||||
/// Clears all the layers contents and resets its bounds.
|
||||
fn reset(&mut self);
|
||||
|
||||
/// Returns the start level of the [`Layer`].
|
||||
///
|
||||
/// A level is a "sublayer" index inside of a [`Layer`].
|
||||
///
|
||||
/// A [`Layer`] may draw multiple primitive types in a certain order.
|
||||
/// The level represents the lowest index of the primitive types it
|
||||
/// contains.
|
||||
///
|
||||
/// Two layers A and B can therefore be merged if they have the same bounds,
|
||||
/// and the end level of A is lower or equal than the start level of B.
|
||||
fn start(&self) -> usize;
|
||||
|
||||
/// Returns the end level of the [`Layer`].
|
||||
fn end(&self) -> usize;
|
||||
|
||||
/// Merges a [`Layer`] with the current one.
|
||||
fn merge(&mut self, _layer: &mut Self);
|
||||
}
|
||||
|
||||
/// A stack of layers used for drawing.
|
||||
|
|
@ -82,7 +103,19 @@ impl<T: Layer> Stack<T> {
|
|||
pub fn pop_clip(&mut self) {
|
||||
self.flush();
|
||||
|
||||
self.current = self.previous.pop().unwrap();
|
||||
let previous = self.previous.pop().unwrap();
|
||||
|
||||
let (head, tail) = self.layers.split_at_mut(previous + 1);
|
||||
let previous_layer = &mut head[previous];
|
||||
let current_layer = &mut tail[self.current - previous - 1];
|
||||
|
||||
if previous_layer.end() <= current_layer.start()
|
||||
&& previous_layer.bounds() == current_layer.bounds()
|
||||
{
|
||||
previous_layer.merge(current_layer);
|
||||
}
|
||||
|
||||
self.current = previous;
|
||||
}
|
||||
|
||||
/// Pushes a new [`Transformation`] in the [`Stack`].
|
||||
|
|
@ -125,12 +158,15 @@ impl<T: Layer> Stack<T> {
|
|||
|
||||
/// Clears the layers of the [`Stack`], allowing reuse.
|
||||
///
|
||||
/// It resizes the base layer bounds to the `new_bounds`.
|
||||
///
|
||||
/// This will normally keep layer allocations for future drawing operations.
|
||||
pub fn clear(&mut self) {
|
||||
pub fn reset(&mut self, new_bounds: Rectangle) {
|
||||
for layer in self.layers[..self.active_count].iter_mut() {
|
||||
layer.reset();
|
||||
}
|
||||
|
||||
self.layers[0].resize(new_bounds);
|
||||
self.current = 0;
|
||||
self.active_count = 1;
|
||||
self.previous.clear();
|
||||
|
|
|
|||
|
|
@ -46,8 +46,8 @@ where
|
|||
delegate!(self, renderer, renderer.fill_quad(quad, background.into()));
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
delegate!(self, renderer, renderer.clear());
|
||||
fn reset(&mut self, new_bounds: Rectangle) {
|
||||
delegate!(self, renderer, renderer.reset(new_bounds));
|
||||
}
|
||||
|
||||
fn start_layer(&mut self, bounds: Rectangle) {
|
||||
|
|
|
|||
|
|
@ -482,10 +482,8 @@ where
|
|||
style: &renderer::Style,
|
||||
cursor: mouse::Cursor,
|
||||
) {
|
||||
// TODO: Move to shell level (?)
|
||||
renderer.clear();
|
||||
|
||||
let viewport = Rectangle::with_size(self.bounds);
|
||||
renderer.reset(viewport);
|
||||
|
||||
let base_cursor = match &self.overlay {
|
||||
None
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@ pub struct Layer {
|
|||
pub bounds: Rectangle,
|
||||
pub quads: Vec<(Quad, Background)>,
|
||||
pub primitives: Vec<Item<Primitive>>,
|
||||
pub text: Vec<Item<Text>>,
|
||||
pub images: Vec<Image>,
|
||||
pub text: Vec<Item<Text>>,
|
||||
}
|
||||
|
||||
impl Layer {
|
||||
|
|
@ -284,6 +284,10 @@ impl graphics::Layer for Layer {
|
|||
}
|
||||
}
|
||||
|
||||
fn bounds(&self) -> Rectangle {
|
||||
self.bounds
|
||||
}
|
||||
|
||||
fn flush(&mut self) {}
|
||||
|
||||
fn resize(&mut self, bounds: Rectangle) {
|
||||
|
|
@ -298,6 +302,49 @@ impl graphics::Layer for Layer {
|
|||
self.text.clear();
|
||||
self.images.clear();
|
||||
}
|
||||
|
||||
fn start(&self) -> usize {
|
||||
if !self.quads.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if !self.primitives.is_empty() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if !self.images.is_empty() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if !self.text.is_empty() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn end(&self) -> usize {
|
||||
if !self.text.is_empty() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if !self.images.is_empty() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if !self.primitives.is_empty() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn merge(&mut self, layer: &mut Self) {
|
||||
self.quads.append(&mut layer.quads);
|
||||
self.primitives.append(&mut layer.primitives);
|
||||
self.text.append(&mut layer.text);
|
||||
self.images.append(&mut layer.images);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
|
|||
|
|
@ -225,8 +225,8 @@ impl core::Renderer for Renderer {
|
|||
layer.draw_quad(quad, background.into(), transformation);
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.layers.clear();
|
||||
fn reset(&mut self, new_bounds: Rectangle) {
|
||||
self.layers.reset(new_bounds);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,4 +11,6 @@ impl Batch {
|
|||
pub fn is_empty(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn append(&mut self, _batch: &mut Self) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -268,6 +268,10 @@ impl graphics::Layer for Layer {
|
|||
}
|
||||
}
|
||||
|
||||
fn bounds(&self) -> Rectangle {
|
||||
self.bounds
|
||||
}
|
||||
|
||||
fn flush(&mut self) {
|
||||
self.flush_meshes();
|
||||
self.flush_text();
|
||||
|
|
@ -288,6 +292,58 @@ impl graphics::Layer for Layer {
|
|||
self.pending_meshes.clear();
|
||||
self.pending_text.clear();
|
||||
}
|
||||
|
||||
fn start(&self) -> usize {
|
||||
if !self.quads.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if !self.triangles.is_empty() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if !self.primitives.is_empty() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if !self.images.is_empty() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if !self.text.is_empty() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn end(&self) -> usize {
|
||||
if !self.text.is_empty() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
if !self.images.is_empty() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if !self.primitives.is_empty() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if !self.triangles.is_empty() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn merge(&mut self, layer: &mut Self) {
|
||||
self.quads.append(&mut layer.quads);
|
||||
self.triangles.append(&mut layer.triangles);
|
||||
self.primitives.append(&mut layer.primitives);
|
||||
self.images.append(&mut layer.images);
|
||||
self.text.append(&mut layer.text);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Layer {
|
||||
|
|
|
|||
|
|
@ -648,8 +648,8 @@ impl core::Renderer for Renderer {
|
|||
layer.draw_quad(quad, background.into(), transformation);
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.layers.clear();
|
||||
fn reset(&mut self, new_bounds: Rectangle) {
|
||||
self.layers.reset(new_bounds);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -304,6 +304,12 @@ impl Batch {
|
|||
self.gradients.clear();
|
||||
self.order.clear();
|
||||
}
|
||||
|
||||
pub fn append(&mut self, batch: &mut Batch) {
|
||||
self.solids.append(&mut batch.solids);
|
||||
self.gradients.append(&mut batch.gradients);
|
||||
self.order.append(&mut batch.order);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ pub struct Stack<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
|
|||
width: Length,
|
||||
height: Length,
|
||||
children: Vec<Element<'a, Message, Theme, Renderer>>,
|
||||
clip: bool,
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> Stack<'a, Message, Theme, Renderer>
|
||||
|
|
@ -62,6 +63,7 @@ where
|
|||
width: Length::Shrink,
|
||||
height: Length::Shrink,
|
||||
children,
|
||||
clip: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -114,6 +116,16 @@ where
|
|||
) -> Self {
|
||||
children.into_iter().fold(self, Self::push)
|
||||
}
|
||||
|
||||
/// Sets whether the [`Stack`] should clip overflowing content.
|
||||
///
|
||||
/// It has a slight performance overhead during presentation.
|
||||
///
|
||||
/// By default, it is set to `false`.
|
||||
pub fn clip(mut self, clip: bool) -> Self {
|
||||
self.clip = clip;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message, Renderer> Default for Stack<'_, Message, Renderer>
|
||||
|
|
@ -277,6 +289,12 @@ where
|
|||
viewport: &Rectangle,
|
||||
) {
|
||||
if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
|
||||
let viewport = if self.clip {
|
||||
&clipped_viewport
|
||||
} else {
|
||||
viewport
|
||||
};
|
||||
|
||||
let layers_below = if cursor.is_over(layout.bounds()) {
|
||||
self.children
|
||||
.iter()
|
||||
|
|
@ -312,26 +330,16 @@ where
|
|||
layout,
|
||||
cursor| {
|
||||
if i > 0 {
|
||||
renderer.with_layer(clipped_viewport, |renderer| {
|
||||
renderer.with_layer(*viewport, |renderer| {
|
||||
layer.as_widget().draw(
|
||||
state,
|
||||
renderer,
|
||||
theme,
|
||||
style,
|
||||
layout,
|
||||
cursor,
|
||||
&clipped_viewport,
|
||||
state, renderer, theme, style, layout, cursor,
|
||||
viewport,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
layer.as_widget().draw(
|
||||
state,
|
||||
renderer,
|
||||
theme,
|
||||
style,
|
||||
layout,
|
||||
cursor,
|
||||
&clipped_viewport,
|
||||
state, renderer, theme, style, layout, cursor,
|
||||
viewport,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue