add server address history

This commit is contained in:
Teatov 2025-02-13 21:16:31 +10:00
parent 030fa8bfaf
commit 6a02e40f01
2 changed files with 39 additions and 2 deletions

View File

@ -78,7 +78,6 @@ size_flags_horizontal = 3
focus_neighbor_top = NodePath("../NameEdit")
focus_neighbor_right = NodePath("../JoinButton")
theme_override_font_sizes/font_size = 26
text = "127.0.0.1"
[node name="JoinButton" type="Button" parent="Menu/PlayMenu/MarginContainer/GridContainer"]
layout_mode = 2

View File

@ -1,5 +1,10 @@
extends Panel
const SERVER_HISTORY_PATH := "user://server_history"
var _server_history: PackedStringArray = []
var _history_position: int = 0
@onready var _name_edit: LineEdit = $MarginContainer/GridContainer/NameEdit
@onready var _address_edit: LineEdit = $MarginContainer/GridContainer/AddressEdit
@onready var _host_button: Button = $MarginContainer/GridContainer/HostButton
@ -20,6 +25,32 @@ func _ready() -> void:
_error_label.text = ""
_name_edit.grab_focus()
var file := FileAccess.open(SERVER_HISTORY_PATH, FileAccess.READ)
if file:
_server_history = file.get_as_text().strip_edges().split("\n")
func _input(event: InputEvent) -> void:
if _address_edit.has_focus():
if (
event.is_action_pressed("ui_up")
and _history_position + 1 <= _server_history.size()
):
_history_position += 1
_set_address_from_history()
if event.is_action_pressed("ui_down") and _history_position - 1 >= 0:
_history_position -= 1
_set_address_from_history()
func _set_address_from_history() -> void:
if _history_position > 0:
var address := _server_history[-_history_position]
_address_edit.text = address
else:
_address_edit.text = ""
func _display_error(message: String) -> void:
_error_label.text = message
@ -42,8 +73,15 @@ func _on_join_button_pressed() -> void:
_join_button.disabled = true
_host_button.disabled = true
var address := _address_edit.text
if not (address in _server_history):
_server_history.append(address)
var file := FileAccess.open(SERVER_HISTORY_PATH, FileAccess.WRITE)
file.store_string("\n".join(_server_history))
Networker.set_local_player_info(_name_edit.text)
Networker.join_game(_address_edit.text)
Networker.join_game(address)
func _on_networker_network_error(message: String) -> void: