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
//! This crate provides support functions to simplify integrating imgui-rs with glutin.
//!
//! # Using the library
//!
//! In your initialization code call `configure_keys`:
//!
//! ```rust,no_run
//! # extern crate imgui;
//! # extern crate imgui_glutin_support;
//! use imgui::ImGui;
//!
//! # fn main() {
//! let mut imgui = ImGui::init();
//! imgui_glutin_support::configure_keys(&mut imgui);
//! # }
//! ```
//!
//! In your main loop you should already be retrieving events from glutin and handling them. All
//! you need to do is pass each event to `imgui_glutin_support` as well:
//!
//! ```rust,no_run
//! # extern crate glutin;
//! # extern crate imgui;
//! # extern crate imgui_glutin_support;
//! # use glutin::EventsLoop;
//! # use imgui::ImGui;
//! # fn main() {
//! # let mut events_loop = EventsLoop::new();
//! # let mut imgui = ImGui::init();
//! events_loop.poll_events(|event| {
//!     // do application-specific stuff with event
//!
//!     imgui_glutin_support::handle_event(&mut imgui, &event);
//! });
//! # }
//! ```
//!
//! # Advanced use cases
//!
//! In more advanced use cases you might want to handle and filter events yourself and call some of
//! the various smaller helper functions exported by the library.
//!
//! For example, you might want to customize mouse wheel line scrolling amount:
//!
//! ```rust,no_run
//! # extern crate glutin;
//! # extern crate imgui;
//! # extern crate imgui_glutin_support;
//! # use glutin::{EventsLoop, Event, WindowEvent, MouseScrollDelta, TouchPhase};
//! # use imgui::ImGui;
//! # fn main() {
//! # let mut events_loop = EventsLoop::new();
//! # let mut imgui = ImGui::init();
//! events_loop.poll_events(|event| {
//!     // do application-specific stuff with event
//!
//!     // default handling for events
//!     imgui_glutin_support::handle_event(&mut imgui, &event);
//!
//!     // Scroll 10 times the pixels per line by handling LineDelta events again and
//!     // overriding the mouse wheel value
//!     if let Event::WindowEvent { event, .. } = event {
//!         match event {
//!             WindowEvent::MouseWheel {
//!                 delta: MouseScrollDelta::LineDelta(_, lines),
//!                 phase: TouchPhase::Moved,
//!                 ..
//!             } => {
//!                 imgui.set_mouse_wheel(lines * 10.0);
//!             }
//!             _ => ()
//!         }
//!     }
//! });
//! # }
//! ```

extern crate glutin;
extern crate imgui;

use glutin::{
    ElementState, Event, KeyboardInput, ModifiersState, MouseButton, MouseCursor, MouseScrollDelta,
    TouchPhase, VirtualKeyCode, Window, WindowEvent,
};
use imgui::{FrameSize, ImGui, ImGuiKey, ImGuiMouseCursor};

/// Configure imgui key map with glutin `VirtualKeyCode` values
pub fn configure_keys(imgui: &mut ImGui) {
    imgui.set_imgui_key(ImGuiKey::Tab, VirtualKeyCode::Tab as _);
    imgui.set_imgui_key(ImGuiKey::LeftArrow, VirtualKeyCode::Left as _);
    imgui.set_imgui_key(ImGuiKey::RightArrow, VirtualKeyCode::Right as _);
    imgui.set_imgui_key(ImGuiKey::UpArrow, VirtualKeyCode::Up as _);
    imgui.set_imgui_key(ImGuiKey::DownArrow, VirtualKeyCode::Down as _);
    imgui.set_imgui_key(ImGuiKey::PageUp, VirtualKeyCode::PageUp as _);
    imgui.set_imgui_key(ImGuiKey::PageDown, VirtualKeyCode::PageDown as _);
    imgui.set_imgui_key(ImGuiKey::Home, VirtualKeyCode::Home as _);
    imgui.set_imgui_key(ImGuiKey::End, VirtualKeyCode::End as _);
    imgui.set_imgui_key(ImGuiKey::Delete, VirtualKeyCode::Delete as _);
    imgui.set_imgui_key(ImGuiKey::Backspace, VirtualKeyCode::Back as _);
    imgui.set_imgui_key(ImGuiKey::Enter, VirtualKeyCode::Return as _);
    imgui.set_imgui_key(ImGuiKey::Escape, VirtualKeyCode::Escape as _);
    imgui.set_imgui_key(ImGuiKey::A, VirtualKeyCode::A as _);
    imgui.set_imgui_key(ImGuiKey::C, VirtualKeyCode::C as _);
    imgui.set_imgui_key(ImGuiKey::V, VirtualKeyCode::V as _);
    imgui.set_imgui_key(ImGuiKey::X, VirtualKeyCode::X as _);
    imgui.set_imgui_key(ImGuiKey::Y, VirtualKeyCode::Y as _);
    imgui.set_imgui_key(ImGuiKey::Z, VirtualKeyCode::Z as _);
}

