egui-ark 0.37.0

Bindings between the egui GUI library and ark
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
534
535
536
537
538
539
540
541
542
543
use ark::applet::input;
use ark::render;
use ark::render::TextureFormat;
use ark::ColorRgba8;
use ark::Vec2;
use std::collections::HashMap;

/// Bindings to the egui library.
///
/// Instantiate one [`EguiArk`] in your module.
///
/// ``` no_run
/// # fn applet() -> ark::applet::Applet { unimplemented!() }
/// # fn render() -> ark::render::Render { unimplemented!() }
/// let mut egui = EguiArk::default();
/// let ctx = egui.begin_frame(&applet());
/// ui(ctx);
/// let ctx = egui.end_frame(&render(), &applet());
///
/// fn ui(ctx: &mut egui::Context) {
///     egui::Window::new("My window").show(ctx, |ui| {
///         ui.label("Hello world!");
///     });
/// }
/// ```
#[derive(Default)]
pub struct EguiArk {
    input_mngr: input::InputManager,
    egui_input: egui::RawInput,
    ctx: egui::Context,
    painter: EguiPainter,
}

impl EguiArk {
    /// Access to the egui context.
    pub fn ctx(&self) -> &egui::Context {
        &self.ctx
    }

    /// Call before using egui. This gathers input and prepares for a new frame.
    pub fn begin_frame(&mut self, applet: &ark::applet::Applet) -> egui::Context {
        puffin::profile_function!();
        if applet.window_state().is_some() {
            self.input_mngr.update(applet);
            update_input(&mut self.egui_input, applet, &self.input_mngr);
        }
        puffin::profile_scope!("egui::begin_frame");
        self.ctx.begin_frame(self.egui_input.take());
        self.ctx.clone()
    }

    /// Call after using egui. This will render the interface.
    pub fn end_frame(&mut self, render: &render::Render, applet: &ark::applet::Applet) {
        puffin::profile_function!();

        let full_output = {
            puffin::profile_scope!("egui::end_frame");
            self.ctx.end_frame()
        };

        let platform_output = full_output.platform_output;

        if applet.window_state().is_some() {
            let paint_jobs = {
                puffin::profile_scope!("egui::tessellate");
                self.ctx.tessellate(full_output.shapes)
            };
            if !platform_output.copied_text.is_empty() {
                applet.set_clipboard_string(&platform_output.copied_text);
            }

            if platform_output.cursor_icon == egui::CursorIcon::None {
                applet.set_cursor_mode(ark::applet::CursorMode::Hide);
            } else {
                applet.set_cursor_mode(ark::applet::CursorMode::None);
                applet.set_cursor_shape(from_egui_cursor(platform_output.cursor_icon));
            }

            self.painter
                .upload_font_textures(render, full_output.textures_delta);
            self.painter.paint(render, &self.ctx, &paint_jobs);
        }
    }

    /// True if egui is currently interested in the pointer (mouse/touch).
    /// Could be the mouse is hovering over a egui window,
    /// or the user is dragging an egui widget.
    /// If false, the mouse is outside of any egui area and so
    /// you may be interested in what it is doing (e.g. controlling your game).
    pub fn wants_pointer_input(&self) -> bool {
        self.ctx.wants_pointer_input()
    }

    /// If true, egui is currently listening on text input (e.g. typing text in a `TextEdit`).
    pub fn wants_keyboard_input(&self) -> bool {
        self.ctx.wants_keyboard_input()
    }

    pub fn is_mouse_down(&self) -> bool {
        self.ctx.input().pointer.any_down()
    }

    /// Save egui state (window positions etc)
    pub fn persist(&self) -> serde_json::Value {
        serde_json::to_value(&*self.ctx.memory()).unwrap_or_default()
    }

    /// Restore egui (window positions etc)
    pub fn restore(&self, value: serde_json::Value) {
        match serde_json::from_value(value) {
            Ok(memory) => {
                *self.ctx.memory() = memory;
            }
            Err(err) => {
                ark::warn!("Failed to restore egui state: {}", err);
            }
        }
    }

