ui_introduction_to_the_ui_system.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. .. _doc_design_interfaces_with_the_control_nodes:
  2. Design interfaces with the Control nodes
  3. ========================================
  4. Computer displays, mobile phones, and TV screen come in all shapes and
  5. sizes. To ship a game, you'll need to support different screen ratios
  6. and resolutions. It can be hard to build responsive interfaces that
  7. adapt to all platforms. Thankfully, Godot comes with robust tools to
  8. design and manage responsive User Interface. To design your UI, you'll
  9. use the Control nodes. These are the nodes with green icons in the
  10. editor. There are dozens of them, to create anything from life bars to
  11. complex applications. Godot's entire editor and plugins use these nodes.
  12. .. figure:: img/godot_editor_ui.png
  13. Godot's editor is made with the engine's UI framework
  14. This guide will get you started with UI design. You will learn:
  15. - The five most useful control nodes to build your games’ interface
  16. - How to work with the anchor of UI elements
  17. - How to efficiently place and arrange your user interface using
  18. containers
  19. - The five most common containers
  20. To learn how to control the interface and connect it to other scripts,
  21. read `Build your first game UI in Godot <#>`__.
  22. Only use Control nodes when you design your interfaces. They have unique
  23. properties that allow them to work with one another. Other nodes like
  24. Node2D, Sprite, etc. will not work. You can still use some nodes that
  25. work with others like the AnimationPlayer, Tween or the StreamPlayer.
  26. Control nodes are CanvasItems like Node2D, so you can apply shaders to
  27. them.
  28. All control nodes share the same main properties:
  29. 1. Anchor
  30. 2. Bounding rectangle
  31. 3. Focus and focus neighbour
  32. 4. Size flags
  33. 5. Margin
  34. 6. The optional UI theme
  35. Once you understand the basics of the Control node, it will take you less time to learn all the
  36. nodes that derive from it.
  37. The 5 most common UI elements
  38. -----------------------------
  39. Godot ships with dozens of Control nodes. A lot of them are here to help
  40. you build editor plugins and applications. To learn more about them,
  41. check the guide about `Advanced UI nodes and Themes <img/#>`__.
  42. For most games, you'll only need five types of UI elements, and a few
  43. Containers. These five Control nodes are:
  44. 1. Label: for displaying text
  45. 2. TextureRect: used mostly for backgrounds, or everything that should
  46. be a static image
  47. 3. TextureProgress: for lifebars, loading bars, horizontal, vertical or
  48. radial
  49. 4. NinePatchRect: for scalable panels
  50. 5. TextureButton: to create buttons
  51. .. figure:: img/five_most_common_nodes.png
  52. The 5 most common Control nodes for UI design
  53. TextureRect
  54. ~~~~~~~~~~~
  55. **TextureRect** displays a texture or image inside a UI.
  56. It seems similar to the Sprite node but it offers multiple scaling modes.
  57. Set the Stretch Mode property to change its behaviour:
  58. - ``Tile`` makes the texture repeat, but it won't scale
  59. - The default, ``Scale and Expand (compat)``, lets you scale the
  60. texture up, but it forces a minimal size
  61. - ``Keep`` and ``Keep Centered`` force the texture to remain at its
  62. original size, in the top left corner or the center of the frame
  63. respectively
  64. As with Sprite nodes, you can modulate the TextureRect's colour. Click
  65. the ``Modulate`` property and use the color picker.
  66. .. figure:: img/five_common_nodes_textureframe.png
  67. TextureRect modulated with a red color
  68. TextureButton
  69. ~~~~~~~~~~~~~
  70. **TextureButton** is like TextureRect, except it has 5 texture slots:
  71. one for each of the button's states. Most of the time, you'll use the
  72. Normal, Pressed, and Hover textures. Focused is useful if your interface
  73. listens to the keyboard's input. The sixth image slot, the Click Mask,
  74. lets you define the clickable area using a 2-bit, pure black and white
  75. image.
  76. In the Base Button section, you'll find a few checkboxes that change how
  77. the button behaves. When ``Toggle Mode`` is on, the button will toggle
  78. between active and normal states when you press it. ``Disabled`` makes it
  79. disabled by default, in which case it will use the ``Disabled`` texture.
  80. TextureButton shares a few properties with the texture frame: it has a
  81. ``modulate`` property, to change its color, and ``Resize`` and ``Stretch`` modes to
  82. change its scale behavior.
  83. .. figure:: img/five_common_nodes_texturebutton.png
  84. TextureButton and its 5 texture slots
  85. TextureProgress
  86. ~~~~~~~~~~~~~~~
  87. **TextureProgress** layers up to 3 sprites to create a progress bar. The
  88. Under and Over textures sandwich the Progress one, which displays the
  89. bar's value.
  90. The ``Mode`` property controls the direction in which the bar grows:
  91. horizontally, vertically, or radially. If you set it to radial, the
  92. ``Initial Angle`` and ``Fill Degrees`` properties let you limit the range of the
  93. gauge.
  94. To animate the bar, you'll want to look at the Range section. Set the
  95. ``Min`` and ``Max`` properties to define the range of the gauge. For instance,
  96. to represent a character's life, you'll want to set ``Min`` to ``0,`` and ``Max`` to
  97. the character's maximum life. Change the ``Value`` property to update the
  98. bar. If you leave the ``Min`` and ``Max`` values to the default of ``1`` and ``100,``
  99. and set the ``Value`` property to ``40``, 40% of the ``Progress`` texture will show
  100. up, and 60% of it will stay hidden.
  101. .. figure:: img/five_common_nodes_textureprogress.png
  102. TextureProgress bar, two thirds filled
  103. Label
  104. ~~~~~
  105. **Label** prints text to the screen. You'll find all its properties in
  106. the Label section, in the Inspector. Write the text in the ``Text``
  107. property, and check Autowrap if you want it to respect the textbox's
  108. size. If Autowrap is off, you won't be able to scale the node. You can
  109. align the text horizontally and vertically with Align and Valign
  110. respectively.
  111. .. figure:: img/five_common_nodes_label.png
  112. Picture of a Label
  113. NinePatchRect
  114. ~~~~~~~~~~~~~
  115. **NinePatchRect** takes a texture split in 3 rows and 3 columns. The
  116. center and the sides tile when you scale the texture, but it never
  117. scales the corners. It is very useful to build panels, dialogue boxes
  118. and scalable backgrounds for your UI.
  119. .. figure:: img/five_common_nodes_ninepatchrect.png
  120. NinePatchRect scaled with the min\_size property
  121. Place UI elements precisely with anchors
  122. ----------------------------------------
  123. Control nodes have a position and size, but they also have anchors and
  124. margins. Anchors define the origin, or the reference point, for the
  125. Left, Top, Right and Bottom edges of the node. Change any of the 4
  126. anchors, and the margins will update automatically.
  127. .. figure:: img/anchor_property.png
  128. The anchor property
  129. How to change the anchor
  130. ~~~~~~~~~~~~~~~~~~~~~~~~
  131. Like any properties, you can edit the 4 anchor points in the Inspector,
  132. but this is not the most convenient way. When you select a control node,
  133. the layout menu appears above the viewport, in the toolbar. It gives you
  134. a list of icons to set all 4 anchors with a single click, instead of
  135. using the inspector’s 4 properties. The layout menu will only show up
  136. when you select a control node.
  137. .. figure:: img/layout_menu.png
  138. The layout menu in the viewport
  139. Anchors are relative to the parent container
  140. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  141. Each anchor is a value between 0 and 1. For the left and top anchors, a
  142. value of 0 means that without any margin, the node's edges will align
  143. with the left and top edges of its parent. For the right and bottom
  144. edges, a value of 1 means they'll align with the parent container's
  145. right and bottom edges. On the other hand, margins represent a distance
  146. to the anchor position in pixels, while anchors are relative to the
  147. parent container's size.
  148. .. figure:: ./img/ui_anchor_and_margins.png
  149. Margins are relative to the anchor position, which is relative to the
  150. anchors. In practice, you'll often let the container update margins
  151. for you
  152. Margins change with the anchor
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. Margins update automatically when you move or resize a control node.
  155. They represent the distance from the control node's edges to its anchor,
  156. which is relative to the parent control node or container. That's why
  157. your control nodes should always be inside a container, as we'll see in
  158. a moment. If there's no parent, the margins will be relative to the
  159. node's own bounding Rectangle, set in the Rect section, in the
  160. inspector.
  161. .. figure:: img/control_node_margin.png
  162. Margins on a CenterContainer set to the "Full Rect" anchor
  163. Try to change the anchors or nest your Control nodes inside Containers:
  164. the margins will update. You'll rarely need to edit the margins
  165. manually. Always try to find a container to help you first; Godot comes
  166. with nodes to solve all the common cases for you. Need to add space
  167. between a lifebar and the border of the screen? Use the MarginContainer.
  168. Want to build a vertical menu? Use the VBoxContainer. More on these
  169. below.
  170. Use size tags to change how UI elements fill the available space
  171. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. Every control node has Size Flags. They tell containers how the UI
  173. elements should scale. If you add the "Fill" flag to the Horizontal or
  174. Vertical property, the node's bounding box will take all the space it
  175. can, but it'll respect its siblings and retain its size. If there are 3
  176. TextureFrame nodes in an HBoxContainer, with the "Fill" flags on both
  177. axes, they'll each take up to a third of the available space, but no
  178. more. The container will take over the node and resize it automatically.
  179. .. figure:: img/textureframe_in_box_container_fill.png
  180. 3 UI elements in an HBoxContainer, they align horizontally
  181. The "Expand" flag lets the UI element take all the space it can, and
  182. push against its siblings. Its bounding rectangle will grow against the
  183. edges of its parent, or until it's blocked by another UI node.
  184. .. figure:: img/textureframe_in_box_container_expand.png
  185. The same example as bove, but the left node has the "Expand" size
  186. flag
  187. You'll need some practice to understand the size tags, as their effect
  188. can change quite a bit depending on how you set up your interface.
  189. Arrange control nodes automatically with containers
  190. ---------------------------------------------------
  191. Containers automatically arrange all children Control nodes including
  192. other containers in rows, columns, and more. Use them to add padding
  193. around your interface or center nodes in their bounding rectangles. All
  194. built-in containers update in the editor so you can see the effect
  195. instantly.
  196. Containers have a few special properties to control how they arrange UI
  197. elements. To change them, navigate down to the Custom Constants section
  198. in the Inspector.
  199. The 5 most useful containers
  200. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  201. If you build tools, you might need all of the containers. But for most
  202. games, a handful will be enough:
  203. - MarginContainer, to add margins around part of the UI
  204. - CenterContainer, to center its children in its bounding box
  205. - VboxContainer and HboxContainer, to arrange UI elements in rows or
  206. columns
  207. - GridContainer, to arrange Controls nodes in a grid-like pattern
  208. CenterContainer centers all its children inside of its bounding
  209. rectangle. It's one you typically use for title screens, if you want the
  210. options to stay in the center of the viewport. As it centers everything,
  211. you'll often want a single container nested inside it. If you use
  212. textures and buttons instead, they'll stack up.
  213. .. figure:: img/five_containers_centercontainer.png
  214. CenterContainer in action. The life bar centers inside its parent
  215. container.
  216. The MarginContainer adds a margin on any side of the child nodes. Add a
  217. MarginContainer that encompasses the entire viewport to add a separation
  218. between the edge of the window and the UI. You can set a margin on the
  219. top, left, right, or bottom side of the container. No need to tick the
  220. checkbox: click the corresponding value box and type any number. It will
  221. activate automatically.
  222. .. figure:: img/five_containers_margincontainer.png
  223. The MarginContainer adds a 40px margin around the Game User Interface
  224. There are two BoxContainers: VBoxContainer and HBoxContainer. You cannot
  225. add the BoxContainer node itself, as it is a helper class, but you can
  226. use vertical and horizontal box containers. They arrange nodes either in
  227. rows or columns. Use them to line up items in a shop, or to build
  228. complex grids with rows and columns of different sizes, as you can nest
  229. them to your heart's content.
  230. .. figure:: img/five_containers_boxcontainer.png
  231. The HBoxContainer horizontally aligns UI elements
  232. VBoxContainer automatically arranges its children into a column. It puts
  233. them one after the other. If you use the separation parameter, it will
  234. leave a gap between its children. HBoxContainer arranges UI elements in
  235. a row. It's similar to the VBoxContainer, with an extra ``add_spacer``
  236. method to add a spacer control node before its first child or after its
  237. last child, from a script.
  238. The GridContainer lets you arrange UI elements in a grid-like pattern.
  239. You can only control the number of columns it has, and it will set the
  240. number of rows by itself, based on its children's count. If you have
  241. nine children and three columns, you will have 9÷3 = 3 rows. Add three
  242. more children and you'll have four rows. In other words, it will create
  243. new rows as you add more textures and buttons. Like the box containers,
  244. it has two properties to set the vertical and horizontal separation
  245. between the rows and columns respectively.
  246. .. figure:: img/five_containers_gridcontainer.png
  247. A GridContainer with 2 columns. It sizes each column automatically.
  248. Godot's UI system is complex, and has a lot more to offer. To learn how
  249. to design more advanced interface, read `Design advanced UI with other
  250. Control nodes <img/#>`__.