Files
MusicPlayer/popup_window.gd
T

38 lines
998 B
GDScript

class_name PopupWindow
extends Control
## A window shown on top of the main window, such as Settings or About.
##
## Clicking outside [member panel] closes it. Popups that need to prepare or clean up
## override [method open] or [method close] and call [code]super()[/code].
## The visible part of the popup. Clicks outside it close the popup.
@export var panel: Control
## Shows the popup.
func open() -> void:
visible = true
## Hides the popup.
func close() -> void:
visible = false
## Opens the popup if it is closed and closes it if it is open.
func toggle() -> void:
if visible:
close()
else:
open()
func _input(event: InputEvent) -> void:
if !visible || !(event is InputEventMouseButton) || !event.pressed:
return
var click: InputEventMouseButton = panel.make_input_local(event)
if !Rect2(Vector2.ZERO, panel.size).has_point(click.position):
close()
# The click only closes the popup; it must not also press what is behind it
get_viewport().set_input_as_handled()