ui_main_menu.rst 12 KB

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