Skip to main content

oxide_engine/ui/
egui_manager.rs

1//! egui manager - context and state management
2//!
3//! Provides egui context and winit integration for debug/editor UI.
4//! Note: Applications should use the context directly for rendering
5//! due to wgpu version compatibility.
6
7use egui_winit::State;
8use wgpu::{Device, TextureFormat};
9use winit::window::Window;
10
11/// Manager for egui state and rendering.
12pub struct EguiManager {
13    /// The egui context.
14    pub context: egui::Context,
15    /// winit integration state.
16    pub winit_state: State,
17}
18
19impl EguiManager {
20    /// Creates a new egui manager.
21    pub fn new(
22        _device: &Device,
23        _output_format: TextureFormat,
24        window: &Window,
25        scale_factor: f32,
26    ) -> Self {
27        let context = egui::Context::default();
28        let viewport_id = context.viewport_id();
29
30        let winit_state = State::new(
31            context.clone(),
32            viewport_id,
33            window,
34            Some(scale_factor),
35            None,
36            None,
37        );
38
39        Self {
40            context,
41            winit_state,
42        }
43    }
44
45    /// Returns true if egui wants pointer input.
46    pub fn wants_pointer_input(&self) -> bool {
47        self.context.wants_pointer_input()
48    }
49
50    /// Returns true if egui wants keyboard input.
51    pub fn wants_keyboard_input(&self) -> bool {
52        self.context.wants_keyboard_input()
53    }
54
55    /// Begins a new egui frame.
56    pub fn begin_frame(&mut self, window: &Window) {
57        let raw_input = self.winit_state.take_egui_input(window);
58        self.context.begin_pass(raw_input);
59    }
60
61    /// Ends the current frame and returns the output.
62    pub fn end_frame(&mut self) -> egui::FullOutput {
63        self.context.end_pass()
64    }
65}