Skip to content

Z-order painting #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions druid/examples/anim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

use std::f64::consts::PI;

use druid::kurbo::Line;
use druid::kurbo::{Circle, Line};
use druid::widget::{Flex, Padding};
use druid::{
AppLauncher, BoxConstraints, Color, Env, Event, EventCtx, LayoutCtx, PaintCtx, Point,
RenderContext, Size, UpdateCtx, Vec2, Widget, WindowDesc,
Expand Down Expand Up @@ -59,14 +60,26 @@ impl Widget<u32> for AnimWidget {
}

fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &u32, _env: &Env) {
let t = self.t;
let center = Point::new(50.0, 50.0);
let ambit = center + 45.0 * Vec2::from_angle((0.75 + self.t) * 2.0 * PI);
paint_ctx.stroke(Line::new(center, ambit), &Color::WHITE, 1.0);

paint_ctx.paint_with_z_index(1, move |ctx| {
let ambit = center + 45.0 * Vec2::from_angle((0.75 + t) * 2.0 * PI);
ctx.stroke(Line::new(center, ambit), &Color::WHITE, 1.0);
});

paint_ctx.fill(Circle::new(center, 50.0), &Color::BLACK)
}
}

fn build_widget() -> impl Widget<u32> {
let anim_widget = AnimWidget { t: 0.0 };

Flex::column().with_child(Padding::new(0.0, anim_widget), 1.0)
}

fn main() {
let window = WindowDesc::new(|| AnimWidget { t: 0.0 });
let window = WindowDesc::new(build_widget);
AppLauncher::with_window(window)
.use_simple_logger()
.launch(0)
Expand Down
33 changes: 32 additions & 1 deletion druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
let mut ctx = PaintCtx {
render_ctx: paint_ctx.render_ctx,
window_id: paint_ctx.window_id,
z_ops: Vec::new(),
region: paint_ctx.region.clone(),
base_state: &self.state,
};
self.inner.paint(&mut ctx, data, &env);
paint_ctx.z_ops.append(&mut ctx.z_ops);
}

/// Paint the widget, translating it by the origin of its layout rectangle.
Expand Down Expand Up @@ -408,6 +410,12 @@ impl BaseState {
}
}

pub struct ZOrderPaintOp {
pub z_index: u32,
pub paint_func: Box<dyn FnOnce(&mut PaintCtx) + 'static>,
pub transform: Affine,
}

/// A context passed to paint methods of widgets.
///
/// Widgets paint their appearance by calling methods on the
Expand All @@ -419,6 +427,8 @@ pub struct PaintCtx<'a, 'b: 'a> {
/// The render context for actually painting.
pub render_ctx: &'a mut Piet<'b>,
pub window_id: WindowId,
/// The z-order paint operations.
pub z_ops: Vec<ZOrderPaintOp>,
/// The currently visible region.
pub(crate) region: Region,
pub(crate) base_state: &'a BaseState,
Expand Down Expand Up @@ -511,16 +521,37 @@ impl<'a, 'b: 'a> PaintCtx<'a, 'b> {
let PaintCtx {
render_ctx,
window_id,
z_ops: _,
base_state,
..
} = self;

let mut child_ctx = PaintCtx {
render_ctx,
base_state,
z_ops: Vec::new(),
window_id: *window_id,
region: region.into(),
};
f(&mut child_ctx)

f(&mut child_ctx);
self.z_ops.append(&mut child_ctx.z_ops);
}

/// Allows to specify order for paint operations.
///
/// Larger `z_idx` indicate that an operation will be executed later.
pub fn paint_with_z_index(
&mut self,
z_index: u32,
paint_func: impl FnOnce(&mut PaintCtx) + 'static,
) {
let current_transform = self.render_ctx.current_transform();
self.z_ops.push(ZOrderPaintOp {
z_index,
paint_func: Box::new(paint_func),
transform: current_transform,
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions druid/src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@ impl<'a, T: Data + 'static> SingleWindowState<'a, T> {
let mut paint_ctx = PaintCtx {
render_ctx: piet,
base_state: &base_state,
z_ops: Vec::new(),
window_id: self.window_id,
region: Rect::ZERO.into(),
};

self.window.paint(&mut paint_ctx, self.data, self.env);
}

Expand Down
14 changes: 14 additions & 0 deletions druid/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

//! Management of multiple windows.

use std::mem;
use std::sync::atomic::{AtomicU32, Ordering};

use crate::kurbo::{Point, Rect, Size};

use crate::piet::RenderContext;
use crate::shell::WindowHandle;
use crate::{
BoxConstraints, Command, Data, Env, Event, EventCtx, LayoutCtx, LocalizedString, MenuDesc,
Expand Down Expand Up @@ -81,6 +83,18 @@ impl<T: Data> Window<T> {
pub fn paint(&mut self, paint_ctx: &mut PaintCtx, data: &T, env: &Env) {
let visible = Rect::from_origin_size(Point::ZERO, self.size);
paint_ctx.with_child_ctx(visible, |ctx| self.root.paint(ctx, data, env));

paint_ctx.z_ops.sort_by_key(|k| k.z_index);

let z_ops = mem::replace(&mut paint_ctx.z_ops, Vec::new());
for z_op in z_ops.into_iter() {
paint_ctx.with_child_ctx(visible, |ctx| {
ctx.render_ctx.save();
ctx.render_ctx.transform(z_op.transform);
(z_op.paint_func)(ctx);
ctx.render_ctx.restore();
});
}
}

pub(crate) fn update_title(&mut self, win_handle: &WindowHandle, data: &T, env: &Env) {
Expand Down