ui_main_menu.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. .. _doc_ui_main_menu:
  2. Design a title screen
  3. =====================
  4. In the next two tutorials, you will build two responsive UI (user interface)
  5. scenes step-by-step using the engine's UI system:
  6. 1. A main menu.
  7. 2. A game UI with a health bar, energy bar, bomb and money counters.
  8. You will learn how to design game UIs efficiently, and how to use Godot's
  9. Control nodes. This page focuses on the visual part: everything you do
  10. from the editor. To learn how to code a life bar,
  11. read :doc:`ui_code_a_life_bar`.
  12. .. figure:: img/ui_main_menu_design_final_result.png
  13. The GUI you're going to create.
  14. Download the project files: :download:`ui_main_menu_design.zip
  15. <files/ui_main_menu_design.zip>` and extract the archive. Import the ``start/``
  16. project in Godot to follow this tutorial. The ``end/`` folder contains the
  17. final result. You'll find all the sprites in the ``start/assets/main_menu``
  18. folder.
  19. .. note::
  20. Read the :doc:`ui_introduction_to_the_ui_system` first to learn how
  21. Godot's UI system works.
  22. How to design your game UI
  23. --------------------------
  24. To design a good UI, you want to come up with a rough mockup first: a
  25. plain drawing version that focuses on the placement of your UI
  26. components, their size, and user interaction. Pen and paper is all you
  27. need. You shouldn't use fancy and final graphics at this stage. Then,
  28. you only need simple placeholder sprites and you're good to jump into
  29. Godot. You want to make sure the players can find their way around the
  30. interface using those placeholders.
  31. .. figure:: img/ui_design_rough.png
  32. The UI's rough plan or mockup
  33. Placeholder doesn't have to mean ugly, but you should keep the graphics
  34. simple and clean. Avoid special effects, animation, and detailed
  35. illustration before you have players playtest your UI. Otherwise:
  36. 1. The graphics might skew the players' perception of the experience and
  37. you'll miss out on valuable feedback.
  38. 2. If the User Experience doesn't work, you'll have to redo some sprites.
  39. .. tip::
  40. Always try to make the interface work with simple text and
  41. boxes first. It's easy to replace the textures later. Professional UX
  42. designers often work with plain outlines and boxes in greyscale. When
  43. you take colors and fancy visuals away, it's a lot easier to size and
  44. place UI elements properly. It helps you refine the design foundation
  45. you'll build upon.
  46. There are two ways to design your UI in Godot. You can:
  47. 1. Build it all in a single scene, and eventually save some branches as
  48. reusable scenes.
  49. 2. Build template scenes for reusable components and create specific
  50. components that inherit from your base scenes.
  51. We will use the first approach, because the first version of your UI may
  52. not work as well as you'd like. You're likely to throw parts away and
  53. redesign components as you go. When you're sure everything works, it's
  54. easy to make some parts reusable, as you'll see below.
  55. .. figure:: img/ui_main_menu_placeholder_assets.png
  56. The files you'll find in Godot. The graphics look cleaner than on the
  57. rough design, but they're still placeholders.
  58. Design the main menu
  59. --------------------
  60. Before we jump into the editor, we want to plan how we'll nest
  61. containers based on our mockup image.
  62. Break down the UI mockup
  63. ~~~~~~~~~~~~~~~~~~~~~~~~
  64. Here are my three rules of thumb to find the right containers:
  65. 1. Break down the UI into nested boxes, from the largest that contains
  66. everything, to the smallest ones, that encompass one widget, like a
  67. bar with its label, a panel or a button.
  68. 2. If there's some padding around an area, use a ``MarginContainer``.
  69. 3. If the elements are arranged in rows or columns, use an
  70. ``HBoxContainer`` or ``VBoxContainer``.
  71. These rules are enough to get us started, and work well for simple
  72. interfaces.
  73. For the main menu, the largest box is the entire game window. There's
  74. padding between the edges of the window and the first components: this
  75. should be a ``MarginContainer``. Then, the screen is split into two
  76. columns, so we'll use an ``HBoxContainer``. In the left column, we'll
  77. manage the rows with a ``VBoxContainer``. And in the right column, we'll
  78. center the illustration with a ``CenterContainer``.
  79. .. figure:: img/ui_mockup_break_down.png
  80. Interface building blocks, broken down using the three rules of thumb.
  81. .. tip::
  82. Containers adapt to the window's resolution and width-to-height
  83. ratio. Although we could place UI elements by hand, containers are
  84. faster, more precise, and **responsive**.
  85. Prepare the Main Menu scene
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. .. note::
  88. This tutorial is based on a window size of 1366×768. To change the project's
  89. base window size, open **Project > Project Settings** at the top of the
  90. editor then change **Display > Window > Size > Width** to ``1366`` and
  91. **Display > Window > Size > Height** to ``768``.
  92. If you forget to change the window size, anchors and containers may not
  93. behave as expected.
  94. Let's create the main menu. We'll build it in a single scene. To create
  95. an empty scene, click on **Scene > New Scene**.
  96. We have to add a root node before we can save the scene. Your UI's root
  97. should be the outermost container or element. In this case it's a
  98. ``MarginContainer``. ``MarginContainer`` is a good starting point for
  99. most interfaces, as you often need padding around the UI. Press
  100. :kbd:`Ctrl + S` (:kbd:`Cmd + S` on macOS) to save the scene to the disk. Name it *MainMenu*.
  101. Select the ``MarginContainer`` again, and head to the inspector to
  102. define the margins' size. Scroll down the ``Control`` class, to the
  103. ``Custom Constants`` section. Unfold it. Set the margins as such:
  104. - Margin Right: *120*
  105. - Margin Top: *80*
  106. - Margin Left: *120*
  107. - Margin Bottom: *80*
  108. We want the container to fit the window. In the toolbar above the Viewport,
  109. open the **Layout** menu and select the last option, **Full Rect**.
  110. Add the UI sprites
  111. ~~~~~~~~~~~~~~~~~~
  112. Select the ``MarginContainer``, and create the UI elements as
  113. ``TextureRect`` nodes. We need:
  114. 1. the title or logo,
  115. 2. the three text options as individual nodes,
  116. 3. the version note,
  117. 4. and the main menu's illustration.
  118. Click the **Add Node** button or press :kbd:`Ctrl + A` (:kbd:`Cmd + A` on macOS) on your keyboard.
  119. Start to type ``TextureRect`` to find the corresponding node and press
  120. enter. With the new node selected, press :kbd:`Ctrl + D` (:kbd:`Cmd + D` on macOS) five times to
  121. create five extra ``TextureRect`` instances.
  122. Click each of the nodes to select it. In the inspector, find the **Texture**
  123. property and click **[empty] > Load**. A file browser opens and lets
  124. you pick a sprite to load into the texture slot.
  125. .. figure:: img/ui_texturerect_load_texture.png
  126. The file browser lets you find and load textures.
  127. Repeat the operation for all ``TextureRect`` nodes. You should have the
  128. logo, the illustration, the three menu options and the version note,
  129. each as a separate node. Then, double click on each of the nodes in the
  130. Scene tab to rename them. Nothing has been placed in containers yet so this
  131. should look messy.
  132. .. figure:: img/ui_main_menu_6_texturerect_nodes.png
  133. The six nodes with textures loaded.
  134. .. note::
  135. If you want to support localization in your game, use
  136. ``Labels`` for menu options instead of ``TextureRect``.
  137. Add containers to place UI elements automatically
  138. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139. Our main menu has some margin around the edges of the screen. It is
  140. split in two parts: on the left, you have the logo and the menu options.
  141. On the right, you have the characters. We can use one of two containers
  142. to achieve this: ``HSplitContainer`` or ``HBoxContainer``. Split
  143. containers split the area into two: a left and a right side or a top and
  144. a bottom side. They also allow the user to resize the left and right
  145. areas using an interactive bar. On the other hand, ``HBoxContainer``
  146. just splits itself into as many columns as it has children. Although you
  147. can deactivate the split container's resize behavior, I recommend to
  148. favor box containers.
  149. Select the ``MarginContainer`` and add an ``HBoxContainer``. Then, we
  150. need two containers as children of our ``HBoxContainer``: a
  151. ``VBoxContainer`` for the menu options on the left, and a
  152. ``CenterContainer`` for the illustration on the right.
  153. .. figure:: img/ui_main_menu_containers_step_1.png
  154. You should have four nested containers and the TextureRect nodes
  155. sitting aside from it.
  156. In the node tree, select all the ``TextureRect`` nodes that should go on the
  157. left side: the logo, the menu options (Continue, NewGame, Options), and the
  158. version note. Drag and drop them into the ``VBoxContainer``. The nodes should
  159. position automatically.
  160. .. figure:: img/ui_main_menu_containers_step_2.png
  161. Containers automatically place and resize textures
  162. We're left with two problems to solve:
  163. 1. The characters on the right aren't centered.
  164. 2. There's no space between the logo and the other UI elements.
  165. To center the characters on the right, first select the ``CenterContainer``.
  166. Then in the Inspector, scroll down to the **Size Flags** category and click
  167. on the field to the right of the **Vertical** property, and check **Expand**
  168. in addition to **Fill**. Do the same for the **Horizontal** property. This
  169. makes the ``CenterContainer`` expand into all available space while
  170. respecting its neighbour ``VBoxContainer``. Finally, drag and drop the
  171. Characters node into the ``CenterContainer``. The Characters element will center
  172. automatically.
  173. .. figure:: img/ui_main_menu_containers_step_3.png
  174. The character node centers inside the right half of the screen as
  175. soon as you place it inside the CenterContainer.
  176. To space out the menu options and the logo on the left, we'll use one
  177. final container and its size flags. Select the ``VBoxContainer`` and
  178. press :kbd:`Ctrl + A` (:kbd:`Cmd + A` on macOS) to add a new node inside it. Add a second
  179. ``VBoxContainer`` and name it *MenuOptions*. Select all three menu
  180. options, ``Continue``, ``NewGame`` and ``Options``, and drag and drop
  181. them inside the new ``VBoxContainer``. The UI's layout should barely
  182. change, if at all.
  183. .. figure:: img/ui_main_menu_containers_step_4.png
  184. Place the new container between the other two nodes to retain the
  185. UI's layout.
  186. Now we grouped the menu options together, we can tell their container to
  187. expand to take as much vertical space as possible. Select the
  188. ``MenuOptions`` node. In the Inspector, scroll down to the
  189. **Size Flags** category. Click on the field to the right of the
  190. **Vertical** property, and check **Expand** in addition to **Fill**.
  191. The container expands to take all the available vertical space
  192. while respecting its neighbors, the ``Logo`` and ``Version`` elements.
  193. To center the nodes in the ``VBoxContainer``, scroll to the top of the
  194. Inspector and change the **Alignment** property to **Center**.
  195. .. figure:: img/ui_main_menu_containers_step_5.png
  196. The menu options should center vertically in the UI's left column.
  197. To wrap things up, let's add some separation between the menu options.
  198. Expand the **Custom Constants** category below **Size Flags**, and click
  199. the field next to the **Separation** parameter. Set it to 30. Once you
  200. press enter, the **Separation** property becomes active and Godot adds
  201. 30 pixels between menu options.
  202. .. figure:: img/ui_main_menu_design_final_result.png
  203. The final interface.
  204. Without a single line of code, we have a precise and responsive main
  205. menu.
  206. Congratulations for getting there! You can download the final
  207. menu :download:`ui_main_menu_design.zip <files/ui_main_menu_design.zip>`
  208. to compare with your own. In the next tutorial, you'll
  209. create a Game User Interface with bars and item counters.
  210. Break down the UI mockup
  211. ~~~~~~~~~~~~~~~~~~~~~~~~
  212. A responsive User Interface is all about making sure our UIs scale well on
  213. all screen types. TV screens and computer displays have different sizes
  214. and ratios. In Godot, we use containers to control the position and the
  215. size of UI elements.
  216. The order in which you nest matters. To see if your
  217. UI adapts nicely to different screen ratios, select the root node, press
  218. :kbd:`Q` to activate the Select Mode, select the container and click
  219. and drag on one of the container's corners to resize it. The UI
  220. components should flow inside of it.
  221. You'll notice that although
  222. containers move sprites around, they don't scale them. This is normal.
  223. We want the UI system to handle different screen ratios, but we also
  224. need the entire game to adapt to different screen resolutions. To do
  225. this, Godot scales the entire window up and down.
  226. You can change the scale mode in the project settings: click
  227. **Project > Project Settings** in the top menu. In the window's left column,
  228. look for the **Display** category. Click on the **Window** sub-category.
  229. On the right side of the window, you'll find a **Stretch** section.
  230. The three settings, **Mode**, **Aspect**, and **Shrink**, control the
  231. screen size. For more information, see :ref:`doc_multiple_resolutions`.