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

avm2: Implement InteractiveObject.focusRect and add tests #16061

Merged
merged 3 commits into from
Apr 29, 2024
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
30 changes: 19 additions & 11 deletions core/src/avm2/globals/flash/display/interactive_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::avm2::parameters::ParametersExt;
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::display_object::{TDisplayObject, TInteractiveObject};
use crate::{avm2_stub_getter, avm2_stub_setter};

/// Implements `flash.display.InteractiveObject`'s native instance constructor.
pub fn native_instance_init<'gc>(
Expand Down Expand Up @@ -179,24 +178,33 @@ pub fn set_tab_index<'gc>(
}

pub fn get_focus_rect<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
avm2_stub_getter!(activation, "flash.display.InteractiveObject", "focusRect");
Ok(Value::Null)
if let Some(obj) = this.as_display_object().and_then(|o| o.as_interactive()) {
Ok(obj.focus_rect().map(Value::Bool).unwrap_or(Value::Null))
} else {
Ok(Value::Null)
}
}

pub fn set_focus_rect<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
// NOTE: all values other than true or null are converted to false. (false/null do differ)

// let's only warn on true, as games sometimes just set focusRect to false for some reason.
if matches!(args.get(0), Some(Value::Bool(true))) {
avm2_stub_setter!(activation, "flash.display.InteractiveObject", "focusRect");
if let Some(obj) = this
.as_display_object()
.and_then(|this| this.as_interactive())
{
let value = match args.get(0) {
Some(Value::Bool(true)) => Some(true),
Some(Value::Null) => None,
// everything else sets focusRect to false
_ => Some(false),
};
obj.set_focus_rect(activation.context.gc(), value);
}

Ok(Value::Null)
Expand Down
89 changes: 89 additions & 0 deletions tests/tests/swfs/avm2/focusrect/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package {

import flash.display.DisplayObject;
import flash.display.InteractiveObject;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.FocusEvent;

[SWF(width="50", height="50", backgroundColor="#000000")]
public class Test extends MovieClip {
var clip1:Sprite;
var clip2:Sprite;
var testStage:int = 0;
var logFocus:Boolean = false;

public function Test() {
super();

this.clip1 = new Sprite();
this.clip1.x = 10;
this.clip1.y = 10;
this.clip1.graphics.beginFill(0xFF66CC);
this.clip1.graphics.drawRect(0, 0, 20, 20);

this.clip2 = new Sprite();
this.clip2.x = 20;
this.clip2.y = 20;
this.clip2.graphics.beginFill(0xFF66CC);
this.clip2.graphics.drawRect(0, 0, 20, 20);

this.clip1.name = "clip1";
this.clip1.tabEnabled = true;
this.clip1.tabIndex = 1;
this.clip2.name = "clip2";
this.clip2.tabEnabled = true;
this.clip2.tabIndex = 2;

this.stage.addChild(this.clip1);
this.stage.addChild(this.clip2);

var test:Test = this;
this.clip1.addEventListener("focusIn", function (evt:FocusEvent):void {
if (test.logFocus && evt.relatedObject != null && evt.target != null) {
trace("Focus changed: " + evt.relatedObject.name + " -> " + evt.target.name);
}
});
this.clip2.addEventListener("focusIn", function (evt:FocusEvent):void {
if (test.logFocus && evt.relatedObject != null && evt.target != null) {
trace("Focus changed: " + evt.relatedObject.name + " -> " + evt.target.name);
}
});
this.stage.addEventListener("keyDown", function(evt:KeyboardEvent) {
if (evt.keyCode == 27) {
test.nextTestStage();
}
});
}

function nextTestStage() {
this.testStage += 1;
trace("Setting test stage to " + this.testStage);
if (testStage == 1) {
this.stage.stageFocusRect = true;
} else if (testStage == 2) {
this.stage.stageFocusRect = false;
} else if (testStage == 3) {
this.stage.stageFocusRect = true;
this.clip1.focusRect = false;
} else if (testStage == 4) {
this.stage.stageFocusRect = false;
this.clip1.focusRect = true;
} else if (testStage == 5) {
this.stage.stageFocusRect = true;
this.clip1.focusRect = null;
} else if (testStage == 6) {
this.stage.stageFocusRect = false;
this.clip1.focusRect = null;
}
this.stage.focus = this.clip2;
this.logFocus = true;
}
}
}
34 changes: 34 additions & 0 deletions tests/tests/swfs/avm2/focusrect/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" },
{ "type": "KeyDown", "key_code": 9 },
{ "type": "Wait" }
]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/tests/swfs/avm2/focusrect/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Setting test stage to 1
Focus changed: clip2 -> clip1
Focus changed: clip1 -> clip2
Setting test stage to 2
Focus changed: clip2 -> clip1
Focus changed: clip1 -> clip2
Setting test stage to 3
Focus changed: clip2 -> clip1
Focus changed: clip1 -> clip2
Setting test stage to 4
Focus changed: clip2 -> clip1
Focus changed: clip1 -> clip2
Setting test stage to 5
Focus changed: clip2 -> clip1
Focus changed: clip1 -> clip2
Setting test stage to 6
Focus changed: clip2 -> clip1
Focus changed: clip1 -> clip2
Binary file added tests/tests/swfs/avm2/focusrect/test.swf
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/tests/swfs/avm2/focusrect/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
num_ticks = 12

