immersive-home/app/content/ui/components/grid_container/grid_container.gd

47 lines
790 B
GDScript3
Raw Permalink Normal View History

2023-11-07 22:07:34 +02:00
@tool
extends Container3D
class_name GridContainer3D
2024-04-23 23:11:18 +03:00
@export var columns := 5:
2023-11-07 22:07:34 +02:00
set(value):
columns = value
2024-04-23 23:11:18 +03:00
_update()
2023-11-07 22:07:34 +02:00
2024-04-23 23:11:18 +03:00
@export var gaps := Vector2(0, 0):
2023-11-07 22:07:34 +02:00
set(value):
2024-04-23 23:11:18 +03:00
gaps = value
_update()
2023-11-07 22:07:34 +02:00
func _ready():
2024-04-23 23:11:18 +03:00
_update()
child_entered_tree.connect(func(_arg):
_update()
)
2023-11-07 22:07:34 +02:00
2024-04-23 23:11:18 +03:00
child_exiting_tree.connect(func(_arg):
_update()
2023-11-07 22:07:34 +02:00
)
2024-04-23 23:11:18 +03:00
child_order_changed.connect(func():
_update()
)
2023-11-07 22:07:34 +02:00
2024-04-23 23:11:18 +03:00
func _update():
var column := 0
var row_pos := 0.0
var column_max_height := 0.0
2023-11-07 22:07:34 +02:00
for child in get_children():
2024-04-23 23:11:18 +03:00
if child is Container3D == false:
continue
column_max_height = max(column_max_height, child.size.y)
child.position = Vector3(column * ((size.x / columns) + gaps.x), row_pos, 0)
2023-11-07 22:07:34 +02:00
2024-04-23 23:11:18 +03:00
column += 1
if column >= columns:
column = 0
row_pos -= column_max_height + gaps.y