raylib_imgui 1.0.0

A Raylib based backend for imgui
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use ffi;
use imgui::{
  self, internal::RawWrapper, BackendFlags, ClipboardBackend, ConfigFlags,
  Context, DrawCmd, DrawCmdParams, DrawVert, FontConfig, Key, Ui,
};
use raylib::ffi::{GetClipboardText, SetClipboardText};
use raylib::prelude::*;
use std::ffi::{c_char, CStr, CString};
use std::fs::{self, File};
use std::io::Write;

pub struct RaylibGui {
  pub context: Context,

  current_mouse_cursor: Option<imgui::MouseCursor>,

  last_frame_focused: bool,
  last_control_pressed: bool,
  last_shift_pressed: bool,
  last_alt_pressed: bool,
  last_super_pressed: bool,

  keys: [(KeyboardKey, Key); 105],
  gamepad_map: [(GamepadButton, Key); 16],
  gamepad_axis: [(GamepadAxis, Key, Key); 4],
}

pub enum Style {
  Dark,
  Light,
  Classic,
}

struct RaylibClipboardBackend;

impl ClipboardBackend for RaylibClipboardBackend {
  fn get(&mut self) -> Option<String> {
    unsafe {
      let c = GetClipboardText();
      let c = CStr::from_ptr(c as *mut c_char);
      c.to_str().map(|s| s.to_owned()).ok()
    }
  }

  fn set(&mut self, value: &str) {
    let s = CString::new(value);
    if let Ok(s) = s {
      unsafe {
        SetClipboardText(s.as_ptr());
      }
    }
  }
}

impl Drop for RaylibGui {
  fn drop(&mut self) {
    unsafe {
      let fonts = self.context.fonts();

      if fonts.tex_id != 0.into() {
        ffi::rlUnloadTexture(fonts.tex_id.id() as _);
        fonts.tex_id = 0.into();
      }

      save_ini_settings(&mut self.context);
    }
  }
}

pub struct Gui<'a> {
  gui: &'a mut RaylibGui,
}

impl<'a> Gui<'a> {
  pub fn begin(&mut self) -> &mut Ui {
    self.gui.context.new_frame()
  }
}

impl Drop for Gui<'_> {
  fn drop(&mut self) {
    self.gui.end();
  }
}

impl<T> RaylibDrawImGui for T where T: RaylibDraw {}
pub trait RaylibDrawImGui: RaylibDraw {
  fn prepare_imgui<'a>(&self, gui: &'a mut RaylibGui) -> Gui<'a> {
    Gui { gui }
  }
}

