astrelis-egui 0.0.1

EGUI integration for Astrelis
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use astrelis_core::profiling::profile_function;
use astrelis_render::RenderableWindow;
use astrelis_winit::event::{ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta};

#[derive(Clone, Copy, Debug, Default)]
pub struct EventResponse {
    /// If true, egui consumed this event, i.e. wants exclusive use of this event
    /// (e.g. a mouse click on an egui window, or entering text into a text field).
    pub consumed: bool,

    /// Do we need an egui refresh because of this event?
    pub repaint: bool,
}

pub struct State {
    context: egui::Context,
    input: egui::RawInput,
    viewport_id: egui::ViewportId,
    pointer_pos_in_points: Option<egui::Pos2>,
    any_pointer_button_down: bool,
    current_cursor_icon: Option<egui::CursorIcon>,
}

impl State {
    pub fn new(
        context: egui::Context,
        viewport_id: egui::ViewportId,
        native_pixels_per_point: Option<f32>,
        max_texture_side: Option<usize>,
    ) -> Self {
        let input = egui::RawInput {
            focused: false,
            ..Default::default()
        };

        let mut slf = Self {
            context,
            viewport_id,
            input,
            pointer_pos_in_points: None,
            any_pointer_button_down: false,
            current_cursor_icon: None,
        };

        slf.input
            .viewports
            .entry(egui::ViewportId::ROOT)
            .or_default()
            .native_pixels_per_point = native_pixels_per_point;

        if let Some(max_texture_side) = max_texture_side {
            slf.input.max_texture_side = Some(max_texture_side);
        }

        slf
    }

    pub fn take_input(&mut self, window: &RenderableWindow) -> egui::RawInput {
        let screen_size_in_pixels = screen_size_in_pixels(window);
        let screen_size_in_points = screen_size_in_pixels / pixels_per_point(&self.context, window);

        self.input.screen_rect = (screen_size_in_points.x > 0.0 && screen_size_in_points.y > 0.0)
            .then(|| egui::Rect::from_min_size(egui::Pos2::ZERO, screen_size_in_points));

        self.input.viewport_id = self.viewport_id;

        self.input
            .viewports
            .entry(self.viewport_id)
            .or_default()
            .native_pixels_per_point = Some(window.window().window.scale_factor() as f32);

        self.input.take()
    }

    pub fn handle_platform_output(
        &mut self,
        _window: &RenderableWindow,
        _output: egui::PlatformOutput,
    ) {
        // TODO: Handle cursor changes, clipboard copies, opening links in browsers
    }

    pub fn on_event(&mut self, window: &RenderableWindow, event: &Event) -> EventResponse {
        profile_function!();
        match event {
            Event::ScaleFactorChanged(scale_factor) => {
                let native_pixels_per_point = *scale_factor as f32;

                self.input
                    .viewports
                    .entry(self.viewport_id)
                    .or_default()
                    .native_pixels_per_point = Some(native_pixels_per_point);

                EventResponse {
                    repaint: true,
                    consumed: false,
                }
            }

            Event::Focused(focused) => {
                self.input.focused = *focused;
                self.input.events.push(egui::Event::WindowFocused(*focused));
                EventResponse {
                    repaint: true,
                    consumed: false,
                }
            }

            Event::MouseButtonDown(button) => {
                self.on_mouse_button_input(ElementState::Pressed, *button);
                EventResponse {
                    repaint: true,
                    consumed: self.context.wants_pointer_input(),
                }
            }

            Event::MouseButtonUp(button) => {
                self.on_mouse_button_input(ElementState::Released, *button);
                EventResponse {
                    repaint: true,
                    consumed: self.context.wants_pointer_input(),
                }
            }

            Event::MouseScrolled(delta) => {
                self.on_mouse_wheel(window, *delta);
                EventResponse {
                    repaint: true,
                    consumed: self.context.wants_pointer_input(),
                }
            }

            Event::MouseMoved(pos) => {
                self.on_cursor_moved(window, *pos);
                EventResponse {
                    repaint: true,
                    consumed: self.context.is_using_pointer(),
                }
            }

            Event::MouseLeft => {
                self.pointer_pos_in_points = None;
                self.input.events.push(egui::Event::PointerGone);
                EventResponse {
                    repaint: true,
                    consumed: false,
                }
            }

            Event::KeyInput(event) => {
                if event.is_synthetic && event.state == ElementState::Pressed {
                    EventResponse {
                        repaint: true,
                        consumed: false,
                    }
                } else {
                    self.on_keyboard_input(event);

                    let consumed = self.context.wants_keyboard_input()
                        || matches!(
                            event.logical_key,
                            astrelis_winit::event::Key::Named(astrelis_winit::event::NamedKey::Tab)
                        );
                    EventResponse {
                        repaint: true,
                        consumed,
                    }
                }
            }

            _ => EventResponse {
                repaint: false,
                consumed: false,
            },
        }
    }

