avm2: Implement TextField.getFirstCharInParagraph

This commit is contained in:
Kamil Jarosz 2024-07-18 12:54:11 +02:00 committed by Nathan Adams
parent e9b8cfae1a
commit 670e5f9ac2
3 changed files with 40 additions and 4 deletions

View File

@ -153,10 +153,7 @@ package flash.text {
return new Rectangle(0, 0, 1, 1); return new Rectangle(0, 0, 1, 1);
} }
public function getFirstCharInParagraph(charIndex:int):int { public native function getFirstCharInParagraph(charIndex:int):int;
stub_method("flash.text.TextField", "getFirstCharInParagraph");
return 0;
}
public function getImageReference(id:String):DisplayObject { public function getImageReference(id:String):DisplayObject {
stub_method("flash.text.TextField", "getImageReference"); stub_method("flash.text.TextField", "getImageReference");

View File

@ -1602,3 +1602,27 @@ pub fn get_line_index_at_point<'gc>(
Ok(Value::Number(-1f64)) Ok(Value::Number(-1f64))
} }
} }
pub fn get_first_char_in_paragraph<'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 char_index = args.get_i32(activation, 0)?;
if char_index < 0 {
return Ok((-1).into());
}
Ok(this
.paragraph_start_index_at(char_index as usize)
.map(|i| i as i32)
.unwrap_or(-1)
.into())
}

View File

@ -1950,6 +1950,21 @@ impl<'gc> EditText<'gc> {
self.0.read().layout.find_line_index_by_position(index) self.0.read().layout.find_line_index_by_position(index)
} }
pub fn paragraph_start_index_at(self, mut index: usize) -> Option<usize> {
let text = self.text();
// Note that the index may equal the text length
if index > text.len() {
return None;
}
while index > 0 && !string_utils::swf_is_newline(text.at(index - 1)) {
index -= 1;
}
Some(index)
}
fn execute_avm1_asfunction( fn execute_avm1_asfunction(
self, self,
context: &mut UpdateContext<'_, 'gc>, context: &mut UpdateContext<'_, 'gc>,