ui_main_menu.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. Let's create the main menu. We'll build it in a single scene. To create
  88. an empty scene, click on **Scene > New Scene**.
  89. We have to add a root node before we can save the scene. Your UI's root
  90. should be the outermost container or element. In this case it's a
  91. ``MarginContainer``. ``MarginContainer`` is a good starting point for
  92. most interfaces, as you often need padding around the UI. Press
  93. :kbd:`Ctrl + S` (:kbd:`Cmd + S` on macOS) to save the scene to the disk. Name it *MainMenu*.
  94. Select the ``MarginContainer`` again, and head to the inspector to
  95. define the margins' size. Scroll down the ``Control`` class, to the
  96. ``Custom Constants`` section. Unfold it. Set the margins as such:
  97. - Margin Right: *120*
  98. - Margin Top: *80*
  99. - Margin Left: *120*
  100. - Margin Bottom: *80*
  101. We want the container to fit the window. In the toolbar above the Viewport,
  102. open the **Layout** menu and select the last option, **Full Rect**.
  103. Add the UI sprites
  104. ~~~~~~~~~~~~~~~~~~
  105. Select the ``MarginContainer``, and create the UI elements as
  106. ``TextureRect`` nodes. We need:
  107. 1. the title or logo,
  108. 2. the three text options as individual nodes,
  109. 3. the version note,
  110. 4. and the main menu's illustration.
  111. Click the **Add Node** button or press :kbd:`Ctrl + A` (:kbd:`Cmd + A` on macOS) on your keyboard.
  112. Start to type ``TextureRect`` to find the corresponding node and press
  113. enter. With the new node selected, press :kbd:`Ctrl + D` (:kbd:`Cmd + D` on macOS) five times to
  114. create five extra ``TextureRect`` instances.
  115. Click each of the nodes to select it. In the inspector, find the **Texture**
  116. property and click **[empty] > Load**. A file browser opens and lets
  117. you pick a sprite to load into the texture slot.
  118. .. figure:: img/ui_texturerect_load_texture.png
  119. The file browser lets you find and load textures.
  120. Repeat the operation for all ``TextureRect`` nodes. You should have the
  121. logo, the illustration, the three menu options and the version note,
  122. each as a separate node. Then, double click on each of the nodes in the
  123. Scene tab to rename them. Nothing has been placed in containers yet so this
  124. should look messy.
  125. .. figure:: img/ui_main_menu_6_texturerect_nodes.png
  126. The six nodes with textures loaded.
  127. .. note::
  128. If you want to support localization in your game, use
  129. ``Labels`` for menu options instead of ``TextureRect``.
  130. Add containers to place UI elements automatically
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. Our main menu has some margin around the edges of the screen. It is
  133. split in two parts: on the left, you have the logo and the menu options.
  134. On the right, you have the characters. We can use one of two containers
  135. to achieve this: ``HSplitContainer`` or ``HBoxContainer``. Split
  136. containers split the area into two: a left and a right side or a top and
  137. a bottom side. They also allow the user to resize the left and right
  138. areas using an interactive bar. On the other hand, ``HBoxContainer``
  139. just splits itself into as many columns as it has children. Although you
  140. can deactivate the split container's resize behavior, I recommend to
  141. favor box containers.
  142. Select the ``MarginContainer`` and add an ``HBoxContainer``. Then, we
  143. need two containers as children of our ``HBoxContainer``: a
  144. ``VBoxContainer`` for the menu options on the left, and a
  145. ``CenterContainer`` for the illustration on the right.
  146. .. figure:: img/ui_main_menu_containers_step_1.png
  147. You should have four nested containers and the TextureRect nodes
  148. sitting aside from it.
  149. In the node tree, select all the ``TextureRect`` nodes that should go on the
  150. left side: the logo, the menu options (Continue, NewGame, Options), and the
  151. version note. Drag and drop them into the ``VBoxContainer``. The nodes should
  152. position automatically.
  153. .. figure:: img/ui_main_menu_containers_step_2.png
  154. Containers automatically place and resize textures
  155. We're left with two problems to solve:
  156. 1. The characters on the right aren't centered.
  157. 2. There's no space between the logo and the other UI elements.
  158. To center the characters on the right, first select the ``CenterContainer``.
  159. Then in the Inspector, scroll down to the **Size Flags** category and click
  160. on the field to the right of the **Vertical** property, and check **Expand**
  161. in addition to **Fill**. Do the same for the **Horizontal** property. This
  162. makes the ``CenterContainer`` expand into all available space while
  163. respecting its neighbour ``VBoxContainer``. Finally, drag and drop the
  164. Characters node into the ``CenterContainer``. The Characters element will center
  165. automatically.
  166. .. figure:: img/ui_main_menu_containers_step_3.png
  167. The character node centers inside the right half of the screen as
  168. soon as you place it inside the CenterContainer.
  169. To space out the menu options and the logo on the left, we'll use one
  170. final container and its size flags. Select the ``VBoxContainer`` and
  171. press :kbd:`Ctrl + A` (:kbd:`Cmd + A` on macOS) to add a new node inside it. Add a second
  172. ``VBoxContainer`` and name it *MenuOptions*. Select all three menu
  173. options, ``Continue``, ``NewGame`` and ``Options``, and drag and drop
  174. them inside the new ``VBoxContainer``. The UI's layout should barely
  175. change, if at all.
  176. .. figure:: img/ui_main_menu_containers_step_4.png
  177. Place the new container between the other two nodes to retain the
  178. UI's layout.
  179. Now we grouped the menu options together, we can tell their container to
  180. expand to take as much vertical space as possible. Select the
  181. ``MenuOptions`` node. In the Inspector, scroll down to the
  182. **Size Flags** category. Click on the field to the right of the
  183. **Vertical** property, and check **Expand** in addition to **Fill**.
  184. The container expands to take all the available vertical space
  185. while respecting its neighbors, the ``Logo`` and ``Version`` elements.
  186. To center the nodes in the ``VBoxContainer``, scroll to the top of the
  187. Inspector and change the **Alignment** property to **Center**.
  188. .. figure:: img/ui_main_menu_containers_step_5.png
  189. The menu options should center vertically in the UI's left column.
  190. To wrap things up, let's add some separation between the menu options.
  191. Expand the **Custom Constants** category below **Size Flags**, and click
  192. the field next to the **Separation** parameter. Set it to 30. Once you
  193. press enter, the **Separation** property becomes active and Godot adds
  194. 30 pixels between menu options.
  195. .. figure:: img/ui_main_menu_design_final_result.png
  196. The final interface.
  197. Without a single line of code, we have a precise and responsive main
  198. menu.
  199. Congratulations for getting there! You can download the final
  200. menu :download:`ui_main_menu_design.zip <files/ui_main_menu_design.zip>`
  201. to compare with your own. In the next tutorial, you'll
  202. create a Game User Interface with bars and item counters.
  203. Break down the UI mockup
  204. ~~~~~~~~~~~~~~~~~~~~~~~~
  205. A responsive User Interface is all about making sure our UIs scale well on
  206. all screen types. TV screens and computer displays have different sizes
  207. and ratios. In Godot, we use containers to control the position and the
  208. size of UI elements.
  209. The order in which you nest matters. To see if your
  210. UI adapts nicely to different screen ratios, select the root node, press
  211. :kbd:`Q` to activate the Select Mode, select the container and click
  212. and drag on one of the container's corners to resize it. The UI
  213. components should flow inside of it.
  214. You'll notice that although
  215. containers move sprites around, they don't scale them. This is normal.
  216. We want the UI system to handle different screen ratios, but we also
  217. need the entire game to adapt to different screen resolutions. To do
  218. this, Godot scales the entire window up and down.
  219. You can change the scale mode in the project settings: click
  220. **Project > Project Settings** in the top menu. In the window's left column,
  221. look for the **Display** category. Click on the **Window** sub-category.
  222. On the right side of the window, you'll find a **Stretch** section.
  223. The three settings, **Mode**, **Aspect**, and **Shrink**, control the
  224. screen size. For more information, see :ref:`doc_multiple_resolutions`.