RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
info_panel.gd
Go to the documentation of this file.
1extends RefCounted
2
3
8
9
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))
14 if owner != null:
15 owner.add_child(popup)
16
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)
27
28 var title := Label.new()
29 title.name = "Title"
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)
35
36 var tabs := TabContainer.new()
37 tabs.name = "Tabs"
38 tabs.size_flags_horizontal = Control.SIZE_EXPAND_FILL
39 tabs.size_flags_vertical = Control.SIZE_EXPAND_FILL
40 content.add_child(tabs)
41
42 for page_value in pages:
43 if page_value is Dictionary:
44 tabs.add_child(_create_page(page_value))
45
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():
53 callback.call()
54 else:
55 popup.hide()
56 )
57 content.add_child(back)
58 return popup
59
60
61func get_tab_container(popup:PopupPanel) -> TabContainer:
62 if popup == null:
63 return null
64 var tabs = popup.get_node_or_null("Content/Tabs")
65 if tabs is TabContainer:
66 return tabs
67 return null
68
69
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:
73 return
74 tabs.current_tab = wrapi(tabs.current_tab + direction, 0, tabs.get_tab_count())
75
76
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:
80 return
81 tabs.current_tab = clampi(tab_index, 0, tabs.get_tab_count() - 1)
82
83
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
90
91 var body := VBoxContainer.new()
92 body.name = "Content"
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)
96
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)
104
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)
112 return scroll
113
114
115func _option_vector2i(options:Dictionary, key:String, default_value:Vector2i) -> Vector2i:
116 if not options.has(key):
117 return default_value
118 var value = options[key]
119 if value is Vector2i:
120 return value
121 if value is Vector2:
122 return Vector2i(int(value.x), int(value.y))
123 return default_value