/// Update imgui keyboard state
pub fn handle_keyboard_input(imgui: &mut ImGui, event: KeyboardInput) {
    handle_modifiers(imgui, event.modifiers);
    if let Some(key) = event.virtual_keycode {
        let state_bool = event.state == ElementState::Pressed;
        imgui.set_key(key as _, state_bool);
        match key {
            VirtualKeyCode::LShift | VirtualKeyCode::RShift => imgui.set_key_shift(state_bool),
            VirtualKeyCode::LControl | VirtualKeyCode::RControl => imgui.set_key_ctrl(state_bool),
            VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => imgui.set_key_alt(state_bool),
            VirtualKeyCode::LWin | VirtualKeyCode::RWin => imgui.set_key_super(state_bool),
            _ => (),
        }
    }
}

/// Update imgui keyboard modifier state
pub fn handle_modifiers(imgui: &mut ImGui, modifiers: ModifiersState) {
    imgui.set_key_shift(modifiers.shift);
    imgui.set_key_ctrl(modifiers.ctrl);
    imgui.set_key_alt(modifiers.alt);
    imgui.set_key_super(modifiers.logo);
}

/// Update imgui mouse wheel position
pub fn handle_mouse_scroll_delta(imgui: &mut ImGui, delta: MouseScrollDelta) {
    match delta {
        MouseScrollDelta::LineDelta(_, y) => imgui.set_mouse_wheel(y),
        MouseScrollDelta::PixelDelta(pos) => imgui.set_mouse_wheel(pos.y as f32),
    }
}

/// Update imgui mouse button state
pub fn handle_mouse_button_state(imgui: &mut ImGui, button: MouseButton, state: ElementState) {
    let mut states = imgui.mouse_down();
    let state_bool = state == ElementState::Pressed;
    match button {
        MouseButton::Left => states[0] = state_bool,
        MouseButton::Right => states[1] = state_bool,
        MouseButton::Middle => states[2] = state_bool,
        MouseButton::Other(idx @ 0...4) => states[idx as usize] = state_bool,
        _ => (),
    }
    imgui.set_mouse_down(states);
}

/// Update imgui state from glutin event
pub fn handle_event(imgui: &mut ImGui, event: &Event) {
    match event {
        &Event::WindowEvent { ref event, .. } => handle_window_event(imgui, event),
        _ => (),
    }
}

/// Update imgui state from glutin window event
pub fn handle_window_event(imgui: &mut ImGui, event: &WindowEvent) {
    use WindowEvent::*;
    match event {
        &KeyboardInput { input, .. } => handle_keyboard_input(imgui, input),
        &ReceivedCharacter(ch) => imgui.add_input_character(ch),
        &CursorMoved {
            position,
            modifiers,
            ..
        } => {
            imgui.set_mouse_pos(position.x as f32, position.y as f32);
            handle_modifiers(imgui, modifiers);
        }
        &MouseWheel {
            delta,
            modifiers,
            phase: TouchPhase::Moved,
            ..
        } => {
            handle_mouse_scroll_delta(imgui, delta);
            handle_modifiers(imgui, modifiers);
        }
        &MouseInput {
            state,
            button,
            modifiers,
            ..
        } => {
            handle_mouse_button_state(imgui, button, state);
            handle_modifiers(imgui, modifiers);
        }
        _ => (),
    }
}

/// Update glutin window mouse cursor state
pub fn update_mouse_cursor(imgui: &ImGui, window: &Window) {
    let mouse_cursor = imgui.mouse_cursor();
    if imgui.mouse_draw_cursor() || mouse_cursor == ImGuiMouseCursor::None {
        // Hide OS cursor
        window.hide_cursor(true);
    } else {
        // Set OS cursor
        window.hide_cursor(false);
        window.set_cursor(match mouse_cursor {
            ImGuiMouseCursor::None => unreachable!("mouse_cursor was None!"),
            ImGuiMouseCursor::Arrow => MouseCursor::Arrow,
            ImGuiMouseCursor::TextInput => MouseCursor::Text,
            ImGuiMouseCursor::Move => MouseCursor::Move,
            ImGuiMouseCursor::ResizeNS => MouseCursor::NsResize,
            ImGuiMouseCursor::ResizeEW => MouseCursor::EwResize,
            ImGuiMouseCursor::ResizeNESW => MouseCursor::NeswResize,
            ImGuiMouseCursor::ResizeNWSE => MouseCursor::NwseResize,
        });
    }
}

/// Get the current frame size for imgui frame rendering.
///
/// Returns `None` if the window no longer exists
pub fn get_frame_size(window: &mut Window) -> Option<FrameSize> {
    window.get_inner_size().map(|logical_size| FrameSize {
        logical_size: logical_size.into(),
        hidpi_factor: window.get_hidpi_factor(),
    })
}