    /// Almost the same as `available_rect()`, however, in *physical* pixels, not *logical* (i.e.
    /// `available_rect()` scaled by the DPI factor).
    ///
    /// How much space is still available after panels has been added.
    /// This is the "background" area, what egui doesn't cover with panels (but may cover with windows).
    /// This is also the area to which windows are constrained.
    pub fn viewport(&self) -> ark::render::Rectangle {
        let available_rect = self.ctx.available_rect();
        let pixels_per_point = self.ctx.input().pixels_per_point();
        ark::render::Rectangle {
            min_x: available_rect.min.x * pixels_per_point,
            min_y: available_rect.min.y * pixels_per_point,
            max_x: available_rect.max.x * pixels_per_point,
            max_y: available_rect.max.y * pixels_per_point,
        }
    }
}

fn from_egui_cursor(cursor: egui::CursorIcon) -> ark::applet::CursorShape {
    use ark::applet::CursorShape;
    match cursor {
        egui::CursorIcon::None => unreachable!("Should have been handled outside this function"),
        egui::CursorIcon::Default => CursorShape::Default,
        egui::CursorIcon::ContextMenu => CursorShape::ContextMenu,
        egui::CursorIcon::Help => CursorShape::Help,
        egui::CursorIcon::PointingHand => CursorShape::Hand,
        egui::CursorIcon::Progress => CursorShape::Progress,
        egui::CursorIcon::Wait => CursorShape::Wait,
        egui::CursorIcon::Cell => CursorShape::Cell,
        egui::CursorIcon::Crosshair => CursorShape::Crosshair,
        egui::CursorIcon::Text => CursorShape::Text,
        egui::CursorIcon::VerticalText => CursorShape::VerticalText,
        egui::CursorIcon::Alias => CursorShape::Alias,
        egui::CursorIcon::Copy => CursorShape::Copy,
        egui::CursorIcon::Move => CursorShape::Move,
        egui::CursorIcon::NoDrop => CursorShape::NoDrop,
        egui::CursorIcon::NotAllowed => CursorShape::NotAllowed,
        egui::CursorIcon::Grab => CursorShape::Grab,
        egui::CursorIcon::Grabbing => CursorShape::Grabbing,
        egui::CursorIcon::AllScroll => CursorShape::AllScroll,
        egui::CursorIcon::ResizeHorizontal => CursorShape::EWResize,
        egui::CursorIcon::ResizeNeSw => CursorShape::NESWResize,
        egui::CursorIcon::ResizeNwSe => CursorShape::NWSEResize,
        egui::CursorIcon::ResizeVertical => CursorShape::NSResize,
        egui::CursorIcon::ZoomIn => CursorShape::ZoomIn,
        egui::CursorIcon::ZoomOut => CursorShape::ZoomOut,
        egui::CursorIcon::ResizeEast => CursorShape::EResize,
        egui::CursorIcon::ResizeSouthEast => CursorShape::SEResize,
        egui::CursorIcon::ResizeSouth => CursorShape::SResize,
        egui::CursorIcon::ResizeSouthWest => CursorShape::SWResize,
        egui::CursorIcon::ResizeWest => CursorShape::WResize,
        egui::CursorIcon::ResizeNorthWest => CursorShape::NWResize,
        egui::CursorIcon::ResizeNorth => CursorShape::NResize,
        egui::CursorIcon::ResizeNorthEast => CursorShape::NEResize,
        egui::CursorIcon::ResizeColumn => CursorShape::RowResize,
        egui::CursorIcon::ResizeRow => CursorShape::ColResize,
    }
}

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

impl serde::Serialize for EguiArk {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        // (&*self.ctx.memory()).serialize(serializer) // ERR: `KeyMustBeString` :(

        self.persist().serialize(serializer)
    }
}

