-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_menu.gd
117 lines (89 loc) · 3.5 KB
/
main_menu.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
extends PanelContainer
var lobby_browser_scene : PackedScene = preload("res://menus/lobby_browser/lobby_browser.tscn")
var lobby_creator_scene : PackedScene = preload("res://menus/lobby_creator.tscn")
var settings_scene : PackedScene = preload("res://menus/settings.tscn")
@export var peer_name_line_edit : LineEdit
@export var name_label: Label
@export var logs : Label
@export var menu: VBoxContainer
@export var set_name_menu: VBoxContainer
@export var set_name_button: Button
@export var multiplayer_button: Button
@export var quit_button: Button
@export var left_spacer: Control
@export var right_spacer: Control
var confirm_exit: ConfirmationDialog
func _ready() -> void:
GlobalLobbyClient.log_updated.connect(_log_updated)
_connected_to_lobby(GlobalLobbyClient.peer, "")
GlobalLobbyClient.connected_to_lobby.connect(_connected_to_lobby)
if not (OS.get_name() in ["Android", "iOS", "Web"]):
quit_button.show()
create_quit_dialog()
func _connected_to_lobby(peer: LobbyPeer, _reconnection_token: String):
var peer_name: String = peer.user_data.get("name", "")
# If no name
if peer_name == "":
menu.visible = false
set_name_menu.visible = true
else: # has name already
menu.visible = true
name_label.visible = true
name_label.text = "Hello, " + peer_name
peer_name_line_edit.text = peer_name
set_name_menu.visible = false
multiplayer_button.grab_focus()
# Don't show name menu on android and ios
if (OS.has_feature("web_android") or OS.has_feature("web_ios") or OS.has_feature("android") or OS.has_feature("ios")):
menu.visible = true
set_name_menu.visible = false
multiplayer_button.grab_focus()
logs.text = ""
func _log_updated(command: String, logs_text: String):
if command == "error":
logs.text = command + " " + logs_text
func _on_button_join_public_pressed() -> void:
if is_inside_tree():
get_tree().change_scene_to_packed(lobby_browser_scene)
func _on_button_lobby_pressed() -> void:
if is_inside_tree():
get_tree().change_scene_to_packed(lobby_creator_scene)
func _on_set_name_pressed() -> void:
var result :LobbyResult= await GlobalLobbyClient.add_peer_user_data({"name": peer_name_line_edit.text}).finished
if result.has_error():
logs.text = result.error
else:
menu.visible = true
set_name_menu.visible = false
name_label.visible = true
name_label.text = "Hello, " + peer_name_line_edit.text
multiplayer_button.grab_focus()
func _on_line_edit_text_submitted(_new_text: String) -> void:
_on_set_name_pressed()
multiplayer_button.grab_focus()
func _on_line_edit_text_changed(new_text: String) -> void:
set_name_button.disabled = new_text == ""
func _on_resized() -> void:
var show_spacers = size.x > 600
left_spacer.visible = show_spacers
right_spacer.visible = show_spacers
func _on_button_settings_pressed() -> void:
if is_inside_tree():
get_tree().change_scene_to_packed(settings_scene)
func create_quit_dialog():
confirm_exit = ConfirmationDialog.new()
confirm_exit.dialog_text = "Are you sure you want to exit?"
confirm_exit.hide()
confirm_exit.min_size = Vector2(240, 128)
add_child(confirm_exit)
var ok: Button = confirm_exit.get_ok_button()
ok.text = "Yes"
ok.pressed.connect(func(): get_tree().quit())
confirm_exit.get_cancel_button().text = "No"
confirm_exit.add_theme_constant_override("buttons_min_width", 100)
confirm_exit.add_theme_constant_override("buttons_min_height", 48)
func _on_quit_button_pressed() -> void:
confirm_exit.popup_centered()
func _input(_event):
if Input.is_action_just_pressed("ui_cancel"):
_on_quit_button_pressed()