egui_window_glfw 0.2.0

glfw window integration for egui
Documentation
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use egui::{Event, Key, PointerButton, Pos2, RawInput};
use egui_backend::raw_window_handle::HasRawWindowHandle;
use egui_backend::WindowBackend;
use egui_backend::*;
use glfw::Action;
use glfw::ClientApiHint;
use glfw::Context;
use glfw::Glfw;
use glfw::OpenGlProfileHint;
use glfw::SwapInterval;
use glfw::WindowEvent;
use glfw::WindowHint;
use std::sync::mpsc::Receiver;

pub use glfw;

pub struct GlfwBackend {
    pub glfw: glfw::Glfw,
    pub events_receiver: Receiver<(f64, WindowEvent)>,
    pub window: glfw::Window,
    pub size_physical_pixels: [u32; 2],
    pub scale: [f32; 2],
    pub cursor_pos_physical_pixels: [f32; 2],
    pub raw_input: RawInput,
    pub frame_events: Vec<WindowEvent>,
    pub resized_event_pending: bool,
    pub backend_settings: BackendSettings,
}

unsafe impl HasRawWindowHandle for GlfwBackend {
    fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
        self.window.raw_window_handle()
    }
}

impl OpenGLWindowContext for GlfwBackend {
    fn swap_buffers(&mut self) {
        self.window.swap_buffers()
    }

    fn get_proc_address(&mut self, symbol: &str) -> *const core::ffi::c_void {
        self.window.get_proc_address(symbol)
    }
}

/// The configuration struct for Glfw Backend
///
#[derive(Default)]
pub struct GlfwConfig {
    /// This callback is called with `&mut Glfw` right after `Glfw` is created
    pub glfw_callback: Option<Box<dyn FnOnce(&mut Glfw)>>,
}
impl WindowBackend for GlfwBackend {
    type Configuration = GlfwConfig;

    fn new(config: Self::Configuration, backend_settings: BackendSettings) -> Self {
        let mut glfw_context =
            glfw::init(glfw::FAIL_ON_ERRORS).expect("failed to create glfw context");
        if let Some(glfw_callback) = config.glfw_callback {
            glfw_callback(&mut glfw_context);
        }
        let mut swap_interval = None;
        let mut opengl = false;
        // set hints based on gfx api config
        match backend_settings.gfx_api_type.clone() {
            GfxApiType::OpenGL { native_config } => {
                opengl = true;
                let NativeGlConfig {
                    major,
                    minor,
                    es,
                    core,
                    depth_bits,
                    stencil_bits,
                    samples,
                    srgb,
                    double_buffer,
                    vsync,
                    debug,
                } = native_config;
                if let Some(major) = major {
                    glfw_context.window_hint(WindowHint::ContextVersionMajor(major.into()));
                }
                if let Some(value) = minor {
                    glfw_context.window_hint(WindowHint::ContextVersionMinor(value.into()));
                }
                if let Some(value) = es {
                    glfw_context.window_hint(WindowHint::ClientApi(if value {
                        ClientApiHint::OpenGlEs
                    } else {
                        ClientApiHint::OpenGl
                    }));
                }
                if let Some(value) = core {
                    glfw_context.window_hint(WindowHint::OpenGlProfile(if value {
                        glfw::OpenGlProfileHint::Core
                    } else {
                        OpenGlProfileHint::Compat
                    }));
                }

                glfw_context.window_hint(WindowHint::DepthBits(depth_bits.map(Into::into)));

                glfw_context.window_hint(WindowHint::StencilBits(stencil_bits.map(Into::into)));

                if let Some(srgb) = srgb {
                    glfw_context.window_hint(WindowHint::SRgbCapable(srgb));
                }
                if let Some(samples) = samples {
                    glfw_context.window_hint(WindowHint::Samples(Some(samples as u32)));
                }
                if let Some(value) = double_buffer {
                    glfw_context.window_hint(WindowHint::DoubleBuffer(value.into()));
                }
                swap_interval = vsync;

                if let Some(debug) = debug {
                    glfw_context.window_hint(WindowHint::OpenGlDebugContext(debug));
                }
            }
            GfxApiType::NoApi => {
                glfw_context.window_hint(WindowHint::ClientApi(ClientApiHint::NoApi));
            }
            GfxApiType::Vulkan => {
                glfw_context.window_hint(WindowHint::ClientApi(ClientApiHint::NoApi));
            }
        }
        // create a window
        let (mut window, events_receiver) = glfw_context
            .create_window(800, 600, "Overlay Window", glfw::WindowMode::Windowed)
            .expect("failed to create glfw window");
        // set which events you care about
        window.set_all_polling(true);
        window.set_store_lock_key_mods(true);
        if opengl {
            window.make_current();

            if let Some(value) = swap_interval {
                glfw_context.set_swap_interval(if value {
                    SwapInterval::Sync(1)
                } else {
                    SwapInterval::None
                });
            }
        }
        // collect details and keep them updated
        let (width, height) = window.get_framebuffer_size();
        let scale = window.get_content_scale();
        let cursor_position = window.get_cursor_pos();
        let size_physical_pixels = [
            width.try_into().expect("width not fit in u32"),
            height.try_into().expect("height not fit in u32"),
        ];
        let mut raw_input = RawInput::default();
        // set raw input screen rect details so that first frame
        // will have correct size even without any resize event
        raw_input.screen_rect = Some(egui::Rect::from_points(&[
            Default::default(),
            [width as f32, height as f32].into(),
        ]));
        raw_input.pixels_per_point = Some(scale.0);
        Self {
            glfw: glfw_context,
            events_receiver,
            window,
            size_physical_pixels,
            scale: [scale.0, scale.1],
            cursor_pos_physical_pixels: [cursor_position.0 as f32, cursor_position.1 as f32],
            raw_input,
            frame_events: vec![],
            resized_event_pending: true, // provide so that on first prepare frame, renderers can set their viewport sizes
            backend_settings,
        }
    }

