gui_containers.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. .. _doc_gui_containers:
  2. Using Containers
  3. ================
  4. :ref:`Anchors <doc_size_and_anchors>` are an efficient way to handle
  5. different aspect ratios for basic multiple resolution handling in GUIs,
  6. For more complex user interfaces, they can become difficult to use.
  7. This is often the case of games, such as RPGs, online chats, tycoons or simulations. Another
  8. common case where more advanced layout features may be required is in-game tools (or simply just tools).
  9. All these situations require a more capable OS-like user interface, with advanced layout and formatting.
  10. For that, :ref:`Containers <class_container>` are more useful.
  11. Container layout
  12. ----------------
  13. Containers provide a huge amount of layout power (as an example, the Godot editor user interface is entirely done using them):
  14. .. image:: img/godot_containers.png
  15. When a :ref:`Container <class_Container>`-derived node is used, all children :ref:`Control <class_Control>` nodes give up their
  16. own positioning ability. This means the *Container* will control their positioning and any attempt to manually alter these
  17. nodes will be either ignored or invalidated the next time their parent is resized.
  18. Likewise, when a *Container* derived node is resized, all its children will be re-positioned according to it,
  19. with a behavior based on the type of container used:
  20. .. image:: img/container_example.gif
  21. Example of *HBoxContainer* resizing children buttons.
  22. The real strength of containers is that they can be nested (as nodes), allowing the creation of very complex layouts that resize effortlessly.
  23. Size flags
  24. ----------
  25. When adding a node to a container, the way the container treats each child depends mainly on their *size flags*. These flags
  26. can be found by inspecting any control that is a child of a *Container*.
  27. .. image:: img/container_size_flags.png
  28. Size flags are independent for vertical and horizontal sizing and not all containers make use of them (but most do):
  29. * **Fill**: Ensures the control *fills* the designated area within the container. No matter if
  30. a control *expands* or not (see below), it will only *fill* the designated area when this is toggled on (it is by default).
  31. * **Expand**: Attempts to use as much space as possible in the parent container (in each axis).
  32. Controls that don't expand will be pushed away by those that do. Between expanding controls, the
  33. amount of space they take from each other is determined by the *Ratio* (see below).
  34. * **Shrink Center** When expanding (and if not filling), try to remain at the center of the expanded
  35. area (by default it remains at the left or top).
  36. * **Ratio** Simple ratio of how much expanded controls take up the available space in relation to each
  37. other. A control with "2", will take up twice as much available space as one with "1".
  38. Experimenting with these flags and different containers is recommended to get a better grasp on how they work.
  39. Container types
  40. ---------------
  41. Godot provides several container types out of the box as they serve different purposes:
  42. Box Containers
  43. ^^^^^^^^^^^^^^
  44. Arrange child controls vertically or horizontally (via :ref:`HBoxContainer <class_HBoxContainer>` and
  45. :ref:`VBoxContainer <class_VBoxContainer>`). In the opposite of the designated direction
  46. (as in, vertical for an horizontal container), it just expands the children.
  47. .. image:: img/containers_box.png
  48. These containers make use of the *Ratio* property for children with the *Expand* flag set.
  49. Grid Container
  50. ^^^^^^^^^^^^^^
  51. Arranges child controls in a grid layout (via :ref:`GridContainer <class_GridContainer>`, amount
  52. of columns must be specified). Uses both the vertical and horizontal expand flags.
  53. .. image:: img/containers_grid.png
  54. Margin Container
  55. ^^^^^^^^^^^^^^^^
  56. Child controls are expanded towards the bounds of this control (via
  57. :ref:`MarginContainer <class_MarginContainer>`). Padding will be added on the margins
  58. depending on the theme configuration.
  59. .. image:: img/containers_margin.png
  60. Again, keep in mind that the margins are a *Theme* value, so they need to be edited from the
  61. constants overrides section of each control:
  62. .. image:: img/containers_margin_constants.png
  63. Tab Container
  64. ^^^^^^^^^^^^^
  65. Allows you to place several child controls stacked on top of each other (via
  66. :ref:`TabContainer <class_TabContainer>`), with only the *current* one visible.
  67. .. image:: img/containers_tab.png
  68. Changing the *current* one is done via tabs located at the top of the container, via clicking:
  69. .. image:: img/containers_tab_click.gif
  70. The titles are generated from the node names by default (although they can be overridden via *TabContainer* API).
  71. Settings such as tab placement and *StyleBox* can be modified in the *TabContainer* theme overrides.
  72. Split Container
  73. ^^^^^^^^^^^^^^^
  74. Accepts only one or two children controls, then places them side to side with a divisor
  75. (via :ref:`HSplitContainer <class_HSplitContainer>` and :ref:`VSplitContainer <class_VSplitContainer>`).
  76. Respects both horizontal and vertical flags, as well as *Ratio*.
  77. .. image:: img/containers_split.png
  78. The divisor can be dragged around to change the size relation between both children:
  79. .. image:: img/containers_split_drag.gif
  80. PanelContainer
  81. ^^^^^^^^^^^^^^
  82. Simple container that draws a *StyleBox*, then expands children to cover its whole area
  83. (via :ref:`PanelContainer <class_PanelContainer>`, respecting the *StyleBox* margins).
  84. It respects both the horizontal and vertical size flags.
  85. .. image:: img/containers_panel.png
  86. This container is useful as top-level, or just to add custom backgrounds to sections of a layout.
  87. ScrollContainer
  88. ^^^^^^^^^^^^^^^
  89. Accepts a single child node. If this node is bigger than the container, scrollbars will be added
  90. to allow panning the node around (via :ref:`ScrollContainer <class_ScrollContainer>`). Both
  91. vertical and horizontal size flags are respected, and the behavior can be turned on or off
  92. per axis in the properties.
  93. .. image:: img/containers_scroll.png
  94. Mouse wheel and touch drag (when touch is available) are also valid ways to pan the child control around.
  95. .. image:: img/containers_center_pan.gif
  96. As in the example above, one of the most common ways to use this container is together with a *VBoxContainer* as child.
  97. ViewportContainer
  98. ^^^^^^^^^^^^^^^^^
  99. This is a special control that will only accept a single *Viewport* node as child, and it will display
  100. it as if it was an image (via :ref:`ViewportContainer <class_ViewportContainer>`).
  101. Creating custom Containers
  102. --------------------------
  103. It is possible to easily create a custom container using script. Here is an example of a simple container that fits children
  104. to its rect size:
  105. .. tabs::
  106. .. code-tab:: gdscript GDScript
  107. extends Container
  108. func _notification(what):
  109. if what == NOTIFICATION_SORT_CHILDREN:
  110. # Must re-sort the children
  111. for c in get_children():
  112. # Fit to own size
  113. fit_child_in_rect( c, Rect2( Vector2(), rect_size ) )
  114. func set_some_setting():
  115. # Some setting changed, ask for children re-sort
  116. queue_sort()