1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

glow painter improvements (#1406)

* Add viewport info to PaintCallback
* glow: be more explicit with more state
* glow: Refactor VAO
This commit is contained in:
Emil Ernerfeldt
2022-03-22 23:11:27 +01:00
committed by GitHub
parent 6f10e2e725
commit ea9393aa9b
7 changed files with 190 additions and 178 deletions

View File

@@ -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},

View File

@@ -642,6 +642,52 @@ fn dashes_from_line(
// ----------------------------------------------------------------------------
/// Information passed along with [`PaintCallback`] ([`Shape::Callback`]).
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 {
/// 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
}
/// 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
}
/// 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
}
/// Viewport width in physical pixels.
#[inline]
pub fn viewport_width_px(&self) -> f32 {
self.rect.width() * self.pixels_per_point
}
/// Viewport width in physical pixels.
#[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.
@@ -650,21 +696,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);
}
}