    fn take_raw_input(&mut self) -> RawInput {
        self.raw_input.take()
    }

    fn run_event_loop<G: GfxBackend<Self>, U: UserApp<Self, G>>(
        mut self,
        mut gfx_backend: G,
        mut user_app: U,
    ) {
        let egui_context = egui::Context::default();
        while !self.window.should_close() {
            // gather events
            self.tick();
            // take egui input
            let input = self.take_raw_input();
            // take any frambuffer resize events

            // prepare surface for drawing
            gfx_backend.prepare_frame(self.resized_event_pending, &mut self);
            self.resized_event_pending = false;
            // begin egui with input
            egui_context.begin_frame(input);
            // run userapp gui function. let user do anything he wants with window or gfx backends
            user_app.run(&egui_context, &mut self, &mut gfx_backend);
            // end frame
            let output = egui_context.end_frame();
            // prepare egui render data for gfx backend
            let gfx_output = EguiGfxOutput {
                meshes: egui_context.tessellate(output.shapes),
                textures_delta: output.textures_delta,
                screen_size_logical: [
                    self.size_physical_pixels[0] as f32 / self.scale[0],
                    self.size_physical_pixels[1] as f32 / self.scale[0],
                ],
            };
            // render egui with gfx backend
            gfx_backend.prepare_render(gfx_output);
            gfx_backend.render();
            // present the frame and loop back
            gfx_backend.present(&mut self);
        }
    }

    fn get_live_physical_size_framebuffer(&mut self) -> [u32; 2] {
        let physical_fb_size = self.window.get_framebuffer_size();
        self.size_physical_pixels = [physical_fb_size.0 as u32, physical_fb_size.1 as u32];
        self.size_physical_pixels
    }

    fn get_settings(&self) -> &BackendSettings {
        &self.backend_settings
    }
}

