Follow the GDScript style guide in every script
- Types on every parameter, return value and variable, and snake_case names (the player name limit is the constant MAX_NAME_LENGTH). - Spelling fixes: *_triggered, character_selected, separator, caught_errors. The server's catched_errors key stays as the server sends it. - Signals are connected with signal.connect() instead of by name. - Code is ordered signals, constants, exported vars, vars, @onready vars, functions, with @onready on one line and two blank lines between functions. - Every script starts with a ## description. Template comments, old LOC notes and commented-out code are gone, apart from the local play code. - Lines are at most 100 characters, apart from the local play path. - The screenshot tour turns off the blinking text cursor so pictures match.
This commit is contained in:
1 parent
674604425f
commit
a0428e3475
45 files changed
+580
-589
No files matched your search
+22
-12
@@ -1,19 +1,28 @@
|
||||
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
|
||||
|
||||
@export
|
||||
var LIMIT: int = 10
|
||||
var current_text = ''
|
||||
var cursor_line = 0
|
||||
var cursor_column = 0
|
||||
## The longest name a player can have.
|
||||
const MAX_NAME_LENGTH: int = 10
|
||||
|
||||
func _ready():
|
||||
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):
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventKey and event.pressed:
|
||||
if event.alt_pressed && event.keycode == KEY_A:
|
||||
close_pressed.emit()
|
||||
@@ -21,15 +30,16 @@ func _input(event):
|
||||
if event.keycode == KEY_ENTER || event.keycode == KEY_KP_ENTER:
|
||||
enter_key_pressed.emit()
|
||||
|
||||
func _on_text_changed():
|
||||
|
||||
func _on_text_changed() -> void:
|
||||
if text.contains("\n"):
|
||||
text = text.replace("\n", "")
|
||||
|
||||
var new_text : String = text
|
||||
if new_text.length() > LIMIT:
|
||||
|
||||
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
|
||||
# text, so move it back to where it was
|
||||
set_caret_line(cursor_line)
|
||||
set_caret_column(cursor_column)
|
||||
|
||||
|
||||
Reference in new issue
Block a user