    fn on_mouse_button_input(&mut self, state: ElementState, button: MouseButton) {
        if let Some(pos) = self.pointer_pos_in_points {
            if let Some(button) = translate_mouse_button(button) {
                let pressed = state == ElementState::Pressed;

                self.input.events.push(egui::Event::PointerButton {
                    pos,
                    button,
                    pressed,
                    modifiers: self.input.modifiers,
                });

                if pressed {
                    self.any_pointer_button_down = true;
                } else {
                    self.any_pointer_button_down = false;
                }
            }
        }
    }

    fn on_cursor_moved(
        &mut self,
        window: &RenderableWindow,
        pos_in_logical: astrelis_core::geometry::Pos<f64>,
    ) {
        // Input is in logical pixels, convert to physical pixels first
        let scale_factor = window.window().window.scale_factor() as f32;
        let pos_in_physical = egui::pos2(
            pos_in_logical.x as f32 * scale_factor,
            pos_in_logical.y as f32 * scale_factor,
        );
        
        // Then convert physical pixels to egui points
        let pixels_per_point = pixels_per_point(&self.context, window);
        let pos_in_points = egui::pos2(
            pos_in_physical.x / pixels_per_point,
            pos_in_physical.y / pixels_per_point,
        );
        self.pointer_pos_in_points = Some(pos_in_points);

        self.input
            .events
            .push(egui::Event::PointerMoved(pos_in_points));
    }

    fn on_mouse_wheel(&mut self, window: &RenderableWindow, delta: MouseScrollDelta) {
        let pixels_per_point = pixels_per_point(&self.context, window);

        let (unit, delta) = match delta {
            MouseScrollDelta::LineDelta(x, y) => (egui::MouseWheelUnit::Line, egui::vec2(x, y)),
            MouseScrollDelta::PixelDelta(pos) => (
                egui::MouseWheelUnit::Point,
                egui::vec2(pos.x as f32, pos.y as f32) / pixels_per_point,
            ),
        };
        let modifiers = self.input.modifiers;
        self.input.events.push(egui::Event::MouseWheel {
            unit,
            delta,
            modifiers,
        });
    }

    fn on_keyboard_input(&mut self, event: &KeyEvent) {
        let KeyEvent {
            physical_key,
            logical_key,
            text,
            state,
            location: _,
            repeat: _,
            ..
        } = event;

        let pressed = *state == ElementState::Pressed;

        let physical_key = if let astrelis_winit::event::PhysicalKey::Code(keycode) = *physical_key
        {
            key_from_key_code(keycode)
        } else {
            None
        };

        let logical_key = key_from_winit_key(logical_key);

        if let Some(active_key) = logical_key.or(physical_key) {
            if pressed {
                if is_cut_command(self.input.modifiers, active_key) {
                    self.input.events.push(egui::Event::Cut);
                    return;
                } else if is_copy_command(self.input.modifiers, active_key) {
                    self.input.events.push(egui::Event::Copy);
                    return;
                } else if is_paste_command(self.input.modifiers, active_key) {
                    // TODO: Support clipboard
                    return;
                }
            }

            self.input.events.push(egui::Event::Key {
                key: active_key,
                physical_key,
                pressed,
                repeat: false,
                modifiers: self.input.modifiers,
            });
        }

        if let Some(text) = &text {
            if !text.is_empty() && text.chars().all(is_printable_char) {
                let is_cmd = self.input.modifiers.ctrl
                    || self.input.modifiers.command
                    || self.input.modifiers.mac_cmd;
                if pressed && !is_cmd {
                    self.input.events.push(egui::Event::Text(text.to_string()));
                }
            }
        }
    }
}