impl GlfwBackend {
    pub fn tick(&mut self) {
        self.glfw.poll_events();
        self.frame_events.clear();
        let cursor_position = self.window.get_cursor_pos();
        let cursor_position = [cursor_position.0 as f32, cursor_position.1 as f32];
        self.cursor_pos_physical_pixels = cursor_position;
        // when we are passthorugh, we use this to get latest position
        // if cursor_position != self.cursor_pos_physical_pixels {
        //     self.cursor_pos_physical_pixels = cursor_position;
        //     self.raw_input.events.push(Event::PointerMoved(
        //         [
        //             cursor_position[0] / self.scale[0],
        //             cursor_position[1] / self.scale[1],
        //         ]
        //         .into(),
        //     ))
        // }

        for (_, event) in glfw::flush_messages(&self.events_receiver) {
            // if let &glfw::WindowEvent::CursorPos(..) = &event {
            //     continue;
            // }
            self.frame_events.push(event.clone());
            if let Some(ev) = match event {
                glfw::WindowEvent::FramebufferSize(w, h) => {
                    self.size_physical_pixels = [w as u32, h as u32];
                    self.resized_event_pending = true;
                    self.raw_input.screen_rect = Some(egui::Rect::from_two_pos(
                        Default::default(),
                        [w as f32 / self.scale[0], h as f32 / self.scale[1]].into(),
                    ));

                    None
                }
                glfw::WindowEvent::MouseButton(mb, a, m) => {
                    let emb = Event::PointerButton {
                        pos: Pos2 {
                            x: cursor_position[0] / self.scale[0],
                            y: cursor_position[1] / self.scale[1],
                        },
                        button: glfw_to_egui_pointer_button(mb),
                        pressed: glfw_to_egui_action(a),
                        modifiers: glfw_to_egui_modifers(m),
                    };
                    Some(emb)
                }
                // we scroll 25 pixels at a time
                glfw::WindowEvent::Scroll(x, y) => {
                    Some(Event::Scroll([x as f32 * 25.0, y as f32 * 25.0].into()))
                }
                glfw::WindowEvent::Key(k, _, a, m) => match k {
                    glfw::Key::C => {
                        if glfw_to_egui_action(a) && m.contains(glfw::Modifiers::Control) {
                            Some(Event::Copy)
                        } else {
                            None
                        }
                    }
                    glfw::Key::X => {
                        if glfw_to_egui_action(a) && m.contains(glfw::Modifiers::Control) {
                            Some(Event::Cut)
                        } else {
                            None
                        }
                    }
                    glfw::Key::V => {
                        if glfw_to_egui_action(a) && m.contains(glfw::Modifiers::Control) {
                            Some(Event::Text(
                                self.window.get_clipboard_string().unwrap_or_default(),
                            ))
                        } else {
                            None
                        }
                    }
                    _ => None,
                }
                .or_else(|| {
                    glfw_to_egui_key(k).map(|key| Event::Key {
                        key,
                        pressed: glfw_to_egui_action(a),
                        modifiers: glfw_to_egui_modifers(m),
                    })
                }),
                glfw::WindowEvent::Char(c) => Some(Event::Text(c.to_string())),
                glfw::WindowEvent::ContentScale(x, y) => {
                    self.raw_input.pixels_per_point = Some(x);
                    self.scale = [x, y];
                    None
                }
                glfw::WindowEvent::Close => {
                    self.window.set_should_close(true);
                    None
                }

                glfw::WindowEvent::FileDrop(f) => {
                    self.raw_input
                        .dropped_files
                        .extend(f.into_iter().map(|p| egui::DroppedFile {
                            path: Some(p),
                            name: "".to_string(),
                            last_modified: None,
                            bytes: None,
                        }));
                    None
                }
                glfw::WindowEvent::CursorPos(x, y) => {
                    Some(egui::Event::PointerMoved([x as f32, y as f32].into()))
                }
                _rest => None,
            } {
                self.raw_input.events.push(ev);
            }
        }
    }
}

