10func create_popup(owner:Node, title_text:String, pages:Array, options:Dictionary = {}) -> PopupPanel:
11 var popup := PopupPanel.new()
12 popup.name = str(options.get(
"name",
"InfoPopup"))
13 popup.size = _option_vector2i(options,
"size", Vector2i(520, 560))
15 owner.add_child(popup)
17 var content := VBoxContainer.new()
18 content.name =
"Content"
19 content.set_anchors_preset(Control.PRESET_FULL_RECT)
20 var margin:int =
int(options.get(
"margin", 16))
21 content.offset_left = margin
22 content.offset_top = margin
23 content.offset_right = -margin
24 content.offset_bottom = -margin
25 content.add_theme_constant_override(
"separation",
int(options.get(
"separation", 10)))
26 popup.add_child(content)
28 var title := Label.new()
30 title.text = title_text
31 title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
32 title.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
33 title.add_theme_font_size_override(
"font_size",
int(options.get(
"title_font_size", 22)))
34 content.add_child(title)
36 var tabs := TabContainer.new()
38 tabs.size_flags_horizontal = Control.SIZE_EXPAND_FILL
39 tabs.size_flags_vertical = Control.SIZE_EXPAND_FILL
40 content.add_child(tabs)
42 for page_value
in pages:
43 if page_value
is Dictionary:
44 tabs.add_child(_create_page(page_value))
46 var back := Button.new()
47 back.name = str(options.get(
"back_name",
"Back"))
48 back.text = str(options.get(
"back_label",
"Back"))
49 back.custom_minimum_size = Vector2(0, float(options.get(
"back_height", 48.0)))
50 var callback:Callable = options.get(
"back_callback",
Callable())
51 back.pressed.connect(func():
52 if callback.is_valid():
57 content.add_child(back)
61func get_tab_container(popup:PopupPanel) -> TabContainer:
64 var tabs = popup.get_node_or_null(
"Content/Tabs")
65 if tabs
is TabContainer:
70func step_tab(popup:PopupPanel, direction:int) -> void:
71 var tabs := get_tab_container(popup)
72 if tabs == null
or tabs.get_tab_count() <= 0:
74 tabs.current_tab = wrapi(tabs.current_tab + direction, 0, tabs.get_tab_count())
77func reset_tab(popup:PopupPanel, tab_index:int = 0) -> void:
78 var tabs := get_tab_container(popup)
79 if tabs == null
or tabs.get_tab_count() <= 0:
81 tabs.current_tab = clampi(tab_index, 0, tabs.get_tab_count() - 1)
84func _create_page(page:Dictionary) -> ScrollContainer:
85 var scroll := ScrollContainer.new()
86 scroll.name = str(page.get(
"id", page.get(
"title",
"InfoPage"))).replace(
" ",
"")
87 scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
88 scroll.size_flags_horizontal = Control.SIZE_EXPAND_FILL
89 scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
91 var body := VBoxContainer.new()
93 body.size_flags_horizontal = Control.SIZE_EXPAND_FILL
94 body.add_theme_constant_override(
"separation",
int(page.get(
"separation", 8)))
95 scroll.add_child(body)
97 var title := Label.new()
98 title.name =
"PageTitle"
99 title.text = str(page.get(
"title",
"Info"))
100 title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
101 title.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
102 title.add_theme_font_size_override(
"font_size",
int(page.get(
"title_font_size", 18)))
103 body.add_child(title)
105 var lines:Array = page.get(
"lines", [])
106 for line_value
in lines:
107 var label := Label.new()
108 label.text = str(line_value)
109 label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
110 label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
111 body.add_child(label)
115func _option_vector2i(options:Dictionary, key:String, default_value:Vector2i) -> Vector2i:
116 if not options.has(key):
118 var value = options[key]
119 if value
is Vector2i:
122 return Vector2i(
int(value.x),
int(value.y))