impl RaylibGui {
  pub fn new(rl: &mut RaylibHandle, _thread: &RaylibThread) -> RaylibGui {
    let mut ctx = Context::create();
    ctx.set_platform_name(Some(String::from("imgui_raylib_platform")));
    ctx.set_renderer_name(Some(String::from("imgui_raylib_renderer")));
    ctx.style_mut().use_dark_colors();
    ctx.set_clipboard_backend(RaylibClipboardBackend);
    ctx.fonts().add_font(&[imgui::FontSource::DefaultFontData {
      config: Some(FontConfig { ..Default::default() }),
    }]);

    load_ini_settings(&mut ctx);

    let io = ctx.io_mut();
    io.backend_flags.insert(BackendFlags::HAS_GAMEPAD);
    io.backend_flags.insert(BackendFlags::HAS_MOUSE_CURSORS);
    io.backend_flags.insert(BackendFlags::HAS_SET_MOUSE_POS);
    io.mouse_pos = [0.0, 0.0];

    let mut gui = RaylibGui {
      context: ctx,
      current_mouse_cursor: None,
      last_frame_focused: rl.is_window_focused(),
      last_control_pressed: false,
      last_shift_pressed: false,
      last_alt_pressed: false,
      last_super_pressed: false,
      keys: [
        // build up a map of raylib keys to ImGuiKeys
        (KeyboardKey::KEY_APOSTROPHE, Key::Apostrophe),
        (KeyboardKey::KEY_COMMA, Key::Comma),
        (KeyboardKey::KEY_MINUS, Key::Minus),
        (KeyboardKey::KEY_PERIOD, Key::Period),
        (KeyboardKey::KEY_SLASH, Key::Slash),
        (KeyboardKey::KEY_ZERO, Key::Keypad0),
        (KeyboardKey::KEY_ONE, Key::Keypad1),
        (KeyboardKey::KEY_TWO, Key::Keypad2),
        (KeyboardKey::KEY_THREE, Key::Keypad3),
        (KeyboardKey::KEY_FOUR, Key::Keypad4),
        (KeyboardKey::KEY_FIVE, Key::Keypad5),
        (KeyboardKey::KEY_SIX, Key::Keypad6),
        (KeyboardKey::KEY_SEVEN, Key::Keypad7),
        (KeyboardKey::KEY_EIGHT, Key::Keypad8),
        (KeyboardKey::KEY_NINE, Key::Keypad9),
        (KeyboardKey::KEY_SEMICOLON, Key::Semicolon),
        (KeyboardKey::KEY_EQUAL, Key::Equal),
        (KeyboardKey::KEY_A, Key::A),
        (KeyboardKey::KEY_B, Key::B),
        (KeyboardKey::KEY_C, Key::C),
        (KeyboardKey::KEY_D, Key::D),
        (KeyboardKey::KEY_E, Key::E),
        (KeyboardKey::KEY_F, Key::F),
        (KeyboardKey::KEY_G, Key::G),
        (KeyboardKey::KEY_H, Key::H),
        (KeyboardKey::KEY_I, Key::I),
        (KeyboardKey::KEY_J, Key::J),
        (KeyboardKey::KEY_K, Key::K),
        (KeyboardKey::KEY_L, Key::L),
        (KeyboardKey::KEY_M, Key::M),
        (KeyboardKey::KEY_N, Key::N),
        (KeyboardKey::KEY_O, Key::O),
        (KeyboardKey::KEY_P, Key::P),
        (KeyboardKey::KEY_Q, Key::Q),
        (KeyboardKey::KEY_R, Key::R),
        (KeyboardKey::KEY_S, Key::S),
        (KeyboardKey::KEY_T, Key::T),
        (KeyboardKey::KEY_U, Key::U),
        (KeyboardKey::KEY_V, Key::V),
        (KeyboardKey::KEY_W, Key::W),
        (KeyboardKey::KEY_X, Key::X),
        (KeyboardKey::KEY_Y, Key::Y),
        (KeyboardKey::KEY_Z, Key::Z),
        (KeyboardKey::KEY_SPACE, Key::Space),
        (KeyboardKey::KEY_ESCAPE, Key::Escape),
        (KeyboardKey::KEY_ENTER, Key::Enter),
        (KeyboardKey::KEY_TAB, Key::Tab),
        (KeyboardKey::KEY_BACKSPACE, Key::Backspace),
        (KeyboardKey::KEY_INSERT, Key::Insert),
        (KeyboardKey::KEY_DELETE, Key::Delete),
        (KeyboardKey::KEY_RIGHT, Key::RightArrow),
        (KeyboardKey::KEY_LEFT, Key::LeftArrow),
        (KeyboardKey::KEY_DOWN, Key::DownArrow),
        (KeyboardKey::KEY_UP, Key::UpArrow),
        (KeyboardKey::KEY_PAGE_UP, Key::PageUp),
        (KeyboardKey::KEY_PAGE_DOWN, Key::PageDown),
        (KeyboardKey::KEY_HOME, Key::Home),
        (KeyboardKey::KEY_END, Key::End),
        (KeyboardKey::KEY_CAPS_LOCK, Key::CapsLock),
        (KeyboardKey::KEY_SCROLL_LOCK, Key::ScrollLock),
        (KeyboardKey::KEY_NUM_LOCK, Key::NumLock),
        (KeyboardKey::KEY_PRINT_SCREEN, Key::PrintScreen),
        (KeyboardKey::KEY_PAUSE, Key::Pause),
        (KeyboardKey::KEY_F1, Key::F1),
        (KeyboardKey::KEY_F2, Key::F2),
        (KeyboardKey::KEY_F3, Key::F3),
        (KeyboardKey::KEY_F4, Key::F4),
        (KeyboardKey::KEY_F5, Key::F5),
        (KeyboardKey::KEY_F6, Key::F6),
        (KeyboardKey::KEY_F7, Key::F7),
        (KeyboardKey::KEY_F8, Key::F8),
        (KeyboardKey::KEY_F9, Key::F9),
        (KeyboardKey::KEY_F10, Key::F10),
        (KeyboardKey::KEY_F11, Key::F11),
        (KeyboardKey::KEY_F12, Key::F12),
        (KeyboardKey::KEY_LEFT_SHIFT, Key::LeftShift),
        (KeyboardKey::KEY_LEFT_CONTROL, Key::LeftCtrl),
        (KeyboardKey::KEY_LEFT_ALT, Key::LeftAlt),
        (KeyboardKey::KEY_LEFT_SUPER, Key::LeftSuper),
        (KeyboardKey::KEY_RIGHT_SHIFT, Key::RightShift),
        (KeyboardKey::KEY_RIGHT_CONTROL, Key::RightCtrl),
        (KeyboardKey::KEY_RIGHT_ALT, Key::RightAlt),
        (KeyboardKey::KEY_RIGHT_SUPER, Key::RightSuper),
        (KeyboardKey::KEY_KB_MENU, Key::Menu),
        (KeyboardKey::KEY_LEFT_BRACKET, Key::LeftBracket),
        (KeyboardKey::KEY_BACKSLASH, Key::Backslash),
        (KeyboardKey::KEY_RIGHT_BRACKET, Key::RightBracket),
        (KeyboardKey::KEY_GRAVE, Key::GraveAccent),
        (KeyboardKey::KEY_KP_0, Key::Keypad0),
        (KeyboardKey::KEY_KP_1, Key::Keypad1),
        (KeyboardKey::KEY_KP_2, Key::Keypad2),
        (KeyboardKey::KEY_KP_3, Key::Keypad3),
        (KeyboardKey::KEY_KP_4, Key::Keypad4),
        (KeyboardKey::KEY_KP_5, Key::Keypad5),
        (KeyboardKey::KEY_KP_6, Key::Keypad6),
        (KeyboardKey::KEY_KP_7, Key::Keypad7),
        (KeyboardKey::KEY_KP_8, Key::Keypad8),
        (KeyboardKey::KEY_KP_9, Key::Keypad9),
        (KeyboardKey::KEY_KP_DECIMAL, Key::KeypadDecimal),
        (KeyboardKey::KEY_KP_DIVIDE, Key::KeypadDivide),
        (KeyboardKey::KEY_KP_MULTIPLY, Key::KeypadMultiply),
        (KeyboardKey::KEY_KP_SUBTRACT, Key::KeypadSubtract),
        (KeyboardKey::KEY_KP_ADD, Key::KeypadAdd),
        (KeyboardKey::KEY_KP_ENTER, Key::KeypadEnter),
        (KeyboardKey::KEY_KP_EQUAL, Key::KeypadEqual),
      ],
      gamepad_map: [
        (GamepadButton::GAMEPAD_BUTTON_LEFT_FACE_UP, Key::GamepadDpadUp),
        (GamepadButton::GAMEPAD_BUTTON_LEFT_FACE_RIGHT, Key::GamepadDpadRight),
        (GamepadButton::GAMEPAD_BUTTON_LEFT_FACE_DOWN, Key::GamepadDpadDown),
        (GamepadButton::GAMEPAD_BUTTON_LEFT_FACE_LEFT, Key::GamepadDpadLeft),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_FACE_UP, Key::GamepadFaceUp),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_FACE_RIGHT, Key::GamepadFaceLeft),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_FACE_DOWN, Key::GamepadFaceDown),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_FACE_LEFT, Key::GamepadFaceRight),
        (GamepadButton::GAMEPAD_BUTTON_LEFT_TRIGGER_1, Key::GamepadL1),
        (GamepadButton::GAMEPAD_BUTTON_LEFT_TRIGGER_2, Key::GamepadL2),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_TRIGGER_1, Key::GamepadR1),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_TRIGGER_2, Key::GamepadR2),
        (GamepadButton::GAMEPAD_BUTTON_LEFT_THUMB, Key::GamepadL3),
        (GamepadButton::GAMEPAD_BUTTON_RIGHT_THUMB, Key::GamepadR3),
        (GamepadButton::GAMEPAD_BUTTON_MIDDLE_LEFT, Key::GamepadStart),
        (GamepadButton::GAMEPAD_BUTTON_MIDDLE_RIGHT, Key::GamepadBack),
      ],
      gamepad_axis: [
        (
          GamepadAxis::GAMEPAD_AXIS_LEFT_X,
          Key::GamepadLStickLeft,
          Key::GamepadLStickRight,
        ),
        (
          GamepadAxis::GAMEPAD_AXIS_LEFT_Y,
          Key::GamepadLStickUp,
          Key::GamepadLStickDown,
        ),
        (
          GamepadAxis::GAMEPAD_AXIS_RIGHT_X,
          Key::GamepadRStickLeft,
          Key::GamepadRStickRight,
        ),
        (
          GamepadAxis::GAMEPAD_AXIS_RIGHT_Y,
          Key::GamepadRStickUp,
          Key::GamepadRStickDown,
        ),
      ],
    };

    gui.reload_fonts();
    gui
  }

  pub fn set_style(&mut self, style: Style) {
    match style {
      Style::Dark => {
        self.context.style_mut().use_dark_colors();
      },
      Style::Light => {
        self.context.style_mut().use_light_colors();
      },
      Style::Classic => {
        self.context.style_mut().use_classic_colors();
      },
    }
  }

  pub fn update(&mut self, rl: &mut RaylibHandle) {
    let delta_time = rl.get_frame_time();
    self.update_delta(rl, delta_time);
  }

  pub fn update_delta(&mut self, rl: &mut RaylibHandle, delta_time: f32) {
    self.handle_events(rl);
    self.prepare_frame(rl, delta_time);
  }

  pub fn begin(&mut self, rl: &mut RaylibHandle) -> &mut Ui {
    let delta_time = rl.get_frame_time();
    self.begin_delta(rl, delta_time)
  }

  pub fn begin_delta(
    &mut self,
    rl: &mut RaylibHandle,
    delta_time: f32,
  ) -> &mut Ui {
    self.handle_events(rl);
    self.prepare_frame(rl, delta_time);
    self.new_frame()
  }

  pub fn end(&mut self) {
    Renderer::render(&mut self.context);
  }

  pub fn reload_fonts(&mut self) {
    let fonts = self.context.fonts();
    let texture = fonts.build_rgba32_texture();

    unsafe {
      let data = texture.data.as_ptr() as *mut std::ffi::c_void;
      let [width, height] = [texture.width as i32, texture.height as i32];
      if fonts.tex_id == 0.into() {
        let id = ffi::rlLoadTexture(
          data,
          width,
          height,
          ffi::rlPixelFormat::RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 as i32,
          1,
        ) as u32;
        fonts.tex_id = (id as usize).into();
      } else {
        ffi::rlUpdateTexture(
          fonts.tex_id.id() as _,
          0,
          0,
          width,
          height,
          ffi::rlPixelFormat::RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 as i32,
          data,
        );
      }
    }
  }

  pub fn new_frame(&mut self) -> &mut Ui {
    self.context.new_frame()
  }

  pub fn prepare_frame(&mut self, rl: &mut RaylibHandle, delta_time: f32) {
    let imgui_cursor = self.context.mouse_cursor();
    let io = self.context.io_mut();

    if rl.is_window_fullscreen() {
      let monitor = window::get_current_monitor();
      io.display_size = [
        window::get_monitor_width(monitor) as f32,
        window::get_monitor_height(monitor) as f32,
      ];
    } else {
      io.display_size =
        [rl.get_screen_width() as f32, rl.get_screen_height() as f32];
    }

    if rl.get_window_state().window_highdpi() {
      let resolution_scale = rl.get_window_scale_dpi();
      io.display_framebuffer_scale = [resolution_scale.x, resolution_scale.y];
    } else {
      io.display_framebuffer_scale = [1.0, 1.0];
    }

    io.delta_time = delta_time;

    if io.want_set_mouse_pos {
      rl.set_mouse_position(Vector2::new(io.mouse_pos[0], io.mouse_pos[1]));
    } else {
      io.add_mouse_pos_event([
        rl.get_mouse_x() as f32,
        rl.get_mouse_y() as f32,
      ]);
    }

    let mut set_mouse_event =
      |ray_mouse: MouseButton, imgui_mouse: imgui::MouseButton| {
        if rl.is_mouse_button_pressed(ray_mouse) {
          io.add_mouse_button_event(imgui_mouse, true);
        } else if rl.is_mouse_button_released(ray_mouse) {
          io.add_mouse_button_event(imgui_mouse, false);
        }
      };

    set_mouse_event(MouseButton::MOUSE_BUTTON_LEFT, imgui::MouseButton::Left);
    set_mouse_event(MouseButton::MOUSE_BUTTON_RIGHT, imgui::MouseButton::Right);
    set_mouse_event(
      MouseButton::MOUSE_BUTTON_MIDDLE,
      imgui::MouseButton::Middle,
    );
    set_mouse_event(
      MouseButton::MOUSE_BUTTON_FORWARD,
      imgui::MouseButton::Extra1,
    );
    set_mouse_event(MouseButton::MOUSE_BUTTON_BACK, imgui::MouseButton::Extra2);

    let mouse_wheel = rl.get_mouse_wheel_move_v();
    io.add_mouse_wheel_event([mouse_wheel.x, mouse_wheel.y]);

    if io.backend_flags.intersects(BackendFlags::HAS_MOUSE_CURSORS) {
      if !io.config_flags.intersects(ConfigFlags::NO_MOUSE_CURSOR_CHANGE) {
        if imgui_cursor != self.current_mouse_cursor || io.mouse_draw_cursor {
          self.current_mouse_cursor = imgui_cursor;
          if io.mouse_draw_cursor || imgui_cursor == None {
            rl.hide_cursor();
          } else if let Some(cursor) = imgui_cursor {
            rl.show_cursor();
            rl.set_mouse_cursor(to_rl_cursor(cursor));
          }
        }
      }
    }
  }

  pub fn handle_events(&mut self, rl: &mut RaylibHandle) {
    let io = self.context.io_mut();

    //let focused = !rl.is_window_focused();
    //if focused != self.last_frame_focused {
    //  io.app_focus_lost = focused; // TODO: missing `add_focus_event()` function?
    //}
    //self.last_frame_focused = focused;

    // TODO: (int keyId = KEY_NULL; keyId < KeyboardKey::KEY_KP_EQUAL; keyId++)
    // get the pressed keys, just walk the keys so we don
    for (rl_key, imgui_key) in self.keys {
      if rl.is_key_pressed(rl_key) {
        io.add_key_event(imgui_key, true);
      }
    }

    // look for any keys that were down last frame and see if they were down and are released
    for (rl_key, imgui_key) in self.keys {
      if rl.is_key_released(rl_key) {
        io.add_key_event(imgui_key, false);
      }
    }

    let mut update_mod = |key: Key, current_state: &mut bool, state: bool| {
      if *current_state != state {
        io.add_key_event(key, state);
      }
      *current_state = state;
    };

    // handle the modifyer key events so that shortcuts work
    update_mod(
      Key::ModCtrl,
      &mut self.last_control_pressed,
      rl.is_key_down(KeyboardKey::KEY_RIGHT_CONTROL)
        || rl.is_key_down(KeyboardKey::KEY_LEFT_CONTROL),
    );
    update_mod(
      Key::ModShift,
      &mut self.last_shift_pressed,
      rl.is_key_down(KeyboardKey::KEY_RIGHT_SHIFT)
        || rl.is_key_down(KeyboardKey::KEY_LEFT_SHIFT),
    );
    update_mod(
      Key::ModAlt,
      &mut self.last_alt_pressed,
      rl.is_key_down(KeyboardKey::KEY_RIGHT_ALT)
        || rl.is_key_down(KeyboardKey::KEY_LEFT_ALT),
    );
    update_mod(
      Key::ModSuper,
      &mut self.last_super_pressed,
      rl.is_key_down(KeyboardKey::KEY_RIGHT_SUPER)
        || rl.is_key_down(KeyboardKey::KEY_LEFT_SUPER),
    );

    if io.want_capture_keyboard {
      // add the text input in order
      while let Some(pressed) = rl.get_char_pressed() {
        io.add_input_character(pressed);
      }
    }

    if io.config_flags.intersects(ConfigFlags::NAV_ENABLE_GAMEPAD)
      && rl.is_gamepad_available(0)
    {
      for (btn, key) in self.gamepad_map {
        if rl.is_gamepad_button_pressed(0, btn) {
          io.add_key_event(key, true);
        } else if rl.is_gamepad_button_released(0, btn) {
          io.add_key_event(key, false);
        }
      }

      for (axis, pos_key, neg_key) in self.gamepad_axis {
        let dead_zone = 0.20;
        let axis_value = rl.get_gamepad_axis_movement(0, axis);

        io.add_key_analog_event(
          neg_key,
          axis_value < -dead_zone,
          if axis_value < -dead_zone { -axis_value } else { 0.0 },
        );
        io.add_key_analog_event(
          pos_key,
          axis_value > dead_zone,
          if axis_value > dead_zone { axis_value } else { 0.0 },
        );
      }
    }
  }

  pub fn render(&mut self) {
    self.end();
  }
}

