-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAddPopup.gd
88 lines (67 loc) · 1.79 KB
/
AddPopup.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
extends WindowDialog
export (String) var empty_name_message = ""
export (String) var bad_name_message = ""
export (String) var exception_message = ""
export (String) var good_name_message = ""
export (Database.Table) var table = Database.Table.FIGHTERS
export (String) var field = ""
onready var NameEdit = find_node("NameEdit")
onready var StatusLabel = find_node("StatusLabel")
onready var OK = find_node("OK")
onready var Cancel = find_node("Cancel")
var add_func:FuncRef = null
var check_valid_func:FuncRef = null
var overwrite_mode:bool = false
var valid := false
func _ready():
pass
func check_valid():
var n = NameEdit.text
var using_check_func = not check_valid_func == null
valid = true
var msg = empty_name_message
if n.empty():
valid = false
if valid and not using_check_func:
if n.findn("_") > -1 or n.findn("-") > -1:
msg = exception_message
valid = false
if valid:
if using_check_func:
valid = check_valid_func.call_func(n)
else:
var data = Database.read(table, overwrite_mode)
for k in data.keys():
var obj = data.get(k)
if n == obj.get(field):
valid = false
break
if not valid:
msg = bad_name_message
if valid:
StatusLabel.modulate = Color.green
StatusLabel.text = good_name_message
else:
StatusLabel.modulate = Color.red
StatusLabel.text = msg
OK.disabled = not valid
func _on_NameEdit_text_changed(new_text):
check_valid()
func _on_NameEdit_text_entered(new_text):
check_valid()
_on_OK_pressed()
func _on_OK_pressed():
if not valid: return
if add_func:
add_func.call_func(NameEdit.text)
else:
assert(false, "Needs an add func")
hide()
func _on_Cancel_pressed():
hide()
func _on_AddPopup_about_to_show():
NameEdit.clear()
check_valid()
NameEdit.call_deferred("grab_focus")
func _on_AddPopup_popup_hide():
pass