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
//! egui SFML integration helpers
//!
//! Contains various types and functions that helps with integrating egui with SFML.

#![warn(missing_docs)]

use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::mem;

use egui::epaint::Primitive;
use egui::{
    Context, Event as EguiEv, FullOutput, ImageData, Modifiers, PointerButton, Pos2, RawInput,
    TextureId,
};
use sfml::graphics::blend_mode::Factor;
use sfml::graphics::{
    BlendMode, Color, PrimitiveType, RenderStates, RenderTarget, RenderWindow, Texture, Vertex,
};
use sfml::system::{Clock, Vector2};
use sfml::window::clipboard;
use sfml::{
    window::{mouse, Event, Key},
    SfBox,
};

pub use egui;
pub use sfml;

fn button_conv(button: mouse::Button) -> Option<PointerButton> {
    let but = match button {
        mouse::Button::Left => PointerButton::Primary,
        mouse::Button::Right => PointerButton::Secondary,
        mouse::Button::Middle => PointerButton::Middle,
        _ => return None,
    };
    Some(but)
}

fn key_conv(code: Key) -> Option<egui::Key> {
    use egui::Key as EKey;
    Some(match code {
        Key::Down => EKey::ArrowDown,
        Key::Left => EKey::ArrowLeft,
        Key::Right => EKey::ArrowRight,
        Key::Up => EKey::ArrowUp,
        Key::Escape => EKey::Escape,
        Key::Tab => EKey::Tab,
        Key::Backspace => EKey::Backspace,
        Key::Enter => EKey::Enter,
        Key::Space => EKey::Space,
        Key::Insert => EKey::Insert,
        Key::Delete => EKey::Delete,
        Key::Home => EKey::Home,
        Key::End => EKey::End,
        Key::PageUp => EKey::PageUp,
        Key::PageDown => EKey::PageDown,
        Key::Num0 => EKey::Num0,
        Key::Num1 => EKey::Num1,
        Key::Num2 => EKey::Num2,
        Key::Num3 => EKey::Num3,
        Key::Num4 => EKey::Num4,
        Key::Num5 => EKey::Num5,
        Key::Num6 => EKey::Num6,
        Key::Num7 => EKey::Num7,
        Key::Num8 => EKey::Num8,
        Key::Num9 => EKey::Num9,
        Key::A => EKey::A,
        Key::B => EKey::B,
        Key::C => EKey::C,
        Key::D => EKey::D,
        Key::E => EKey::E,
        Key::F => EKey::F,
        Key::G => EKey::G,
        Key::H => EKey::H,
        Key::I => EKey::I,
        Key::J => EKey::J,
        Key::K => EKey::K,
        Key::L => EKey::L,
        Key::M => EKey::M,
        Key::N => EKey::N,
        Key::O => EKey::O,
        Key::P => EKey::P,
        Key::Q => EKey::Q,
        Key::R => EKey::R,
        Key::S => EKey::S,
        Key::T => EKey::T,
        Key::U => EKey::U,
        Key::V => EKey::V,
        Key::W => EKey::W,
        Key::X => EKey::X,
        Key::Y => EKey::Y,
        Key::Z => EKey::Z,
        Key::F1 => EKey::F1,
        Key::F2 => EKey::F2,
        Key::F3 => EKey::F3,
        Key::F4 => EKey::F4,
        Key::F5 => EKey::F5,
        Key::F6 => EKey::F6,
        Key::F7 => EKey::F7,
        Key::F8 => EKey::F8,
        Key::F9 => EKey::F9,
        Key::F10 => EKey::F10,
        Key::F11 => EKey::F11,
        Key::F12 => EKey::F12,
        _ => return None,
    })
}

fn modifier(alt: bool, ctrl: bool, shift: bool) -> egui::Modifiers {
    egui::Modifiers {
        alt,
        ctrl,
        shift,
        command: ctrl,
        mac_cmd: false,
    }
}

