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 flash.filters.BitmapFilter and flash.filters.BlurFilter avm1 built-ins #1041

Merged
merged 5 commits into from
Sep 2, 2020
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
44 changes: 44 additions & 0 deletions core/src/avm1/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::f64;

mod array;
pub(crate) mod as_broadcaster;
mod bitmap_filter;
mod blur_filter;
pub(crate) mod boolean;
pub(crate) mod button;
mod color;
Expand Down Expand Up @@ -333,6 +335,10 @@ pub struct SystemPrototypes<'gc> {
pub context_menu_constructor: Object<'gc>,
pub context_menu_item: Object<'gc>,
pub context_menu_item_constructor: Object<'gc>,
pub bitmap_filter: Object<'gc>,
pub bitmap_filter_constructor: Object<'gc>,
pub blur_filter: Object<'gc>,
pub blur_filter_constructor: Object<'gc>,
}

/// Initialize default global scope and builtins for an AVM1 instance.
Expand Down Expand Up @@ -485,6 +491,8 @@ pub fn create_globals<'gc>(

let flash = ScriptObject::object(gc_context, Some(object_proto));
let geom = ScriptObject::object(gc_context, Some(object_proto));
let filters = ScriptObject::object(gc_context, Some(object_proto));

let matrix = matrix::create_matrix_object(gc_context, matrix_proto, Some(function_proto));
let point = point::create_point_object(gc_context, point_proto, Some(function_proto));
let rectangle =
Expand All @@ -503,6 +511,7 @@ pub fn create_globals<'gc>(
);

flash.define_value(gc_context, "geom", geom.into(), EnumSet::empty());
flash.define_value(gc_context, "filters", filters.into(), EnumSet::empty());
geom.define_value(gc_context, "Matrix", matrix.into(), EnumSet::empty());
geom.define_value(gc_context, "Point", point.into(), EnumSet::empty());
geom.define_value(gc_context, "Rectangle", rectangle.into(), EnumSet::empty());
Expand All @@ -514,6 +523,37 @@ pub fn create_globals<'gc>(
);
geom.define_value(gc_context, "Transform", transform.into(), EnumSet::empty());

let bitmap_filter_proto =
bitmap_filter::create_proto(gc_context, object_proto, Some(function_proto));
let bitmap_filter = FunctionObject::constructor(
gc_context,
Executable::Native(bitmap_filter::constructor),
Some(function_proto),
bitmap_filter_proto,
);

let blur_filter_proto =
blur_filter::create_proto(gc_context, bitmap_filter_proto, function_proto);
let blur_filter = FunctionObject::constructor(
gc_context,
Executable::Native(blur_filter::constructor),
Some(function_proto),
blur_filter_proto,
);

filters.define_value(
gc_context,
"BitmapFilter",
bitmap_filter.into(),
EnumSet::empty(),
);
filters.define_value(
gc_context,
"BlurFilter",
blur_filter.into(),
EnumSet::empty(),
);

let mut globals = ScriptObject::bare_object(gc_context);
globals.define_value(
gc_context,
Expand Down Expand Up @@ -766,6 +806,10 @@ pub fn create_globals<'gc>(
context_menu_constructor: context_menu,
context_menu_item: context_menu_item_proto,
context_menu_item_constructor: context_menu_item,
bitmap_filter: bitmap_filter_proto,
bitmap_filter_constructor: bitmap_filter,
blur_filter: blur_filter_proto,
blur_filter_constructor: blur_filter,
},
globals.into(),
broadcaster_functions,
Expand Down
35 changes: 35 additions & 0 deletions core/src/avm1/globals/bitmap_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! flash.filter.BitmapFilter object

use crate::avm1::activation::Activation;
use crate::avm1::error::Error;
use crate::avm1::{Object, ScriptObject, Value};
use enumset::EnumSet;
use gc_arena::MutationContext;

pub fn constructor<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
_this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Undefined)
}

pub fn clone<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
_this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Undefined)
}

pub fn create_proto<'gc>(
gc_context: MutationContext<'gc, '_>,
proto: Object<'gc>,
fn_proto: Option<Object<'gc>>,
) -> Object<'gc> {
let mut object = ScriptObject::object(gc_context, Some(proto));

object.force_set_function("clone", clone, gc_context, EnumSet::empty(), fn_proto);

object.into()
}
201 changes: 201 additions & 0 deletions core/src/avm1/globals/blur_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//! flash.filter.BlurFilter object

use crate::avm1::activation::Activation;
use crate::avm1::error::Error;
use crate::avm1::function::{Executable, FunctionObject};
use crate::avm1::object::blur_filter::BlurFilterObject;
use crate::avm1::{Object, TObject, Value};
use enumset::EnumSet;
use gc_arena::MutationContext;

pub fn constructor<'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.max(0.0).min(255.0))?;

let blur_y = args
.get(1)
.unwrap_or(&4.into())
.coerce_to_f64(activation)
.map(|x| x.max(0.0).min(255.0))?;

let quality = args
.get(2)
.unwrap_or(&1.into())
.coerce_to_i32(activation)
.map(|x| x.max(0).min(15))?;

