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
use js_sys::Object;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{Document, Element, Event, EventTarget, MouseEvent};

pub struct PointerLock<'a> {
    pub trigger: &'a EventTarget,
    pub target: &'a EventTarget,
    click_cb: Closure<dyn FnMut(&Event)>,
    change_cb: Closure<dyn FnMut(&Event)>,
    change_cb_moz: Closure<dyn FnMut(&Event)>,
    change_cb_webkit: Closure<dyn FnMut(&Event)>,
    document: &'a Document,
}

impl<'a> PointerLock<'a> {
    pub fn start<F, G, H>(
        trigger: &'a Element,
        target: &'a Element,
        document: &'a Document,
        on_start: F,
        on_move: G,
        on_end: H,
    ) -> Self
    where
        F: Fn(&Element) + 'static,
        G: Fn(i32, i32) + 'static,
        H: Fn() + 'static,
    {
        let is_locked = Rc::new(Cell::new(false));

        let request_lock = {
            let target = target.clone();
            let is_locked = Rc::clone(&is_locked);
            move |_: &_| {
                if !is_locked.get() {
                    target.request_pointer_lock();
                    is_locked.set(true);
                }
            }
        };

        let on_pointer_move = Rc::new(move |evt: &Event| {
            let evt: MouseEvent = JsValue::from(evt).into();
            let (x, y) = (evt.movement_x(), evt.movement_y());

            on_move(x, y);
        });

        let pointer_lock_change = {
            let target = target.clone();
            let document = document.clone();
            let is_locked = Rc::clone(&is_locked);

            let mut listener: Option<Closure<dyn FnMut(&Event)>> = None;

            Rc::new(RefCell::new(move |_initial_evt: &Event| {
                let lock_enabled = match document.pointer_lock_element() {
                    None => false,
                    Some(element) => elements_are_equal(&element, &target),
                };
                is_locked.set(lock_enabled);

                if lock_enabled {
                    on_start(&target);

                    
                    #[cfg(feature = "debug_log")]
                    log::info!("pointer lock enabled!");

                    let move_cb = Closure::wrap(Box::new({
                        let f = Rc::clone(&on_pointer_move);
                        move |e: &Event| f(e)
                    }) as Box<dyn FnMut(&Event)>);
                    document
                        .add_event_listener_with_callback(
                            "mousemove",
                            move_cb.as_ref().unchecked_ref(),
                        )
                        .unwrap_throw();
                    listener = Some(move_cb);
                } else {
                    on_end();
                    if let Some(l) = listener.take() {
                        document
                            .remove_event_listener_with_callback(
                                "mousemove",
                                l.as_ref().unchecked_ref(),
                            )
                            .unwrap_throw();
                    }
                }
            }))
        };

        let click_cb = Closure::wrap(Box::new(request_lock) as Box<dyn FnMut(&Event)>);
        trigger
            .add_event_listener_with_callback("click", click_cb.as_ref().unchecked_ref())
            .unwrap_throw();

        let change_cb = Closure::wrap(Box::new({
            let f = pointer_lock_change.clone();
            move |e: &Event| {
                let f = &mut *f.borrow_mut();
                f(e);
            }
        }) as Box<dyn FnMut(&Event)>);
        document
            .add_event_listener_with_callback(
                "pointerlockchange",
                change_cb.as_ref().unchecked_ref(),
            )
            .unwrap_throw();

        let change_cb_moz = Closure::wrap(Box::new({
            let f = pointer_lock_change.clone();
            move |e: &Event| {
                let f = &mut *f.borrow_mut();
                f(e);
            }
        }) as Box<dyn FnMut(&Event)>);
        document
            .add_event_listener_with_callback(
                "mozpointerlockchange",
                change_cb_moz.as_ref().unchecked_ref(),
            )
            .unwrap_throw();
        let change_cb_webkit = Closure::wrap(Box::new({
            let f = pointer_lock_change.clone();
            move |e: &Event| {
                let f = &mut *f.borrow_mut();
                f(e);
            }
        }) as Box<dyn FnMut(&Event)>);
        document
            .add_event_listener_with_callback(
                "webkitpointerlockchange",
                change_cb_webkit.as_ref().unchecked_ref(),
            )
            .unwrap_throw();

        Self {
            click_cb,
            change_cb,
            change_cb_moz,
            change_cb_webkit,
            target,
            trigger,
            document,
        }
    }
}

impl<'a> Drop for PointerLock<'a> {
    fn drop(&mut self) {
        self.trigger
            .remove_event_listener_with_callback("click", self.click_cb.as_ref().unchecked_ref())
            .unwrap_throw();
        self.document
            .remove_event_listener_with_callback(
                "pointerlockchange",
                self.change_cb.as_ref().unchecked_ref(),
            )
            .unwrap_throw();
        self.document
            .remove_event_listener_with_callback(
                "mozpointerlockchange",
                self.change_cb_moz.as_ref().unchecked_ref(),
            )
            .unwrap_throw();
        self.document
            .remove_event_listener_with_callback(
                "webkitpointerlockchange",
                self.change_cb_webkit.as_ref().unchecked_ref(),
            )
            .unwrap_throw();

        #[cfg(feature = "debug_log")]
        log::info!("dropped pointer lock!");
    }
}

fn elements_are_equal(el1: &Element, el2: &Element) -> bool {
    //See: https://github.com/rustwasm/wasm-bindgen/issues/1672
    Object::is(&el1, &el2)
}