add Menu class and refactor men-related cursor capturing

This commit is contained in:
2025-02-14 23:49:00 +10:00
parent 7ba087f6e1
commit 2d9933b627
7 changed files with 56 additions and 19 deletions

View File

@@ -1,7 +1,5 @@
class_name Chat
extends CanvasLayer
var open: bool = false
extends Menu
var _chat_message_scene := preload("res://scenes/ui/chat_message.tscn")
@@ -25,29 +23,26 @@ func _exit_tree() -> void:
func _input(event: InputEvent) -> void:
if event.is_action_pressed("menu"):
call_deferred("_close")
call_deferred("close")
func _unhandled_input(event: InputEvent) -> void:
if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
return
if event.is_action_pressed("chat") and not is_open:
open()
if event.is_action_pressed("chat") and not open:
_open()
func _open()->void:
open = true
func open() -> void:
super.open()
_message_edit.visible = true
_message_edit.grab_focus()
_msg_scroll.vertical_scroll_mode = ScrollContainer.SCROLL_MODE_AUTO
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
func _close() -> void:
open = false
func close() -> void:
super.close()
_message_edit.release_focus()
_message_edit.visible = false
_msg_scroll.vertical_scroll_mode = ScrollContainer.SCROLL_MODE_SHOW_NEVER
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
@rpc("any_peer", "call_local", "reliable", 1)
@@ -71,7 +66,7 @@ func _on_message_edit_text_submitted(message: String) -> void:
if _message_edit.text == "":
return
_message_edit.text = ""
_close()
close()
_send_message.rpc(message)

16
scripts/ui/menu.gd Normal file
View File

@@ -0,0 +1,16 @@
class_name Menu
extends CanvasLayer
var is_open: bool = false
func open() -> void:
is_open = true
Referencer.main.menu = self
Referencer.set_menu_opened(self)
func close() -> void:
is_open = false
Referencer.main.menu = null
Referencer.set_menu_closed(self)