impl<'de> serde::Deserialize<'de> for EguiArk {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let egui_ark = Self::default();

        // *egui_ark.ctx.memory() = egui::Memory::deserialize(deserializer)?; // Can't do this due to Flexbuffers bug

        egui_ark.restore(serde_json::Value::deserialize(deserializer)?);

        Ok(egui_ark)
    }
}

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

#[derive(Default)]
struct EguiPainter {
    /// The font textures used by egui
    egui_textures: HashMap<egui::TextureId, render::Texture>,

    // Reuse buffers to avoid allocations:
    positions: Vec<Vec2>,
    colors: Vec<ColorRgba8>,
    uvs: Vec<Vec2>,
}

impl EguiPainter {
    pub fn upload_font_textures(
        &mut self,
        render: &render::Render,
        textures_delta: egui::TexturesDelta,
    ) {
        puffin::profile_function!();
        for (texture_id, image_delta) in textures_delta.set {
            if let Some(pos) = image_delta.pos {
                // This is an update. Ignore for now until the API is added.
                if let Some(texture) = self.egui_textures.get_mut(&texture_id) {
                    // TODO
                    let pixels = get_rgba_pixels(&image_delta.image);
                    texture.update_rectangle(
                        pos[0] as u32,
                        pos[1] as u32,
                        image_delta.image.width() as u32,
                        image_delta.image.height() as u32,
                        &pixels,
                    );
                }
            } else {
                self.egui_textures
                    .insert(texture_id, as_ark_texture(render, &image_delta));
            }
        }

        for free in textures_delta.free {
            self.egui_textures.remove(&free);
        }
    }

    pub fn paint(
        &mut self,
        render: &render::Render,
        egui_ctx: &egui::Context,
        meshes: &[egui::ClippedPrimitive],
    ) {
        puffin::profile_function!();
        let dpi_factor = egui_ctx.input().pixels_per_point();
        for egui::ClippedPrimitive {
            primitive,
            clip_rect,
        } in meshes
        {
            self.paint_mesh(render, dpi_factor, clip_rect, primitive);
        }
    }

    fn paint_mesh(
        &mut self,
        render: &render::Render,
        dpi_factor: f32,
        clip_rect: &egui::Rect,
        primitive: &egui::epaint::Primitive,
    ) {
        puffin::profile_function!();

        let mesh = match primitive {
            egui::epaint::Primitive::Mesh(mesh) => mesh,
            _ => return,
        };

        if !mesh.is_valid() {
            ark::error!("egui generated an invalid triangle mesh");
            return;
        }

        self.positions.clear();
        self.colors.clear();
        self.uvs.clear();
        for v in &mesh.vertices {
            self.positions
                .push(dpi_factor * Vec2::new(v.pos.x, v.pos.y));
            self.colors.push(ColorRgba8(v.color.to_array()));
            self.uvs.push(Vec2::new(v.uv.x, v.uv.y));
        }

        let clip_rect = render::Rectangle {
            min_x: dpi_factor * clip_rect.min.x,
            min_y: dpi_factor * clip_rect.min.y,
            max_x: dpi_factor * clip_rect.max.x,
            max_y: dpi_factor * clip_rect.max.y,
        };

        let texture_handle = if let Some(texture) = self.egui_textures.get(&mesh.texture_id) {
            texture.handle()
        } else {
            return;
        };

        let indices: &[u32] = &mesh.indices;
        let indices: &[[u32; 3]] = unsafe { transmute_slice(indices) };
        assert_eq!(mesh.indices.len(), 3 * indices.len());

        puffin::profile_scope!("draw_textured_triangles");
        render.draw_textured_triangles(
            &clip_rect,
            texture_handle,
            indices,
            &self.positions,
            &self.colors,
            &self.uvs,
        );
    }
}

