Skip to content

Commit

Permalink
PaintCallback: supply enough context to create a viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Mar 22, 2022
1 parent 35cc21a commit 9181dc7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
4 changes: 2 additions & 2 deletions egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ pub use epaint::{
color, mutex,
text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak},
textures::TexturesDelta,
AlphaImage, ClippedPrimitive, Color32, ColorImage, ImageData, Mesh, PaintCallback, Rgba,
Rounding, Shape, Stroke, TextureHandle, TextureId,
AlphaImage, ClippedPrimitive, Color32, ColorImage, ImageData, Mesh, PaintCallback,
PaintCallbackInfo, Rgba, Rounding, Shape, Stroke, TextureHandle, TextureId,
};

pub mod text {
Expand Down
8 changes: 7 additions & 1 deletion egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,13 @@ impl Painter {
);
}

callback.call(self);
let info = egui::PaintCallbackInfo {
rect: callback.rect,
pixels_per_point,
screen_size_px: inner_size,
};

callback.call(&info, self);

// Restore state:
unsafe {
Expand Down
5 changes: 4 additions & 1 deletion epaint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub use {
image::{AlphaImage, ColorImage, ImageData, ImageDelta},
mesh::{Mesh, Mesh16, Vertex},
shadow::Shadow,
shape::{CircleShape, PaintCallback, PathShape, RectShape, Rounding, Shape, TextShape},
shape::{
CircleShape, PaintCallback, PaintCallbackInfo, PathShape, RectShape, Rounding, Shape,
TextShape,
},
stats::PaintStats,
stroke::Stroke,
tessellator::{tessellate_shapes, TessellationOptions, Tessellator},
Expand Down
50 changes: 45 additions & 5 deletions epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,45 @@ fn dashes_from_line(

// ----------------------------------------------------------------------------

pub struct PaintCallbackInfo {
/// Viewport in points.
pub rect: Rect,

/// Pixels per point.
pub pixels_per_point: f32,

/// Full size of the screen, in pixels.
pub screen_size_px: [u32; 2],
}

impl PaintCallbackInfo {
#[inline]
pub fn viewport_left_px(&self) -> f32 {
self.rect.min.x * self.pixels_per_point
}

#[inline]
pub fn viewport_top_px(&self) -> f32 {
self.rect.min.y * self.pixels_per_point
}

// This is what `glViewport` etc expects.
#[inline]
pub fn viewport_from_bottom_px(&self) -> f32 {
self.screen_size_px[1] as f32 - self.rect.max.y * self.pixels_per_point
}

#[inline]
pub fn viewport_width_px(&self) -> f32 {
self.rect.width() * self.pixels_per_point
}

#[inline]
pub fn viewport_height_px(&self) -> f32 {
self.rect.height() * self.pixels_per_point
}
}

/// If you want to paint some 3D shapes inside an egui region, you can use this.
///
/// This is advanced usage, and is backend specific.
Expand All @@ -650,21 +689,22 @@ pub struct PaintCallback {
/// Where to paint.
pub rect: Rect,

/// Paint something custom using.
/// Paint something custom (e.g. 3D stuff).
///
/// The argument is the render context, and what it contains depends on the backend.
/// In `eframe` it will be `egui_glow::Painter`.
///
/// The rendering backend is responsible for first setting the active viewport to [`Self::rect`].
/// The rendering backend is also responsible for restoring any state it needs,
///
/// The rendering backend is also responsible for restoring any state,
/// such as the bound shader program and vertex array.
pub callback: std::sync::Arc<dyn Fn(&dyn std::any::Any) + Send + Sync>,
pub callback: std::sync::Arc<dyn Fn(&PaintCallbackInfo, &dyn std::any::Any) + Send + Sync>,
}

impl PaintCallback {
#[inline]
pub fn call(&self, render_ctx: &dyn std::any::Any) {
(self.callback)(render_ctx);
pub fn call(&self, info: &PaintCallbackInfo, render_ctx: &dyn std::any::Any) {
(self.callback)(info, render_ctx);
}
}

Expand Down

0 comments on commit 9181dc7

Please sign in to comment.