mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-17 08:28:44 +00:00
examples: Add support for changing mouse cursor
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
This commit is contained in:
parent
785f773966
commit
813365e6a8
@ -1,4 +1,4 @@
|
||||
use imgui::{ImGui, Ui};
|
||||
use imgui::{ImGui, ImGuiMouseCursor, Ui};
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug, Default)]
|
||||
@ -105,6 +105,26 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
|
||||
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 = (
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use imgui::{ImGui, Ui};
|
||||
use imgui::{ImGui, ImGuiMouseCursor, Ui};
|
||||
use imgui_gfx_renderer::{Renderer, Shaders};
|
||||
use std::time::Instant;
|
||||
|
||||
@ -131,6 +131,25 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
|
||||
|
||||
update_mouse(&mut imgui, &mut mouse_state);
|
||||
|
||||
let mouse_cursor = imgui.mouse_cursor();
|
||||
if imgui.mouse_draw_cursor() || mouse_cursor == ImGuiMouseCursor::None {
|
||||
// Hide OS cursor
|
||||
window.set_cursor_state(glutin::CursorState::Hide).unwrap();
|
||||
} else {
|
||||
// Set OS cursor
|
||||
window.set_cursor_state(glutin::CursorState::Normal).unwrap();
|
||||
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 = window.get_inner_size().unwrap();
|
||||
let hdipi = window.hidpi_factor();
|
||||
let size_points = (
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user