pub fn screen_size_in_pixels(window: &RenderableWindow) -> egui::Vec2 {
    let size = window.window().window.inner_size();
    egui::vec2(size.width as f32, size.height as f32)
}

pub fn pixels_per_point(context: &egui::Context, window: &RenderableWindow) -> f32 {
    let native_pixels_per_point = window.window().window.scale_factor() as f32;
    let egui_zoom_factor = context.zoom_factor();
    egui_zoom_factor * native_pixels_per_point
}

fn is_printable_char(chr: char) -> bool {
    let is_in_private_use_area = '\u{e000}' <= chr && chr <= '\u{f8ff}'
        || '\u{f0000}' <= chr && chr <= '\u{ffffd}'
        || '\u{100000}' <= chr && chr <= '\u{10fffd}';

    !is_in_private_use_area && !chr.is_ascii_control()
}

fn is_cut_command(modifiers: egui::Modifiers, keycode: egui::Key) -> bool {
    keycode == egui::Key::Cut
        || (modifiers.command && keycode == egui::Key::X)
        || (cfg!(target_os = "windows") && modifiers.shift && keycode == egui::Key::Delete)
}

fn is_copy_command(modifiers: egui::Modifiers, keycode: egui::Key) -> bool {
    keycode == egui::Key::Copy
        || (modifiers.command && keycode == egui::Key::C)
        || (cfg!(target_os = "windows") && modifiers.ctrl && keycode == egui::Key::Insert)
}

fn is_paste_command(modifiers: egui::Modifiers, keycode: egui::Key) -> bool {
    keycode == egui::Key::Paste
        || (modifiers.command && keycode == egui::Key::V)
        || (cfg!(target_os = "windows") && modifiers.shift && keycode == egui::Key::Insert)
}

fn translate_mouse_button(button: MouseButton) -> Option<egui::PointerButton> {
    match button {
        MouseButton::Left => Some(egui::PointerButton::Primary),
        MouseButton::Right => Some(egui::PointerButton::Secondary),
        MouseButton::Middle => Some(egui::PointerButton::Middle),
        MouseButton::Back => Some(egui::PointerButton::Extra1),
        MouseButton::Forward => Some(egui::PointerButton::Extra2),
        MouseButton::Other(_) => None,
    }
}

fn key_from_winit_key(key: &astrelis_winit::event::Key) -> Option<egui::Key> {
    use astrelis_winit::event::Key;

    match key {
        Key::Named(named_key) => key_from_named_key(*named_key),
        Key::Character(str) => egui::Key::from_name(str.as_str()),
        Key::Unidentified(_) | Key::Dead(_) => None,
    }
}