let blur_filter = this.as_blur_filter_object().unwrap();

blur_filter.set_blur_x(activation.context.gc_context, blur_x);
blur_filter.set_blur_y(activation.context.gc_context, blur_y);
blur_filter.set_quality(activation.context.gc_context, quality);

Ok(Value::Undefined)
}

pub fn clone<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let proto = activation.context.avm1.prototypes.blur_filter_constructor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone doesn't seem to be overridden in the filter subclasses. (Does it copy the underlying object instead?)

import flash.filters.*;
trace(BlurFilter.prototype.clone == BitmapFilter.prototype.clone);
var bf = new BlurFilter(10, 10);
var bf2 = BitmapFilter.prototype.clone.apply(bf);
trace(bf2.blurX);

Flash: true, 10
Ruffle: false, undefined


let blur_x = this.get("blurX", activation)?;
let blur_y = this.get("blurY", activation)?;
let quality = this.get("quality", activation)?;

let cloned = proto.construct(activation, &[blur_x, blur_y, quality])?;
Ok(cloned.into())
}

pub fn get_blur_x<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(this.as_blur_filter_object().unwrap().get_blur_x().into())
}

pub 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(&Value::Undefined)
.coerce_to_f64(activation)
.map(|x| x.max(0.0).min(255.0))?;

this.as_blur_filter_object()
.unwrap()
.set_blur_x(activation.context.gc_context, blur_x);

Ok(Value::Undefined)
}

pub fn get_blur_y<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(this.as_blur_filter_object().unwrap().get_blur_y().into())
}

pub 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(&Value::Undefined)
.coerce_to_f64(activation)
.map(|x| x.max(0.0).min(255.0))?;

this.as_blur_filter_object()
.unwrap()
.set_blur_y(activation.context.gc_context, blur_y);

Ok(Value::Undefined)
}

pub fn get_quality<'gc>(
_activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
Ok(this.as_blur_filter_object().unwrap().get_quality().into())
}

pub fn set_quality<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let blur_y = args
.get(0)
.unwrap_or(&Value::Undefined)
.coerce_to_i32(activation)
.map(|x| x.max(0).min(15))?;

this.as_blur_filter_object()
.unwrap()
.set_quality(activation.context.gc_context, blur_y);

Ok(Value::Undefined)
}

pub fn create_proto<'gc>(
gc_context: MutationContext<'gc, '_>,
proto: Object<'gc>,
fn_proto: Object<'gc>,
) -> Object<'gc> {
let blur_filter = BlurFilterObject::empty_object(gc_context, Some(proto));
let mut object = blur_filter.as_script_object().unwrap();

object.force_set_function("clone", clone, gc_context, EnumSet::empty(), Some(fn_proto));

object.add_property(
gc_context,
"blurX",
FunctionObject::function(
gc_context,
Executable::Native(get_blur_x),
Some(fn_proto),
fn_proto,
),
Some(FunctionObject::function(
gc_context,
Executable::Native(set_blur_x),
Some(fn_proto),
fn_proto,
)),
EnumSet::empty(),
);

object.add_property(
gc_context,
"blurY",
FunctionObject::function(
gc_context,
Executable::Native(get_blur_y),
Some(fn_proto),
fn_proto,
),
Some(FunctionObject::function(
gc_context,
Executable::Native(set_blur_y),
Some(fn_proto),
fn_proto,
)),
EnumSet::empty(),
);

object.add_property(
gc_context,
"quality",
FunctionObject::function(
gc_context,
Executable::Native(get_quality),
Some(fn_proto),
fn_proto,
),
Some(FunctionObject::function(
gc_context,
Executable::Native(set_quality),
Some(fn_proto),
fn_proto,
)),
EnumSet::empty(),
);

blur_filter.into()
}
8 changes: 8 additions & 0 deletions core/src/avm1/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::avm1::object::value_object::ValueObject;
use crate::avm1::property::Attribute;

use crate::avm1::activation::Activation;
use crate::avm1::object::blur_filter::BlurFilterObject;
use crate::avm1::object::color_transform_object::ColorTransformObject;
use crate::avm1::object::transform_object::TransformObject;
use crate::avm1::object::xml_attributes_object::XMLAttributesObject;
Expand All @@ -23,6 +24,7 @@ use ruffle_macros::enum_trait_object;
use std::borrow::Cow;
use std::fmt::Debug;

pub mod blur_filter;
pub mod color_transform_object;
mod custom_object;
pub mod script_object;
Expand Down Expand Up @@ -54,6 +56,7 @@ pub mod xml_object;
SharedObject(SharedObject<'gc>),
ColorTransformObject(ColorTransformObject<'gc>),
TransformObject(TransformObject<'gc>),
BlurFilterObject(BlurFilterObject<'gc>),
}
)]
pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy {
Expand Down Expand Up @@ -425,6 +428,11 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
None
}

/// Get the underlying `BlurFilterObject`, if it exists
fn as_blur_filter_object(&self) -> Option<BlurFilterObject<'gc>> {
None
}

fn as_ptr(&self) -> *const ObjectPtr;

/// Check if this object is in the prototype chain of the specified test object.
Expand Down
Loading