/// Converts an SFML event to an egui event and adds it to the `RawInput`.
fn handle_event(raw_input: &mut egui::RawInput, event: &sfml::window::Event) {
    match *event {
        Event::KeyPressed {
            code,
            alt,
            ctrl,
            shift,
            system: _,
        } => {
            if ctrl {
                match code {
                    Key::V => raw_input
                        .events
                        .push(egui::Event::Text(clipboard::get_string())),
                    Key::C => raw_input.events.push(egui::Event::Copy),
                    Key::X => raw_input.events.push(egui::Event::Cut),
                    _ => {}
                }
            }
            if let Some(key) = key_conv(code) {
                raw_input.events.push(egui::Event::Key {
                    key,
                    modifiers: modifier(alt, ctrl, shift),
                    pressed: true,
                    repeat: false,
                });
            }
        }
        Event::KeyReleased {
            code,
            alt,
            ctrl,
            shift,
            system: _,
        } => {
            if let Some(key) = key_conv(code) {
                raw_input.events.push(egui::Event::Key {
                    key,
                    modifiers: modifier(alt, ctrl, shift),
                    pressed: false,
                    repeat: false,
                });
            }
        }
        Event::MouseMoved { x, y } => {
            raw_input
                .events
                .push(EguiEv::PointerMoved(Pos2::new(x as f32, y as f32)));
        }
        Event::MouseButtonPressed { x, y, button } => {
            if let Some(button) = button_conv(button) {
                raw_input.events.push(EguiEv::PointerButton {
                    pos: Pos2::new(x as f32, y as f32),
                    button,
                    pressed: true,
                    modifiers: Modifiers::default(),
                });
            }
        }
        Event::MouseButtonReleased { x, y, button } => {
            if let Some(button) = button_conv(button) {
                raw_input.events.push(EguiEv::PointerButton {
                    pos: Pos2::new(x as f32, y as f32),
                    button,
                    pressed: false,
                    modifiers: Modifiers::default(),
                });
            }
        }
        Event::TextEntered { unicode } => {
            if !unicode.is_control() {
                raw_input.events.push(EguiEv::Text(unicode.to_string()));
            }
        }
        Event::MouseWheelScrolled { delta, .. } => {
            if sfml::window::Key::LControl.is_pressed() {
                raw_input
                    .events
                    .push(EguiEv::Zoom(if delta > 0.0 { 1.1 } else { 0.9 }));
            }
        }
        Event::Resized { width, height } => {
            raw_input.screen_rect = Some(raw_input_screen_rect(width, height));
        }
        _ => {}
    }
}

/// Creates a `RawInput` that fits the window.
fn make_raw_input(window: &RenderWindow) -> RawInput {
    let Vector2 { x: w, y: h } = window.size();
    RawInput {
        screen_rect: Some(raw_input_screen_rect(w, h)),
        ..Default::default()
    }
}

fn raw_input_screen_rect(w: u32, h: u32) -> egui::Rect {
    egui::Rect {
        min: Pos2::new(0., 0.),
        max: Pos2::new(w as f32, h as f32),
    }
}

/// A source for egui user textures.
///
/// You can create a struct that contains all the necessary information to get a user texture from
/// an id, and implement this trait for it.
pub trait UserTexSource {
    /// Get the texture that corresponds to `id`.
    ///
    /// Returns (width, height, texture).
    fn get_texture(&mut self, id: u64) -> (f32, f32, &Texture);
}

/// A dummy texture source in case you don't care about providing user textures
struct DummyTexSource {
    tex: SfBox<Texture>,
}

impl Default for DummyTexSource {
    fn default() -> Self {
        Self {
            tex: Texture::new().unwrap(),
        }
    }
}

impl UserTexSource for DummyTexSource {
    fn get_texture(&mut self, _id: u64) -> (f32, f32, &Texture) {
        (0., 0., &self.tex)
    }
}

/// `Egui` integration for SFML.
pub struct SfEgui {
    clock: SfBox<Clock>,
    ctx: Context,
    raw_input: RawInput,
    egui_result: FullOutput,
    textures: HashMap<TextureId, SfBox<Texture>>,
}

