avm2: Implement TextField.getLineOffset()

This commit is contained in:
Kamil Jarosz 2024-06-27 15:22:43 +02:00 committed by Nathan Adams
parent 57b371900b
commit 9a6708f4b1
3 changed files with 33 additions and 5 deletions

View File

@ -148,6 +148,8 @@ package flash.text {
public native function getLineText(lineIndex:int):String;
public native function getLineOffset(lineIndex:int):int;
public function getCharBoundaries(charIndex:int):Rectangle {
stub_method("flash.text.TextField", "getCharBoundaries");
return new Rectangle(0, 0, 1, 1);
@ -173,11 +175,6 @@ package flash.text {
return 0;
}
public function getLineOffset(lineIndex:int):int {
stub_method("flash.text.TextField", "getLineOffset");
return 0;
}
public function getParagraphLength(charIndex:int):int {
stub_method("flash.text.TextField", "getParagraphLength");
return 0;

View File

@ -1191,6 +1191,30 @@ pub fn get_line_text<'gc>(
Ok(Value::Undefined)
}
pub fn get_line_offset<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let Some(this) = this
.as_display_object()
.and_then(|this| this.as_edit_text())
else {
return Ok(Value::Undefined);
};
let line_num = args.get_i32(activation, 0)?;
if line_num < 0 {
return Err(make_error_2006(activation));
}
return if let Some(offset) = this.line_offset(line_num as usize) {
Ok(offset.into())
} else {
Err(make_error_2006(activation))
};
}
pub fn get_bottom_scroll_v<'gc>(
_activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,

View File

@ -1934,6 +1934,13 @@ impl<'gc> EditText<'gc> {
Some(line_text)
}
pub fn line_offset(self, line: usize) -> Option<usize> {
let read = self.0.read();
let line = read.layout.lines().get(line)?;
let first_box = line.boxes_iter().next()?;
Some(first_box.start())
}
fn execute_avm1_asfunction(
self,
context: &mut UpdateContext<'_, 'gc>,