ui_introduction_to_the_ui_system.rst 15 KB

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