/// Convert e.g. &[[u32; 3]] to three times as long slice of &[u32]
#[allow(clippy::integer_division)]
unsafe fn transmute_slice<Target: Copy, Source: Copy>(source: &[Source]) -> &[Target] {
    use std::mem::size_of;

    let target_len = source.len() * size_of::<Source>() / size_of::<Target>();

    assert_eq!(
        target_len * size_of::<Target>(),
        source.len() * size_of::<Source>(),
        "Source slice length is not an even multiple of the target"
    );
    unsafe { std::slice::from_raw_parts(source.as_ptr().cast::<Target>(), target_len) }
}

fn get_rgba_pixels(image: &egui::epaint::ImageData) -> Vec<u8> {
    let mut pixels = Vec::with_capacity(image.width() * image.height() * 4);
    match image {
        egui::epaint::ImageData::Font(font) => {
            for srgba in font.srgba_pixels(1.0) {
                pixels.push(srgba[0]);
                pixels.push(srgba[1]);
                pixels.push(srgba[2]);
                pixels.push(srgba[3]);
            }
        }
        _ => {
            pixels.shrink_to_fit();
        }
    }

    pixels
}

fn as_ark_texture(
    render: &render::Render,
    image_delta: &egui::epaint::ImageDelta,
) -> render::Texture {
    let pixels = get_rgba_pixels(&image_delta.image);

    puffin::profile_scope!("render::Texture::create");
    render
        .create_texture()
        .name("egui")
        .dimensions(image_delta.image.width(), image_delta.image.height())
        .format(TextureFormat::R8G8B8A8_UNORM)
        .data(&pixels)
        .build()
        .expect("Failed to create egui texture")
}

/// Update the input given to egui with input pulled from ark
fn update_input(
    egui_input: &mut egui::RawInput,
    applet: &ark::applet::Applet,
    input_mngr: &input::InputManager,
) {
    puffin::profile_function!();
    let window_state = if let Some(window_state) = applet.window_state() {
        window_state
    } else {
        return;
    };

    egui_input.screen_rect = Some(egui::Rect::from_min_size(
        Default::default(),
        egui::vec2(window_state.width, window_state.height),
    ));
    egui_input.pixels_per_point = Some(window_state.dpi_factor);
    egui_input.time = Some(applet.real_time_since_start());
    egui_input.events.clear();
    egui_input.modifiers = as_egui_modifiers(&input_mngr.state().modifiers);

    for (state, event) in input_mngr.events() {
        match event {
            input::Event::Key { key, pressed } => {
                if let Some(key) = as_egui_key(*key) {
                    egui_input.events.push(egui::Event::Key {
                        pressed: *pressed,
                        key,
                        modifiers: as_egui_modifiers(&state.modifiers),
                    });
                }
            }
            input::Event::Char(chr) => {
                if *chr != '\r' {
                    egui_input.events.push(egui::Event::Text(chr.to_string()));
                }
            }

            input::Event::PointerMove { pos, primary, .. } => {
                if *primary {
                    egui_input
                        .events
                        .push(egui::Event::PointerMoved(egui::pos2(pos.x, pos.y)));
                }
            }
            input::Event::PointerButton {
                pressed,
                button,
                pos,
                ..
            } => {
                egui_input.events.push(egui::Event::PointerButton {
                    pos: egui::pos2(pos.x, pos.y),
                    button: as_egui_button(button),
                    pressed: *pressed,
                    modifiers: as_egui_modifiers(&state.modifiers),
                });
            }
            input::Event::PointerDelta { .. } => {}
            input::Event::Scroll { delta, .. } => {
                egui_input
                    .events
                    .push(egui::Event::Scroll(egui::vec2(delta.x, delta.y)));
            }
            input::Event::Command(command) => {
                match command {
                    input::Command::Copy => egui_input.events.push(egui::Event::Copy),
                    input::Command::Cut => egui_input.events.push(egui::Event::Cut),
                    input::Command::Paste => {
                        if let Some(s) = applet.clipboard_string() {
                            egui_input.events.push(egui::Event::Text(s));
                        }
                    }
                    // egui checks for Cmd+Z itself
                    input::Command::Undo | input::Command::Redo | input::Command::Save |
                    // egui doesn't care
                    input::Command::New => {}
                }
            }
            input::Event::Axis { .. }
            | input::Event::GamepadButton { .. }
            | input::Event::RawMidi { .. } => {}
        }
    }
}

