aboutsummaryrefslogtreecommitdiff
path: root/ui/modal_popup/modal_popup.gd
blob: 857f90e3ed5cd2ab68430786815692f146d65822 (plain)
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
extends ColorRect
class_name ModalPopup

signal dismissed		# ()

export var clear_signals_on_hide := true

var control					: Control
var control_parent			: Node

onready var title			:= get_node("title") as Label
onready var background		:= get_node("background") as Panel
onready var dismiss_button	:= get_node("dismiss") as Button


func _init():
	self.anchor_right		= 1.0
	self.anchor_bottom		= 1.0
	self.rect_clip_content	= true
	self.connect("hide", self, "_clear_signals")


func _ready():
	dismiss_button.connect("pressed", self, "dismiss")


func _clear_signals():
	if clear_signals_on_hide == false:
		return
	
	for signal_name in ["dismissed"]:
		for it in get_signal_connection_list(signal_name):
			disconnect(it.signal, it.target, it.method)


func dismiss():
	emit_signal("dismissed")
	close_popup()


func open_popup(title: String, item: Control):
	if visible == true:
		return
	
	self.color = get_color("background", "theme")
	self.color.a = 0.85
	self.title.text = title
	
	control = item
	control.connect("hide", self, "close_popup")
	control_parent = control.get_parent()
	control_parent.remove_child(control)
	self.add_child(control)
	
	control.anchor_left = background.anchor_left
	control.anchor_top = background.anchor_top
	control.anchor_right = background.anchor_right
	control.anchor_bottom = background.anchor_bottom
	control.margin_left = 20
	control.margin_top = 20
	control.margin_right = -20
	control.margin_bottom = -20
	
	self.show()
	control.show()


func close_popup():
	if visible == false:
		return

	control.disconnect("hide", self, "close_popup")
	control.hide()
	self.hide()
	remove_child(control)
	control_parent.add_child(control)
	control_parent = null