Skip to content

avm2: hitTestPoint should only use local coordinates for the player root #20054

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 21 additions & 4 deletions core/src/avm2/globals/flash/display/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,28 @@ pub fn hit_test_point<'gc>(
let y = args.get_f64(activation, 1)?;
let shape_flag = args.get_bool(2);

// Transform the coordinates from root to world space.
let avm_root = dobj.avm2_root();
let is_player_root = match avm_root {
// We can detect the root of the player by the fact that it doesn't have a loader.
Some(root) => root
.loader_info()
.as_ref()
.and_then(|loader_info| loader_info.as_loader_info_object())
.and_then(|loader_info_obj| loader_info_obj.loader())
.is_none(),
None => false,
};

let local = Point::from_pixels(x, y);
let global = dobj
.avm2_root()
.map_or(local, |root| root.local_to_global(local));
let global = if is_player_root {
// If this root is the root of the player, match avm1 hitTest behavior and treat the
// input as root-relative coordinates (even if no longer a direct child of the stage)
avm_root.unwrap().local_to_global(local)
} else {
// Otherwise, the docs are truthful and the coordinates are relative to the stage
// (aka nothing for us to do)
local
};

if shape_flag {
if !dobj.is_on_stage(activation.context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package {
import flash.display.MovieClip;

public class EmptyContainer extends MovieClip {
public function EmptyContainer() {
}
}
}
Binary file not shown.
56 changes: 56 additions & 0 deletions tests/tests/swfs/avm2/displayobject_hittestpoint_root/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;
import flash.net.URLRequest;

public class Test extends MovieClip {
public function Test() {
var shape = new Shape();
shape.name = "shape";
shape.graphics.beginFill(0xFF0000);
shape.graphics.drawRect(0, 0, 100, 100);
shape.graphics.endFill();

addChild(shape);

trace("// stage root - onscreen");
Print(shape);

trace("// stage root - offscreen");
var savedStage = this.stage;
savedStage.removeChild(this);
Print(shape);
savedStage.addChild(this);

var loader = new Loader();
loader.name = "loader";
loader.load(new URLRequest("EmptyContainer.swf"));

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e: Event) : void {
trace("// load complete");

var container: MovieClip = e.target.content;
//container.name = "container"; // makes fp angry.. okay
container.addChild(shape);

trace("// loader root - offscreen");
Print(shape);

addChild(container);

trace("// loader root - onscreen");
Print(shape);
});
}

public function Print(shape: Shape) : void
{
trace(shape.hitTestPoint(50, 50));
shape.root.x += 100;
trace(shape.hitTestPoint(50, 50));
shape.root.x -= 100;
}
}
}
13 changes: 13 additions & 0 deletions tests/tests/swfs/avm2/displayobject_hittestpoint_root/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// stage root - onscreen
true
true
// stage root - offscreen
true
true
// load complete
// loader root - offscreen
true
false
// loader root - onscreen
true
false
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_ticks = 2
Loading