avm2: Implement `mouseX` and `mouseY`.

Note that we cannot write a reproducible test for this.
This commit is contained in:
David Wendt 2020-12-02 22:42:04 -05:00 committed by Mike Welsh
parent 06ba2f898a
commit b18b87e566
1 changed files with 38 additions and 0 deletions

View File

@ -403,6 +403,36 @@ pub fn set_visible<'gc>(
Ok(Value::Undefined)
}
/// Implements `mouseX`.
pub fn mouse_x<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(dobj) = this.and_then(|this| this.as_display_object()) {
let local_mouse = dobj.global_to_local(*activation.context.mouse_position);
return Ok(local_mouse.0.to_pixels().into());
}
Ok(Value::Undefined)
}
/// Implements `mouseY`.
pub fn mouse_y<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error> {
if let Some(dobj) = this.and_then(|this| this.as_display_object()) {
let local_mouse = dobj.global_to_local(*activation.context.mouse_position);
return Ok(local_mouse.1.to_pixels().into());
}
Ok(Value::Undefined)
}
/// Construct `DisplayObject`'s class.
pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> {
let class = Class::new(
@ -503,6 +533,14 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>
QName::new(Namespace::package(""), "visible"),
Method::from_builtin(set_visible),
));
write.define_instance_trait(Trait::from_getter(
QName::new(Namespace::package(""), "mouseX"),
Method::from_builtin(mouse_x),
));
write.define_instance_trait(Trait::from_getter(
QName::new(Namespace::package(""), "mouseY"),
Method::from_builtin(mouse_y),
));
class
}