-
-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avm1: Migrate
BlurFilter
to NativeObject
- Loading branch information
Showing
5 changed files
with
135 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,137 @@ | ||
//! flash.filters.BlurFilter object | ||
use crate::avm1::activation::Activation; | ||
use crate::avm1::error::Error; | ||
use crate::avm1::object::blur_filter::BlurFilterObject; | ||
use crate::avm1::function::{Executable, FunctionObject}; | ||
use crate::avm1::object::NativeObject; | ||
use crate::avm1::property_decl::{define_properties_on, Declaration}; | ||
use crate::avm1::{Object, TObject, Value}; | ||
use gc_arena::MutationContext; | ||
use crate::avm1::{Activation, Error, Object, ScriptObject, TObject, Value}; | ||
use gc_arena::{Collect, GcCell, MutationContext}; | ||
|
||
#[derive(Clone, Debug, Collect)] | ||
#[collect(require_static)] | ||
pub struct BlurFilterObject { | ||
blur_x: f64, | ||
blur_y: f64, | ||
quality: i32, | ||
} | ||
|
||
macro_rules! blur_filter_method { | ||
($index:literal) => { | ||
|activation, this, args| method(activation, this, args, $index) | ||
}; | ||
} | ||
|
||
const PROTO_DECLS: &[Declaration] = declare_properties! { | ||
"blurX" => property(blur_x, set_blur_x); | ||
"blurY" => property(get_blur_y, set_blur_y); | ||
"quality" => property(get_quality, set_quality); | ||
"blurX" => property(blur_filter_method!(1), blur_filter_method!(2)); | ||
"blurY" => property(blur_filter_method!(3), blur_filter_method!(4)); | ||
"quality" => property(blur_filter_method!(5), blur_filter_method!(6)); | ||
}; | ||
|
||
pub fn constructor<'gc>( | ||
fn method<'gc>( | ||
activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
args: &[Value<'gc>], | ||
index: u8, | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
set_blur_x(activation, this, args.get(0..1).unwrap_or_default())?; | ||
set_blur_y(activation, this, args.get(1..2).unwrap_or_default())?; | ||
set_quality(activation, this, args.get(2..3).unwrap_or_default())?; | ||
|
||
Ok(this.into()) | ||
} | ||
|
||
pub fn blur_x<'gc>( | ||
_activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
_args: &[Value<'gc>], | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
if let Some(filter) = this.as_blur_filter_object() { | ||
return Ok(filter.blur_x().into()); | ||
const CONSTRUCTOR: u8 = 0; | ||
const GET_BLUR_X: u8 = 1; | ||
const SET_BLUR_X: u8 = 2; | ||
const GET_BLUR_Y: u8 = 3; | ||
const SET_BLUR_Y: u8 = 4; | ||
const GET_QUALITY: u8 = 5; | ||
const SET_QUALITY: u8 = 6; | ||
|
||
if index == CONSTRUCTOR { | ||
let blur_filter = GcCell::allocate( | ||
activation.context.gc_context, | ||
BlurFilterObject { | ||
blur_x: 4.0, | ||
blur_y: 4.0, | ||
quality: 1, | ||
}, | ||
); | ||
set_blur_x(activation, blur_filter, args.get(0))?; | ||
set_blur_y(activation, blur_filter, args.get(1))?; | ||
set_quality(activation, blur_filter, args.get(2))?; | ||
this.set_native( | ||
activation.context.gc_context, | ||
NativeObject::BlurFilter(blur_filter), | ||
); | ||
return Ok(this.into()); | ||
} | ||
|
||
Ok(this.into()) | ||
let blur_filter = match this.native() { | ||
NativeObject::BlurFilter(blur_filter) => blur_filter, | ||
_ => return Ok(Value::Undefined), | ||
}; | ||
|
||
Ok(match index { | ||
GET_BLUR_X => blur_filter.read().blur_x.into(), | ||
SET_BLUR_X => { | ||
set_blur_x(activation, blur_filter, args.get(0))?; | ||
Value::Undefined | ||
} | ||
GET_BLUR_Y => blur_filter.read().blur_y.into(), | ||
SET_BLUR_Y => { | ||
set_blur_y(activation, blur_filter, args.get(0))?; | ||
Value::Undefined | ||
} | ||
GET_QUALITY => blur_filter.read().quality.into(), | ||
SET_QUALITY => { | ||
set_quality(activation, blur_filter, args.get(0))?; | ||
Value::Undefined | ||
} | ||
_ => Value::Undefined, | ||
}) | ||
} | ||
|
||
pub fn set_blur_x<'gc>( | ||
fn set_blur_x<'gc>( | ||
activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
args: &[Value<'gc>], | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
let blur_x = args | ||
.get(0) | ||
.unwrap_or(&4.into()) | ||
.coerce_to_f64(activation) | ||
.map(|x| x.clamp(0.0, 255.0))?; | ||
|
||
if let Some(filter) = this.as_blur_filter_object() { | ||
filter.set_blur_x(activation.context.gc_context, blur_x); | ||
blur_filter: GcCell<'gc, BlurFilterObject>, | ||
value: Option<&Value<'gc>>, | ||
) -> Result<(), Error<'gc>> { | ||
if let Some(value) = value { | ||
let blur_x = value.coerce_to_f64(activation)?.clamp(0.0, 255.0); | ||
blur_filter.write(activation.context.gc_context).blur_x = blur_x; | ||
} | ||
|
||
Ok(Value::Undefined) | ||
Ok(()) | ||
} | ||
|
||
pub fn get_blur_y<'gc>( | ||
_activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
_args: &[Value<'gc>], | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
if let Some(filter) = this.as_blur_filter_object() { | ||
return Ok(filter.blur_y().into()); | ||
} | ||
|
||
Ok(Value::Undefined) | ||
} | ||
|
||
pub fn set_blur_y<'gc>( | ||
fn set_blur_y<'gc>( | ||
activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
args: &[Value<'gc>], | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
let blur_y = args | ||
.get(0) | ||
.unwrap_or(&4.into()) | ||
.coerce_to_f64(activation) | ||
.map(|x| x.clamp(0.0, 255.0))?; | ||
|
||
if let Some(filter) = this.as_blur_filter_object() { | ||
filter.set_blur_y(activation.context.gc_context, blur_y); | ||
blur_filter: GcCell<'gc, BlurFilterObject>, | ||
value: Option<&Value<'gc>>, | ||
) -> Result<(), Error<'gc>> { | ||
if let Some(value) = value { | ||
let blur_y = value.coerce_to_f64(activation)?.clamp(0.0, 255.0); | ||
blur_filter.write(activation.context.gc_context).blur_y = blur_y; | ||
} | ||
|
||
Ok(Value::Undefined) | ||
Ok(()) | ||
} | ||
|
||
pub fn get_quality<'gc>( | ||
_activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
_args: &[Value<'gc>], | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
if let Some(filter) = this.as_blur_filter_object() { | ||
return Ok(filter.quality().into()); | ||
} | ||
|
||
Ok(Value::Undefined) | ||
} | ||
|
||
pub fn set_quality<'gc>( | ||
fn set_quality<'gc>( | ||
activation: &mut Activation<'_, 'gc, '_>, | ||
this: Object<'gc>, | ||
args: &[Value<'gc>], | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
let quality = args | ||
.get(0) | ||
.unwrap_or(&1.into()) | ||
.coerce_to_i32(activation) | ||
.map(|x| x.clamp(0, 15))?; | ||
|
||
if let Some(filter) = this.as_blur_filter_object() { | ||
filter.set_quality(activation.context.gc_context, quality); | ||
blur_filter: GcCell<'gc, BlurFilterObject>, | ||
value: Option<&Value<'gc>>, | ||
) -> Result<(), Error<'gc>> { | ||
if let Some(value) = value { | ||
let quality = value.coerce_to_i32(activation)?.clamp(0, 15); | ||
blur_filter.write(activation.context.gc_context).quality = quality; | ||
} | ||
|
||
Ok(Value::Undefined) | ||
Ok(()) | ||
} | ||
|
||
pub fn create_proto<'gc>( | ||
pub fn create_constructor<'gc>( | ||
gc_context: MutationContext<'gc, '_>, | ||
proto: Object<'gc>, | ||
fn_proto: Object<'gc>, | ||
) -> Object<'gc> { | ||
let blur_filter = BlurFilterObject::empty_object(gc_context, proto); | ||
let object = blur_filter.as_script_object().unwrap(); | ||
define_properties_on(PROTO_DECLS, gc_context, object, fn_proto); | ||
blur_filter.into() | ||
let blur_filter_proto = ScriptObject::new(gc_context, Some(proto)); | ||
define_properties_on(PROTO_DECLS, gc_context, blur_filter_proto, fn_proto); | ||
FunctionObject::constructor( | ||
gc_context, | ||
Executable::Native(blur_filter_method!(0)), | ||
constructor_to_fn!(blur_filter_method!(0)), | ||
fn_proto, | ||
blur_filter_proto.into(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.