impl SfEgui {
    /// Create a new `SfEgui`.
    ///
    /// The size of the egui ui will be the same as `window`'s size.
    pub fn new(window: &RenderWindow) -> Self {
        Self {
            clock: sfml::system::Clock::start(),
            raw_input: make_raw_input(window),
            ctx: Context::default(),
            egui_result: Default::default(),
            textures: HashMap::default(),
        }
    }
    /// Convert an SFML event into an egui event and add it for later use by egui.
    ///
    /// Call this in an event polling loop for each event.
    pub fn add_event(&mut self, event: &Event) {
        handle_event(&mut self.raw_input, event);
    }
    /// Does an egui frame with a user supplied ui function.
    ///
    /// The `f` parameter is a user supplied ui function that does the desired ui
    pub fn do_frame(&mut self, f: impl FnOnce(&Context)) -> Result<(), DoFrameError> {
        self.raw_input.time = Some(self.clock.elapsed_time().as_seconds() as f64);
        // Update modifiers every frame, otherwise querying them (input.modifiers.*) doesn't seem
        // up-to-date
        self.raw_input.modifiers.alt = Key::LAlt.is_pressed() || Key::RAlt.is_pressed();
        self.raw_input.modifiers.ctrl = Key::LControl.is_pressed() || Key::RControl.is_pressed();
        self.raw_input.modifiers.shift = Key::LShift.is_pressed() || Key::RShift.is_pressed();
        self.egui_result = self.ctx.run(self.raw_input.take(), f);
        let clip_str = &self.egui_result.platform_output.copied_text;
        if !clip_str.is_empty() {
            clipboard::set_string(clip_str);
        }
        for (id, delta) in &self.egui_result.textures_delta.set {
            let [w, h] = delta.image.size();
            let tex = match self.textures.entry(*id) {
                Entry::Occupied(en) => en.into_mut(),
                Entry::Vacant(en) => {
                    let mut tex = Texture::new().unwrap();
                    if !tex.create(w as u32, h as u32) {
                        return Err(DoFrameError::TextureCreateError(TextureCreateError {
                            width: w,
                            height: h,
                        }));
                    }
                    en.insert(tex)
                }
            };
            update_tex_from_delta(tex, delta)?;
        }
        Ok(())
    }
    /// Draw the ui to a `RenderWindow`.
    ///
    /// Takes an optional [`UserTexSource`] to act as a user texture source.
    pub fn draw(
        &mut self,
        window: &mut RenderWindow,
        user_tex_src: Option<&mut dyn UserTexSource>,
    ) {
        draw(
            window,
            &self.ctx,
            mem::take(&mut self.egui_result.shapes),
            user_tex_src.unwrap_or(&mut DummyTexSource::default()),
            &self.textures,
        )
    }
    /// Returns a handle to the egui context
    ///
    /// `CtxRef` can be cloned, but beware that it will be outdated after a call to
    /// [`do_frame`](Self::do_frame)
    pub fn context(&self) -> &Context {
        &self.ctx
    }
}

#[derive(Debug)]
/// Error when failing to create a texture
pub struct TextureCreateError {
    /// The width of the requested texture
    pub width: usize,
    /// The height of the requested texture
    pub height: usize,
}

/// Error that can happen when doing an egui frame
#[non_exhaustive]
#[derive(Debug)]
pub enum DoFrameError {
    /// Failed to create a texture
    TextureCreateError(TextureCreateError),
}

impl From<TextureCreateError> for DoFrameError {
    fn from(src: TextureCreateError) -> Self {
        Self::TextureCreateError(src)
    }
}

fn update_tex_from_delta(
    tex: &mut SfBox<Texture>,
    delta: &egui::epaint::ImageDelta,
) -> Result<(), TextureCreateError> {
    let mut x = 0;
    let mut y = 0;
    let [w, h] = delta.image.size();
    if let Some([xx, yy]) = delta.pos {
        x = xx as u32;
        y = yy as u32;
    }
    match &delta.image {
        ImageData::Color(color) => {
            let srgba: Vec<u8> = color.pixels.iter().flat_map(|c32| c32.to_array()).collect();
            unsafe {
                tex.update_from_pixels(&srgba, w as u32, h as u32, x, y);
            }
        }
        ImageData::Font(font_image) => {
            let srgba: Vec<u8> = font_image
                .srgba_pixels(None)
                .flat_map(|c32| c32.to_array())
                .collect();
            if w > tex.size().x as usize || h > tex.size().y as usize {
                // Resize texture
                let ok = tex.create(w as u32, h as u32);
                if !ok {
                    return Err(TextureCreateError {
                        width: w,
                        height: h,
                    });
                }
            }
            unsafe {
                tex.update_from_pixels(&srgba, w as u32, h as u32, x, y);
            }
        }
    }
    Ok(())
}

