extends Control
@onready var model_text: RichTextLabel = $RichTextLabel
@onready var user_input: TextEdit = $TextEdit
var original_text: String = """第一行范文内容
第二行继续范文
第三行结束"""
func _ready():
model_text.text= original_text
user_input.text_changed.connect(_on_text_changed)
func _on_text_changed():
var user_lines = user_input.text.split("\n")
var model_lines = original_text.split("\n")
var result_text := ""
for line_idx in range(model_lines.size()):
var model_line = model_lines[line_idx]
var user_line = user_lines[line_idx] if line_idx < user_lines.size() else ""
for char_idx in range(model_line.length()):
if char_idx < user_line.length():
if user_line[char_idx] == model_line[char_idx]:
result_text += "[color=green]%s[/color]" % model_line[char_idx]
else:
result_text += "[color=red]%s[/color]" % model_line[char_idx]
else:
result_text += model_line[char_idx]
if line_idx < model_lines.size() - 1:
result_text += "\n"
model_text.text= result_text
@onready var model_text: RichTextLabel = $RichTextLabel
@onready var user_input: TextEdit = $TextEdit
var original_text: String = """第一行范文内容
第二行继续范文
第三行结束"""
func _ready():
model_text.text= original_text
user_input.text_changed.connect(_on_text_changed)
func _on_text_changed():
var user_lines = user_input.text.split("\n")
var model_lines = original_text.split("\n")
var result_text := ""
for line_idx in range(model_lines.size()):
var model_line = model_lines[line_idx]
var user_line = user_lines[line_idx] if line_idx < user_lines.size() else ""
for char_idx in range(model_line.length()):
if char_idx < user_line.length():
if user_line[char_idx] == model_line[char_idx]:
result_text += "[color=green]%s[/color]" % model_line[char_idx]
else:
result_text += "[color=red]%s[/color]" % model_line[char_idx]
else:
result_text += model_line[char_idx]
if line_idx < model_lines.size() - 1:
result_text += "\n"
model_text.text= result_text