fn key_from_named_key(named_key: astrelis_winit::event::NamedKey) -> Option<egui::Key> {
    use astrelis_winit::event::NamedKey;
    use egui::Key;

    Some(match named_key {
        NamedKey::Enter => Key::Enter,
        NamedKey::Tab => Key::Tab,
        NamedKey::ArrowDown => Key::ArrowDown,
        NamedKey::ArrowLeft => Key::ArrowLeft,
        NamedKey::ArrowRight => Key::ArrowRight,
        NamedKey::ArrowUp => Key::ArrowUp,
        NamedKey::End => Key::End,
        NamedKey::Home => Key::Home,
        NamedKey::PageDown => Key::PageDown,
        NamedKey::PageUp => Key::PageUp,
        NamedKey::Backspace => Key::Backspace,
        NamedKey::Delete => Key::Delete,
        NamedKey::Insert => Key::Insert,
        NamedKey::Escape => Key::Escape,
        NamedKey::Cut => Key::Cut,
        NamedKey::Copy => Key::Copy,
        NamedKey::Paste => Key::Paste,
        NamedKey::Space => Key::Space,
        NamedKey::F1 => Key::F1,
        NamedKey::F2 => Key::F2,
        NamedKey::F3 => Key::F3,
        NamedKey::F4 => Key::F4,
        NamedKey::F5 => Key::F5,
        NamedKey::F6 => Key::F6,
        NamedKey::F7 => Key::F7,
        NamedKey::F8 => Key::F8,
        NamedKey::F9 => Key::F9,
        NamedKey::F10 => Key::F10,
        NamedKey::F11 => Key::F11,
        NamedKey::F12 => Key::F12,
        NamedKey::F13 => Key::F13,
        NamedKey::F14 => Key::F14,
        NamedKey::F15 => Key::F15,
        NamedKey::F16 => Key::F16,
        NamedKey::F17 => Key::F17,
        NamedKey::F18 => Key::F18,
        NamedKey::F19 => Key::F19,
        NamedKey::F20 => Key::F20,
        NamedKey::F21 => Key::F21,
        NamedKey::F22 => Key::F22,
        NamedKey::F23 => Key::F23,
        NamedKey::F24 => Key::F24,
        NamedKey::F25 => Key::F25,
        NamedKey::F26 => Key::F26,
        NamedKey::F27 => Key::F27,
        NamedKey::F28 => Key::F28,
        NamedKey::F29 => Key::F29,
        NamedKey::F30 => Key::F30,
        NamedKey::F31 => Key::F31,
        NamedKey::F32 => Key::F32,
        NamedKey::F33 => Key::F33,
        NamedKey::F34 => Key::F34,
        NamedKey::F35 => Key::F35,
        _ => {
            tracing::trace!("Unknown key: {named_key:?}");
            return None;
        }
    })
}

