Pass clip rectangle to PaintCallback
This commit is contained in:
parent
2745699bd6
commit
f3e31391e0
4 changed files with 67 additions and 35 deletions
|
@ -380,7 +380,8 @@ impl Painter {
|
|||
}
|
||||
|
||||
let info = egui::PaintCallbackInfo {
|
||||
rect: callback.rect,
|
||||
viewport: callback.rect,
|
||||
clip_rect: *clip_rect,
|
||||
pixels_per_point,
|
||||
screen_size_px: inner_size,
|
||||
};
|
||||
|
|
|
@ -659,7 +659,15 @@ fn dashes_from_line(
|
|||
/// Information passed along with [`PaintCallback`] ([`Shape::Callback`]).
|
||||
pub struct PaintCallbackInfo {
|
||||
/// Viewport in points.
|
||||
pub rect: Rect,
|
||||
///
|
||||
/// This specifies where on the screen to paint, and the borders of this
|
||||
/// Rect is the [-1, +1] of the Normalized Device Coordinates.
|
||||
///
|
||||
/// Note than only a portion of this may be visible due to [`Self::clip`].
|
||||
pub viewport: Rect,
|
||||
|
||||
/// Clip rectangle in points.
|
||||
pub clip_rect: Rect,
|
||||
|
||||
/// Pixels per point.
|
||||
pub pixels_per_point: f32,
|
||||
|
@ -668,37 +676,44 @@ pub struct PaintCallbackInfo {
|
|||
pub screen_size_px: [u32; 2],
|
||||
}
|
||||
|
||||
impl PaintCallbackInfo {
|
||||
pub struct ViewportInPixels {
|
||||
/// Physical pixel offset for left side of the viewport.
|
||||
#[inline]
|
||||
pub fn viewport_left_px(&self) -> f32 {
|
||||
self.rect.min.x * self.pixels_per_point
|
||||
}
|
||||
pub left_px: f32,
|
||||
|
||||
/// Physical pixel offset for top side of the viewport.
|
||||
#[inline]
|
||||
pub fn viewport_top_px(&self) -> f32 {
|
||||
self.rect.min.y * self.pixels_per_point
|
||||
}
|
||||
pub top_px: f32,
|
||||
|
||||
/// Physical pixel offset for bottom side of the viewport.
|
||||
///
|
||||
/// This is what `glViewport` etc expects for the y axis.
|
||||
#[inline]
|
||||
pub fn viewport_from_bottom_px(&self) -> f32 {
|
||||
self.screen_size_px[1] as f32 - self.rect.max.y * self.pixels_per_point
|
||||
}
|
||||
/// This is what `glViewport`, `glScissor` etc expects for the y axis.
|
||||
pub from_bottom_px: f32,
|
||||
|
||||
/// Viewport width in physical pixels.
|
||||
#[inline]
|
||||
pub fn viewport_width_px(&self) -> f32 {
|
||||
self.rect.width() * self.pixels_per_point
|
||||
}
|
||||
pub width_px: f32,
|
||||
|
||||
/// Viewport width in physical pixels.
|
||||
#[inline]
|
||||
pub fn viewport_height_px(&self) -> f32 {
|
||||
self.rect.height() * self.pixels_per_point
|
||||
pub height_px: f32,
|
||||
}
|
||||
|
||||
impl PaintCallbackInfo {
|
||||
fn points_to_pixels(&self, rect: &Rect) -> ViewportInPixels {
|
||||
ViewportInPixels {
|
||||
left_px: rect.min.x * self.pixels_per_point,
|
||||
top_px: rect.min.y * self.pixels_per_point,
|
||||
from_bottom_px: self.screen_size_px[1] as f32 - rect.max.y * self.pixels_per_point,
|
||||
width_px: rect.width() * self.pixels_per_point,
|
||||
height_px: rect.height() * self.pixels_per_point,
|
||||
}
|
||||
}
|
||||
|
||||
/// The viewport rectangle.
|
||||
pub fn viewport_in_pixels(&self) -> ViewportInPixels {
|
||||
self.points_to_pixels(&self.viewport)
|
||||
}
|
||||
|
||||
/// The "scissor" or "clip" rectangle.
|
||||
pub fn clip_rect_in_pixels(&self) -> ViewportInPixels {
|
||||
self.points_to_pixels(&self.clip_rect)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -99,11 +99,25 @@ fn paint_with_three_d(three_d: &three_d::Context, info: &egui::PaintCallbackInfo
|
|||
// Based on https://github.com/asny/three-d/blob/master/examples/triangle/src/main.rs
|
||||
use three_d::*;
|
||||
|
||||
// Set where to paint
|
||||
let viewport = info.viewport_in_pixels();
|
||||
let viewport = Viewport {
|
||||
x: info.viewport_left_px().round() as _,
|
||||
y: info.viewport_from_bottom_px().round() as _,
|
||||
width: info.viewport_width_px().round() as _,
|
||||
height: info.viewport_height_px().round() as _,
|
||||
x: viewport.left_px.round() as _,
|
||||
y: viewport.from_bottom_px.round() as _,
|
||||
width: viewport.width_px.round() as _,
|
||||
height: viewport.height_px.round() as _,
|
||||
};
|
||||
|
||||
// Respect the egui clip region (e.g. if we are inside an `egui::ScrollArea`).
|
||||
let clip_rect = info.clip_rect_in_pixels();
|
||||
let render_states = RenderStates {
|
||||
clip: Clip::Enabled {
|
||||
x: clip_rect.left_px.round() as _,
|
||||
y: clip_rect.from_bottom_px.round() as _,
|
||||
width: clip_rect.width_px.round() as _,
|
||||
height: clip_rect.height_px.round() as _,
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let camera = Camera::new_perspective(
|
||||
|
@ -135,8 +149,11 @@ fn paint_with_three_d(three_d: &three_d::Context, info: &egui::PaintCallbackInfo
|
|||
..Default::default()
|
||||
};
|
||||
|
||||
// Construct a model, with a default color material, thereby transferring the mesh data to the GPU
|
||||
let mut model = Model::new(three_d, &cpu_mesh).unwrap();
|
||||
let material = ColorMaterial {
|
||||
render_states,
|
||||
..Default::default()
|
||||
};
|
||||
let mut model = Model::new_with_material(three_d, &cpu_mesh, material).unwrap();
|
||||
|
||||
// Set the current transformation of the triangle
|
||||
model.set_transformation(Mat4::from_angle_y(radians(angle)));
|
||||
|
|
|
@ -15,17 +15,16 @@ fn heading3() -> TextStyle {
|
|||
|
||||
fn configure_text_styles(ctx: &egui::Context) {
|
||||
use FontFamily::Proportional;
|
||||
use TextStyle::*;
|
||||
|
||||
let mut style = (*ctx.style()).clone();
|
||||
style.text_styles = [
|
||||
(Heading, FontId::new(30.0, Proportional)),
|
||||
(TextStyle::Heading, FontId::new(30.0, Proportional)),
|
||||
(heading2(), FontId::new(25.0, Proportional)),
|
||||
(heading3(), FontId::new(23.0, Proportional)),
|
||||
(Body, FontId::new(18.0, Proportional)),
|
||||
(Monospace, FontId::new(14.0, Proportional)),
|
||||
(Button, FontId::new(14.0, Proportional)),
|
||||
(Small, FontId::new(10.0, Proportional)),
|
||||
(TextStyle::Body, FontId::new(18.0, Proportional)),
|
||||
(TextStyle::Monospace, FontId::new(14.0, Proportional)),
|
||||
(TextStyle::Button, FontId::new(14.0, Proportional)),
|
||||
(TextStyle::Small, FontId::new(10.0, Proportional)),
|
||||
]
|
||||
.into();
|
||||
ctx.set_style(style);
|
||||
|
|
Loading…
Reference in a new issue