Skip to content

Commit

Permalink
wasm: Don't reclaim pointer lock when cancelled.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Jul 23, 2024
1 parent cf775e7 commit ffe50de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
13 changes: 9 additions & 4 deletions all-is-cubes-ui/src/apps/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,16 @@ impl InputProcessor {
self.mouselook_mode.as_source()
}

// TODO: duplicated with the keybinding impl because of borrow conflicts
pub(crate) fn toggle_mouselook_mode(&mut self) {
let new_state = !*self.mouselook_mode.get();
self.mouselook_mode.set(new_state);
if new_state {
self.set_mouselook_mode(!*self.mouselook_mode.get());
}

/// Activates or deactivates mouselook mode, identically to user input doing so.
pub fn set_mouselook_mode(&mut self, active: bool) {
// Note: duplicated with the keybinding impl because of borrow conflicts
let was_active = *self.mouselook_mode.get();
self.mouselook_mode.set(active);
if active && !was_active {
// Clear delta tracking just in case
self.mouse_previous_pixel_position = None;
}
Expand Down
54 changes: 48 additions & 6 deletions all-is-cubes-wasm/src/web_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use web_sys::{
FocusEvent, HtmlElement, HtmlProgressElement, KeyboardEvent, MouseEvent, Text,
};

use all_is_cubes_render::camera::{GraphicsOptions, StandardCameras, Viewport};
use all_is_cubes::euclid::{Point2D, Vector2D};
use all_is_cubes::listen::ListenableCell;
use all_is_cubes::universe::{Universe, UniverseStepInfo};
use all_is_cubes_gpu::in_wgpu;
use all_is_cubes_port::file::NonDiskFile;
use all_is_cubes_render::camera::{GraphicsOptions, StandardCameras, Viewport};
use all_is_cubes_ui::apps::{CursorIcon, Key};

use crate::js_bindings::GuiHelpers;
Expand Down Expand Up @@ -109,6 +109,15 @@ impl WebSession {
/// This method is broken out of `new()` so we can just use `self`. Well, some of the time.
/// TODO: reconsider
fn init_dom(self: Rc<Self>) {
let document = &self
.gui_helpers
.canvas_helper()
.canvas()
.owner_document()
.unwrap();
let mut options_passive_true = AddEventListenerOptions::new();
options_passive_true.passive(true);

self.add_canvas_to_self_event_listener(
"keydown",
false,
Expand Down Expand Up @@ -207,19 +216,52 @@ impl WebSession {
{
let weak_self_ref: Weak<Self> = Rc::downgrade(&self);
let ch = self.gui_helpers.canvas_helper();
let target = &ch.canvas().owner_document().unwrap();
let listener = move |_event: Event| {
Self::upgrade_in_callback(&weak_self_ref, |this, _inner| {
let state = Some(ch.is_fullscreen());
log::warn!("got fullscreenchange {state:?}");
this.fullscreen_cell.set(state);
})
};
let mut options = AddEventListenerOptions::new();
options.passive(true);
add_event_listener(target, "fullscreenchange", listener.clone(), &options);
add_event_listener(
document,
"fullscreenchange",
listener.clone(),
&options_passive_true,
);
// Safari still does not have unprefixed fullscreen API as of version 16.1
add_event_listener(target, "webkitfullscreenchange", listener, &options);
add_event_listener(
document,
"webkitfullscreenchange",
listener,
&options_passive_true,
);
}

// pointerlock* listeners, which go on the document, not the canvas
{
let weak_self_ref: Weak<Self> = Rc::downgrade(&self);
let listener = move |_event: Event| {
Self::upgrade_in_callback(&weak_self_ref, |this, inner| {
if !this.check_pointer_lock() {
// If pointer lock was active and stopped, or if it failed to activate,
// then acknowledge this in the UI and stop trying.
inner.session.input_processor.set_mouselook_mode(false);
}
})
};
add_event_listener(
document,
"pointerlockchange",
listener.clone(),
&options_passive_true,
);
add_event_listener(
document,
"pointerlockerror",
listener,
&options_passive_true,
);
}

// File drop listener.
Expand Down

0 comments on commit ffe50de

Please sign in to comment.