fn draw(
    window: &mut RenderWindow,
    egui_ctx: &egui::Context,
    shapes: Vec<egui::epaint::ClippedShape>,
    user_tex_source: &mut dyn UserTexSource,
    textures: &HashMap<TextureId, SfBox<Texture>>,
) {
    window.set_active(true);
    unsafe {
        glu_sys::glEnable(glu_sys::GL_SCISSOR_TEST);
    }
    let mut vertices = Vec::new();
    for egui::ClippedPrimitive {
        clip_rect,
        primitive,
    } in egui_ctx.tessellate(shapes)
    {
        let mesh = match primitive {
            Primitive::Mesh(mesh) => mesh,
            _ => continue,
        };
        let (tw, th, tex) = match mesh.texture_id {
            TextureId::Managed(id) => {
                let tex = &*textures[&TextureId::Managed(id)];
                let (egui_tex_w, egui_tex_h) = (tex.size().x as f32, tex.size().y as f32);
                (egui_tex_w, egui_tex_h, tex)
            }
            TextureId::User(id) => user_tex_source.get_texture(id),
        };
        for idx in mesh.indices {
            let v = mesh.vertices[idx as usize];
            let sf_v = Vertex::new(
                (v.pos.x, v.pos.y).into(),
                Color::rgba(v.color.r(), v.color.g(), v.color.b(), v.color.a()),
                (v.uv.x * tw, v.uv.y * th).into(),
            );
            vertices.push(sf_v);
        }
        let mut rs = RenderStates::default();
        rs.blend_mode = BlendMode {
            color_src_factor: Factor::One,
            color_dst_factor: Factor::OneMinusSrcAlpha,
            alpha_src_factor: Factor::OneMinusDstAlpha,
            alpha_dst_factor: Factor::One,
            ..Default::default()
        };
        rs.set_texture(Some(tex));
        let pixels_per_point = 1.;
        let win_size = window.size();
        let width_in_pixels = win_size.x;
        let height_in_pixels = win_size.y;
        // Code copied from egui_glium (https://github.com/emilk/egui)
        // Transform clip rect to physical pixels:
        let clip_min_x = pixels_per_point * clip_rect.min.x;
        let clip_min_y = pixels_per_point * clip_rect.min.y;
        let clip_max_x = pixels_per_point * clip_rect.max.x;
        let clip_max_y = pixels_per_point * clip_rect.max.y;

        // Make sure clip rect can fit within a `u32`:
        let clip_min_x = clip_min_x.clamp(0.0, width_in_pixels as f32);
        let clip_min_y = clip_min_y.clamp(0.0, height_in_pixels as f32);
        let clip_max_x = clip_max_x.clamp(clip_min_x, width_in_pixels as f32);
        let clip_max_y = clip_max_y.clamp(clip_min_y, height_in_pixels as f32);

        let clip_min_x = clip_min_x.round() as u32;
        let clip_min_y = clip_min_y.round() as u32;
        let clip_max_x = clip_max_x.round() as u32;
        let clip_max_y = clip_max_y.round() as u32;
        unsafe {
            glu_sys::glScissor(
                clip_min_x as _,
                (height_in_pixels - clip_max_y) as _,
                (clip_max_x - clip_min_x) as _,
                (clip_max_y - clip_min_y) as _,
            );
        }
        window.draw_primitives(&vertices, PrimitiveType::TRIANGLES, &rs);
        vertices.clear();
    }
    unsafe {
        glu_sys::glDisable(glu_sys::GL_SCISSOR_TEST);
    }
    window.set_active(false);
}