Advertisement
freswinn

Godot 4.x: VBoxColumnOrganizer

Feb 23rd, 2025
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @tool
  2. extends VBoxContainer
  3. class_name VBoxColumnOrganizer
  4. ## Every immediate child of this node that is an HBoxContainer will have its
  5. ## children's [code]custom_minimum.size.x[/code] set by this node's
  6. ## [code]column_width[/code]. The number of column widths automatically set by
  7. ## this node is equal to the number of elements in the [code]column_width[/code]
  8. ## array.
  9.  
  10. ## Sets the minimum width of all Control nodes that are children of this node's
  11. ## immediate HBoxContainer children.[br]
  12. ## [b]Note:[/b] These values are being set in real time, and removing a column does not
  13. ## reset the affected elements' minimum widths to what they were prior to the
  14. ## adjustment. If you want to reset all of those elements to zero, you can set
  15. ## them to zero before deleting the column's setting from the array.
  16. @export var column_width : Array[int] = [0] : set = set_column_width
  17. ## This will automatically set the last child in each HBoxContainer to expand
  18. ## horizontally.
  19. @export var expand_last_column : bool = true : set = set_expand_last_column
  20. ## Selecting this will allow CheckBox and CheckButton nodes in the last column
  21. ## to expand when [code]expand_last_column[/code] is set to true. This is
  22. ## [b]typically not the behavior you want,[/b] so it is set to false by default.
  23. @export var also_expand_checkbox : bool = false : set = set_also_expand_checkbox
  24.  
  25. func set_column_width(n):
  26.     column_width = n
  27.     _act()
  28.  
  29. func set_expand_last_column(n):
  30.     expand_last_column = n
  31.     _act()
  32.  
  33. func set_also_expand_checkbox(n):
  34.     also_expand_checkbox = n
  35.     _act()
  36.  
  37. func _ready():
  38.     _act()
  39.     child_entered_tree.connect(_act)
  40.     child_exiting_tree.connect(_act)
  41.     child_order_changed.connect(_act)
  42.  
  43.  
  44. func _act(_input = null):
  45.     var children = get_children()
  46.     for i in children:
  47.         if i is HBoxContainer: _adjust_row(i)
  48.  
  49.  
  50. func _adjust_row(row_hbox : HBoxContainer):
  51.     var children : Array = row_hbox.get_children()
  52.     for i in len(column_width):
  53.         if children[i] is Control:
  54.             children[i].custom_minimum_size.x = column_width[i]
  55.     if expand_last_column:
  56.         var c = children[-1]
  57.         if !also_expand_checkbox:
  58.             if c is CheckBox or c is CheckButton: return
  59.         if c is Control:
  60.             children[-1].size_flags_horizontal = 3
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement