main/ holds the main window and top bar, quiz/ the quiz and its parts, player/ the music player, popups/ every popup and what they share, data/ the songs, players, playlist and settings, server/ the MusicServer client, common/ the list row and GameLog, and themes/ the themes and style boxes.
50 lines
1.3 KiB
GDScript
50 lines
1.3 KiB
GDScript
class_name PlayerNameField
|
|
extends TextEdit
|
|
## The field for a new player's name. It keeps to one line of at most
|
|
## [constant MAX_NAME_LENGTH] characters, and reports Enter so the player can be added.
|
|
|
|
## Emitted when Enter is pressed while the field has focus.
|
|
|
|
signal enter_key_pressed
|
|
|
|
## Emitted when Alt+A is pressed.
|
|
signal close_pressed
|
|
|
|
## The longest name a player can have.
|
|
const MAX_NAME_LENGTH: int = 10
|
|
|
|
var current_text: String = ""
|
|
var cursor_line: int = 0
|
|
var cursor_column: int = 0
|
|
|
|
|
|
func _ready() -> void:
|
|
text_changed.connect(_on_text_changed)
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventKey and event.pressed:
|
|
if event.alt_pressed && event.keycode == KEY_A:
|
|
close_pressed.emit()
|
|
if has_focus():
|
|
if event.keycode == KEY_ENTER || event.keycode == KEY_KP_ENTER:
|
|
enter_key_pressed.emit()
|
|
|
|
|
|
func _on_text_changed() -> void:
|
|
if text.contains("\n"):
|
|
text = text.replace("\n", "")
|
|
|
|
var new_text: String = text
|
|
if new_text.length() > MAX_NAME_LENGTH:
|
|
text = current_text
|
|
# when replacing the text, the cursor will get moved to the beginning of the
|
|
# text, so move it back to where it was
|
|
set_caret_line(cursor_line)
|
|
set_caret_column(cursor_column)
|
|
|
|
current_text = text
|
|
# save current position of cursor for when we have reached the limit
|
|
cursor_line = get_caret_line()
|
|
cursor_column = get_caret_column()
|