/// a function to get the matching egui key event for a given glfw key. egui does not support all the keys provided here.
fn glfw_to_egui_key(key: glfw::Key) -> Option<Key> {
    match key {
        glfw::Key::Space => Some(Key::Space),
        glfw::Key::Num0 => Some(Key::Num0),
        glfw::Key::Num1 => Some(Key::Num1),
        glfw::Key::Num2 => Some(Key::Num2),
        glfw::Key::Num3 => Some(Key::Num3),
        glfw::Key::Num4 => Some(Key::Num4),
        glfw::Key::Num5 => Some(Key::Num5),
        glfw::Key::Num6 => Some(Key::Num6),
        glfw::Key::Num7 => Some(Key::Num7),
        glfw::Key::Num8 => Some(Key::Num8),
        glfw::Key::Num9 => Some(Key::Num9),
        glfw::Key::A => Some(Key::A),
        glfw::Key::B => Some(Key::B),
        glfw::Key::C => Some(Key::C),
        glfw::Key::D => Some(Key::D),
        glfw::Key::E => Some(Key::E),
        glfw::Key::F => Some(Key::F),
        glfw::Key::G => Some(Key::G),
        glfw::Key::H => Some(Key::H),
        glfw::Key::I => Some(Key::I),
        glfw::Key::J => Some(Key::J),
        glfw::Key::K => Some(Key::K),
        glfw::Key::L => Some(Key::L),
        glfw::Key::M => Some(Key::M),
        glfw::Key::N => Some(Key::N),
        glfw::Key::O => Some(Key::O),
        glfw::Key::P => Some(Key::P),
        glfw::Key::Q => Some(Key::Q),
        glfw::Key::R => Some(Key::R),
        glfw::Key::S => Some(Key::S),
        glfw::Key::T => Some(Key::T),
        glfw::Key::U => Some(Key::U),
        glfw::Key::V => Some(Key::V),
        glfw::Key::W => Some(Key::W),
        glfw::Key::X => Some(Key::X),
        glfw::Key::Y => Some(Key::Y),
        glfw::Key::Z => Some(Key::Z),
        glfw::Key::Escape => Some(Key::Escape),
        glfw::Key::Enter => Some(Key::Enter),
        glfw::Key::Tab => Some(Key::Tab),
        glfw::Key::Backspace => Some(Key::Backspace),
        glfw::Key::Insert => Some(Key::Insert),
        glfw::Key::Delete => Some(Key::Delete),
        glfw::Key::Right => Some(Key::ArrowRight),
        glfw::Key::Left => Some(Key::ArrowLeft),
        glfw::Key::Down => Some(Key::ArrowDown),
        glfw::Key::Up => Some(Key::ArrowUp),
        glfw::Key::PageUp => Some(Key::PageUp),
        glfw::Key::PageDown => Some(Key::PageDown),
        glfw::Key::Home => Some(Key::Home),
        glfw::Key::End => Some(Key::End),
        _ => None,
    }
}

pub fn glfw_to_egui_modifers(modifiers: glfw::Modifiers) -> egui::Modifiers {
    egui::Modifiers {
        alt: modifiers.contains(glfw::Modifiers::Alt),
        ctrl: modifiers.contains(glfw::Modifiers::Control),
        shift: modifiers.contains(glfw::Modifiers::Shift),
        mac_cmd: false,
        command: modifiers.contains(glfw::Modifiers::Control),
    }
}

pub fn glfw_to_egui_pointer_button(mb: glfw::MouseButton) -> PointerButton {
    match mb {
        glfw::MouseButton::Button1 => PointerButton::Primary,
        glfw::MouseButton::Button2 => PointerButton::Secondary,
        glfw::MouseButton::Button3 => PointerButton::Middle,
        _ => PointerButton::Secondary,
    }
}

pub fn glfw_to_egui_action(a: glfw::Action) -> bool {
    match a {
        Action::Release => false,
        Action::Press => true,
        Action::Repeat => true,
    }
}