diff --git a/core/src/avm2/globals/flash/display/displayobject.rs b/core/src/avm2/globals/flash/display/displayobject.rs index d8e2031bc..c8941a451 100644 --- a/core/src/avm2/globals/flash/display/displayobject.rs +++ b/core/src/avm2/globals/flash/display/displayobject.rs @@ -9,7 +9,7 @@ use crate::avm2::traits::Trait; use crate::avm2::value::Value; use crate::avm2::Error; use crate::display_object::TDisplayObject; -use crate::types::Percent; +use crate::types::{Degrees, Percent}; use gc_arena::{GcCell, MutationContext}; /// Implements `flash.display.DisplayObject`'s instance constructor. @@ -259,6 +259,45 @@ pub fn set_y<'gc>( Ok(Value::Undefined) } +/// Implements `rotation`'s getter. +pub fn rotation<'gc>( + activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(dobj) = this.and_then(|this| this.as_display_object()) { + let rot: f64 = dobj.rotation(activation.context.gc_context).into(); + let rem = rot % 360.0; + + if rem <= 180.0 { + return Ok(Value::Number(rem)); + } else { + return Ok(Value::Number(rem - 360.0)); + } + } + + Ok(Value::Undefined) +} + +/// Implements `rotation`'s setter. +pub fn set_rotation<'gc>( + activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + args: &[Value<'gc>], +) -> Result, Error> { + if let Some(dobj) = this.and_then(|this| this.as_display_object()) { + let new_rotation = args + .get(0) + .cloned() + .unwrap_or(Value::Undefined) + .coerce_to_number(activation)?; + + dobj.set_rotation(activation.context.gc_context, Degrees::from(new_rotation)); + } + + Ok(Value::Undefined) +} + /// Construct `DisplayObject`'s class. pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { let class = Class::new( @@ -327,6 +366,14 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc> QName::new(Namespace::package(""), "y"), Method::from_builtin(set_y), )); + write.define_instance_trait(Trait::from_getter( + QName::new(Namespace::package(""), "rotation"), + Method::from_builtin(rotation), + )); + write.define_instance_trait(Trait::from_setter( + QName::new(Namespace::package(""), "rotation"), + Method::from_builtin(set_rotation), + )); class } diff --git a/core/tests/regression_tests.rs b/core/tests/regression_tests.rs index 372a9df1d..30674a229 100644 --- a/core/tests/regression_tests.rs +++ b/core/tests/regression_tests.rs @@ -458,6 +458,7 @@ swf_tests_approx! { (as3_math, "avm2/math", 1, max_relative = 30.0 * std::f64::EPSILON), (as3_displayobject_height, "avm2/displayobject_height", 7, epsilon = 0.06), // TODO: height/width appears to be off by 1 twip sometimes (as3_displayobject_width, "avm2/displayobject_width", 7, epsilon = 0.06), + (as3_displayobject_rotation, "avm2/displayobject_rotation", 1, epsilon = 0.0000000001), } #[test]