ui_game_user_interface.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. .. _doc_ui_game_user_interface:
  2. Design the GUI
  3. ==============
  4. Now that you've nailed the basics, we're going to see how to build a
  5. Game User Interface (GUI) with reusable UI components: a life bar, an
  6. energy bar, and bomb and rupee counters. By the end of this tutorial,
  7. you'll have a game GUI, ready to control with GDscript or VisualScript:
  8. .. figure:: ./img/ui_gui_design_final_result.png
  9. The final result
  10. You'll also learn to:
  11. 1. Create flexible UI components
  12. 2. Use scene inheritance
  13. 3. Build a complex UI
  14. Download the project files: :download:`ui_gui_design.zip <files/ui_gui_design.zip>` and extract the archive. Import the `start/` project in Godot to follow this tutorial. The `end/` folder contains the final result.
  15. Breaking down the UI
  16. --------------------
  17. Let's break down the final UI and plan the containers we'll use. As in
  18. the :doc:`ui_main_menu`, it starts with a ``MarginContainer``.
  19. Then, we can see up to three columns:
  20. 1. The life and energy counters on the left
  21. 2. The life and energy bars
  22. 3. The bomb and rupee counters on the right
  23. But the bar's label and the gauge are two parts of the same UI element.
  24. If we think of them this way, we're left with two columns:
  25. 1. The life and energy bars on the left
  26. 2. The bomb and rupee counters on the right
  27. This makes it easier to nest containers: we have some margins around the
  28. border of the screen using a ``MarginContainer``, followed by an
  29. ``HBoxContainer`` to manage our two columns. The two bars stack on top
  30. of one another inside a ``VBoxContainer``. And we'll need a last
  31. ``HBoxContainer`` in the right column to place the bomb and rupee
  32. counters side-by-side.
  33. .. figure:: ./img/ui_gui_step_tutorial_containers_structure.png
  34. We get a clean UI layout with only 4 containers
  35. We will need extra containers inside the individual UI components, but
  36. this gives us the main GUI scene's structure. With this plan in place,
  37. we can jump into Godot and create our GUI.
  38. Create the base GUI
  39. -------------------
  40. There are two possible approaches to the GUI: we can design elements in
  41. separate scenes and put them together, or prototype everything in a
  42. single scene and break it down later. I recommend working with a single
  43. scene as you can play with your UI's placement and proportions faster
  44. this way. Once it looks good, you can save entire sections of the node
  45. tree as reusable sub-scenes. We'll do that in a moment.
  46. For now, let's start with a few containers.
  47. Create a new scene and add a ``MarginContainer``. Select the node and
  48. name it ``GUI``. Then save the scene as ``GUI.tscn``. It will
  49. contain the entire GUI.
  50. With the ``MarginContainer`` selected, head to the inspector and scroll
  51. down to the custom constants section. Unfold it and click the field next
  52. to each of the ``Margin`` properties. Set them all to ``20`` pixels.
  53. Next, add an ``HBoxContainer`` node. This one will contain our two bars
  54. on the left and separate them from the two counters on the right.
  55. We want to stack the bars vertically inside the ``HBoxContainer``. To do
  56. this, let's add a ``VBoxContainer``. Name it ``Bars``. Select the parent
  57. ``HBoxContainer`` again and this time, add another ``HBoxContainer``.
  58. This one will hold the counters, so call it ``Counters``. With these
  59. four containers, we have the base for our GUI scene.
  60. .. figure:: ./img/ui_gui_containers_structure_in_godot.png
  61. You should have 4 containers that look like this
  62. .. note::
  63. We can work this way because we first broke down our UI design
  64. and took a few moments to think about the containers we'd use. When you
  65. follow a tutorial like this, it may seem weird. But once you're working
  66. on real games, you'll see it's an efficient workflow.
  67. Create the bars' base
  68. ~~~~~~~~~~~~~~~~~~~~~
  69. Each bar is split into two sub-elements that align horizontally: the
  70. label with the health count on the left, and the gauge on the right.
  71. Once again, the ``HBoxContainer`` is the perfect tool for the job.
  72. Select the ``Bars`` node and add a new ``HBoxContainer`` inside of it.
  73. Name it ``Bar``.
  74. The label itself requires at least three nodes: a ``NinePatchRect``
  75. for the background, on top of which we'll add a texture on the left,
  76. either ``HP`` or ``EP``, and a ``Label`` on the right for the value. We
  77. can nest ``Control`` nodes however we want. We could use the
  78. ``NinePatchRect`` as a parent for the two other elements, as it
  79. encompasses them. In general, you want to use containers instead, as
  80. their role is to help organize UI components. We'll need a
  81. ``MarginContainer`` later anyway to add some space between the life
  82. count and the gauge. Select the ``Bar`` and add a ``MarginContainer``.
  83. Name it ``Count``. Inside of it, add three nodes:
  84. 1. A ``NinePatchRect`` named ``Background``
  85. 2. A ``TextureRect`` named ``Title``
  86. 3. And a ``Label`` named ``Number``
  87. To add the nodes as siblings, always select the ``Count`` node first.
  88. .. figure:: ./img/ui_gui_step_tutorial_bar_template_1.png
  89. Your scene tree should look like this. We're ready to throw in some
  90. textures
  91. Our scene is still empty. It's time to throw in some textures. To load
  92. the textures, head to the FileSystem dock to the left of the viewport.
  93. Browse down to the res://assets/GUI folder.
  94. .. figure:: ./img/ui_gui_step_tutorial_textures_in_FileSystem_tab.png
  95. You should see a list of textures that we'll use to skin our
  96. interface.
  97. Select the ``Background`` in the Scene dock. In the Inspector, you
  98. should see a ``Texture`` property. In the FileSystem tab, click and drag
  99. ``label_HP_bg.png`` onto the ``Texture`` slot. It stays squashed. The
  100. parent MarginContainer will force its size down to 0 until we force
  101. elements inside the container to have a minimum size. Select the
  102. ``Background`` node. In the Inspector, scroll down to the Rect section.
  103. Set ``Min Size`` to (100, 40). You should see the ``Background`` resize
  104. along with its parent containers.
  105. Next, select the ``Title`` and drag and drop ``label_HP.png`` into its
  106. ``Texture`` slot. Select the ``Number`` node, click the field next to
  107. the ``Text`` property and type ``10``. This way, we can see both nodes
  108. in the viewport. They should stack up in the top-left corner of their
  109. parent ``MarginContainer``.
  110. .. figure:: ./img/ui_gui_step_tutorial_bar_label_stacked.png
  111. If you select both nodes, you should see something like this
  112. As they have a container as their direct parent, we cannot move them
  113. freely: the ``Count`` node will always reset their anchors, their size
  114. and position. Try to move and resize the nodes in the viewport. Then,
  115. select any of the three textures and press Ctrl Up or Ctrl Down to
  116. reorder them in the Scene dock. They'll snap back to their previous size
  117. and position.
  118. Parent containers control the size, the scale, the margins, and the
  119. anchors of their direct children. To modify the nodes, you must nest
  120. them inside a regular Control or another UI element. We'll use the
  121. ``Background`` as a parent for the ``Title`` and ``Number``. Select both
  122. the ``Title`` and ``Number``, and drag and drop them onto
  123. ``Background``.
  124. .. figure:: ./img/ui_gui_step_tutorial_bar_nesting_inside_background.png
  125. By using the Background node as the two textures' parent, we take
  126. control away from the Count MarginContainer
  127. Select the ``Title`` and in the Inspector, change its ``Stretch Mode`` property
  128. to ``Keep Centered``. Next find the ``Rect`` category in the Inspector and
  129. change the ``Size`` property to (50, 40) so it only takes the left half of
  130. the background. Next, select the ``Number`` node. In the viewport, click the
  131. ``Layout`` menu and click ``Full Rect``. The node will resize to fit
  132. the ``Background``. Head to the Inspector and change its ``Align``
  133. property to ``Right``, and the ``VAlign`` property to ``Center``. The
  134. text should snap to the center of the ``Background``'s right edge.
  135. Resize the node horizontally so it takes the right half of the
  136. ``Background`` and there's a bit of padding with the right edge.
  137. .. figure:: ./img/ui_gui_step_tutorial_bar_placed_title_and_label.png
  138. Here's how the nodes' bounding boxes should look in the viewport.
  139. Keep it rough, you don't need to place them too precisely for now.
  140. Replace the Label's font
  141. ~~~~~~~~~~~~~~~~~~~~~~~~
  142. The label's font is too small. We need to replace it. Select the
  143. ``Number`` node and in the Inspector, scroll down to the ``Control``
  144. class, and find the ``Custom Font`` category. Click the field next to
  145. the ``Font`` property and click on ``New Dynamic Font``. Click on the
  146. field again and select Edit.
  147. You will enter the ``Dynamic Font`` resource. Unfold the ``Font``
  148. category and click the field next to ``Font Data``. Click the ``Load``
  149. button. In the file browser, navigate down to the assets/font folder and
  150. double click ``Comfortaa-Bold.ttf`` to open it. You should see the font
  151. update in the viewport. Unfold the settings category to change the font
  152. size. Set the ``Size`` property to a higher value, like ``24`` or
  153. ``28``.
  154. We now need the text's baseline, the number's lower edge, to align with
  155. the HP texture on the left. To do so, still in the ``DynamicFont``
  156. resource, you can tweak the ``Bottom`` property under the
  157. ``Extra Spacing`` category. It adds some bottom padding to the text.
  158. Click the ``Number`` node in the Scene tab to go back to the node's
  159. properties and change the ``VAlign`` to ``Bottom``. To adjust the text's
  160. baseline, click on the font field under the ``Custom Font`` category
  161. again and tweak the ``Bottom`` property until the text aligns with the
  162. ``Title`` node. I used a value of ``2`` pixels.
  163. .. figure:: ./img/ui_gui_step_tutorial_number_baseline.png
  164. With a Bottom value of 2 pixels, the Number aligns with the Title
  165. With this, we finished the hardest part of the GUI.
  166. Congratulations! Let's move on to the simpler nodes.
  167. Add the progress bar
  168. ~~~~~~~~~~~~~~~~~~~~
  169. We need one last element to complete our life bar: the gauge itself.
  170. Godot ships with a ``TextureProgress`` node that has everything we need.
  171. Select the Bar node and add a ``TextureProgress`` inside of it. Name it
  172. ``Gauge``. In the inspector unfold the ``Textures`` section. Head to the
  173. FileSystem dock and drag and drop the ``lifebar_bg.png`` texture onto
  174. the ``Under`` slot. Do the same with the ``lifebar_fill.png`` image and
  175. drop it onto the ``Progress`` slot. Under the ``Range`` class in the
  176. inspector, change the ``Value`` property to ``50`` to see the gauge fill
  177. up.
  178. With only five ``Control`` nodes, our first bar is ready to use.
  179. .. figure:: ./img/ui_gui_step_tutorial_bar_final.png
  180. That's it, our life bar is ready. This last part was quick, wasn't
  181. it? That's thanks to our robust container setup.
  182. Design the bomb and rupee counters
  183. ----------------------------------
  184. The bomb and rupee counters are like the bar's ``Count`` node. So we'll
  185. duplicate it and use it as a template.
  186. Under the ``Bar`` node, select ``Count`` and press Ctrl D to duplicate
  187. it. Drag and drop the new node under the ``Counters`` ``HBoxContainer``
  188. at the bottom of the scene tree. You should see it resize automatically.
  189. Don't worry about this for now, we'll fix the size soon.
  190. Rename the ``Count2`` node to ``Counter``. Unlike the bars, we want the
  191. number to be on the left, and an icon to sit on the right. The setup is
  192. the same: we need background, a ``NinePatchFrame``, the title, and the
  193. number nodes. The ``Title`` node is a ``TextureRect``, so it's what we
  194. need to display the icon. In the scene tree, select the ``Title`` node,
  195. and rename it to ``Icon``.
  196. .. figure:: ./img/ui_gui_step_tutorial_counter_design_1.png
  197. Here's how your node tree should look so far
  198. With the ``Icon`` node selected, in the inspector, scroll to the top to
  199. see the ``Texture`` slot. Head to the FileSystem dock on the left and
  200. select the ``bombs_icon.png``. Drag and drop it onto the ``Texture``
  201. slot. In the Scene Tab select both the ``Icon`` and the ``Number``
  202. nodes. Click the Layout menu in the toolbar at the top of the viewport
  203. and select ``Full Rect``. Both nodes will update to fit
  204. the size of the ``Background``.
  205. .. figure:: ./img/ui_gui_step_tutorial_counter_design_2.png
  206. The nodes anchor to the entire Background, but their position is off
  207. Let's change the ``Number``'s align properties to move it to the left
  208. and center of the ``Background``. Select the ``Number`` node, change its
  209. ``Align`` property to left and the ``VAlign`` property to centre. Then
  210. resize its left edge a little bit to add some padding between the left
  211. edge of the ``Background`` and the text.
  212. .. figure:: ./img/ui_gui_step_tutorial_counter_design_3.png
  213. The Number node aligned to the left and centre
  214. To overlap the Icon and the background, we need a few tweaks. First, our
  215. background is a bit too tall. It's because it's inside a margin
  216. container that is controlled by the top-most GUI node. Select the GUI
  217. node at the top of the scene tree and downsize it vertically so that
  218. it's as thin as possible. You'll see the gauge prevents you from making
  219. it too small. A container cannot be smaller than the minimal size of its
  220. children. The container's margins also weigh in.
  221. Select the Icon, click the Layout menu, and select
  222. ``Full Rect`` to re-center it. We need it to anchor to
  223. the ``Background``'s right edge. Open the Layout menu again and select
  224. ``Center Right``. Move the icon up so it is centered vertically with the
  225. ``Background``.
  226. .. figure:: ./img/ui_gui_step_tutorial_counter_design_4.png
  227. The bomb icon anchors to the Background's right edge. Resize the
  228. Counter container to see the Icon node stick to its right side
  229. Because we duplicated the ``Counter`` from the bar's ``Count``, the
  230. ``Number`` node's font is off. Select the ``Number`` node again, head to
  231. the ``Font`` property, and click it to access the ``DynamicFont``
  232. resource. In the ``Extra Spacing`` section, change the ``Bottom`` value
  233. to ``0`` to reset the font's baseline. Our counter now works as
  234. expected.
  235. While we are at it, let's make it so the ``Counters`` snap to the right
  236. edge of the viewport. To achieve this we will set the ``Bars`` container
  237. to expand and take all the horizontal space. Select the ``Bars`` node
  238. and scroll down to the ``Size Flags`` category. In the ``Horizontal``
  239. category, check the ``Expand`` value. The ``Bars`` node should resize
  240. and push the counter to the rightmost of the screen.
  241. .. figure:: ./img/ui_gui_step_tutorial_counter_design_5.png
  242. An expanding container eats all the space it can from its parent,
  243. pushing everything else along the way
  244. Turn the bar and counter into reusable UI components
  245. ----------------------------------------------------
  246. We have one bar and one counter widget. But we need two of each. We may
  247. need to change the bars' design or their functionality later on. It'd be
  248. great if we could have a single scene to store a UI element's template,
  249. and child scenes to work on variations. Godot lets us do this with
  250. Inherited Scenes.
  251. Let's save both the ``Counter`` and the ``Bar`` branches as separate
  252. scenes that we'll reduce to create the ``LifeBar``, the ``EnergyBar``,
  253. the ``BombCounter``, and the ``RupeeCounter``. Select the ``Bar``
  254. HBoxContainer. Right click on it and click on ``Save Branch as Scene``.
  255. Save the scene as ``Bar.tscn``. You should see the node branch turn it
  256. to a single ``Bar`` node.
  257. .. tip::
  258. A scene is a tree of nodes. The topmost node is the tree's
  259. **root**, and the children at the bottom of the hierarchy are
  260. **leaves**. Any node other than the root along with one more children is
  261. a **branch**. We can encapsulate node branches into separate scenes, or
  262. load and merge them from other scenes into the active one. Right click
  263. on any node in the Scene dock and select ``Save Branch as Scene`` or
  264. ``Merge from Scene``.
  265. Then, select the ``Counter`` node and do the same. Right click,
  266. ``Save Branch as Scene``, and save it as ``Counter.tscn``. A new edit
  267. scene icon appears to the right of the nodes in the scene tree. Click on
  268. the one next to ``Bar`` to open the corresponding scene. Resize the
  269. ``Bar`` node so that its bounding box fits its content. The way we named
  270. and place the Control nodes, we're ready to inherit this template and
  271. create the life bar. It's the same for the ``Counter``.
  272. .. figure:: ./img/ui_gui_step_tutorial_bar_template_scene.png
  273. With no extra changes, our Bar is ready to use
  274. Use Scene Inheritance to create the remaining elements
  275. ------------------------------------------------------
  276. We need two bars that work the same way: they should feature a label on
  277. the left, with some value, and a horizontal gauge on the right. The only
  278. difference is that one has the HP label and is green, while the other is
  279. called EP and is yellow. Godot gives us a powerful tool to create a
  280. common base to reuse for all bars in the game: **inherited scenes**.
  281. .. figure:: img/gui_step_tutorial_gui_scene_hierarchy.png
  282. Inherited scenes help us keep the GUI scene clean. In the end, we
  283. will only have containers and one node for each UI component.
  284. On an inherited scene, you can change any property of every node in the
  285. inspector, aside from its name. If you modify and save the parent scene,
  286. all the inherited scenes update to reflect the changes. If you change a
  287. value in the inherited scene, it will always overrides the parent's
  288. property. It's useful for UIs as they often require variations of the same
  289. elements. In general, in UI design, buttons, panels etc. share a common
  290. base style and interactions. We don't want to copy it over to all
  291. variations manually.
  292. A reload icon will appear next to the properties you override. Click it
  293. to reset the value to the parent scene's default.
  294. .. note::
  295. Think of scene inheritance like the node tree, or the
  296. ``extends`` keyword in GDScript. An inherited scene does everything like
  297. its parent, but you can override properties, resources and add extra
  298. nodes and scripts to extend its functionality.
  299. Inherit the Bar Scene to build the LifeBar
  300. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  301. Go to ``Scene -> New Inherited Scene`` to create a new type of ``Bar``.
  302. Select the Bar scene and open it. You should see a new [unsaved] tab,
  303. that's like your ``Bar``, but with all nodes except the root in grey.
  304. Press ``Meta+S`` to save the new inherited scene and name it
  305. ``LifeBar``.
  306. .. figure:: img/ui_gui_step_tutorial_inherited_scene_parent.png
  307. You can't rename grey nodes. This tells you they have a parent scene
  308. First, rename the root or top level node to ``LifeBar``. We always want
  309. the root to describe exactly what this UI component is. The name
  310. differentiates this bar from the ``EnergyBar`` we'll create next. The
  311. other nodes inside the scene should describe the component's structure
  312. with broad terms, so it works with all inherited scenes. Like our
  313. ``TextureProgress`` and ``Number`` nodes.
  314. .. note::
  315. If you've ever done web design, it's the same spirit as
  316. working with CSS: you create a base class, and add variations with
  317. modifier classes. From a base button class, you'll have button-green and
  318. button-red variations for the user to accept and refuse prompts. The new
  319. class contains the name of the parent element and an extra keyword to
  320. explain how it modifies it. When we create an inherited scene and change
  321. the name of the top level node, we're doing the same thing.
  322. Design the EnergyBar
  323. ~~~~~~~~~~~~~~~~~~~~
  324. We already setup the ``LifeBar``'s design with the main ``Bar`` scene.
  325. Now we need the ``EnergyBar``.
  326. Let's create a new inherited scene, and once again select the
  327. ``Bar.tscn`` scene and open it. Double-click on the ``Bar`` root node and rename it
  328. to ``EnergyBar``. Save the new scene as ``EnergyBar.tscn``.
  329. We need to replace the HP texture with EP one, and to
  330. change the textures on the gauge.
  331. Head to the FileSystem dock on the left, select the ``Title`` node in
  332. the Scene tree and drag and drop the ``label_EP.png`` file onto the
  333. texture slot. Select the ``Number`` node and change the ``Text``
  334. property to a different value like ``14``.
  335. You'll notice the EP texture is smaller than the HP one. We should
  336. update the ``Number``'s font size to better fit it. A font is a
  337. resource. All the nodes in the entire project that use this resource
  338. will be affected by any property we change. You can try to change the
  339. size to a huge value like ``40`` and switch back to the ``LifeBar`` or
  340. the ``Bar`` scenes. You will see the text increased in size.
  341. .. figure:: ./img/ui_gui_step_tutorial_design_EnergyBar_1.png
  342. If we change the font resource, all the nodes that use it are
  343. affected
  344. To change the font size on this node only, we must create a copy of the
  345. font resource. Select the ``Number`` node again and click on the wrench
  346. and screwdriver icon on the top right of the inspector. In the drop-down
  347. menu, select the ``Make Sub-Resources Unique`` option. Godot will find
  348. all the resources this node uses and create unique copies for us.
  349. .. figure:: ./img/ui_gui_step_tutorial_design_EnergyBar_2.png
  350. Use this option to create unique copies of the resources for one node
  351. .. tip::
  352. When you duplicate a node from the Scene tree, with
  353. ``Meta+D``, it shares its resources with the original node. You
  354. need to use ``Make Sub-Resources Unique`` before you can tweak the
  355. resources without affecting the source node.
  356. Scroll down to the ``Custom Font`` section and open ``Font``. Lower the
  357. ``Size`` to a smaller value like ``20`` or ``22``. You may also need to
  358. adjust the ``Bottom`` spacing value to align the text's baseline with
  359. the EP label on the left.
  360. .. figure:: ./img/ui_gui_step_tutorial_design_EnergyBar_3.png
  361. The EP Count widget, with a smaller font than its HP counterpart
  362. Now, select the ``TextureProgress`` node. Drag the ``energy_bar_bg.png``
  363. file onto the ``Under`` slot and do the same for ``energy_bar_fill.png``
  364. and drop it onto the ``Progress`` texture slot.
  365. You can resize the node vertically so that its bounding rectangle fits
  366. the gauge. Do the same with the ``Count`` node until its size aligns
  367. with that of the bar. Because the minimal size of ``TextureProgress`` is
  368. set based on its textures, you won't be able to downsize the ``Count``
  369. node below that. That is also the size the ``Bar`` container will have.
  370. You may downscale this one as well.
  371. Last but not least, the ``Background`` container has a minimum size that
  372. makes it a bit large. Select it and in the ``Rect`` section, change the
  373. ``Min Size`` property down to ``80`` pixels. It should resize
  374. automatically and the ``Title`` and ``Number`` nodes should reposition
  375. as well.
  376. .. figure:: ./img/ui_gui_step_tutorial_design_EnergyBar_4.png
  377. The Count looks better now it's a bit smaller
  378. .. tip::
  379. The Count node's size affects the position of the
  380. TextureProgress. As we'll align our bars vertically in a moment, we're
  381. better off using the Counter's left margin to resize our EP label. This
  382. way both the EnergyBar's Count and the LifeBar's Count nodes are one
  383. hundred pixels wide, so both gauges will align perfectly.
  384. Prepare the bomb and rupee counters
  385. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  386. Let us now take care of the counters. Go to
  387. ``Scene -> New Inherited Scene`` and select the ``Counter.tscn`` as a
  388. base. Rename the root node as ``BombCounter`` too.
  389. Save the new scene as ``BombCounter.tscn``. That's all for this scene.
  390. .. figure:: ./img/ui_gui_step_tutorial_design_counters_1.png
  391. The bomb counter is the same as the original Counter scene
  392. Go to ``Scene -> New Inherited Scene`` again and select ``Counter.tscn``
  393. once more. Rename the root node ``RupeeCounter`` and save the scene as ``RupeeCounter.tscn``.
  394. For this one, we mainly need to replace the bomb icon
  395. with the rupee icon. In the FileSystem tab, drag the ``rupees_icon.png``
  396. onto the ``Icon`` node's ``Texture`` slot. ``Icon`` already anchors to
  397. the right edge of the ``Background`` node so we can change its position
  398. and it will scale and reposition with the ``RupeeCounter`` container.
  399. Shift the rupee icon a little bit to the right and down. Use the Arrow
  400. Keys on the keyboard to nudge its position. Save, and we're done with
  401. all the UI elements.
  402. .. figure:: ./img/ui_gui_step_tutorial_design_counters_2.png
  403. The rupee counter should look about like this
  404. Add the UI components to the final GUI
  405. --------------------------------------
  406. Time to add all the UI elements to the main GUI scene. Open the
  407. ``GUI.tscn`` scene again, and delete the ``Bar`` and ``Counter`` nodes.
  408. In the FileSystem dock, find the ``LifeBar.tscn`` and drag and drop it
  409. onto the ``Bars`` container in the scene tree. Do the same for the
  410. ``EnergyBar``. You should see them align vertically.
  411. .. figure:: ./img/ui_gui_step_tutorial_assemble_final_gui_1.png
  412. The LifeBar and the EnergyBar align automatically
  413. Now, drag and drop the ``BombCounter.tscn`` and ``RupeeCounter.tscn`` scenes onto the
  414. ``Counters`` node. They'll resize automatically.
  415. .. figure:: ./img/ui_gui_step_tutorial_assemble_final_gui_2.png
  416. The nodes resize to take all the available vertical space
  417. To let the ``RupeeCounter`` and ``BombCounter`` use the size we defined
  418. in ``Counter.tscn``, we need to change the ``Size Flags`` on the
  419. ``Counters`` container. Select the ``Counters`` node and unfold the
  420. ``Size Flags`` section in the Inspector. Uncheck the ``Fill`` tag for
  421. the ``Vertical`` property, and check ``Shrink Center`` so the container
  422. centers inside the ``HBoxContainer``.
  423. .. figure:: ./img/ui_gui_step_tutorial_assemble_final_gui_3.png
  424. Now both counters have a decent size
  425. .. tip::
  426. Change the ``Min Size`` property of the ``Counters`` container
  427. to control the height of the counters' background.
  428. We have one small issue left with the EP label on the EnergyBar: the 2
  429. bars should align vertically. Click the icon next to the ``EnergyBar``
  430. node to open its scene. Select the ``Count`` node and scroll down to the
  431. ``Custom Constant`` section. Add a ``Margin Left`` of ``20``. In
  432. the ``Rect`` section set the node's ``Min Size`` back to 100, the same
  433. value as on the LifeBar. The ``Count`` should now have some margin on
  434. the left. If you save and go back to the GUI scene, it will be aligned
  435. vertically with the ``LifeBar``.
  436. .. figure:: ./img/ui_gui_step_tutorial_assemble_final_gui_4.png
  437. The 2 bars align perfectly
  438. .. note::
  439. We could have setup the ``EnergyBar`` this way a few moments
  440. ago. But this shows you that you can go back to any scene anytime, tweak
  441. it, and see the changes propagate through the project!
  442. Place the GUI onto the game's mockup
  443. ------------------------------------
  444. To wrap up the tutorial we're going to insert the GUI onto the game's
  445. mockup scene.
  446. Head to the FileSystem dock and open ``LevelMockup.tscn``.
  447. Drag-and-drop the ``GUI.tscn`` scene right below the ``bg`` node and
  448. above the ``Characters``. The GUI will scale to fit the entire viewport.
  449. Head to the Layout menu and select the ``Center Top`` option so it anchors
  450. to the top edge of the game window. Then resize the GUI to make it as
  451. small as possible vertically. Now you can see how the interface looks in
  452. the context of the game.
  453. Congratulations for getting to the end of this long tutorial. You can
  454. find final project `here <#>`__.
  455. .. figure:: ./img/ui_gui_design_final_result.png
  456. The final result
  457. .. note::
  458. **A final note about Responsive Design**. If you resize the
  459. GUI, you'll see the nodes move, but the textures and text won't scale.
  460. The GUI also has a minimum size, based on the textures inside of it. In
  461. games, we don’t need the interface to be as flexible as that of a
  462. website. You almost never want to support both landscape and portrait
  463. screen orientations. It’s one or the other. In landscape orientation,
  464. the most common ratios range from 4:3 to 16:9. They are close to one
  465. another. That's why it’s enough for the GUI elements to only move
  466. horizontally when we change the window size.