fn save_ini_settings(ctx: &mut Context) {
  if let Some(ini_path) = ctx.ini_filename() {
    let mut settings = String::new();
    ctx.save_ini_settings(&mut settings);
    let mut file = File::create(&ini_path).unwrap();
    file.write_all(&settings.as_bytes()).unwrap();
  }
}

fn load_ini_settings(ctx: &mut Context) {
  if let Some(ini_path) = ctx.ini_filename() {
    if let Ok(contents) = fs::read_to_string(ini_path) {
      ctx.load_ini_settings(&contents.to_string());
    }
  }
}

fn to_rl_cursor(cursor: imgui::MouseCursor) -> MouseCursor {
  match cursor {
    imgui::MouseCursor::Arrow => MouseCursor::MOUSE_CURSOR_ARROW,
    imgui::MouseCursor::TextInput => MouseCursor::MOUSE_CURSOR_IBEAM,
    imgui::MouseCursor::ResizeAll => MouseCursor::MOUSE_CURSOR_RESIZE_ALL,
    imgui::MouseCursor::ResizeNS => MouseCursor::MOUSE_CURSOR_RESIZE_NS,
    imgui::MouseCursor::ResizeEW => MouseCursor::MOUSE_CURSOR_RESIZE_EW,
    imgui::MouseCursor::ResizeNESW => MouseCursor::MOUSE_CURSOR_RESIZE_NESW,
    imgui::MouseCursor::ResizeNWSE => MouseCursor::MOUSE_CURSOR_RESIZE_NWSE,
    imgui::MouseCursor::Hand => MouseCursor::MOUSE_CURSOR_POINTING_HAND,
    imgui::MouseCursor::NotAllowed => MouseCursor::MOUSE_CURSOR_NOT_ALLOWED,
  }
}

