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

avm1,avm2: Implement TextField.restrict #14634

Merged
merged 5 commits into from
Jan 11, 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
34 changes: 34 additions & 0 deletions core/src/avm1/globals/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"maxChars" => property(tf_getter!(max_chars), tf_setter!(set_max_chars));
"multiline" => property(tf_getter!(multiline), tf_setter!(set_multiline));
"password" => property(tf_getter!(password), tf_setter!(set_password));
"restrict" => property(tf_getter!(restrict), tf_setter!(set_restrict));
"scroll" => property(tf_getter!(scroll), tf_setter!(set_scroll));
"selectable" => property(tf_getter!(selectable), tf_setter!(set_selectable));
"text" => property(tf_getter!(text), tf_setter!(set_text));
Expand Down Expand Up @@ -859,3 +860,36 @@ fn set_filters<'gc>(
this.set_filters(activation.context.gc_context, filters);
Ok(())
}

fn restrict<'gc>(
this: EditText<'gc>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
match this.restrict() {
Some(value) => Ok(AvmString::new(activation.context.gc_context, value).into()),
None => Ok(Value::Null),
}
}

fn set_restrict<'gc>(
this: EditText<'gc>,
activation: &mut Activation<'_, 'gc>,
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
match value {
Value::Undefined | Value::Null => {
this.set_restrict(None, &mut activation.context);
}
_ => {
let text = value.coerce_to_string(activation)?;
if text.is_empty() {
// According to docs, an empty string means that you cannot enter any character,
// but according to reality, an empty string is equivalent to null in AVM1.
torokati44 marked this conversation as resolved.
Show resolved Hide resolved
this.set_restrict(None, &mut activation.context);
} else {
this.set_restrict(Some(&text), &mut activation.context);
}
}
};
Ok(())
}
29 changes: 23 additions & 6 deletions core/src/avm2/globals/flash/text/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,18 +1325,35 @@ pub fn set_mouse_wheel_enabled<'gc>(

pub fn get_restrict<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
avm2_stub_getter!(activation, "flash.text.TextField", "restrict");
Ok(Value::Null)
if let Some(this) = this
.as_display_object()
.and_then(|this| this.as_edit_text())
{
return match this.restrict() {
Some(value) => Ok(AvmString::new(activation.context.gc_context, value).into()),
None => Ok(Value::Null),
};
}

Ok(Value::Undefined)
}

pub fn set_restrict<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
_args: &[Value<'gc>],
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
avm2_stub_setter!(activation, "flash.text.TextField", "restrict");
if let Some(this) = this
.as_display_object()
.and_then(|this| this.as_edit_text())
{
this.set_restrict(
args.try_get_string(activation, 0)?.as_deref(),
&mut activation.context,
);
}
Ok(Value::Undefined)
}
Loading