aboutsummaryrefslogtreecommitdiffstats
path: root/examples/grabbing.rs
blob: 07e38735bf10d53f1e381c8074ce3083ed81f37f (plain)
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
#![feature(std_misc)]

#[cfg(target_os = "android")]
#[macro_use]
extern crate android_glue;

extern crate glutin;

use glutin::{Event, ElementState};

mod support;

#[cfg(target_os = "android")]
android_start!(main);

#[cfg(not(feature = "window"))]
fn main() { println!("This example requires glutin to be compiled with the `window` feature"); }

#[cfg(feature = "window")]
fn main() {
    let window = glutin::Window::new().unwrap();
    window.set_title("glutin - Cursor grabbing test");
    unsafe { window.make_current() };

    let context = support::load(&window);
    let mut grabbed = false;
    
    while !window.is_closed() {
        context.draw_frame((0.0, 1.0, 0.0, 1.0));
        window.swap_buffers();

        for event in window.poll_events() {
            match event {
                Event::KeyboardInput(ElementState::Pressed, _, _) => {
                    if grabbed {
                        grabbed = false;
                        window.set_cursor_state(glutin::CursorState::Normal)
                              .ok().expect("could not ungrab mouse cursor");
                    } else {
                        grabbed = true;
                        window.set_cursor_state(glutin::CursorState::Grab)
                              .ok().expect("could not grab mouse cursor");
                    }
                },
                _ => (),
            }
            
        }
        
    }
}