struct Renderer(());

impl Renderer {
  pub fn render(ctx: &mut Context) {
    unsafe {
      ffi::rlDrawRenderBatchActive();
      ffi::rlDisableBackfaceCulling();

      let [width, height] = ctx.io().display_size;
      let [scale_w, scale_h] = ctx.io().display_framebuffer_scale;
      let [fb_width, fb_height] = [width * scale_w, height * scale_h];

      let draw_data = ctx.render();
      if draw_data.draw_lists_count() > 0 {
        for draw_list in draw_data.draw_lists() {
          let idx_buffer = draw_list.idx_buffer();
          let vtx_buffer = draw_list.vtx_buffer();
          for cmd in draw_list.commands() {
            match cmd {
              DrawCmd::Elements {
                count,
                cmd_params:
                  DrawCmdParams { clip_rect, texture_id, idx_offset, .. },
              } => {
                enable_scissor(
                  clip_rect,
                  [scale_w, scale_h],
                  [fb_width, fb_height],
                );
                render_triangles(
                  count, idx_offset, idx_buffer, vtx_buffer, texture_id,
                );
                ffi::rlDrawRenderBatchActive();
              },
              DrawCmd::ResetRenderState => unimplemented!(),
              DrawCmd::RawCallback { callback, raw_cmd } => {
                let clip_rect = &(*raw_cmd).ClipRect;
                enable_scissor(
                  [clip_rect.x, clip_rect.y, clip_rect.z, clip_rect.w],
                  [scale_w, scale_h],
                  [fb_width, fb_height],
                );
                callback(draw_list.raw(), raw_cmd);
              },
            }
          }
        }
      }

      ffi::rlSetTexture(0);
      ffi::rlDisableScissorTest();
      ffi::rlEnableBackfaceCulling();
    }
  }
}

