1pub use egui;
31#[cfg(feature = "glow-backend")]
32pub use egui_glow;
33pub use sdl2;
34
35#[cfg(feature = "canvas-backend")]
36pub mod canvas;
37#[cfg(feature = "glow-backend")]
38pub mod glow;
39pub mod painter;
40pub mod state;
41
42#[cfg(feature = "canvas-backend")]
43pub use canvas::*;
44#[cfg(feature = "glow-backend")]
45pub use glow::*;
46pub use state::*;
47
48struct EguiBackend {
49 pub ctx: egui::Context,
50
51 shapes: Vec<egui::epaint::ClippedShape>,
53 pixels_per_point: f32,
54 textures_delta: egui::TexturesDelta,
55}
56
57impl EguiBackend {
58 pub fn new(ctx: egui::Context) -> Self {
59 Self {
60 ctx,
61 shapes: Default::default(),
62 pixels_per_point: 1.0,
63 textures_delta: Default::default(),
64 }
65 }
66
67 #[inline]
68 pub fn run(&mut self, state: &mut State, run_ui: impl FnMut(&egui::Context)) {
69 let raw_input = state.take_egui_input();
70 let egui::FullOutput {
71 platform_output,
72 viewport_output: _,
73 textures_delta,
74 shapes,
75 pixels_per_point,
76 } = self.ctx.run(raw_input, run_ui);
77 state.handle_platform_output(platform_output);
78
79 self.shapes = shapes;
80 self.textures_delta.append(textures_delta);
81 self.pixels_per_point = pixels_per_point;
82 }
83
84 #[inline]
85 pub fn paint(&mut self, state: &State, painter: &mut impl PainterTrait) {
86 let mut textures_delta = std::mem::take(&mut self.textures_delta);
87
88 for (id, image_delta) in textures_delta.set {
89 painter.set_texture(id, &image_delta);
90 }
91
92 let pixels_per_point = self.pixels_per_point;
93 let shapes = std::mem::take(&mut self.shapes);
94 let clipped_primitives = self.ctx.tessellate(shapes, pixels_per_point);
95 let size = state.get_window_size();
96 painter.paint_primitives(size.into(), pixels_per_point, clipped_primitives);
97
98 for id in textures_delta.free.drain(..) {
99 painter.free_texture(id);
100 }
101 }
102}
103
104trait PainterTrait {
105 fn paint_primitives(
106 &mut self,
107 screen_size_px: [u32; 2],
108 pixels_per_point: f32,
109 clipped_primitives: Vec<egui::ClippedPrimitive>,
110 );
111 fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta);
112 fn free_texture(&mut self, tex_id: egui::TextureId);
113}