Titles, result text and details (card, inspector, Lore Entries) get red wavy lines under misspelled words; right-click gives suggestions, Add to dictionary (saved with the spread) and Ignore (this session). Uses the Godot-Hunspell add-on's SpellChecker when it is installed; off on Android and when the add-on is missing. English (en_US) dictionary included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
216 lines
6.7 KiB
GDScript
216 lines
6.7 KiB
GDScript
class_name SpellUnderline
|
|
extends Control
|
|
## Red wavy lines under misspelled words in one LineEdit or TextEdit (Build
|
|
## only), laid over the field. Right-clicking such a word opens a menu of
|
|
## suggestions, "Add to dictionary" (the spread's own words) and "Ignore",
|
|
## then the usual Cut, Copy and Paste. Spellcheck does the checking.
|
|
|
|
const GROUP := "spell_underline"
|
|
const WAVE := 2.0
|
|
const ID_ADD := 100
|
|
const ID_IGNORE := 101
|
|
const ID_CUT := 102
|
|
const ID_COPY := 103
|
|
const ID_PASTE := 104
|
|
|
|
var field: Control
|
|
var m: Main
|
|
## Misspelled word ranges in the field's text, from the last check.
|
|
var bad: Array[Vector2i] = []
|
|
var _menu: PopupMenu
|
|
var _menu_range := Vector2i(-1, -1)
|
|
|
|
|
|
## Adds underlines to `field`, if spellchecking is available.
|
|
static func attach(field: Control, p_main: Main) -> void:
|
|
if p_main == null or field.has_meta("spell") or not Spellcheck.available():
|
|
return
|
|
var u := SpellUnderline.new()
|
|
u.field = field
|
|
u.m = p_main
|
|
field.set_meta("spell", u)
|
|
field.add_child(u)
|
|
|
|
|
|
func _ready() -> void:
|
|
set_anchors_and_offsets_preset(PRESET_FULL_RECT)
|
|
mouse_filter = MOUSE_FILTER_IGNORE
|
|
add_to_group(GROUP)
|
|
if field is LineEdit:
|
|
(field as LineEdit).text_changed.connect(func(_s: String) -> void: recheck())
|
|
else:
|
|
(field as TextEdit).text_changed.connect(recheck)
|
|
(field as TextEdit).get_v_scroll_bar().value_changed.connect(func(_v: float) -> void: queue_redraw())
|
|
field.resized.connect(queue_redraw)
|
|
field.gui_input.connect(_on_field_input)
|
|
recheck()
|
|
|
|
|
|
## The field's text scrolls as the caret moves, so a focused field redraws.
|
|
func _process(_delta: float) -> void:
|
|
if field.has_focus():
|
|
queue_redraw()
|
|
|
|
|
|
func text() -> String:
|
|
return (field as LineEdit).text if field is LineEdit else (field as TextEdit).text
|
|
|
|
|
|
func recheck() -> void:
|
|
bad = Spellcheck.misspelled(text(), m.data) if m.mode == Main.Mode.BUILD else ([] as Array[Vector2i])
|
|
queue_redraw()
|
|
|
|
|
|
func _draw() -> void:
|
|
for r in bad:
|
|
for seg in _segments(r):
|
|
_wave(seg[0], seg[1], seg[2])
|
|
|
|
|
|
## Wavy line from x0 to x1 at height y.
|
|
func _wave(x0: float, x1: float, y: float) -> void:
|
|
var pts := PackedVector2Array()
|
|
var x := x0
|
|
var up := true
|
|
while x <= x1:
|
|
pts.append(Vector2(x, y + (-WAVE if up else WAVE) * 0.5))
|
|
x += WAVE
|
|
up = not up
|
|
if pts.size() >= 2:
|
|
draw_polyline(pts, Style.DANGER, 1.2, true)
|
|
|
|
|
|
## [x0, x1, y] pieces under range `r` (one per line it covers).
|
|
func _segments(r: Vector2i) -> Array:
|
|
if field is LineEdit:
|
|
var y := size.y - _line_margin_bottom()
|
|
return [[_line_x(r.x), _line_x(r.y), y]]
|
|
var edit := field as TextEdit
|
|
var out: Array = []
|
|
for i in range(r.x, r.y):
|
|
var lc := _line_col(i)
|
|
var rect := edit.get_rect_at_line_column(lc.x, lc.y)
|
|
if rect.size.x <= 0:
|
|
continue
|
|
var y := float(rect.position.y + rect.size.y) - 1.0
|
|
if not out.is_empty() and is_equal_approx(out[-1][2], y):
|
|
out[-1][1] = float(rect.position.x + rect.size.x)
|
|
else:
|
|
out.append([float(rect.position.x), float(rect.position.x + rect.size.x), y])
|
|
return out
|
|
|
|
|
|
func _line_margin_bottom() -> float:
|
|
var sb := field.get_theme_stylebox("normal")
|
|
return maxf(3.0, sb.get_margin(SIDE_BOTTOM) * 0.6) if sb else 4.0
|
|
|
|
|
|
## LineEdit: the x of the gap before character `column`.
|
|
func _line_x(column: int) -> float:
|
|
var line := field as LineEdit
|
|
var font := line.get_theme_font("font")
|
|
var font_size := line.get_theme_font_size("font_size")
|
|
var sb := line.get_theme_stylebox("normal")
|
|
var left := sb.get_margin(SIDE_LEFT) if sb else 0.0
|
|
var scroll: float = line.call("get_scroll_offset") if line.has_method("get_scroll_offset") else 0.0
|
|
return left - scroll + font.get_string_size(line.text.substr(0, column), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x
|
|
|
|
|
|
## TextEdit: Vector2i(line, column) of character offset `at`.
|
|
func _line_col(at: int) -> Vector2i:
|
|
var edit := field as TextEdit
|
|
var rest := at
|
|
for i in edit.get_line_count():
|
|
var n := edit.get_line(i).length()
|
|
if rest <= n:
|
|
return Vector2i(i, rest)
|
|
rest -= n + 1
|
|
return Vector2i(edit.get_line_count() - 1, 0)
|
|
|
|
|
|
## The character offset under a point in the field.
|
|
func offset_at(p: Vector2) -> int:
|
|
if field is LineEdit:
|
|
var best := 0
|
|
for column in (field as LineEdit).text.length() + 1:
|
|
if _line_x(column) <= p.x:
|
|
best = column
|
|
return best
|
|
var edit := field as TextEdit
|
|
var lc := edit.get_line_column_at_pos(Vector2i(p), false)
|
|
var offset := 0
|
|
for i in lc.y:
|
|
offset += edit.get_line(i).length() + 1
|
|
return offset + lc.x
|
|
|
|
|
|
func _on_field_input(ev: InputEvent) -> void:
|
|
var mb := ev as InputEventMouseButton
|
|
if mb == null or not mb.pressed or mb.button_index != MOUSE_BUTTON_RIGHT:
|
|
return
|
|
var at := offset_at(mb.position)
|
|
for r in bad:
|
|
if at >= r.x and at <= r.y:
|
|
field.accept_event()
|
|
open_menu(r, field.get_screen_position() + mb.position)
|
|
return
|
|
|
|
|
|
## The spelling menu for word range `r`, at a screen position.
|
|
func open_menu(r: Vector2i, at: Vector2) -> void:
|
|
if _menu == null:
|
|
_menu = PopupMenu.new()
|
|
ModalCard.style_popup(_menu)
|
|
_menu.id_pressed.connect(_on_menu)
|
|
add_child(_menu)
|
|
_menu.clear()
|
|
_menu_range = r
|
|
var word := text().substr(r.x, r.y - r.x)
|
|
var sugs := Spellcheck.suggestions(word)
|
|
for i in sugs.size():
|
|
_menu.add_item(sugs[i], i)
|
|
if sugs.is_empty():
|
|
_menu.add_item("No suggestions", 99)
|
|
_menu.set_item_disabled(_menu.get_item_index(99), true)
|
|
_menu.add_separator()
|
|
_menu.add_item("Add “%s” to dictionary" % word, ID_ADD)
|
|
_menu.set_item_tooltip(_menu.get_item_index(ID_ADD), "This spread accepts the word from now on (it travels with the spread)")
|
|
_menu.add_item("Ignore", ID_IGNORE)
|
|
_menu.set_item_tooltip(_menu.get_item_index(ID_IGNORE), "Accept it until the app closes")
|
|
_menu.add_separator()
|
|
_menu.add_item("Cut", ID_CUT)
|
|
_menu.add_item("Copy", ID_COPY)
|
|
_menu.add_item("Paste", ID_PASTE)
|
|
_menu.popup(Rect2i(Vector2i(at), Vector2i.ZERO))
|
|
|
|
|
|
func _on_menu(id: int) -> void:
|
|
var word := text().substr(_menu_range.x, _menu_range.y - _menu_range.x)
|
|
match id:
|
|
ID_ADD:
|
|
Spellcheck.add_to_dictionary(m, word)
|
|
ID_IGNORE:
|
|
Spellcheck.ignore(m, word)
|
|
ID_CUT:
|
|
field.call("menu_option", LineEdit.MENU_CUT if field is LineEdit else TextEdit.MENU_CUT)
|
|
ID_COPY:
|
|
field.call("menu_option", LineEdit.MENU_COPY if field is LineEdit else TextEdit.MENU_COPY)
|
|
ID_PASTE:
|
|
field.call("menu_option", LineEdit.MENU_PASTE if field is LineEdit else TextEdit.MENU_PASTE)
|
|
_:
|
|
if id >= 0 and id < Spellcheck.MAX_SUGGESTIONS:
|
|
replace(_menu_range, _menu.get_item_text(_menu.get_item_index(id)))
|
|
|
|
|
|
## Puts `word` in place of range `r` and tells the field's listeners.
|
|
func replace(r: Vector2i, word: String) -> void:
|
|
var new_text := SpellWords.replace(text(), r, word)
|
|
if field is LineEdit:
|
|
var line := field as LineEdit
|
|
line.text = new_text
|
|
line.caret_column = r.x + word.length()
|
|
line.text_changed.emit(line.text)
|
|
else:
|
|
var edit := field as TextEdit
|
|
edit.text = new_text
|
|
edit.text_changed.emit()
|