unsafe fn enable_scissor(
  clip_rect: [f32; 4],
  scale: [f32; 2],
  fb_size: [f32; 2],
) {
  let [x, y, z, w] = clip_rect;
  let [scale_w, scale_h] = scale;
  let [_fb_width, fb_height] = fb_size;
  ffi::rlEnableScissorTest();
  ffi::rlScissor(
    (x * scale_w) as i32,
    (fb_height - w * scale_h) as i32,
    ((z - x) * scale_w) as i32,
    ((w - y) * scale_h) as i32,
  );
}

fn render_triangles(
  count: usize,
  idx_offset: usize,
  idx_buffer: &[u16],
  vtx_buffer: &[DrawVert],
  texture_id: imgui::TextureId,
) {
  if count < 3 {
    return;
  }

  unsafe {
    ffi::rlBegin(ffi::RL_TRIANGLES as i32);
    ffi::rlSetTexture(texture_id.id() as _);

    for i in (0..=count - 3).step_by(3) {
      let index_a = idx_buffer[idx_offset + i];
      let index_b = idx_buffer[idx_offset + i + 1];
      let index_c = idx_buffer[idx_offset + i + 2];

      let vertex_a = vtx_buffer[index_a as usize];
      let vertex_b = vtx_buffer[index_b as usize];
      let vertex_c = vtx_buffer[index_c as usize];

      triangle_vert(vertex_a);
      triangle_vert(vertex_b);
      triangle_vert(vertex_c);
    }
    ffi::rlEnd();
  }
}

unsafe fn triangle_vert(idx_vert: DrawVert) {
  let [r, g, b, a] = idx_vert.col;
  let [uv_x, uv_y] = idx_vert.uv;
  let [x, y] = idx_vert.pos;
  ffi::rlColor4ub(r, g, b, a);
  ffi::rlTexCoord2f(uv_x, uv_y);
  ffi::rlVertex2f(x, y);
}