avm2: Implement `Event.preventDefault` and `Event.isDefaultPrevented`
This commit is contained in:
parent
8b9a804ae9
commit
d393a5f4f8
|
@ -103,6 +103,16 @@ impl<'gc> Event<'gc> {
|
||||||
self.cancelable = cancelable;
|
self.cancelable = cancelable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_cancelled(&self) -> bool {
|
||||||
|
self.cancelled
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cancel(&mut self) {
|
||||||
|
if self.cancelable {
|
||||||
|
self.cancelled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn phase(&self) -> EventPhase {
|
pub fn phase(&self) -> EventPhase {
|
||||||
self.event_phase
|
self.event_phase
|
||||||
}
|
}
|
||||||
|
|
|
@ -191,6 +191,32 @@ pub fn format_to_string<'gc>(
|
||||||
Ok(Value::Undefined)
|
Ok(Value::Undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Implements `isDefaultPrevented`
|
||||||
|
pub fn is_default_prevented<'gc>(
|
||||||
|
_activation: &mut Activation<'_, 'gc, '_>,
|
||||||
|
this: Option<Object<'gc>>,
|
||||||
|
_args: &[Value<'gc>],
|
||||||
|
) -> Result<Value<'gc>, Error> {
|
||||||
|
if let Some(evt) = this.unwrap().as_event() {
|
||||||
|
return Ok(evt.is_cancelled().into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Value::Undefined)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements `preventDefault`
|
||||||
|
pub fn prevent_default<'gc>(
|
||||||
|
activation: &mut Activation<'_, 'gc, '_>,
|
||||||
|
this: Option<Object<'gc>>,
|
||||||
|
_args: &[Value<'gc>],
|
||||||
|
) -> Result<Value<'gc>, Error> {
|
||||||
|
if let Some(mut evt) = this.unwrap().as_event_mut(activation.context.gc_context) {
|
||||||
|
evt.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Value::Undefined)
|
||||||
|
}
|
||||||
|
|
||||||
/// Construct `Event`'s class.
|
/// Construct `Event`'s class.
|
||||||
pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> {
|
pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> {
|
||||||
let class = Class::new(
|
let class = Class::new(
|
||||||
|
@ -235,6 +261,14 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>
|
||||||
QName::new(Namespace::public_namespace(), "formatToString"),
|
QName::new(Namespace::public_namespace(), "formatToString"),
|
||||||
Method::from_builtin(format_to_string),
|
Method::from_builtin(format_to_string),
|
||||||
));
|
));
|
||||||
|
write.define_instance_trait(Trait::from_method(
|
||||||
|
QName::new(Namespace::public_namespace(), "isDefaultPrevented"),
|
||||||
|
Method::from_builtin(is_default_prevented),
|
||||||
|
));
|
||||||
|
write.define_instance_trait(Trait::from_method(
|
||||||
|
QName::new(Namespace::public_namespace(), "preventDefault"),
|
||||||
|
Method::from_builtin(prevent_default),
|
||||||
|
));
|
||||||
|
|
||||||
class
|
class
|
||||||
}
|
}
|
||||||
|
|
|
@ -451,6 +451,7 @@ swf_tests! {
|
||||||
(as3_event_type, "avm2/event_type", 1),
|
(as3_event_type, "avm2/event_type", 1),
|
||||||
(as3_event_clone, "avm2/event_clone", 1),
|
(as3_event_clone, "avm2/event_clone", 1),
|
||||||
(as3_event_formattostring, "avm2/event_formattostring", 1),
|
(as3_event_formattostring, "avm2/event_formattostring", 1),
|
||||||
|
(as3_event_isdefaultprevented, "avm2/event_isdefaultprevented", 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: These tests have some inaccuracies currently, so we use approx_eq to test that numeric values are close enough.
|
// TODO: These tests have some inaccuracies currently, so we use approx_eq to test that numeric values are close enough.
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package {
|
||||||
|
public class Test {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
import flash.events.Event;
|
||||||
|
|
||||||
|
trace("//var e = new Event(\"test_event\", false, false);");
|
||||||
|
var e = new Event("test_event", false, false);
|
||||||
|
|
||||||
|
trace("//e.isDefaultPrevented();");
|
||||||
|
trace(e.isDefaultPrevented());
|
||||||
|
|
||||||
|
trace("//e.preventDefault()");
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
trace("//e.isDefaultPrevented();");
|
||||||
|
trace(e.isDefaultPrevented());
|
||||||
|
|
||||||
|
trace("//var e = new Event(\"test_event\", true, true);");
|
||||||
|
var e = new Event("test_event", true, true);
|
||||||
|
|
||||||
|
trace("//e.isDefaultPrevented();");
|
||||||
|
trace(e.isDefaultPrevented());
|
||||||
|
|
||||||
|
trace("//e.preventDefault()");
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
trace("//e.isDefaultPrevented();");
|
||||||
|
trace(e.isDefaultPrevented());
|
|
@ -0,0 +1,12 @@
|
||||||
|
//var e = new Event("test_event", false, false);
|
||||||
|
//e.isDefaultPrevented();
|
||||||
|
false
|
||||||
|
//e.preventDefault()
|
||||||
|
//e.isDefaultPrevented();
|
||||||
|
false
|
||||||
|
//var e = new Event("test_event", true, true);
|
||||||
|
//e.isDefaultPrevented();
|
||||||
|
false
|
||||||
|
//e.preventDefault()
|
||||||
|
//e.isDefaultPrevented();
|
||||||
|
true
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue