Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the experimental pointer lock API #247

Merged
merged 9 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ pub mod web {
PointerLeaveEvent,
GotPointerCaptureEvent,
LostPointerCaptureEvent,
PointerLockChangeEvent,
PointerLockErrorEvent
};

pub use webapi::events::keyboard::{
Expand Down
27 changes: 26 additions & 1 deletion src/webapi/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use webcore::value::Reference;
use webcore::value::{Reference, Value};
use webcore::try_from::TryInto;
use webapi::event_target::{IEventTarget, EventTarget};
use webapi::node::{INode, Node};
Expand Down Expand Up @@ -141,4 +141,29 @@ impl Document {
).try_into().unwrap()
}
}

/// Returns the Element that the pointer is locked to, if it is locked to any
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/pointerLockElement)
// https://w3c.github.io/pointerlock/#dom-documentorshadowroot-pointerlockelement
pub fn pointer_lock_element( &self ) -> Option< Element > {
let value = js!(
return @{self}.pointerLockElement;
);
match value {
Value::Null | Value::Undefined => None,
Value::Reference(reference) => Some(reference.try_into().unwrap()),
_ => unreachable!()
}
}

/// Exit the pointer lock on the current element
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Document/exitPointerLock)
// https://w3c.github.io/pointerlock/#dom-document-exitpointerlock
pub fn exit_pointer_lock( &self ) {
js!( @(no_return)
@{self}.exitPointerLock();
);
}
}
44 changes: 44 additions & 0 deletions src/webapi/events/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@ impl ConcreteEvent for LostPointerCaptureEvent {
const EVENT_TYPE: &'static str = "lostpointercapture";
}

/// The `PointerLockChangeEvent` fires when the pointer is locked or unlocked
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/pointerlockchange)
// http://www.w3.org/TR/pointerlock/#pointerlockchange-and-pointerlockerror-events
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "Event")] // TODO: Better type check.
#[reference(subclass_of(Event))]
pub struct PointerLockChangeEvent( Reference );

impl IEvent for PointerLockChangeEvent {}
impl ConcreteEvent for PointerLockChangeEvent {
const EVENT_TYPE: &'static str = "pointerlockchange";
}

/// The `PointerLockErrorEvent` fires when an error occurs locking a pointer
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/pointerlockerror)
// http://www.w3.org/TR/pointerlock/#pointerlockchange-and-pointerlockerror-events
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "Event")] // TODO: Better type check.
#[reference(subclass_of(Event))]
pub struct PointerLockErrorEvent( Reference );

impl IEvent for PointerLockErrorEvent {}
impl ConcreteEvent for PointerLockErrorEvent {
const EVENT_TYPE: &'static str = "pointerlockerror";
}

#[cfg(all(test, feature = "web_test"))]
mod tests {
use super::*;
Expand Down Expand Up @@ -488,4 +516,20 @@ mod tests {
).try_into().unwrap();
assert_eq!( event.event_type(), LostPointerCaptureEvent::EVENT_TYPE );
}

#[test]
fn test_pointer_lock_change_event() {
let event: PointerLockChangeEvent = js!(
return new Event( @{PointerLockChangeEvent::EVENT_TYPE} );
).try_into().unwrap();
assert_eq!( event.event_type(), PointerLockChangeEvent::EVENT_TYPE );
}

#[test]
fn test_pointer_lock_error_event() {
let event: PointerLockErrorEvent = js!(
return new Event( @{PointerLockErrorEvent::EVENT_TYPE} );
).try_into().unwrap();
assert_eq!( event.event_type(), PointerLockErrorEvent::EVENT_TYPE );
}
}