RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
field_menu_controller.gd
Go to the documentation of this file.
1extends RefCounted
2
3
10
11var ui_flow_manager = preload("res://scripts/systems/uiFlowManager/ui_flow_manager.gd").new()
12
13func create_popup(options:Dictionary, actions:Array) -> PopupPanel:
14 var popup = PopupPanel.new()
15 popup.name = str(options.get("name", "FieldMenuPopup"))
16 _apply_popup_style(popup, options)
17 var content = VBoxContainer.new()
18 content.name = "Content"
19 content.custom_minimum_size = Vector2(
20 float(options.get("width", 320.0)),
21 float(options.get("height", 260.0))
22 )
23 content.add_theme_constant_override("separation", int(options.get("separation", 10)))
24 var title = Label.new()
25 title.name = "Title"
26 title.text = str(options.get("title", "Field Menu"))
27 title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
28 title.add_theme_font_size_override("font_size", int(options.get("title_size", 20)))
29 content.add_child(title)
30 var message = Label.new()
31 message.name = "Message"
32 message.text = str(options.get("message", "Choose what to do with the current run."))
33 message.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
34 message.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
35 content.add_child(message)
36 for action in actions:
37 if not action is Dictionary:
38 continue
39 content.add_child(_create_action_button(popup, action, options))
40 popup.add_child(content)
41 return popup
42
43
44func popup_centered_for_viewport(popup:PopupPanel, viewport_size:Vector2, options:Dictionary = {}) -> void:
45 if popup == null or not is_instance_valid(popup):
46 return
47 var width = min(int(options.get("max_width", 380)), max(220, int(viewport_size.x) - 24))
48 var height = min(int(options.get("max_height", 360)), max(180, int(viewport_size.y) - 24))
49 popup.popup_centered(Vector2i(width, height))
50 call_deferred("_focus_first_button", popup)
51
52
53func _create_action_button(popup:PopupPanel, action:Dictionary, options:Dictionary) -> Button:
54 var button = Button.new()
55 button.name = str(action.get("name", "%sButton" % str(action.get("id", "Action")).capitalize()))
56 button.text = str(action.get("label", "Action"))
57 button.custom_minimum_size = Vector2(0, float(options.get("button_height", 44.0)))
58 button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
59 var callback = action.get("callback", Callable())
60 button.pressed.connect(func():
61 if bool(action.get("hide_before_callback", true)):
62 popup.hide()
63 if callback is Callable and callback.is_valid():
64 callback.call()
65 )
66 return button
67
68
69func _apply_popup_style(popup:PopupPanel, options:Dictionary) -> void:
70 var style = StyleBoxFlat.new()
71 style.bg_color = Color(str(options.get("background", "#101820")))
72 style.border_color = Color(str(options.get("border", "#f0c85a")))
73 style.set_border_width_all(int(options.get("border_width", 2)))
74 style.set_corner_radius_all(int(options.get("radius", 8)))
75 style.content_margin_left = int(options.get("padding", 14))
76 style.content_margin_right = int(options.get("padding", 14))
77 style.content_margin_top = int(options.get("padding", 14))
78 style.content_margin_bottom = int(options.get("padding", 14))
79 popup.add_theme_stylebox_override("panel", style)
80
81
82func _focus_first_button(popup:PopupPanel) -> void:
83 if popup == null or not is_instance_valid(popup) or not popup.is_inside_tree():
84 return
85 ui_flow_manager.focus_first(popup)