add Menu class and refactor men-related cursor capturing
This commit is contained in:
parent
7ba087f6e1
commit
2d9933b627
@ -3,6 +3,8 @@ extends Node
|
||||
|
||||
func _ready() -> void:
|
||||
Referencer.main_loaded.connect(_on_referencer_main_loaded)
|
||||
Referencer.menu_opened.connect(_on_referencer_menu_opened)
|
||||
Referencer.menu_closed.connect(_on_referencer_menu_closed)
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
@ -15,3 +17,11 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
|
||||
func _on_referencer_main_loaded() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
|
||||
func _on_referencer_menu_opened(_menu: Menu) -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
|
||||
|
||||
func _on_referencer_menu_closed(_menu: Menu) -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
@ -53,6 +53,10 @@ func _input(event: InputEvent) -> void:
|
||||
Debugger.text("input", _get_event_prompt_current_mode(event))
|
||||
|
||||
|
||||
func can_control_player() -> bool:
|
||||
return Referencer.main.menu == null
|
||||
|
||||
|
||||
func get_action_prompt(action: StringName) -> String:
|
||||
var events := InputMap.action_get_events(action)
|
||||
var bbcode: PackedStringArray = []
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
extends Node
|
||||
|
||||
signal main_loaded
|
||||
signal menu_opened(menu: Menu)
|
||||
signal menu_closed(menu: Menu)
|
||||
|
||||
var camera: Camera3D
|
||||
var player: Player
|
||||
@ -10,3 +12,11 @@ var main: Main:
|
||||
main_loaded.emit()
|
||||
main = value
|
||||
var chat: Chat
|
||||
|
||||
|
||||
func set_menu_opened(menu: Menu) -> void:
|
||||
menu_opened.emit(menu)
|
||||
|
||||
|
||||
func set_menu_closed(menu: Menu) -> void:
|
||||
menu_closed.emit(menu)
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
class_name Main
|
||||
extends Node
|
||||
|
||||
var menu: Menu
|
||||
|
||||
@onready var player_holder: Node = $Players
|
||||
@onready var player_spawner: MultiplayerSpawner = $PlayerSpawner
|
||||
|
||||
@ -16,7 +18,7 @@ func _exit_tree() -> void:
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("menu") and not Referencer.chat.open:
|
||||
if event.is_action_pressed("menu") and Inputer.can_control_player():
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
if not is_multiplayer_authority():
|
||||
return
|
||||
|
||||
if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
||||
if not Inputer.can_control_player():
|
||||
return
|
||||
|
||||
if event is InputEventMouseMotion:
|
||||
@ -168,7 +168,7 @@ func _lateral_movement(delta: float) -> void:
|
||||
"move_left", "move_right", "move_forward", "move_back"
|
||||
)
|
||||
var has_input := (
|
||||
input_dir.length() > 0 and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED
|
||||
input_dir.length() > 0 and Inputer.can_control_player()
|
||||
)
|
||||
|
||||
if has_input:
|
||||
@ -203,7 +203,7 @@ func _vertical_movement(delta: float) -> void:
|
||||
|
||||
func _jumping() -> void:
|
||||
if (
|
||||
Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
|
||||
Inputer.can_control_player()
|
||||
and Input.is_action_just_pressed("jump")
|
||||
and is_on_floor()
|
||||
):
|
||||
|
||||
@ -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
16
scripts/ui/menu.gd
Normal 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)
|
||||
Loading…
Reference in New Issue
Block a user