image_comparisons."output.01a".trigger = 1
image_comparisons."output.01b".trigger = 2
image_comparisons."output.02a".trigger = 3
image_comparisons."output.02b".trigger = 4
image_comparisons."output.03a".trigger = 5
image_comparisons."output.03b".trigger = 6
image_comparisons."output.04a".trigger = 7
image_comparisons."output.04b".trigger = 8
image_comparisons."output.05a".trigger = 9
image_comparisons."output.05b".trigger = 10
image_comparisons."output.06a".trigger = 11
image_comparisons."output.06b".trigger = 12

[player_options]
with_renderer = { optional = false, sample_count = 1 }
76 changes: 76 additions & 0 deletions tests/tests/swfs/avm2/focusrect_property/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package {

import flash.display.DisplayObject;
import flash.display.InteractiveObject;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.FocusEvent;

public class Test extends MovieClip {
public function Test() {
super();

var text1:TextField = new TextField();
var text2:TextField = new TextField();
text2.type = "input";
var button:SimpleButton = new SimpleButton();
var mc1:MovieClip = new MovieClip();
var mc2:MovieClip = new MovieClip();
mc2.buttonMode = true;
var sprite:Sprite = new Sprite();

trace("===== stage =====");
this.testProperty(this.stage);
trace("===== text1 =====");
this.testProperty(text1);
trace("===== text2 =====");
this.testProperty(text2);
trace("===== button =====");
this.testProperty(button);
trace("===== mc1 =====");
this.testProperty(mc1);
trace("===== mc2 =====");
this.testProperty(mc2);
trace("===== sprite =====");
this.testProperty(sprite);
}

function logError(f:*):void {
try {
f();
} catch (error:Error) {
trace(' Error: ' + error);
}
}

function testProperty(obj:InteractiveObject):void {
trace(" default value: " + obj.focusRect);
this.testPropertyValue(obj, true);
this.testPropertyValue(obj, false);
this.testPropertyValue(obj, null);
this.testPropertyValue(obj, undefined);
this.testPropertyValue(obj, 0);
this.testPropertyValue(obj, 1);
this.testPropertyValue(obj, -1);
this.testPropertyValue(obj, 0.2);
this.testPropertyValue(obj, 'test');
this.testPropertyValue(obj, 1.0/0.0);
this.testPropertyValue(obj, 0.0/0.0);
this.testPropertyValue(obj, new Object());
}

function testPropertyValue(obj:InteractiveObject, value:*):void {
this.logError(function() {
obj.focusRect = value;
});
trace(" after set to " + value + ": " + obj.focusRect);

}
}
}
Loading