ui_main_menu.rst 12 KB

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