fn key_from_key_code(key: astrelis_winit::event::KeyCode) -> Option<egui::Key> {
    use astrelis_winit::event::KeyCode;
    use egui::Key;

    Some(match key {
        KeyCode::ArrowDown => Key::ArrowDown,
        KeyCode::ArrowLeft => Key::ArrowLeft,
        KeyCode::ArrowRight => Key::ArrowRight,
        KeyCode::ArrowUp => Key::ArrowUp,
        KeyCode::Escape => Key::Escape,
        KeyCode::Tab => Key::Tab,
        KeyCode::Backspace => Key::Backspace,
        KeyCode::Enter | KeyCode::NumpadEnter => Key::Enter,
        KeyCode::Insert => Key::Insert,
        KeyCode::Delete => Key::Delete,
        KeyCode::Home => Key::Home,
        KeyCode::End => Key::End,
        KeyCode::PageUp => Key::PageUp,
        KeyCode::PageDown => Key::PageDown,
        KeyCode::Space => Key::Space,
        KeyCode::Comma => Key::Comma,
        KeyCode::Period => Key::Period,
        KeyCode::Semicolon => Key::Semicolon,
        KeyCode::Backslash => Key::Backslash,
        KeyCode::Slash | KeyCode::NumpadDivide => Key::Slash,
        KeyCode::BracketLeft => Key::OpenBracket,
        KeyCode::BracketRight => Key::CloseBracket,
        KeyCode::Backquote => Key::Backtick,
        KeyCode::Quote => Key::Quote,
        KeyCode::Cut => Key::Cut,
        KeyCode::Copy => Key::Copy,
        KeyCode::Paste => Key::Paste,
        KeyCode::Minus | KeyCode::NumpadSubtract => Key::Minus,
        KeyCode::NumpadAdd => Key::Plus,
        KeyCode::Equal => Key::Equals,
        KeyCode::Digit0 | KeyCode::Numpad0 => Key::Num0,
        KeyCode::Digit1 | KeyCode::Numpad1 => Key::Num1,
        KeyCode::Digit2 | KeyCode::Numpad2 => Key::Num2,
        KeyCode::Digit3 | KeyCode::Numpad3 => Key::Num3,
        KeyCode::Digit4 | KeyCode::Numpad4 => Key::Num4,
        KeyCode::Digit5 | KeyCode::Numpad5 => Key::Num5,
        KeyCode::Digit6 | KeyCode::Numpad6 => Key::Num6,
        KeyCode::Digit7 | KeyCode::Numpad7 => Key::Num7,
        KeyCode::Digit8 | KeyCode::Numpad8 => Key::Num8,
        KeyCode::Digit9 | KeyCode::Numpad9 => Key::Num9,
        KeyCode::KeyA => Key::A,
        KeyCode::KeyB => Key::B,
        KeyCode::KeyC => Key::C,
        KeyCode::KeyD => Key::D,
        KeyCode::KeyE => Key::E,
        KeyCode::KeyF => Key::F,
        KeyCode::KeyG => Key::G,
        KeyCode::KeyH => Key::H,
        KeyCode::KeyI => Key::I,
        KeyCode::KeyJ => Key::J,
        KeyCode::KeyK => Key::K,
        KeyCode::KeyL => Key::L,
        KeyCode::KeyM => Key::M,
        KeyCode::KeyN => Key::N,
        KeyCode::KeyO => Key::O,
        KeyCode::KeyP => Key::P,
        KeyCode::KeyQ => Key::Q,
        KeyCode::KeyR => Key::R,
        KeyCode::KeyS => Key::S,
        KeyCode::KeyT => Key::T,
        KeyCode::KeyU => Key::U,
        KeyCode::KeyV => Key::V,
        KeyCode::KeyW => Key::W,
        KeyCode::KeyX => Key::X,
        KeyCode::KeyY => Key::Y,
        KeyCode::KeyZ => Key::Z,
        KeyCode::F1 => Key::F1,
        KeyCode::F2 => Key::F2,
        KeyCode::F3 => Key::F3,
        KeyCode::F4 => Key::F4,
        KeyCode::F5 => Key::F5,
        KeyCode::F6 => Key::F6,
        KeyCode::F7 => Key::F7,
        KeyCode::F8 => Key::F8,
        KeyCode::F9 => Key::F9,
        KeyCode::F10 => Key::F10,
        KeyCode::F11 => Key::F11,
        KeyCode::F12 => Key::F12,
        KeyCode::F13 => Key::F13,
        KeyCode::F14 => Key::F14,
        KeyCode::F15 => Key::F15,
        KeyCode::F16 => Key::F16,
        KeyCode::F17 => Key::F17,
        KeyCode::F18 => Key::F18,
        KeyCode::F19 => Key::F19,
        KeyCode::F20 => Key::F20,
        KeyCode::F21 => Key::F21,
        KeyCode::F22 => Key::F22,
        KeyCode::F23 => Key::F23,
        KeyCode::F24 => Key::F24,
        KeyCode::F25 => Key::F25,
        KeyCode::F26 => Key::F26,
        KeyCode::F27 => Key::F27,
        KeyCode::F28 => Key::F28,
        KeyCode::F29 => Key::F29,
        KeyCode::F30 => Key::F30,
        KeyCode::F31 => Key::F31,
        KeyCode::F32 => Key::F32,
        KeyCode::F33 => Key::F33,
        KeyCode::F34 => Key::F34,
        KeyCode::F35 => Key::F35,
        _ => return None,
    })
}