fn as_egui_key(code: input::Key) -> Option<egui::Key> {
    match code {
        input::Key::Down => Some(egui::Key::ArrowDown),
        input::Key::Left => Some(egui::Key::ArrowLeft),
        input::Key::Right => Some(egui::Key::ArrowRight),
        input::Key::Up => Some(egui::Key::ArrowUp),

        input::Key::Escape => Some(egui::Key::Escape),
        input::Key::Tab => Some(egui::Key::Tab),
        input::Key::Back => Some(egui::Key::Backspace),
        input::Key::Return => Some(egui::Key::Enter),
        input::Key::Space => Some(egui::Key::Space),

        input::Key::Insert => Some(egui::Key::Insert),
        input::Key::Delete => Some(egui::Key::Delete),
        input::Key::Home => Some(egui::Key::Home),
        input::Key::End => Some(egui::Key::End),
        input::Key::PageUp => Some(egui::Key::PageUp),
        input::Key::PageDown => Some(egui::Key::PageDown),

        input::Key::Key0 => Some(egui::Key::Num0),
        input::Key::Key1 => Some(egui::Key::Num1),
        input::Key::Key2 => Some(egui::Key::Num2),
        input::Key::Key3 => Some(egui::Key::Num3),
        input::Key::Key4 => Some(egui::Key::Num4),
        input::Key::Key5 => Some(egui::Key::Num5),
        input::Key::Key6 => Some(egui::Key::Num6),
        input::Key::Key7 => Some(egui::Key::Num7),
        input::Key::Key8 => Some(egui::Key::Num8),
        input::Key::Key9 => Some(egui::Key::Num9),

        input::Key::A => Some(egui::Key::A),
        input::Key::B => Some(egui::Key::B),
        input::Key::C => Some(egui::Key::C),
        input::Key::D => Some(egui::Key::D),
        input::Key::E => Some(egui::Key::E),
        input::Key::F => Some(egui::Key::F),
        input::Key::G => Some(egui::Key::G),
        input::Key::H => Some(egui::Key::H),
        input::Key::I => Some(egui::Key::I),
        input::Key::J => Some(egui::Key::J),
        input::Key::K => Some(egui::Key::K),
        input::Key::L => Some(egui::Key::L),
        input::Key::M => Some(egui::Key::M),
        input::Key::N => Some(egui::Key::N),
        input::Key::O => Some(egui::Key::O),
        input::Key::P => Some(egui::Key::P),
        input::Key::Q => Some(egui::Key::Q),
        input::Key::R => Some(egui::Key::R),
        input::Key::S => Some(egui::Key::S),
        input::Key::T => Some(egui::Key::T),
        input::Key::U => Some(egui::Key::U),
        input::Key::V => Some(egui::Key::V),
        input::Key::W => Some(egui::Key::W),
        input::Key::X => Some(egui::Key::X),
        input::Key::Y => Some(egui::Key::Y),
        input::Key::Z => Some(egui::Key::Z),

        _ => None,
    }
}

fn as_egui_modifiers(modifiers: &input::Modifiers) -> egui::Modifiers {
    egui::Modifiers {
        alt: modifiers.alt,
        ctrl: modifiers.ctrl,
        shift: modifiers.shift,
        mac_cmd: modifiers.cmd, // close enough
        command: modifiers.cmd,
    }
}

fn as_egui_button(button: &input::PointerButton) -> egui::PointerButton {
    match button {
        input::PointerButton::Primary => egui::PointerButton::Primary,
        input::PointerButton::Secondary => egui::PointerButton::Secondary,
        input::PointerButton::Middle => egui::PointerButton::Middle,
    }
}