gui_containers.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. :article_outdated: True
  2. .. _doc_gui_containers:
  3. Using Containers
  4. ================
  5. :ref:`Anchors <doc_size_and_anchors>` are an efficient way to handle
  6. different aspect ratios for basic multiple resolution handling in GUIs.
  7. For more complex user interfaces, they can become difficult to use.
  8. This is often the case of games, such as RPGs, online chats, tycoons or simulations. Another
  9. common case where more advanced layout features may be required is in-game tools (or simply just tools).
  10. All these situations require a more capable OS-like user interface, with advanced layout and formatting.
  11. For that, :ref:`Containers <class_container>` are more useful.
  12. Container layout
  13. ----------------
  14. Containers provide a huge amount of layout power (as an example, the Godot editor user interface is entirely done using them):
  15. .. image:: img/godot_containers.png
  16. When a :ref:`Container <class_Container>`-derived node is used, all children :ref:`Control <class_Control>` nodes give up their
  17. own positioning ability. This means the *Container* will control their positioning and any attempt to manually alter these
  18. nodes will be either ignored or invalidated the next time their parent is resized.
  19. Likewise, when a *Container* derived node is resized, all its children will be re-positioned according to it,
  20. with a behavior based on the type of container used:
  21. .. image:: img/container_example.gif
  22. Example of *HBoxContainer* resizing children buttons.
  23. The real strength of containers is that they can be nested (as nodes), allowing the creation of very complex layouts that resize effortlessly.
  24. Sizing options
  25. --------------
  26. When adding a node to a container, the way the container treats each child depends mainly on their *container sizing options*. These options
  27. can be found by inspecting the layout of any *Control* that is a child of a *Container*.
  28. .. image:: img/container_sizing_options.webp
  29. Sizing options are independent for vertical and horizontal sizing and not all containers make use of them (but most do):
  30. * **Fill**: Ensures the control *fills* the designated area within the container. No matter if
  31. a control *expands* or not (see below), it will only *fill* the designated area when this is toggled on (it is by default).
  32. * **Expand**: Attempts to use as much space as possible in the parent container (in each axis).
  33. Controls that don't expand will be pushed away by those that do. Between expanding controls, the
  34. amount of space they take from each other is determined by the *Stretch Ratio* (see below).
  35. This option is only available when the parent Container is of the right type, for example the *HBoxContainer* has this option
  36. for horizontal sizing.
  37. * **Shrink Begin** When expanding, try to remain at the left or top of the expanded
  38. area.
  39. * **Shrink Center** When expanding, try to remain at the center of the expanded
  40. area.
  41. * **Shrink End** When expanding, try to remain at the right or bottom of the expanded
  42. area.
  43. * **Stretch Ratio**: The ratio of how much expanded controls take up the available space in relation to each
  44. other. A control with "2", will take up twice as much available space as one with "1".
  45. Experimenting with these flags and different containers is recommended to get a better grasp on how they work.
  46. Container types
  47. ---------------
  48. Godot provides several container types out of the box as they serve different purposes:
  49. Box Containers
  50. ^^^^^^^^^^^^^^
  51. Arranges child controls vertically or horizontally (via :ref:`HBoxContainer <class_HBoxContainer>` and
  52. :ref:`VBoxContainer <class_VBoxContainer>`). In the opposite of the designated direction
  53. (as in, vertical for a horizontal container), it just expands the children.
  54. .. image:: img/containers_box.png
  55. These containers make use of the *Stretch Ratio* property for children with the *Expand* flag set.
  56. Grid Container
  57. ^^^^^^^^^^^^^^
  58. Arranges child controls in a grid layout (via :ref:`GridContainer <class_GridContainer>`, amount
  59. of columns must be specified). Uses both the vertical and horizontal expand flags.
  60. .. image:: img/containers_grid.png
  61. Margin Container
  62. ^^^^^^^^^^^^^^^^
  63. Child controls are expanded towards the bounds of this control (via
  64. :ref:`MarginContainer <class_MarginContainer>`). Padding will be added on the margins
  65. depending on the theme configuration.
  66. .. image:: img/containers_margin.png
  67. Again, keep in mind that the margins are a *Theme* value, so they need to be edited from the
  68. constants overrides section of each control:
  69. .. image:: img/containers_margin_constants.png
  70. Tab Container
  71. ^^^^^^^^^^^^^
  72. Allows you to place several child controls stacked on top of each other (via
  73. :ref:`TabContainer <class_TabContainer>`), with only the *current* one visible.
  74. .. image:: img/containers_tab.png
  75. Changing the *current* one is done via tabs located at the top of the container, via clicking:
  76. .. image:: img/containers_tab_click.gif
  77. The titles are generated from the node names by default (although they can be overridden via *TabContainer* API).
  78. Settings such as tab placement and *StyleBox* can be modified in the *TabContainer* theme overrides.
  79. Split Container
  80. ^^^^^^^^^^^^^^^
  81. Accepts only one or two children controls, then places them side to side with a divisor
  82. (via :ref:`HSplitContainer <class_HSplitContainer>` and :ref:`VSplitContainer <class_VSplitContainer>`).
  83. Respects both horizontal and vertical flags, as well as *Ratio*.
  84. .. image:: img/containers_split.png
  85. The divisor can be dragged around to change the size relation between both children:
  86. .. image:: img/containers_split_drag.gif
  87. PanelContainer
  88. ^^^^^^^^^^^^^^
  89. A container that draws a *StyleBox*, then expands children to cover its whole area
  90. (via :ref:`PanelContainer <class_PanelContainer>`, respecting the *StyleBox* margins).
  91. It respects both the horizontal and vertical sizing options.
  92. .. image:: img/containers_panel.png
  93. This container is useful as top-level, or just to add custom backgrounds to sections of a layout.
  94. ScrollContainer
  95. ^^^^^^^^^^^^^^^
  96. Accepts a single child node. If this node is bigger than the container, scrollbars will be added
  97. to allow panning the node around (via :ref:`ScrollContainer <class_ScrollContainer>`). Both
  98. vertical and horizontal size options are respected, and the behavior can be turned on or off
  99. per axis in the properties.
  100. .. image:: img/containers_scroll.png
  101. Mouse wheel and touch drag (when touch is available) are also valid ways to pan the child control around.
  102. .. image:: img/containers_center_pan.gif
  103. As in the example above, one of the most common ways to use this container is together with a *VBoxContainer* as child.
  104. AspectRatioContainer
  105. ^^^^^^^^^^^^^^^^^^^^
  106. A container type that arranges its child controls in a way that preserves their proportions
  107. automatically when the container is resized.
  108. (via :ref:`AspectRatioContainer <class_AspectRatioContainer>`).
  109. It has multiple stretch modes, providing options for adjusting the child controls' sizes concerning the container:
  110. "fill," "width control height," "height control width," and "cover."
  111. .. image:: img/containers_aspectratio.webp
  112. useful when you have a container that needs to be dynamic and responsive to different screen sizes,
  113. and you want the child elements to scale proportionally without losing their intended shapes.
  114. .. image:: img/containers_aspectratio_drag.webp
  115. FlowContainer
  116. ^^^^^^^^^^^^^^
  117. FlowContainer is a container that arranges its child controls either horizontally or vertically,
  118. (via :ref:`HFlowContainer <class_HFlowContainer>` and via :ref:`VFlowContainer <class_VFlowContainer>`).
  119. and when the available space runs out, it wraps the children to the next line or column, similar to how text wraps in a book.
  120. .. image:: img/containers_hflow.webp
  121. useful for creating flexible layouts where the child controls adjust automatically to the available space without overlapping.
  122. .. image:: img/containers_hflow_drag.webp
  123. CenterContainer
  124. ^^^^^^^^^^^^^^^^^^^^
  125. CenterContainer is a container that automatically keeps all of its child controls centered within it at their minimum size.
  126. It ensures that the child controls are always aligned to the center, making it easier to create centered layouts without manual positioning.
  127. (via :ref:`CenterContainer <class_CenterContainer>`).
  128. .. image:: img/containers_center.webp
  129. .. image:: img/containers_center_drag.webp
  130. SubViewportContainer
  131. ^^^^^^^^^^^^^^^^^^^^
  132. This is a special control that will only accept a single *Viewport* node as child, and it will display
  133. it as if it was an image (via :ref:`SubViewportContainer <class_SubViewportContainer>`).
  134. Creating custom Containers
  135. --------------------------
  136. It is possible to create a custom container using a script.
  137. Here is an example of a container that fits children to its rect size:
  138. .. tabs::
  139. .. code-tab:: gdscript GDScript
  140. extends Container
  141. func _notification(what):
  142. if what == NOTIFICATION_SORT_CHILDREN:
  143. # Must re-sort the children
  144. for c in get_children():
  145. # Fit to own size
  146. fit_child_in_rect(c, Rect2(Vector2(), rect_size))
  147. func set_some_setting():
  148. # Some setting changed, ask for children re-sort.
  149. queue_sort()
  150. .. code-tab:: csharp
  151. using Godot;
  152. public partial class CustomContainer : Container
  153. {
  154. public override void _Notification(int what)
  155. {
  156. if (what == NotificationSortChildren)
  157. {
  158. // Must re-sort the children
  159. foreach (Control c in GetChildren())
  160. {
  161. // Fit to own size
  162. FitChildInRect(c, new Rect2(new Vector2(), RectSize));
  163. }
  164. }
  165. }
  166. public void SetSomeSetting()
  167. {
  168. // Some setting changed, ask for children re-sort.
  169. QueueSort();
  170. }
  171. }