-
Notifications
You must be signed in to change notification settings - Fork 0
/
PromptMenu.gd
151 lines (122 loc) · 3.56 KB
/
PromptMenu.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
extends PanelContainer
signal item_selected(item, extra)
signal menu_closed
var prefix = ''
var mode = "numbers"
var main_value = ''
var prompt_value = 0
var min_number = 1
var max_number = 1
func update_value():
pass
func set_prefix(text : String):
prefix = text
find_node('Prefix').text = text
func set_mode(new_mode : String):
mode = new_mode
func set_main_value(value : String):
main_value = value
func get_prompt_value():
return prompt_value
func set_numbers_mode(main_value : String, n_start : int, n_max : int, n_min : int = 1) -> void:
set_main_value(main_value)
set_mode("numbers")
min_number = n_min
max_number = n_max
prompt_value = n_start
find_node('ConfirmButtons').visible = false
find_node('Numbers').visible = true
update_prompt()
func set_confirm_mode(main_value : String, default : int = 0):
set_main_value(main_value)
set_mode("confirm")
prompt_value = 0 if default == 0 else 1
var buttons = find_node('ConfirmButtons')
buttons.visible = true
find_node('Numbers').visible = false
var yes = buttons.get_node('YesOption')
yes.set_value(main_value)
yes.set_extra(1)
var no = buttons.get_node('NoOption')
no.set_value(main_value)
no.set_extra(0)
if default == 0:
no.set_active()
else:
yes.set_active()
func close():
emit_signal("menu_closed")
visible = false
queue_free()
func update_prompt() -> void:
if mode == "numbers":
var left_button = find_node("LeftButton")
if prompt_value > min_number:
left_button.disabled = false
else:
left_button.disabled = true
var right_button = find_node("RightButton")
if prompt_value < max_number:
right_button.disabled = false
else:
right_button.disabled = true
# val_text = ("<" if prompt_value > min_number else " ") + " "
# val_text += str(prompt_value) + " "
# val_text += (">" if prompt_value < max_number else " ")
#
# find_node('Prefix').text = prefix + '\n' + val_text
find_node('NumberLabel').text = str(prompt_value)
if mode == "confirm":
var active = 'YesOption' if prompt_value > 0 else 'NoOption'
var inactive = 'NoOption' if prompt_value > 0 else 'YesOption'
find_node(active).set_active()
find_node(inactive).set_inactive()
func number_decrease():
if prompt_value > min_number:
prompt_value -= 1
update_prompt()
func number_increase():
if prompt_value < max_number:
prompt_value += 1
update_prompt()
# warning-ignore:unused_argument
func _process(delta) -> void:
if Input.is_action_just_pressed("menu_back"):
close()
return
if Input.is_action_just_pressed("menu_select"):
if mode == "confirm":
find_node('YesOption' if prompt_value > 0 else 'NoOption').select()
elif mode == "numbers":
emit_signal("item_selected", main_value, prompt_value)
close()
return
if mode == "numbers":
if Input.is_action_just_pressed("move_left"):
number_decrease()
if Input.is_action_just_pressed("move_right"):
number_increase()
if mode == "confirm":
if Input.is_action_just_pressed("move_right") and prompt_value > 0:
prompt_value = 0
update_prompt()
if Input.is_action_just_pressed("move_left") and prompt_value < 1:
prompt_value = 1
update_prompt()
func _on_option_selected(value, extra):
emit_signal("item_selected", value, extra)
close()
func _on_mouse_entered_item(item):
if not mode == "confirm":
return
find_node('YesOption').set_inactive()
find_node('NoOption').set_inactive()
item.set_active()
prompt_value = item.get_extra()
func _on_LeftButton_pressed():
number_decrease()
func _on_RightButton_pressed():
number_increase()
func _on_OkButton_pressed():
emit_signal("item_selected", main_value, prompt_value)
close()