avm2: Implement `Transform.pixelBounds`

This commit is contained in:
Udeshya 2023-04-03 00:25:10 +01:00 committed by GitHub
parent f0a8e50be1
commit 34775965ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 137 additions and 1 deletions

View File

@ -15,5 +15,6 @@ package flash.geom {
public native function get concatenatedColorTransform():ColorTransform;
public native function get concatenatedMatrix():Matrix;
public native function get pixelBounds():Rectangle;
}
}

View File

@ -5,7 +5,7 @@ use crate::avm2_stub_getter;
use crate::display_object::TDisplayObject;
use crate::prelude::{DisplayObject, Matrix, Twips};
use ruffle_render::quality::StageQuality;
use swf::{ColorTransform, Fixed8};
use swf::{ColorTransform, Fixed8, Rectangle};
fn get_display_object<'gc>(
this: Object<'gc>,
@ -246,3 +246,29 @@ pub fn object_to_matrix<'gc>(
Ok(Matrix { a, b, c, d, tx, ty })
}
pub fn get_pixel_bounds<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let this = this.unwrap();
let display_object = get_display_object(this, activation)?;
rectangle_to_object(display_object.world_bounds(), activation)
}
fn rectangle_to_object<'gc>(
rectangle: Rectangle<Twips>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
let object = activation.avm2().classes().rectangle.construct(
activation,
&[
rectangle.x_min.to_pixels().into(),
rectangle.y_min.to_pixels().into(),
rectangle.width().to_pixels().into(),
rectangle.height().to_pixels().into(),
],
)?;
Ok(object.into())
}

View File

@ -0,0 +1,109 @@
package
{
import flash.display.*;
import flash.geom.Transform;
import flash.text.*;
public dynamic class Test extends MovieClip
{
public function Test()
{
super();
addFrameScript(0,this.frame1);
}
public function run(stage:Stage)
{
var firstTransform;
var secondTransform;
var tempMat;
var tempColor;
var otherMat;
var otherColor;
var printTransform = function(trans:Transform)
{
trace("colorTransform=" + trans.colorTransform);
trace("matrix=" + trans.matrix);
trace("concatenatedMatrix=" + trans.concatenatedMatrix);
};
var checkQuals = function(stage:Stage, child:DisplayObject)
{
var i = undefined;
var qual = undefined;
var quals = ["best","high","16x16","16x16linear","8x8","8x8linear","low","medium"];
for(i in quals)
{
qual = quals[i];
stage.quality = qual;
trace(qual + " TextField " + child.transform.concatenatedMatrix);
trace(qual + " stage" + stage.transform.concatenatedMatrix);
}
};
var firstParent:Sprite = new Sprite();
var secondParent:Sprite = new Sprite();
var child:TextField = new TextField();
firstParent.addChild(child);
trace("Checking stage qualities with non-stage child");
checkQuals(stage,child);
stage.addChild(firstParent);
trace("");
trace("Checking stage qualities with child on stage");
checkQuals(stage,child);
trace("// child.transform == child.transform");
trace(child.transform == child.transform);
firstTransform = child.transform;
secondTransform = child.transform;
trace("// firstTransform");
printTransform(firstTransform);
trace("// secondTransform");
printTransform(secondTransform);
firstTransform.matrix.a = 99;
firstTransform.colorTransform.redOffset = 249;
trace("// firstTransform after no-op modifications");
printTransform(firstTransform);
tempMat = firstTransform.matrix;
tempMat.a = 42;
firstTransform.matrix = tempMat;
trace("// firstTransform after matrix modification");
printTransform(firstTransform);
trace("// secondTransform");
printTransform(secondTransform);
tempColor = child.transform.colorTransform;
tempColor.redOffset = 12;
child.transform.colorTransform = tempColor;
trace("// firstTransform after color modification");
printTransform(firstTransform);
trace("// secondTransform");
printTransform(secondTransform);
otherMat = secondParent.transform.matrix;
otherColor = secondParent.transform.colorTransform;
otherMat.a = otherMat.b = otherMat.c = otherMat.d = 42;
otherColor.redMultiplier = otherColor.greenMultiplier = otherColor.blueMultiplier = 3;
secondParent.transform.matrix = otherMat;
secondParent.transform.colorTransform = otherColor;
secondParent.addChild(child);
trace("// firstTransform after setting parent");
printTransform(firstTransform);
trace("// secondTransform after setting parent");
printTransform(secondTransform);
firstParent.addChild(secondParent);
stage.addChild(firstParent);
trace("// firstTransform after indirectly added to stage");
printTransform(firstTransform);
trace("// secondTransform after indirectly added to stage");
printTransform(secondTransform);
stage.removeChild(firstParent);
trace("// firstTransform after indirectly removed from stage");
printTransform(firstTransform);
trace("// secondTransform after indirectly removed forrm stage");
printTransform(secondTransform);
}
internal function frame1() : *
{
this.run(stage);
}
}
}