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
|
extends Control
class_name OptionSet
export var placeholder: String
var text: String setget set_text, get_text
func set_text(var value: String):
input.text = value
func get_text() -> String:
return input.text
var selected_idx: int
onready var input := get_node("input") as LineEdit
onready var button := get_node("options") as Button # @DAM Maybe rename this. Also requires renaming on stage.
onready var popup := get_node("/root/main/popup") as ModalPopup
onready var options := get_node("/root/main/option_set_list") as OptionSetList
func _ready():
assert(popup != null, "OptionSet failed to get 'popup' node.")
input.placeholder_text = placeholder
input.connect("focus_entered", input, "set", ["caret_position", input.max_length])
input.connect("focus_exited", input, "deselect")
func show_options(options_array: Array):
options.clear_items()
options.add_items(options_array)
if options_array[selected_idx] == input.text:
options.select(selected_idx)
else:
options.unselect()
options.connect("item_selected", self, "popup_result", [])
options.connect("nothing_selected", self, "popup_result", [-1, ""])
popup.connect("dismissed", self, "popup_result", [selected_idx, input.text])
popup.open_popup(input.placeholder_text, options)
func popup_result(index: int, text: String):
options.disconnect("item_selected", self, "popup_result")
options.disconnect("nothing_selected", self, "popup_result")
popup.disconnect("dismissed", self, "popup_result")
selected_idx = index
input.text = text
input.caret_position = input.max_length
popup.close_popup()
|