1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! [`egui`] bindings for [`glium`](https://github.com/glium/glium).
//!
//! The main type you want to use is [`EguiGlium`].
//!
//! If you are writing an app, you may want to look at [`eframe`](https://docs.rs/eframe) instead.
//!
//! ## Feature flags
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
//!

#![allow(clippy::float_cmp)]
#![allow(clippy::manual_range_contains)]
#![forbid(unsafe_code)]

mod painter;
use glium::glutin::surface::WindowSurface;
pub use painter::Painter;

pub use egui_winit;

use egui_winit::winit::event_loop::EventLoopWindowTarget;
pub use egui_winit::EventResponse;

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

/// Convenience wrapper for using [`egui`] from a [`glium`] app.
pub struct EguiGlium {
    pub egui_winit: egui_winit::State,
    pub painter: crate::Painter,

    shapes: Vec<egui::epaint::ClippedShape>,
    textures_delta: egui::TexturesDelta,
}

impl EguiGlium {
    pub fn new<E>(
        viewport_id: egui::ViewportId,
        display: &glium::Display<WindowSurface>,
        window: &winit::window::Window,
        event_loop: &EventLoopWindowTarget<E>,
    ) -> Self {
        let painter = crate::Painter::new(display);

        let pixels_per_point = window.scale_factor() as f32;
        let egui_winit = egui_winit::State::new(
            Default::default(),
            viewport_id,
            event_loop,
            Some(pixels_per_point),
            Some(painter.max_texture_side()),
        );

        Self {
            egui_winit,
            painter,
            shapes: Default::default(),
            textures_delta: Default::default(),
        }
    }

    pub fn egui_ctx(&self) -> &egui::Context {
        self.egui_winit.egui_ctx()
    }

    pub fn on_event(
        &mut self,
        window: &winit::window::Window,
        event: &winit::event::WindowEvent,
    ) -> EventResponse {
        self.egui_winit.on_window_event(window, event)
    }

    /// Runs the main egui render.
    ///
    /// Call [`Self::paint`] later to paint.
    pub fn run(&mut self, window: &winit::window::Window, run_ui: impl FnMut(&egui::Context)) {
        let raw_input = self.egui_winit.take_egui_input(window);
        let egui::FullOutput {
            platform_output,
            textures_delta,
            shapes,
            ..
        } = self.egui_ctx().run(raw_input, run_ui);

        self.egui_winit
            .handle_platform_output(window, platform_output);

        self.shapes = shapes;
        self.textures_delta.append(textures_delta);
    }

    /// Paint the results of the last call to [`Self::run`].
    pub fn paint<T: glium::Surface>(
        &mut self,
        display: &glium::Display<WindowSurface>,
        target: &mut T,
    ) {
        let shapes = std::mem::take(&mut self.shapes);
        let textures_delta = std::mem::take(&mut self.textures_delta);
        let clipped_primitives = self
            .egui_ctx()
            .tessellate(shapes, self.egui_ctx().pixels_per_point());
        self.painter.paint_and_update_textures(
            display,
            target,
            self.egui_ctx().pixels_per_point(),
            &clipped_primitives,
            &textures_delta,
        );
    }
}