mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-12 05:58:35 +00:00
If ImGui's mouse_draw_cursor is set to `true`, then ImGui draws the cursor by itself, so this commits hids the OS cursor. If ImGui's mouse_draw_cursor is set to `false`, then this commits updates the OS cursor to the value set by the `set_mouse_cursor` function provided by the ImGui instance. For our use case, it seems safe to unwrap the result of the call to glutin::Window::set_cursor_state, as an error can only potentially when the `Grab` cursor state is used [1]. In ImGui's use case, the `Grab` state is never used. [1] https://docs.rs/crate/winit/0.11.2/source/src/platform/linux/x11/window.rs
198 lines
8.0 KiB
Rust
198 lines
8.0 KiB
Rust
use imgui::{ImGui, ImGuiMouseCursor, Ui};
|
|
use std::time::Instant;
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Default)]
|
|
struct MouseState {
|
|
pos: (i32, i32),
|
|
pressed: (bool, bool, bool),
|
|
wheel: f32,
|
|
}
|
|
|
|
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
|
|
use glium::glutin;
|
|
use glium::{Display, Surface};
|
|
use imgui_glium_renderer::Renderer;
|
|
|
|
let mut events_loop = glutin::EventsLoop::new();
|
|
let context = glutin::ContextBuilder::new().with_vsync(true);
|
|
let window = glutin::WindowBuilder::new()
|
|
.with_title(title)
|
|
.with_dimensions(1024, 768);
|
|
let display = Display::new(window, context, &events_loop).unwrap();
|
|
|
|
let mut imgui = ImGui::init();
|
|
imgui.set_ini_filename(None);
|
|
let mut renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
|
|
|
|
configure_keys(&mut imgui);
|
|
|
|
let mut last_frame = Instant::now();
|
|
let mut mouse_state = MouseState::default();
|
|
let mut quit = false;
|
|
|
|
loop {
|
|
events_loop.poll_events(|event| {
|
|
use glium::glutin::WindowEvent::*;
|
|
use glium::glutin::ElementState::Pressed;
|
|
use glium::glutin::{Event, MouseButton, MouseScrollDelta, TouchPhase};
|
|
|
|
if let Event::WindowEvent { event, .. } = event {
|
|
match event {
|
|
Closed => quit = true,
|
|
KeyboardInput { input, .. } => {
|
|
use glium::glutin::VirtualKeyCode as Key;
|
|
|
|
let pressed = input.state == Pressed;
|
|
match input.virtual_keycode {
|
|
Some(Key::Tab) => imgui.set_key(0, pressed),
|
|
Some(Key::Left) => imgui.set_key(1, pressed),
|
|
Some(Key::Right) => imgui.set_key(2, pressed),
|
|
Some(Key::Up) => imgui.set_key(3, pressed),
|
|
Some(Key::Down) => imgui.set_key(4, pressed),
|
|
Some(Key::PageUp) => imgui.set_key(5, pressed),
|
|
Some(Key::PageDown) => imgui.set_key(6, pressed),
|
|
Some(Key::Home) => imgui.set_key(7, pressed),
|
|
Some(Key::End) => imgui.set_key(8, pressed),
|
|
Some(Key::Delete) => imgui.set_key(9, pressed),
|
|
Some(Key::Back) => imgui.set_key(10, pressed),
|
|
Some(Key::Return) => imgui.set_key(11, pressed),
|
|
Some(Key::Escape) => imgui.set_key(12, pressed),
|
|
Some(Key::A) => imgui.set_key(13, pressed),
|
|
Some(Key::C) => imgui.set_key(14, pressed),
|
|
Some(Key::V) => imgui.set_key(15, pressed),
|
|
Some(Key::X) => imgui.set_key(16, pressed),
|
|
Some(Key::Y) => imgui.set_key(17, pressed),
|
|
Some(Key::Z) => imgui.set_key(18, pressed),
|
|
Some(Key::LControl) |
|
|
Some(Key::RControl) => imgui.set_key_ctrl(pressed),
|
|
Some(Key::LShift) |
|
|
Some(Key::RShift) => imgui.set_key_shift(pressed),
|
|
Some(Key::LAlt) | Some(Key::RAlt) => imgui.set_key_alt(pressed),
|
|
Some(Key::LWin) | Some(Key::RWin) => imgui.set_key_super(pressed),
|
|
_ => {}
|
|
}
|
|
}
|
|
CursorMoved { position: (x, y), .. } => mouse_state.pos = (x as i32, y as i32),
|
|
MouseInput { state, button, .. } => {
|
|
match button {
|
|
MouseButton::Left => mouse_state.pressed.0 = state == Pressed,
|
|
MouseButton::Right => mouse_state.pressed.1 = state == Pressed,
|
|
MouseButton::Middle => mouse_state.pressed.2 = state == Pressed,
|
|
_ => {}
|
|
}
|
|
}
|
|
MouseWheel {
|
|
delta: MouseScrollDelta::LineDelta(_, y),
|
|
phase: TouchPhase::Moved,
|
|
..
|
|
} |
|
|
MouseWheel {
|
|
delta: MouseScrollDelta::PixelDelta(_, y),
|
|
phase: TouchPhase::Moved,
|
|
..
|
|
} => mouse_state.wheel = y,
|
|
ReceivedCharacter(c) => imgui.add_input_character(c),
|
|
_ => (),
|
|
}
|
|
}
|
|
});
|
|
|
|
let now = Instant::now();
|
|
let delta = now - last_frame;
|
|
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
|
|
last_frame = now;
|
|
|
|
update_mouse(&mut imgui, &mut mouse_state);
|
|
|
|
let gl_window = display.gl_window();
|
|
|
|
let mouse_cursor = imgui.mouse_cursor();
|
|
if imgui.mouse_draw_cursor() || mouse_cursor == ImGuiMouseCursor::None {
|
|
// Hide OS cursor
|
|
gl_window.set_cursor_state(glutin::CursorState::Hide).unwrap();
|
|
} else {
|
|
// Set OS cursor
|
|
gl_window.set_cursor_state(glutin::CursorState::Normal).unwrap();
|
|
gl_window.set_cursor(match mouse_cursor {
|
|
ImGuiMouseCursor::None => unreachable!("mouse_cursor was None!"),
|
|
ImGuiMouseCursor::Arrow => glutin::MouseCursor::Arrow,
|
|
ImGuiMouseCursor::TextInput => glutin::MouseCursor::Text,
|
|
ImGuiMouseCursor::Move => glutin::MouseCursor::Move,
|
|
ImGuiMouseCursor::ResizeNS => glutin::MouseCursor::NsResize,
|
|
ImGuiMouseCursor::ResizeEW => glutin::MouseCursor::EwResize,
|
|
ImGuiMouseCursor::ResizeNESW => glutin::MouseCursor::NeswResize,
|
|
ImGuiMouseCursor::ResizeNWSE => glutin::MouseCursor::NwseResize,
|
|
});
|
|
}
|
|
|
|
let size_pixels = gl_window.get_inner_size().unwrap();
|
|
let hdipi = gl_window.hidpi_factor();
|
|
let size_points = (
|
|
(size_pixels.0 as f32 / hdipi) as u32,
|
|
(size_pixels.1 as f32 / hdipi) as u32,
|
|
);
|
|
|
|
let ui = imgui.frame(size_points, size_pixels, delta_s);
|
|
if !run_ui(&ui) {
|
|
break;
|
|
}
|
|
|
|
let mut target = display.draw();
|
|
target.clear_color(
|
|
clear_color[0],
|
|
clear_color[1],
|
|
clear_color[2],
|
|
clear_color[3],
|
|
);
|
|
renderer.render(&mut target, ui).expect("Rendering failed");
|
|
target.finish().unwrap();
|
|
|
|
if quit {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
fn configure_keys(imgui: &mut ImGui) {
|
|
use imgui::ImGuiKey;
|
|
|
|
imgui.set_imgui_key(ImGuiKey::Tab, 0);
|
|
imgui.set_imgui_key(ImGuiKey::LeftArrow, 1);
|
|
imgui.set_imgui_key(ImGuiKey::RightArrow, 2);
|
|
imgui.set_imgui_key(ImGuiKey::UpArrow, 3);
|
|
imgui.set_imgui_key(ImGuiKey::DownArrow, 4);
|
|
imgui.set_imgui_key(ImGuiKey::PageUp, 5);
|
|
imgui.set_imgui_key(ImGuiKey::PageDown, 6);
|
|
imgui.set_imgui_key(ImGuiKey::Home, 7);
|
|
imgui.set_imgui_key(ImGuiKey::End, 8);
|
|
imgui.set_imgui_key(ImGuiKey::Delete, 9);
|
|
imgui.set_imgui_key(ImGuiKey::Backspace, 10);
|
|
imgui.set_imgui_key(ImGuiKey::Enter, 11);
|
|
imgui.set_imgui_key(ImGuiKey::Escape, 12);
|
|
imgui.set_imgui_key(ImGuiKey::A, 13);
|
|
imgui.set_imgui_key(ImGuiKey::C, 14);
|
|
imgui.set_imgui_key(ImGuiKey::V, 15);
|
|
imgui.set_imgui_key(ImGuiKey::X, 16);
|
|
imgui.set_imgui_key(ImGuiKey::Y, 17);
|
|
imgui.set_imgui_key(ImGuiKey::Z, 18);
|
|
}
|
|
|
|
fn update_mouse(imgui: &mut ImGui, mouse_state: &mut MouseState) {
|
|
let scale = imgui.display_framebuffer_scale();
|
|
imgui.set_mouse_pos(
|
|
mouse_state.pos.0 as f32 / scale.0,
|
|
mouse_state.pos.1 as f32 / scale.1,
|
|
);
|
|
imgui.set_mouse_down(
|
|
&[
|
|
mouse_state.pressed.0,
|
|
mouse_state.pressed.1,
|
|
mouse_state.pressed.2,
|
|
false,
|
|
false,
|
|
],
|
|
);
|
|
imgui.set_mouse_wheel(mouse_state.wheel / scale.1);
|
|
mouse_state.wheel = 0.0;
|
|
}
|