ui_game_user_interface.rst 27 KB

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